SynonymsEntityAutocomplete.php in Synonyms 8
File
src/Element/SynonymsEntityAutocomplete.php
View source
<?php
namespace Drupal\synonyms\Element;
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\Tags;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\Textfield;
use Drupal\Core\Site\Settings;
class SynonymsEntityAutocomplete extends Textfield {
public function getInfo() {
$info = parent::getInfo();
$info['#target_type'] = NULL;
$info['#target_bundles'] = NULL;
$info['#suggestion_size'] = 10;
$info['#suggest_only_unique'] = FALSE;
$info['#match'] = 'CONTAINS';
array_unshift($info['#process'], [
get_class($this),
'elementSynonymsEntityAutocomplete',
]);
$info['#element_validate'][] = [
get_class($this),
'validateEntityAutocomplete',
];
return $info;
}
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
$entities = NULL;
if ($input === FALSE) {
if (isset($element['#default_value'])) {
$entities = [];
foreach ($element['#default_value'] as $entity) {
$entities[] = $entity;
}
}
}
elseif ($input !== FALSE && is_array($input)) {
$entity_ids = array_map(function (array $item) {
return $item['target_id'];
}, $input);
$entities = \Drupal::entityTypeManager()
->getStorage($element['#target_type'])
->loadMultiple($entity_ids);
}
if (is_array($entities)) {
$value = [];
foreach ($entities as $entity) {
$value[] = $entity
->label() . ' (' . $entity
->id() . ')';
}
return Tags::implode($value);
}
}
public static function elementSynonymsEntityAutocomplete(array &$element, FormStateInterface $form_state, array &$complete_form) {
$data = [
'target_type' => $element['#target_type'],
'target_bundles' => $element['#target_bundles'],
'suggestion_size' => $element['#suggestion_size'],
'suggest_only_unique' => $element['#suggest_only_unique'],
'match' => $element['#match'],
];
$token = Crypt::hmacBase64(serialize($data), Settings::getHashSalt());
$key_value_storage = \Drupal::keyValue('synonyms_entity_autocomplete');
$key_value_storage
->setIfNotExists($token, $data);
$element['#autocomplete_route_name'] = 'synonyms.entity_autocomplete';
$element['#autocomplete_route_parameters'] = [
'target_type' => $element['#target_type'],
'token' => $token,
];
return $element;
}
public static function validateEntityAutocomplete(array &$element, FormStateInterface $form_state, array &$complete_form) {
$tokens = Tags::explode($form_state
->getValue($element['#parents']));
$value = [];
$autocomplete_service = \Drupal::getContainer()
->get('synonyms.behavior.autocomplete');
foreach ($tokens as $token) {
$entity_id = self::extractEntityIdFromAutocompleteInput($token);
if (!$entity_id) {
$lookup = $autocomplete_service
->autocompleteLookup($token, $element['#autocomplete_route_parameters']['token']);
$lookup = array_shift($lookup);
if ($lookup) {
$entity_id = $lookup['entity_id'];
}
}
if ($entity_id) {
$value[] = [
'target_id' => $entity_id,
];
}
}
$form_state
->setValueForElement($element, $value);
}
public static function extractEntityIdFromAutocompleteInput($input) {
$match = NULL;
if (preg_match("/.+\\s\\((\\d+)\\)/", $input, $matches)) {
$match = $matches[1];
}
return $match;
}
}