You are here

public static function EntityBrowserElement::processEntityBrowser in Entity Browser 8.2

Same name and namespace in other branches
  1. 8 src/Element/EntityBrowserElement.php \Drupal\entity_browser\Element\EntityBrowserElement::processEntityBrowser()

Render API callback: Processes the entity browser element.

File

src/Element/EntityBrowserElement.php, line 140

Class

EntityBrowserElement
Provides an Entity Browser form element.

Namespace

Drupal\entity_browser\Element

Code

public static function processEntityBrowser(&$element, FormStateInterface $form_state, &$complete_form) {

  /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */
  if (is_string($element['#entity_browser'])) {
    $entity_browser = EntityBrowser::load($element['#entity_browser']);
  }
  else {
    $entity_browser = $element['#entity_browser'];
  }

  // Propagate selection if edit selection mode is used.
  $entity_browser_preselected_entities = [];
  if ($element['#selection_mode'] === static::SELECTION_MODE_EDIT) {
    if ($entity_browser
      ->getSelectionDisplay()
      ->supportsPreselection()) {
      $entity_browser_preselected_entities = $element['#default_value'];
    }
    else {
      $field_element = NestedArray::getValue($complete_form, array_slice($element['#array_parents'], 0, -1));
      $tparams = [
        '@field_name' => !empty($field_element['#title']) ? $field_element['#title'] : $element['#custom_hidden_id'],
        '%selection_mode' => static::getSelectionModeOptions()[static::SELECTION_MODE_EDIT],
        '@browser_link' => $entity_browser
          ->toLink($entity_browser
          ->label(), 'edit-form')
          ->toString(),
      ];
      \Drupal::messenger()
        ->addWarning(t('There is a configuration problem with field "@field_name". The selection mode %selection_mode requires an entity browser with a selection display plugin that supports preselection.  Either change the selection mode or update the @browser_link entity browser to use a selection display plugin that supports preselection.', $tparams));
    }
  }
  $default_value = implode(' ', array_map(function (EntityInterface $item) {
    return $item
      ->getEntityTypeId() . ':' . $item
      ->id();
  }, $entity_browser_preselected_entities));
  $validators = array_merge($element['#entity_browser_validators'], [
    'cardinality' => [
      'cardinality' => $element['#cardinality'],
    ],
  ]);

  // Display error message if the entity browser was not found.
  if (!$entity_browser) {
    $element['entity_browser'] = [
      '#type' => 'markup',
      '#markup' => is_string($element['#entity_browser']) ? t('Entity browser @browser not found.', [
        '@browser' => $element['#entity_browser'],
      ]) : t('Entity browser not found.'),
    ];
  }
  else {
    $display = $entity_browser
      ->getDisplay();
    $display
      ->setUuid(sha1(implode('-', array_merge([
      $complete_form['#build_id'],
    ], $element['#parents']))));
    $element['entity_browser'] = [
      '#eb_parents' => array_merge($element['#parents'], [
        'entity_browser',
      ]),
    ];
    $element['entity_browser'] = $display
      ->displayEntityBrowser($element['entity_browser'], $form_state, $complete_form, [
      'validators' => $validators,
      'selected_entities' => $entity_browser_preselected_entities,
      'widget_context' => $element['#widget_context'],
    ]);
    $hidden_id = Html::getUniqueId($element['#id'] . '-target');
    $element['entity_ids'] = [
      '#type' => 'hidden',
      '#id' => $hidden_id,
      // We need to repeat ID here as it is otherwise skipped when rendering.
      '#attributes' => [
        'id' => $hidden_id,
        'class' => [
          'eb-target',
        ],
      ],
      '#default_value' => $default_value,
    ];
    $element['#attached']['drupalSettings']['entity_browser'] = [
      $entity_browser
        ->getDisplay()
        ->getUuid() => [
        'cardinality' => $element['#cardinality'],
        'selection_mode' => $element['#selection_mode'],
        'selector' => '#' . $hidden_id,
      ],
    ];
  }
  return $element;
}