LoginSignup
0
0

More than 3 years have passed since last update.

【CraftCMS】MatrixFieldに嵌められた話

Posted at

先日og情報を動的に云々話を書いた後に起きた出来事

今回の作業として
MatrixFieldにテキストフィールドが格納されていれば
その情報をog:Descriptionに出力する 、という対応を行った。

ざっとこういう感じである

{% set desc = entry.detail.type('blockText').one %}
{% if desc is not empty %}
〜省略

で順番的にその後でもページ表示項目としてdetailフィールド(matrix)を使用するわけだが

{% set article = entry.detail.all %}
{% if article | length > 0 %}
    {% for unit in article %}
        {% if unit.type == "blockText" %}
                {{unit.text}}
        {% elseif unit.type == "blockImage" %}
                <img src="{{unit.image.one.url}}" />
        {% elseif unit.type == "blockYoutube" %}{% endif %}
    {% endfor %}
{% endif %}

ここで何故かテキストフィールドしか出力されない謎が発生
text,image,youtubeと各種登録したけど出ない

{{article | length}} ← 3と出てほしいのに1と出る

どうも冒頭のDescriptionの取得処理が悪さをしているらしく、
blockTextのフィルタが残ったままになっているような感じである

対策として後半のMatrixのall処理を以下のようにする

{% set article = entry.detail.type("*").all %}

type指定を「全て」となるようにワイルドカードを使う。

そういうもんだと言われると納得せざるを得ないがなんかモヤモヤする話だった…

0
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0