You are here

protected function EntityReferenceActionsHandler::getVisibleStateConditions in Entity reference actions 1.x

Get the conditions to show the ERA button.

Parameters

array $element: The element with the attached action form.

array $context: The context of this form.

1 call to EntityReferenceActionsHandler::getVisibleStateConditions()
EntityReferenceActionsHandler::formAlter in src/EntityReferenceActionsHandler.php
Build the form elements.

File

src/EntityReferenceActionsHandler.php, line 532

Class

EntityReferenceActionsHandler
Provides the form functions to call actions on referenced entities.

Namespace

Drupal\entity_reference_actions

Code

protected function getVisibleStateConditions(array $element, array $context) {

  /** @var \Drupal\Core\Field\FieldItemListInterface $items */
  $items = $context['items'];
  $parents = $element['widget']['#parents'];
  $first_parent = array_shift($parents) . '[';
  $secondary_parents = '';
  if ($parents) {
    $secondary_parents = implode('][', $parents) . ']';
  }
  $field_selector = 'name^="' . $first_parent . $secondary_parents . '"';
  $state_selector = [
    ":input[{$field_selector}]" => [
      'filled' => TRUE,
    ],
  ];
  $multiple = $items
    ->getFieldDefinition()
    ->getFieldStorageDefinition()
    ->isMultiple();
  if ($context['widget'] instanceof OptionsWidgetBase) {
    $state_selector = [
      ":input[{$field_selector}]" => [
        'checked' => TRUE,
      ],
    ];
  }
  if ($context['widget'] instanceof OptionsButtonsWidget && !$multiple) {
    $state_selector = [
      ":input[{$field_selector}]" => [
        '!value' => isset($element['widget']['#empty_value']) ? $element['widget']['#empty_value'] : '_none',
      ],
    ];
  }
  if ($context['widget'] instanceof OptionsSelectWidget) {
    $state_selector = [
      "select[{$field_selector}]" => [
        '!value' => isset($element['widget']['#empty_value']) ? $element['widget']['#empty_value'] : '_none',
      ],
    ];

    // States doesn't work for a multiple select lists.
    // See https://www.drupal.org/project/drupal/issues/1149078
    if ($multiple) {
      $state_selector = [];
    }
  }
  return $state_selector;
}