protected function EntityReferenceBrowserWidget::formElementEntities in Entity Browser 8
Same name and namespace in other branches
- 8.2 src/Plugin/Field/FieldWidget/EntityReferenceBrowserWidget.php \Drupal\entity_browser\Plugin\Field\FieldWidget\EntityReferenceBrowserWidget::formElementEntities()
Determines the entities used for the form element.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field item to extract the entities from.
array $element: The form element.
\Drupal\Core\Form\FormStateInterface $form_state: The form state.
Return value
\Drupal\Core\Entity\EntityInterface[] The list of entities for the form element.
1 call to EntityReferenceBrowserWidget::formElementEntities()
- EntityReferenceBrowserWidget::formElement in src/
Plugin/ Field/ FieldWidget/ EntityReferenceBrowserWidget.php - Returns the form for a single field widget.
File
- src/
Plugin/ Field/ FieldWidget/ EntityReferenceBrowserWidget.php, line 794
Class
- EntityReferenceBrowserWidget
- Plugin implementation of the 'entity_reference' widget for entity browser.
Namespace
Drupal\entity_browser\Plugin\Field\FieldWidgetCode
protected function formElementEntities(FieldItemListInterface $items, array $element, FormStateInterface $form_state) {
$entities = [];
$entity_type = $this->fieldDefinition
->getFieldStorageDefinition()
->getSetting('target_type');
$entity_storage = $this->entityTypeManager
->getStorage($entity_type);
// Find IDs from target_id element (it stores selected entities in form).
// This was added to help solve a really edge casey bug in IEF.
if (($target_id_entities = $this
->getEntitiesByTargetId($element, $form_state)) !== FALSE) {
return $target_id_entities;
}
// Determine if we're submitting and if submit came from this widget.
$is_relevant_submit = FALSE;
if ($trigger = $form_state
->getTriggeringElement()) {
// Can be triggered by hidden target_id element or "Remove" button.
$last_parent = end($trigger['#parents']);
if (in_array($last_parent, [
'target_id',
'remove_button',
'replace_button',
])) {
$is_relevant_submit = TRUE;
// In case there are more instances of this widget on the same page we
// need to check if submit came from this instance.
$field_name_key = end($trigger['#parents']) === 'target_id' ? 2 : static::$deleteDepth + 1;
$field_name_key = count($trigger['#parents']) - $field_name_key;
$is_relevant_submit &= $trigger['#parents'][$field_name_key] === $this->fieldDefinition
->getName() && array_slice($trigger['#parents'], 0, count($element['#field_parents'])) == $element['#field_parents'];
}
}
if ($is_relevant_submit) {
// Submit was triggered by hidden "target_id" element when entities were
// added via entity browser.
if (!empty($trigger['#ajax']['event']) && $trigger['#ajax']['event'] == 'entity_browser_value_updated') {
$parents = $trigger['#parents'];
}
elseif ($trigger['#type'] == 'submit' && strpos($trigger['#name'], $this->fieldDefinition
->getName() . '_remove_') === 0) {
$parents = array_merge(array_slice($trigger['#parents'], 0, -static::$deleteDepth), [
'target_id',
]);
}
if (isset($parents) && ($value = $form_state
->getValue($parents))) {
$entities = EntityBrowserElement::processEntityIds($value);
return $entities;
}
return $entities;
}
elseif ($form_state
->has([
'entity_browser_widget',
$this
->getFormStateKey($items),
])) {
$stored_ids = $form_state
->get([
'entity_browser_widget',
$this
->getFormStateKey($items),
]);
$indexed_entities = $entity_storage
->loadMultiple($stored_ids);
// Selection can contain same entity multiple times. Since loadMultiple()
// returns unique list of entities, it's necessary to recreate list of
// entities in order to preserve selection of duplicated entities.
foreach ($stored_ids as $entity_id) {
if (isset($indexed_entities[$entity_id])) {
$entities[] = $indexed_entities[$entity_id];
}
}
return $entities;
}
else {
foreach ($items as $item) {
if (isset($item->target_id)) {
$entity = $entity_storage
->load($item->target_id);
if (!empty($entity)) {
$entities[] = $entity;
}
}
}
return $entities;
}
}