Saltar al contenido

Registrar atributos

Los atributos se definen dentro de la matriz blockstudio.attributes en tu archivo block.json.

Ejemplo básico

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

Propiedades de los atributos

Todos los atributos comparten las siguientes propiedades:

Propiedad Tipo Descripción
id string Identificador único del atributo
type string Tipo de campo (text, textarea, select, etc.)
label string Etiqueta que se muestra en el editor
default mixed Valor predeterminado del atributo
help string Texto de ayuda que se muestra como información emergente junto a la etiqueta
position string Dónde aparece el campo: inspector (predeterminado) o toolbar
conditions array Lógica condicional para mostrar u ocultar el campo

Posición

De forma predeterminada, todos los campos se muestran en el inspector del bloque (barra lateral). Puedes cambiarlo con la propiedad 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" }
        ]
      }
    ]
  }
}

Valores predeterminados

Establece valores predeterminados con la propiedad default:

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

Texto de ayuda

Añade texto de ayuda para orientar a los usuarios:

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

Acceso a los atributos

En tu plantilla, los atributos están disponibles mediante la notación abreviada $a (o $attributes):

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

O en Twig:

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

Recetario de patrones de campos