public static function SynonymsEntitySelect::elementSynonymsEntitySelect in Synonyms 2.0.x
Form element process callback for 'synonyms_entity_select' type.
File
- modules/
synonyms_select/ src/ Element/ SynonymsEntitySelect.php, line 69
Class
- SynonymsEntitySelect
- Form element for synonyms-friendly entity select.
Namespace
Drupal\synonyms_select\ElementCode
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::service('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::service('synonyms.behavior.select')
->selectGetSynonymsMultiple(array_intersect_key($entities, $entity_ids));
$bundle_key = isset($bundle_info[$bundle]) ? $bundle_info[$bundle]['label'] : $bundle;
$bundle_key = (string) $bundle_key;
$options[$bundle_key] = [];
foreach ($entity_ids as $entity_id => $label) {
$options[$bundle_key][$entity_id] = $label;
foreach ($synonyms[$entity_id] as $synonym) {
$options[$bundle_key][$entity_id . self::DELIMITER . $synonym['synonym']] = $synonym['wording'];
}
}
}
if (count($options) == 1) {
// Strip the bundle nesting if there is only 1 bundle after all.
$options = reset($options);
}
// Now we can "mutate" into a simple "select" element type.
$element['#type'] = 'select';
$element['#options'] = $options;
return $element;
}