View source
<?php
namespace Drupal\commerce\Element;
use Drupal\commerce\EntityHelper;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\FormElement;
class EntitySelect extends FormElement {
public function getInfo() {
$class = get_class($this);
return [
'#input' => TRUE,
'#target_type' => '',
'#multiple' => FALSE,
'#hide_single_entity' => TRUE,
'#autocomplete_threshold' => 7,
'#autocomplete_size' => 60,
'#autocomplete_placeholder' => '',
'#process' => [
[
$class,
'processEntitySelect',
],
[
$class,
'processAjaxForm',
],
],
'#element_validate' => [
[
$class,
'validateEntitySelect',
],
],
];
}
public static function processEntitySelect(&$element, FormStateInterface $form_state, &$complete_form) {
if (empty($element['#target_type'])) {
throw new \InvalidArgumentException('Missing required #target_type parameter.');
}
$storage = \Drupal::service('entity_type.manager')
->getStorage($element['#target_type']);
$entity_count = $storage
->getQuery()
->accessCheck(TRUE)
->count()
->execute();
$element['#tree'] = TRUE;
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']) {
$entity_ids = $storage
->getQuery()
->accessCheck(TRUE)
->execute();
$entities = $storage
->loadMultiple($entity_ids);
$entity_labels = EntityHelper::extractLabels($entities);
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'])) {
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,
];
}
foreach ([
'#title',
'#title_display',
'#description',
'#ajax',
] as $key) {
if (isset($element[$key])) {
$element['value'][$key] = $element[$key];
unset($element[$key]);
}
}
return $element;
}
public static function validateEntitySelect(&$element, FormStateInterface $form_state, &$complete_form) {
$value_element = $element['value'];
$value = $form_state
->getValue($value_element['#parents']);
if (is_array($value)) {
if ($value_element['#type'] == 'checkboxes') {
$value = array_filter($value);
$value = array_values($value);
}
elseif (!empty($value_element['#tags'])) {
$value = array_column($value, 'target_id');
}
}
$form_state
->setValueForElement($element, $value);
}
}