You are here

public function EntityReferenceActionsHandler::formAlter in Entity reference actions 1.x

Build the form elements.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

array $context: The context of this form.

File

src/EntityReferenceActionsHandler.php, line 165

Class

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

Namespace

Drupal\entity_reference_actions

Code

public function formAlter(array &$element, FormStateInterface $form_state, array $context) {
  if (!$this->settings['enabled']) {
    return;
  }

  /** @var \Drupal\Core\Field\FieldItemListInterface $items */
  $items = $context['items'];
  $field_definition = $items
    ->getFieldDefinition();
  $uuid = 'entity_reference_actions-' . $this->uuidGenerator
    ->generate();
  $form_state
    ->set($uuid, $context);
  $element['entity_reference_actions_messages'] = [
    '#type' => 'container',
    '#attributes' => [
      'data-entity-reference-actions-messages' => '',
    ],
  ];
  $element['entity_reference_actions'] = [
    '#type' => 'simple_actions',
    '#uuid' => $uuid,
    '#attached' => [
      'library' => [
        'core/drupal.dialog.ajax',
        'core/drupal.states',
      ],
    ],
    '#states' => [
      'visible' => $this
        ->getVisibleStateConditions($element, $context),
    ],
  ];
  $bulk_options = $this
    ->getBulkOptions();
  foreach ($bulk_options as $id => $label) {
    $element['entity_reference_actions'][$id] = [
      '#type' => 'submit',
      '#limit_validation_errors' => [
        $element['widget']['#parents'],
      ],
      '#submit' => [],
      '#id' => $field_definition
        ->getName() . '_' . $id . '_button',
      '#name' => $field_definition
        ->getName() . '_' . $id . '_button',
      '#value' => $label,
      '#ajax' => [
        'callback' => [
          $this,
          'submitForm',
        ],
      ],
    ];
    if (count($bulk_options) > 1) {
      $element['entity_reference_actions'][$id]['#dropbutton'] = 'bulk_edit';
    }
  }
}