View source
<?php
namespace Drupal\select2boxes\Plugin\Field\FieldWidget;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsSelectWidget;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\Url;
use Drupal\select2boxes\AutoCreationProcessTrait;
use Drupal\select2boxes\FlatteningOptionsTrait;
use Drupal\select2boxes\PreloadBuildTrait;
class MultiSelect2BoxesAutocompleteWidget extends OptionsSelectWidget {
use PreloadBuildTrait;
use FlatteningOptionsTrait;
use AutoCreationProcessTrait;
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$this
->flatteningOptions($element['#options']);
$field_name = $this->fieldDefinition
->getName();
$identifiers = isset($element['#field_parents']) ? $element['#field_parents'] : [];
$identifiers[] = $field_name;
$fieldIdentifier = implode('_', $identifiers);
$element['#attributes'] = [
'data-jquery-once-autocomplete' => 'true',
'data-select2-multiple' => 'true',
'data-autocomplete-path' => $this
->getAutocompletePath(),
'class' => [
'select2-widget',
'select2-boxes-widget',
],
'data-field-name' => $fieldIdentifier,
];
$settings = $this
->getFieldSettings();
if (isset($settings['handler_settings']['auto_create']) && $settings['handler_settings']['auto_create'] == TRUE) {
$element['#attributes']['data-auto-create-entity'] = 'enabled';
}
$element['#select2'] = [
'fieldName' => $field_name,
] + $settings;
$element['#value_callback'] = [
get_class($this),
'processAutoCreation',
];
$element['#attached']['library'][] = 'select2boxes/widget';
$settings = $this
->getThirdPartySettings('select2boxes');
if (isset($settings['enable_preload']) && $settings['enable_preload'] == '1') {
$this
->attachPreload($element['#attached'], $settings['preload_count'], $this->fieldDefinition);
}
$element['#needs_validation'] = FALSE;
$element['#multiple'] = $element['#validated'] = TRUE;
$element['#attached']['drupalSettings']['initValues'][$fieldIdentifier] = $this
->buildInitValues($items);
$element['#options'] = array_intersect_key($element['#options'], array_flip($this
->getSelectedOptions($items)));
return $element;
}
protected function buildInitValues(FieldItemListInterface $items) {
$target_type = $this
->getFieldSetting('target_type');
$definition = \Drupal::entityTypeManager()
->getDefinition($target_type);
$id_key = $definition
->getKey('id');
$label_key = $definition
->getKey('label');
if ($target_type == 'user') {
$label_key = 'name';
}
$ids = [];
$data_table = $definition
->getDataTable();
foreach ($items
->getIterator() as $item) {
$value = $item
->getValue();
if (!empty($value) && isset($value['target_id'])) {
$ids[] = $item
->getValue()['target_id'];
}
}
if (empty($ids)) {
return [];
}
$select = \Drupal::database()
->select($data_table, 'init');
$select
->fields('init', [
$id_key,
$label_key,
]);
$select
->condition($id_key, $ids, 'IN');
$entities = $select
->execute()
->fetchAllKeyed();
if ($target_type == 'user' && !empty($entities) && isset($entities[0])) {
$entities[0] = $this
->t('Anonymous');
}
return !empty($entities) ? $entities : [];
}
protected function getAutocompletePath() {
$selection_settings = $this
->getFieldSetting('handler_settings');
$data = serialize($selection_settings) . $this
->getFieldSetting('target_type') . $this
->getFieldSetting('handler');
$selection_settings_key = Crypt::hmacBase64($data, Settings::getHashSalt());
$key_value_storage = \Drupal::keyValue('entity_autocomplete');
if (!$key_value_storage
->has($selection_settings_key)) {
$key_value_storage
->set($selection_settings_key, $selection_settings);
}
$params = [
'target_type' => $this
->getFieldSetting('target_type'),
'selection_handler' => $this
->getFieldSetting('handler'),
'selection_settings_key' => $selection_settings_key,
];
return Url::fromRoute('system.entity_autocomplete', $params)
->toString();
}
protected function attachPreload(array &$attached, $count, FieldDefinitionInterface $fieldDefinition) {
$attached['drupalSettings']['preloaded_entries'][$fieldDefinition
->getName()] = $this
->buildPreLoaded($count, $fieldDefinition);
}
}