コンテンツへスキップ

属性の登録

属性は、block.jsonファイル内のblockstudio.attributes配列で定義します。

基本的な例

JSON
{
  "name": "my-theme/my-block",
  "title": "My Block",
  "blockstudio": {
    "attributes": [
      {
        "id": "title",
        "type": "text",
        "label": "Title"
      },
      {
        "id": "content",
        "type": "textarea",
        "label": "Content"
      }
    ]
  }
}

属性のプロパティ

すべての属性には次のプロパティがあります。

プロパティ 型 説明
id string 属性を識別する一意のID
type string フィールド型(text、textarea、selectなど)
label string エディターに表示するラベル
default mixed 属性のデフォルト値
help string ラベルの横にツールチップとして表示するヘルプテキスト
position string フィールドの表示場所:inspector(デフォルト)またはtoolbar
conditions array フィールドの表示・非表示を切り替える条件ロジック

表示位置

デフォルトでは、すべてのフィールドがブロックインスペクター(サイドバー)に表示されます。positionプロパティを使って変更できます。

JSON
{
  "blockstudio": {
    "attributes": [
      {
        "id": "alignment",
        "type": "select",
        "label": "Alignment",
        "position": "toolbar",
        "options": [
          { "value": "left", "label": "Left" },
          { "value": "center", "label": "Center" },
          { "value": "right", "label": "Right" }
        ]
      }
    ]
  }
}

デフォルト値

defaultプロパティでデフォルト値を設定します。

JSON
{
  "blockstudio": {
    "attributes": [
      {
        "id": "title",
        "type": "text",
        "label": "Title",
        "default": "Hello World"
      },
      {
        "id": "showBorder",
        "type": "toggle",
        "label": "Show Border",
        "default": true
      }
    ]
  }
}

ヘルプテキスト

ユーザーを案内するヘルプテキストを追加します。

JSON
{
  "blockstudio": {
    "attributes": [
      {
        "id": "apiKey",
        "type": "text",
        "label": "API Key",
        "help": "Enter your API key from the settings page"
      }
    ]
  }
}

属性へのアクセス

テンプレートでは、$a短縮構文(または$attributes)を使って属性にアクセスできます。

PHP
<h1><?php echo $a['title']; ?></h1>
<p><?php echo $a['content']; ?></p>

Twigでの書き方:

Twig
<h1>{{ a.title }}</h1>
<p>{{ a.content }}</p>

フィールドパターンのレシピ集