trait AutoCreationProcessTrait in Select2 Boxes 8
Trait AutoCreationProcessTrait.
@package Drupal\select2boxes
Hierarchy
- trait \Drupal\select2boxes\AutoCreationProcessTrait uses EntityCreationTrait
2 files declare their use of AutoCreationProcessTrait
- MultiSelect2BoxesAutocompleteWidget.php in src/
Plugin/ Field/ FieldWidget/ MultiSelect2BoxesAutocompleteWidget.php - SingleSelect2BoxesAutocompleteWidget.php in src/
Plugin/ Field/ FieldWidget/ SingleSelect2BoxesAutocompleteWidget.php
File
- src/
AutoCreationProcessTrait.php, line 12
Namespace
Drupal\select2boxesView source
trait AutoCreationProcessTrait {
use EntityCreationTrait;
/**
* Process the auto-creations and then normalise the input.
*
* @param array $element
* The element.
* @param mixed $input
* The submitted data for the element.
*
* @return mixed
* The processed input.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public static function processAutoCreation(array &$element, $input) {
// If '_none' is being sent as input then we bail out early.
if ($input === '_none') {
return $element['#multiple'] ? [] : NULL;
}
$data = $element['#select2'];
if (!(isset($data['handler_settings']['auto_create']) && $data['handler_settings']['auto_create']) || !$input) {
// Don't return false even though we were given it... That is bad somehow.
return $input !== FALSE ? $input : ($element['#multiple'] ? [] : NULL);
}
// Prepare the data.
$output = NULL;
$options = $element['#options'];
$target_type = $data['target_type'];
$definition = \Drupal::entityTypeManager()
->getDefinition($target_type);
$target_bundle = !empty($data['handler_settings']['auto_create_bundle']) ? $data['handler_settings']['auto_create_bundle'] : reset($data['handler_settings']['target_bundles']);
// Prepare entity metadata array.
$entity_metadata = [
$target_type,
$target_bundle,
$definition,
];
// Handle multi-value widget.
if ($element['#multiple']) {
// Process each item.
foreach ($input as $item) {
$output[] = self::processValueItem($entity_metadata, $item, $options);
}
}
else {
// Handle single-value widget.
$output = self::processValueItem($entity_metadata, $input, $options);
}
$element['#options'] = $options;
return $output;
}
/**
* Process value item.
*
* @param array $entity_metadata
* Entity metadata array:
* array(entity_type_id, entity_bundle, entity_type_definitions).
* @param mixed $item
* Item value.
* @param array &$options
* Options array passed by reference.
*
* @return int|null|string
* Entity ID of precessed value item.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Core\Entity\EntityStorageException
*/
private static function processValueItem(array $entity_metadata, $item, array &$options) {
list($type, $bundle, $definition) = $entity_metadata;
$entity = static::getEntity($type, $item);
if (!$entity instanceof EntityInterface) {
// Get or create entity (ensuring no accidental duplicates).
$entity = static::getOrCreateEntity($type, [
$definition
->getKey('label') => $item,
$definition
->getKey('bundle') => $bundle,
]);
}
$options += [
$entity
->id() => $entity
->label(),
];
return $entity
->id();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AutoCreationProcessTrait:: |
public static | function | Process the auto-creations and then normalise the input. | |
AutoCreationProcessTrait:: |
private static | function | Process value item. | |
EntityCreationTrait:: |
protected static | function | Get an entity by its ID and entity type. | |
EntityCreationTrait:: |
private static | function | Get entity storage handler for specified entity type. | |
EntityCreationTrait:: |
protected static | function | Get an entity by its properties, or create it if it doesn't exist. |