Przejdź do treści

Rejestrowanie atrybutów

Atrybuty definiuje się w tablicy blockstudio.attributes w pliku block.json.

Podstawowy przykład

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

Właściwości atrybutów

Wszystkie atrybuty mają następujące właściwości:

Właściwość Typ Opis
id string Unikalny identyfikator atrybutu
type string Typ pola (text, textarea, select itd.)
label string Etykieta wyświetlana w edytorze
default mixed Wartość domyślna atrybutu
help string Tekst pomocy wyświetlany jako podpowiedź obok etykiety
position string Miejsce wyświetlania pola: inspector (domyślnie) lub toolbar
conditions array Logika warunkowa wyświetlania lub ukrywania pola

Pozycja

Domyślnie wszystkie pola są wyświetlane w inspektorze bloku (pasku bocznym). Możesz to zmienić za pomocą właściwości 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" }
        ]
      }
    ]
  }
}

Wartości domyślne

Ustaw wartości domyślne za pomocą właściwości default:

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

Tekst pomocy

Dodaj tekst pomocy, aby ułatwić użytkownikom korzystanie z pola:

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

Dostęp do atrybutów

W szablonie atrybuty są dostępne za pomocą skrótu $a (lub $attributes):

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

Lub w Twig:

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

Przepisy na wzorce pól