Hoppa till innehåll

Villkorslogik

Fält kan visas eller döljas beroende på villkor.

Operatorer

Det finns 8 olika operatorer som kan användas:

Operator Beskrivning
== Värdena är lika
!= Värdena är inte lika
includes Värdet ingår i referensvärdet
!includes Värdet ingår inte i referensvärdet
empty Värdet är tomt
!empty Värdet är inte tomt
< Värdet är mindre än referensvärdet
> Värdet är större än referensvärdet
<= Värdet är mindre än eller lika med referensvärdet
>= Värdet är större än eller lika med referensvärdet

Globala villkor

Som standard har Blockstudio fyra globala villkor: inläggstyp, inläggs-ID, användarroll och användar-ID.

JSON
{
  "$schema": "https://blockstudio.dev/schema/block",
  "name": "blockstudio/native",
  "title": "Native Block",
  "blockstudio": {
    "attributes": [
      {
        "id": "message",
        "type": "text",
        "label": "My message",
        "conditions": [
          [
            {
              "type": "postId",
              "operator": "==",
              "value": "1386"
            },
            {
              "type": "postType",
              "operator": "==",
              "value": "post"
            }
          ]
        ]
      }
    ]
  }
}

I exemplet ovan visas textattributet bara i redigeraren om inläggs-ID:t är 1386 OCH inläggstypen är post.

Observera att konventionen camelCase används för typnycklarna: postType, postId, userRole, userId.

ELLER-villkor

Skapa eller-villkor genom att placera varje villkorsgrupp i en egen array:

JSON
{
  "blockstudio": {
    "attributes": [
      {
        "id": "message",
        "type": "text",
        "label": "My message",
        "conditions": [
          [
            {
              "type": "postId",
              "operator": "==",
              "value": "1386"
            },
            {
              "type": "postType",
              "operator": "==",
              "value": "post"
            }
          ],
          [
            {
              "type": "postType",
              "operator": "==",
              "value": "jobs"
            }
          ]
        ]
      }
    ]
  }
}

Textattributet visas om (inläggs-ID:t är 1386 OCH inläggstypen är post) ELLER (inläggstypen är jobs).

Anpassade villkor

Anpassade villkor kan läggas till med filtret blockstudio/blocks/conditions. Filtret körs i PHP när redigeraren läses in och returnerar ett värde per villkorstyp. Villkoren utvärderas i redigeraren, som jämför det värdet med villkorets value med hjälp av operatorn.

PHP
add_filter('blockstudio/blocks/conditions', function($conditions) {
  // Check if the current user purchased the product.
  $conditions['purchasedProduct'] = user_has_purchased(123) ? 'yes' : 'no';
  return $conditions;
});

Returnera en icke-tom sträng för varje tillstånd. En villkorstyp vars värde är tomt eller saknas hoppas över, så fältet förblir synligt.

JSON
{
  "blockstudio": {
    "attributes": [
      {
        "id": "message",
        "type": "text",
        "label": "My message",
        "conditions": [
          [
            {
              "type": "purchasedProduct",
              "operator": "==",
              "value": "yes"
            }
          ]
        ]
      }
    ]
  }
}

Blockvillkor

Du kan ange villkor som beror på andra attribut. Använd id för attributet du vill kontrollera i stället för att ange en nyckel för type:

JSON
{
  "blockstudio": {
    "attributes": [
      {
        "type": "toggle",
        "id": "copyButton",
        "label": "Copy Button"
      },
      {
        "type": "text",
        "id": "copyButtonText",
        "label": "Copy Button Text",
        "default": "Copy",
        "conditions": [
          [
            {
              "id": "copyButton",
              "operator": "==",
              "value": true
            }
          ]
        ]
      }
    ]
  }
}

Du kan kombinera globala villkor med blockvillkor efter behov.

Repeater-kontext

Som standard beror villkor för attribut i repeaters på attributen för det element som upprepas just nu.

JSON
{
  "blockstudio": {
    "attributes": [
      {
        "type": "toggle",
        "id": "toggle",
        "label": "Toggle"
      },
      {
        "type": "repeater",
        "id": "repeater",
        "label": "Repeater",
        "attributes": [
          {
            "type": "toggle",
            "id": "toggle",
            "label": "Toggle"
          },
          {
            "type": "text",
            "id": "text",
            "label": "Text",
            "conditions": [
              [
                {
                  "id": "toggle",
                  "operator": "==",
                  "value": true
                }
              ]
            ]
          }
        ]
      }
    ]
  }
}

Eftersom attribut-ID:n är begränsade till det aktuella repeater-elementet visas textattributet i repeattern bara om växlingsknappen inne i repeatern är satt till true.

Lägg till "context": "main" i villkoret om du vill kontrollera växlingsknappen utanför repeatern:

JSON
{
  "blockstudio": {
    "attributes": [
      {
        "type": "toggle",
        "id": "toggle",
        "label": "Toggle"
      },
      {
        "type": "repeater",
        "id": "repeater",
        "label": "Repeater",
        "attributes": [
          {
            "type": "text",
            "id": "text",
            "label": "Text",
            "conditions": [
              [
                {
                  "id": "toggle",
                  "operator": "==",
                  "value": true,
                  "context": "main"
                }
              ]
            ]
          }
        ]
      }
    ]
  }
}

Kokbok med fältmönster