SynonymsEntitySelect.php in Synonyms 8
File
src/Element/SynonymsEntitySelect.php
View source
<?php
namespace Drupal\synonyms\Element;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\Select;
class SynonymsEntitySelect extends Select {
const DELIMITER = ':';
public function getInfo() {
$info = parent::getInfo();
array_unshift($info['#process'], [
get_class($this),
'elementSynonymsEntitySelect',
]);
$info['#element_validate'][] = [
get_class($this),
'validateEntitySelect',
];
return $info;
}
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
$return = parent::valueCallback($element, $input, $form_state);
if (is_null($return) && isset($element['#default_value'])) {
$return = $element['#default_value'];
}
if (is_array($return)) {
$return = array_map(function ($item) {
return (string) $item;
}, $return);
}
elseif (!is_null($return)) {
$return = (string) $return;
}
return $return;
}
public static function elementSynonymsEntitySelect(array &$element, FormStateInterface $form_state, array &$complete_form) {
$options = [];
$selection = \Drupal::service('plugin.manager.entity_reference_selection')
->getInstance([
'target_type' => $element['#target_type'],
'target_bundles' => $element['#target_bundles'],
'entity' => NULL,
]);
$bundle_info = \Drupal::getContainer()
->get('entity_type.bundle.info')
->getBundleInfo($element['#target_type']);
$referenceable_entities = $selection
->getReferenceableEntities();
$entities = [];
foreach ($referenceable_entities as $bundle_entity_ids) {
$entities = array_merge($entities, array_keys($bundle_entity_ids));
}
if (!empty($entities)) {
$entities = \Drupal::entityTypeManager()
->getStorage($element['#target_type'])
->loadMultiple($entities);
}
foreach ($referenceable_entities as $bundle => $entity_ids) {
$synonyms = \Drupal::getContainer()
->get('synonyms.behavior.select')
->getSynonymsMultiple(array_intersect_key($entities, $entity_ids));
foreach ($entity_ids as $entity_id => $label) {
$options[$entity_id] = $label;
foreach ($synonyms[$entity_id] as $synonym) {
$options[$entity_id . self::DELIMITER . $synonym['synonym']] = $synonym['wording'];
}
}
}
$element['#options'] = $options;
return $element;
}
public static function validateEntitySelect(array &$element, FormStateInterface $form_state, array &$complete_form) {
$value = $form_state
->getValue($element['#parents']);
if (!isset($element['#multiple']) || !$element['#multiple']) {
$value = [
$value,
];
}
$unique = [];
foreach ($value as $v) {
if (!empty($v)) {
if (!is_numeric($v)) {
$v = explode(self::DELIMITER, $v, 2)[0];
}
$unique[$v] = $v;
}
}
$items = [];
foreach ($unique as $v) {
$items[] = [
$element['#key_column'] => $v,
];
}
if (!isset($element['#multiple']) || !$element['#multiple']) {
$items = reset($items);
}
$form_state
->setValueForElement($element, $items);
}
}