You are here

public static function EntitySelect::processEntitySelect in Commerce Core 8.2

Process callback.

File

src/Element/EntitySelect.php, line 69

Class

EntitySelect
Provides a form input element for selecting one or multiple entities.

Namespace

Drupal\commerce\Element

Code

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

  // Nothing to do if there is no target entity type.
  if (empty($element['#target_type'])) {
    throw new \InvalidArgumentException('Missing required #target_type parameter.');
  }

  /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
  $storage = \Drupal::service('entity_type.manager')
    ->getStorage($element['#target_type']);
  $entity_count = $storage
    ->getQuery()
    ->accessCheck(TRUE)
    ->count()
    ->execute();
  $element['#tree'] = TRUE;

  // No need to show anything, there's only one possible value.
  if ($element['#required'] && $entity_count == 1 && $element['#hide_single_entity']) {
    $entity_ids = $storage
      ->getQuery()
      ->accessCheck(TRUE)
      ->execute();
    $element['value'] = [
      '#type' => 'hidden',
      '#value' => reset($entity_ids),
    ];
    return $element;
  }
  if ($entity_count <= $element['#autocomplete_threshold']) {

    // Start with a query to get only access-filtered results.
    $entity_ids = $storage
      ->getQuery()
      ->accessCheck(TRUE)
      ->execute();
    $entities = $storage
      ->loadMultiple($entity_ids);
    $entity_labels = EntityHelper::extractLabels($entities);

    // Radio buttons don't have a None option by default.
    if (!$element['#multiple'] && !$element['#required']) {
      $entity_labels = [
        '' => t('None'),
      ] + $entity_labels;
    }
    $element['value'] = [
      '#type' => $element['#multiple'] ? 'checkboxes' : 'radios',
      '#required' => $element['#required'],
      '#options' => $entity_labels,
    ];
    if (!empty($element['#default_value'])) {
      $element['value']['#default_value'] = $element['#default_value'];
    }
  }
  else {
    $default_value = NULL;
    if (!empty($element['#default_value'])) {

      // Upcast ids into entities, as expected by entity_autocomplete.
      if ($element['#multiple']) {
        $default_value = $storage
          ->loadMultiple($element['#default_value']);
      }
      else {
        $default_value = $storage
          ->load($element['#default_value']);
      }
    }
    $element['value'] = [
      '#type' => 'entity_autocomplete',
      '#target_type' => $element['#target_type'],
      '#tags' => $element['#multiple'],
      '#required' => $element['#required'],
      '#default_value' => $default_value,
      '#size' => $element['#autocomplete_size'],
      '#placeholder' => $element['#autocomplete_placeholder'],
      '#maxlength' => NULL,
    ];
  }

  // These keys only make sense on the actual input element.
  foreach ([
    '#title',
    '#title_display',
    '#description',
    '#ajax',
  ] as $key) {
    if (isset($element[$key])) {
      $element['value'][$key] = $element[$key];
      unset($element[$key]);
    }
  }
  return $element;
}