ReferenceWidget.php in Select (or other) 8.3
File
src/Plugin/Field/FieldWidget/EntityReference/ReferenceWidget.php
View source
<?php
namespace Drupal\select_or_other\Plugin\Field\FieldWidget\EntityReference;
use Drupal\Component\Utility\Tags;
use Drupal\Core\Entity\Element\EntityAutocomplete;
use Drupal\Core\Entity\Entity;
use Drupal\Core\Entity\EntityReferenceSelection\SelectionWithAutocreateInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\select_or_other\Plugin\Field\FieldWidget\SelectOrOtherWidgetBase;
use Drupal\user\EntityOwnerInterface;
class ReferenceWidget extends SelectOrOtherWidgetBase {
protected static function prepareElementValuesForValidation(array &$element) {
if ($element['#tags']) {
$element['#value'] = Tags::implode($element['#value']);
}
}
protected function getEntityStorage() {
$target_type = $this
->getFieldSetting('target_type');
return \Drupal::entityTypeManager()
->getStorage($target_type);
}
protected function getBundleKey() {
$entity_keys = $this
->getEntityStorage()
->getEntityType()
->get('entity_keys');
return $entity_keys['bundle'];
}
protected function getOptions() {
$options = [];
$entityStorage = $this
->getEntityStorage();
$bundle_key = $this
->getBundleKey();
$target_bundles = $this
->getSelectionHandlerSetting('target_bundles');
$properties = [
$bundle_key => $target_bundles,
];
$entities = $entityStorage
->loadByProperties($properties);
foreach ($entities as $entity) {
$options["{$entity->label()} ({$entity->id()})"] = $entity
->label();
}
return $options;
}
protected function prepareSelectedOptions(array $options) {
$prepared_options = [];
$entities = $this
->getEntityStorage()
->loadMultiple($options);
foreach ($entities as $entity) {
$prepared_options[] = "{$entity->label()} ({$entity->id()})";
}
return $prepared_options;
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$entity = $items
->getEntity();
$element = $element + [
'#target_type' => $this
->getFieldSetting('target_type'),
'#selection_handler' => $this
->getFieldSetting('handler'),
'#selection_settings' => $this
->getFieldSetting('handler_settings'),
'#autocreate' => [
'bundle' => $this
->getAutocreateBundle(),
'uid' => $entity instanceof EntityOwnerInterface ? $entity
->getOwnerId() : \Drupal::currentUser()
->id(),
],
'#validate_reference' => TRUE,
'#tags' => $this
->getFieldSetting('target_type') === 'taxonomy_term',
'#merged_values' => TRUE,
];
$element['#element_validate'] = [
[
get_class($this),
'validateReferenceWidget',
],
];
return $element;
}
protected function getSelectionHandlerSetting($setting_name) {
$settings = $this
->getFieldSetting('handler_settings');
return isset($settings[$setting_name]) ? $settings[$setting_name] : NULL;
}
protected function getAutocreateBundle() {
$bundle = NULL;
if ($this
->getSelectionHandlerSetting('auto_create')) {
if (($target_bundles = $this
->getSelectionHandlerSetting('target_bundles')) && count($target_bundles) == 1) {
$bundle = reset($target_bundles);
}
else {
$bundles = \Drupal::entityManager()
->getBundleInfo($this
->getFieldSetting('target_type'));
$bundle = key($bundles);
}
}
return $bundle;
}
public static function validateReferenceWidget(array &$element, FormStateInterface $form_state, array &$complete_form) {
self::prepareElementValuesForValidation($element);
EntityAutocomplete::validateEntityAutocomplete($element, $form_state, $complete_form);
}
public static function isApplicable(FieldDefinitionInterface $field_definition) {
$options = $field_definition
->getSettings();
$handler = \Drupal::service('plugin.manager.entity_reference_selection')
->getInstance($options);
return $handler instanceof SelectionWithAutocreateInterface && $options['handler_settings']['auto_create'];
}
}
Classes
Name |
Description |
ReferenceWidget |
Plugin implementation of the 'select_or_other_reference' widget. |