You are here

public function EntityBrowserFormTrait::getEntityBrowserForm in Helper 8

Adds the Entity Browser element to a form.

Parameters

string $entity_browser_id: The ID of the entity browser to use.

string $default_value: The default value for the entity browser.

int $cardinality: The cardinality of the entity browser.

string $view_mode: The view mode to use when displaying the selected entity in the table.

string|bool $form_mode: The form mode to use when showing the edit button for the selected entity in the table. Use FALSE to disable the edit button.

Return value

array The form element containing the entity browser.

File

src/EntityBrowserFormTrait.php, line 34

Class

EntityBrowserFormTrait
Provides helpers for adding an entity browser element to a form.

Namespace

Drupal\helper

Code

public function getEntityBrowserForm($entity_browser_id, $default_value, $cardinality = EntityBrowserElement::CARDINALITY_UNLIMITED, $view_mode = 'default', $form_mode = 'default') {

  // We need a wrapping container for AJAX operations.
  $element = [
    '#type' => 'container',
    '#attributes' => [
      'id' => Html::getUniqueId('entity-browser-' . $entity_browser_id . '-wrapper'),
    ],
  ];
  $element['browser'] = [
    '#type' => 'entity_browser',
    '#entity_browser' => $entity_browser_id,
    '#process' => [
      [
        self::class,
        'processEntityBrowser',
      ],
    ],
    '#cardinality' => $cardinality,
    '#selection_mode' => $cardinality === 1 ? EntityBrowserElement::SELECTION_MODE_PREPEND : EntityBrowserElement::SELECTION_MODE_APPEND,
    '#default_value' => $default_value,
    '#wrapper_id' => &$element['#attributes']['id'],
  ];
  $element['selected'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Item'),
      $this
        ->t('Operations'),
    ],
    '#empty' => $this
      ->t('No items selected yet'),
    '#process' => [
      [
        self::class,
        'processEntityBrowserSelected',
      ],
    ],
    '#view_mode' => $view_mode,
    '#form_mode' => $form_mode,
    '#wrapper_id' => &$element['#attributes']['id'],
  ];
  return $element;
}