You are here

public function MultiStepDisplay::getForm in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/EntityBrowser/SelectionDisplay/MultiStepDisplay.php \Drupal\entity_browser\Plugin\EntityBrowser\SelectionDisplay\MultiStepDisplay::getForm()

Returns selection display form.

Parameters

array $original_form: Entire form built up to this point. Form elements for selection display should generally not be added directly to it but returned from function as a separated unit.

\Drupal\Core\Form\FormStateInterface $form_state: Form state object.

Return value

array Form structure.

Overrides SelectionDisplayInterface::getForm

File

src/Plugin/EntityBrowser/SelectionDisplay/MultiStepDisplay.php, line 87

Class

MultiStepDisplay
Show current selection and delivers selected entities.

Namespace

Drupal\entity_browser\Plugin\EntityBrowser\SelectionDisplay

Code

public function getForm(array &$original_form, FormStateInterface $form_state) {

  // Check if trigger element is dedicated to handle front-end commands.
  if (($triggering_element = $form_state
    ->getTriggeringElement()) && $triggering_element['#name'] === 'ajax_commands_handler' && !empty($triggering_element['#value'])) {
    $this
      ->executeJsCommand($form_state);
  }
  $selected_entities = $form_state
    ->get([
    'entity_browser',
    'selected_entities',
  ]);
  $form = [];
  $form['#attached']['library'][] = 'entity_browser/multi_step_display';
  $form['selected'] = [
    '#theme_wrappers' => [
      'container',
    ],
    '#attributes' => [
      'class' => [
        'entities-list',
      ],
    ],
    '#tree' => TRUE,
  ];
  if ($this->configuration['selection_hidden']) {
    $form['selected']['#attributes']['class'][] = 'hidden';
  }
  foreach ($selected_entities as $id => $entity) {
    $display_plugin = $this->fieldDisplayManager
      ->createInstance($this->configuration['display'], $this->configuration['display_settings'] + [
      'entity_type' => $this->configuration['entity_type'],
    ]);
    $display = $display_plugin
      ->view($entity);
    if (is_string($display)) {
      $display = [
        '#markup' => $display,
      ];
    }
    $form['selected']['items_' . $entity
      ->id() . '_' . $id] = [
      '#theme_wrappers' => [
        'container',
      ],
      '#attributes' => [
        'class' => [
          'item-container',
        ],
        'data-entity-id' => $entity
          ->id(),
      ],
      'display' => $display,
      'remove_button' => [
        '#type' => 'submit',
        '#value' => $this
          ->t('Remove'),
        '#submit' => [
          [
            get_class($this),
            'removeItemSubmit',
          ],
        ],
        '#name' => 'remove_' . $entity
          ->id() . '_' . $id,
        '#attributes' => [
          'class' => [
            'entity-browser-remove-selected-entity',
          ],
          'data-row-id' => $id,
          'data-remove-entity' => 'items_' . $entity
            ->id(),
        ],
      ],
      'weight' => [
        '#type' => 'hidden',
        '#default_value' => $id,
        '#attributes' => [
          'class' => [
            'weight',
          ],
        ],
      ],
    ];
  }

  // Add hidden element used to make execution of front-end commands.
  $form['ajax_commands_handler'] = [
    '#type' => 'hidden',
    '#name' => 'ajax_commands_handler',
    '#id' => 'ajax_commands_handler',
    '#attributes' => [
      'id' => 'ajax_commands_handler',
    ],
    '#ajax' => [
      'callback' => [
        get_class($this),
        'handleAjaxCommand',
      ],
      'wrapper' => 'edit-selected',
      'event' => 'execute_js_commands',
      'progress' => [
        'type' => 'fullscreen',
      ],
    ],
  ];
  $form['use_selected'] = [
    '#type' => 'submit',
    '#value' => $this->configuration['select_text'],
    '#name' => 'use_selected',
    '#attributes' => [
      'class' => [
        'entity-browser-use-selected',
        'button--primary',
      ],
    ],
    '#access' => empty($selected_entities) ? FALSE : TRUE,
  ];
  $form['show_selection'] = [
    '#type' => 'button',
    '#value' => $this
      ->t('Show selected'),
    '#attributes' => [
      'class' => [
        'entity-browser-show-selection',
      ],
    ],
    '#access' => empty($selected_entities) ? FALSE : TRUE,
  ];
  return $form;
}