Registrare gli attributi
Gli attributi vengono definiti nell’array blockstudio.attributes del file block.json.
Esempio di base
JSON
{
"name": "my-theme/my-block",
"title": "My Block",
"blockstudio": {
"attributes": [
{
"id": "title",
"type": "text",
"label": "Title"
},
{
"id": "content",
"type": "textarea",
"label": "Content"
}
]
}
}
Proprietà degli attributi
Tutti gli attributi condividono le seguenti proprietà:
| Proprietà | Tipo | Descrizione |
|---|---|---|
id |
string | Identificatore univoco dell’attributo |
type |
string | Tipo di campo (text, textarea, select, ecc.) |
label |
string | Etichetta visualizzata nell’editor |
default |
mixed | Valore predefinito dell’attributo |
help |
string | Testo di aiuto visualizzato come tooltip accanto all’etichetta |
position |
string | Dove compare il campo: inspector (predefinito) o toolbar |
conditions |
array | Logica condizionale per mostrare o nascondere il campo |
Posizione
Per impostazione predefinita, tutti i campi vengono visualizzati nell’ispettore del blocco (barra laterale). Potete cambiarlo usando la proprietà 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" }
]
}
]
}
}
Valori predefiniti
Impostate i valori predefiniti usando la proprietà default:
JSON
{
"blockstudio": {
"attributes": [
{
"id": "title",
"type": "text",
"label": "Title",
"default": "Hello World"
},
{
"id": "showBorder",
"type": "toggle",
"label": "Show Border",
"default": true
}
]
}
}
Testo di aiuto
Aggiungete un testo di aiuto per guidare gli utenti:
JSON
{
"blockstudio": {
"attributes": [
{
"id": "apiKey",
"type": "text",
"label": "API Key",
"help": "Enter your API key from the settings page"
}
]
}
}
Accesso agli attributi
Nel template, gli attributi sono disponibili tramite la forma abbreviata $a (o $attributes):
PHP
<h1><?php echo $a['title']; ?></h1>
<p><?php echo $a['content']; ?></p>
Oppure in Twig:
Twig
<h1>{{ a.title }}</h1>
<p>{{ a.content }}</p>