View source
<?php
namespace Drupal\dynamic_entity_reference\Plugin\Field\FieldType;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\DataReferenceTargetDefinition;
use Drupal\dynamic_entity_reference\DataDynamicReferenceDefinition;
class DynamicEntityReferenceItem extends EntityReferenceItem {
public static function defaultStorageSettings() {
return [
'exclude_entity_types' => TRUE,
'entity_type_ids' => [],
];
}
public static function defaultFieldSettings() {
$default_settings = [];
$labels = \Drupal::service('entity_type.repository')
->getEntityTypeLabels(TRUE);
$options = $labels[(string) t('Content', [], [
'context' => 'Entity type group',
])];
foreach (array_keys($options) as $entity_type_id) {
$default_settings[$entity_type_id] = [
'handler' => "default:{$entity_type_id}",
'handler_settings' => [],
];
}
return $default_settings;
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['target_id'] = DataReferenceTargetDefinition::create('integer')
->setLabel(t('Entity ID'))
->setSetting('unsigned', TRUE)
->setRequired(TRUE);
$properties['target_type'] = DataReferenceTargetDefinition::create('string')
->setLabel(new TranslatableMarkup('Target Entity Type'))
->setRequired(TRUE);
$properties['entity'] = DataDynamicReferenceDefinition::create('entity')
->setLabel(t('Entity'))
->setDescription(new TranslatableMarkup('The referenced entity'))
->setComputed(TRUE)
->setReadOnly(FALSE);
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
$columns = [
'target_id' => [
'description' => 'The ID of the target entity.',
'type' => 'int',
'unsigned' => TRUE,
],
'target_type' => [
'description' => 'The Entity Type ID of the target entity.',
'type' => 'varchar',
'length' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
],
];
$schema = [
'columns' => $columns,
'indexes' => [
'target_id' => [
'target_id',
'target_type',
],
],
];
return $schema;
}
public function onChange($property_name, $notify = TRUE) {
$entity_property = $this
->get('entity');
if ($property_name == 'target_type' && !$entity_property
->getValue()) {
$entity_property
->getTargetDefinition()
->setEntityTypeId($this
->get('target_type')
->getValue());
}
elseif ($property_name == 'entity') {
$this
->writePropertyValue('target_type', $entity_property
->getValue()
->getEntityTypeId());
}
elseif ($property_name == 'target_id') {
$entity_property
->getTargetDefinition()
->setEntityTypeId($this
->get('target_type')
->getValue() ?: '');
}
parent::onChange($property_name, $notify);
}
public function getSettableOptions(AccountInterface $account = NULL) {
$field_definition = $this
->getFieldDefinition();
$entity_type_manager = \Drupal::entityTypeManager();
$entity_type_bundles_info = \Drupal::service('entity_type.bundle.info');
$selection_manager = \Drupal::service('plugin.manager.dynamic_entity_reference_selection');
$options = [];
$settings = $this
->getSettings();
$target_types = static::getTargetTypes($settings);
foreach ($target_types as $target_type) {
$options[$target_type] = $selection_manager
->getSelectionHandler($field_definition, $this
->getEntity(), $target_type)
->getReferenceableEntities();
}
if (empty($options)) {
return [];
}
$return = [];
foreach ($options as $target_type => $referenceable_entities) {
$target_type_info = $entity_type_manager
->getDefinition($target_type);
$target_type_label = $target_type_info
->getLabel();
$bundles = $entity_type_bundles_info
->getBundleInfo($target_type);
foreach ($referenceable_entities as $bundle => $entities) {
$bundle_label = $bundles[$bundle]['label'];
foreach ($entities as $id => $entity_label) {
if (count($target_types) > 1) {
if ($target_type_info
->hasKey('bundle')) {
$return[(string) $target_type_label . ': ' . (string) $bundle_label]["{$target_type}-{$id}"] = "{$entity_label} ({$target_type_label}:{$id})";
}
else {
$return[(string) $target_type_label]["{$target_type}-{$id}"] = "{$entity_label} ({$target_type_label}:{$id})";
}
}
else {
$return[(string) $bundle_label]["{$target_type}-{$id}"] = "{$entity_label} ({$target_type_label}:{$id})";
}
}
}
}
return $return;
}
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$labels = \Drupal::service('entity_type.repository')
->getEntityTypeLabels(TRUE);
$options = $labels[(string) t('Content', [], [
'context' => 'Entity type group',
])];
foreach (array_keys($options) as $entity_type_id) {
if (!static::entityHasIntegerId($entity_type_id)) {
unset($options[$entity_type_id]);
}
}
$element['exclude_entity_types'] = [
'#type' => 'checkbox',
'#title' => t('Exclude the selected items'),
'#default_value' => $this
->getSetting('exclude_entity_types'),
'#disabled' => $has_data,
];
$element['entity_type_ids'] = [
'#type' => 'select',
'#title' => t('Select items'),
'#options' => $options,
'#default_value' => $this
->getSetting('entity_type_ids'),
'#disabled' => $has_data,
'#multiple' => TRUE,
'#element_validate' => [
[
DynamicEntityReferenceItem::class,
'storageSettingsFormValidate',
],
],
];
return $element;
}
public static function storageSettingsFormValidate(array &$element, FormStateInterface $form_state, array $form) {
$labels = \Drupal::service('entity_type.repository')
->getEntityTypeLabels(TRUE);
$options = array_filter(array_keys($labels[(string) t('Content', [], [
'context' => 'Entity type group',
])]), function ($entity_type_id) {
return static::entityHasIntegerId($entity_type_id);
});
$exclude_entity_types = $form_state
->getValue([
'settings',
'exclude_entity_types',
], 0);
$entity_type_ids = $form_state
->getValue([
'settings',
'entity_type_ids',
], []);
$diff = array_diff($options, $entity_type_ids);
if (!$exclude_entity_types && empty($entity_type_ids) || $exclude_entity_types && empty($diff)) {
$form_state
->setError($element, t('Select at least one entity type ID.'));
}
}
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$settings_form = [];
$settings = $this
->getSettings();
foreach (static::getTargetTypes($settings) as $target_type) {
$entity_type = \Drupal::entityTypeManager()
->getDefinition($target_type);
$settings_form[$target_type] = $this
->targetTypeFieldSettingsForm($form, $form_state, $target_type);
$settings_form[$target_type]['handler']['#title'] = t('Reference type for @target_type', [
'@target_type' => $entity_type
->getLabel(),
]);
}
return $settings_form;
}
protected function targetTypeFieldSettingsForm(array $form, FormStateInterface $form_state, $target_type) {
$field = $form_state
->getFormObject()
->getEntity();
$field_settings = $field
->getSettings();
$manager = \Drupal::service('plugin.manager.dynamic_entity_reference_selection');
$selection_plugins = $manager
->getSelectionGroups($target_type);
$handlers_options = [];
foreach (array_keys($selection_plugins) as $selection_group_id) {
if (array_key_exists($selection_group_id, $selection_plugins[$selection_group_id])) {
$handlers_options[$selection_group_id] = Html::escape($selection_plugins[$selection_group_id][$selection_group_id]['label']);
}
elseif (array_key_exists($selection_group_id . ':' . $target_type, $selection_plugins[$selection_group_id])) {
$selection_group_plugin = $selection_group_id . ':' . $target_type;
$handlers_options[$selection_group_plugin] = Html::escape($selection_plugins[$selection_group_id][$selection_group_plugin]['base_plugin_label']);
}
}
$form = [
'#type' => 'container',
'#process' => [
[
EntityReferenceItem::class,
'fieldSettingsAjaxProcess',
],
],
'#element_validate' => [
[
DynamicEntityReferenceItem::class,
'fieldSettingsFormValidate',
],
],
];
$form['handler'] = [
'#type' => 'details',
'#title' => t('Reference type'),
'#open' => TRUE,
'#tree' => TRUE,
'#process' => [
[
EntityReferenceItem::class,
'formProcessMergeParent',
],
],
];
$form['handler']['handler'] = [
'#type' => 'select',
'#title' => t('Reference method'),
'#options' => $handlers_options,
'#default_value' => $field_settings[$target_type]['handler'],
'#required' => TRUE,
'#ajax' => TRUE,
'#limit_validation_errors' => [],
];
$form['handler']['handler_submit'] = [
'#type' => 'submit',
'#value' => t('Change handler'),
'#limit_validation_errors' => [],
'#attributes' => [
'class' => [
'js-hide',
],
],
'#submit' => [
'entity_reference_settings_ajax_submit',
],
];
$form['handler']['handler_settings'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'entity_reference-settings',
],
],
];
$handler = $manager
->getSelectionHandler($field, NULL, $target_type);
$form['handler']['handler_settings'] += $handler
->buildConfigurationForm([], $form_state);
return $form;
}
public static function fieldSettingsFormValidate(array $form, FormStateInterface $form_state) {
$field = $form_state
->getFormObject()
->getEntity();
foreach (static::getTargetTypes($field
->getSettings()) as $target_type) {
if ($form_state
->getValue([
'settings',
$target_type,
'handler_settings',
'target_bundles',
]) === []) {
$form_state
->setValue([
'settings',
$target_type,
'handler_settings',
'target_bundles',
], NULL);
}
$form_state
->unsetValue([
'settings',
$target_type,
'handler_settings',
'target_bundles_update',
]);
$form_state
->unsetValue([
'settings',
$target_type,
'handler_submit',
]);
}
}
public static function mainPropertyName() {
return 'target_id';
}
public function setValue($values, $notify = TRUE) {
if (isset($values) && !is_array($values)) {
$this
->set('entity', $values, $notify);
}
else {
if (empty($values['target_type']) && !empty($values['target_id']) && !(isset($values['entity']) && $values['entity'] instanceof EntityInterface)) {
throw new \InvalidArgumentException('No entity type was provided, value is not a valid entity.');
}
FieldItemBase::setValue($values, FALSE);
if (is_array($values) && array_key_exists('target_id', $values) && array_key_exists('target_type', $values) && !isset($values['entity'])) {
$this
->onChange('target_type', FALSE);
$this
->onChange('target_id', FALSE);
}
elseif (is_array($values) && !array_key_exists('target_id', $values) && !array_key_exists('target_type', $values) && isset($values['entity'])) {
$this
->onChange('entity', FALSE);
}
elseif (is_array($values) && array_key_exists('target_id', $values) && array_key_exists('target_type', $values) && isset($values['entity'])) {
$entity_property = $this
->get('entity');
$entity_id = $entity_property
->getTargetIdentifier();
$target_definition = $entity_property
->getTargetDefinition();
$entity_type = $target_definition
->getEntityTypeId();
if (!$this->entity
->isNew() && $values['target_id'] !== NULL && ($entity_id !== $values['target_id'] || $entity_type !== $values['target_type'])) {
throw new \InvalidArgumentException('The target id, target type and entity passed to the dynamic entity reference item do not match.');
}
}
if ($notify && $this->parent) {
$this->parent
->onChange($this
->getName());
}
}
}
public function getValue() {
$values = parent::getValue();
if (!empty($values['target_type'])) {
$this
->get('entity')
->getTargetDefinition()
->setEntityTypeId($values['target_type']);
}
return $values;
}
public function isEmpty() {
if ($this->target_id !== NULL && $this->target_type !== NULL) {
return FALSE;
}
if ($this->entity && $this->entity instanceof EntityInterface) {
return FALSE;
}
return TRUE;
}
public function preSave() {
if ($this
->hasNewEntity()) {
if ($this->entity
->isNew()) {
$this->entity
->save();
}
$this->target_id = $this->entity
->id();
$this->target_type = $this->entity
->getEntityTypeId();
}
if (!$this
->isEmpty() && $this->target_id === NULL) {
$this->target_id = $this->entity
->id();
}
}
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$manager = \Drupal::service('plugin.manager.dynamic_entity_reference_selection');
$settings = $field_definition
->getSettings();
foreach (static::getTargetTypes($settings) as $target_type) {
$values['target_type'] = $target_type;
if ($referenceable = $manager
->getSelectionHandler($field_definition, NULL, $target_type)
->getReferenceableEntities()) {
$group = array_rand($referenceable);
$values['target_id'] = array_rand($referenceable[$group]);
return $values;
}
}
}
public static function calculateDependencies(FieldDefinitionInterface $field_definition) {
$dependencies = FieldItemBase::calculateDependencies($field_definition);
$entity_repository = \Drupal::service('entity.repository');
if ($default_value = $field_definition
->getDefaultValueLiteral()) {
foreach ($default_value as $value) {
if (is_array($value) && isset($value['target_uuid']) && isset($value['target_type'])) {
$entity = $entity_repository
->loadEntityByUuid($value['target_type'], $value['target_uuid']);
if ($entity) {
$dependencies[$entity
->getEntityType()
->getConfigDependencyKey()][] = $entity
->getConfigDependencyName();
}
}
}
}
$entity_type_manager = \Drupal::entityTypeManager();
$settings = $field_definition
->getSettings();
foreach (static::getTargetTypes($settings) as $target_type) {
$handler = $settings[$target_type]['handler_settings'];
if (!empty($handler['target_bundles'])) {
$target_entity_type = $entity_type_manager
->getDefinition($target_type);
if ($bundle_entity_type_id = $target_entity_type
->getBundleEntityType()) {
if ($storage = $entity_type_manager
->getStorage($bundle_entity_type_id)) {
foreach ($storage
->loadMultiple($handler['target_bundles']) as $bundle) {
$dependencies[$bundle
->getConfigDependencyKey()][] = $bundle
->getConfigDependencyName();
}
}
}
}
}
return $dependencies;
}
public static function onDependencyRemoval(FieldDefinitionInterface $field_definition, array $dependencies) {
$changed = FALSE;
if ($default_value = $field_definition
->getDefaultValueLiteral()) {
foreach ($default_value as $key => $value) {
if (is_array($value) && isset($value['target_uuid']) && isset($value['target_type'])) {
$entity = \Drupal::service('entity.repository')
->loadEntityByUuid($value['target_type'], $value['target_uuid']);
if ($entity && isset($dependencies[$entity
->getConfigDependencyKey()][$entity
->getConfigDependencyName()])) {
unset($default_value[$key]);
$changed = TRUE;
}
}
}
if ($changed) {
$field_definition
->setDefaultValue($default_value);
}
}
$entity_type_manager = \Drupal::entityTypeManager();
$settings = $field_definition
->getSettings();
foreach (static::getTargetTypes($settings) as $target_type) {
$bundles_changed = FALSE;
$handler_settings = $settings[$target_type]['handler_settings'];
if (!empty($handler_settings['target_bundles'])) {
$target_entity_type = $entity_type_manager
->getDefinition($target_type);
if ($bundle_entity_type_id = $target_entity_type
->getBundleEntityType()) {
if ($storage = $entity_type_manager
->getStorage($bundle_entity_type_id)) {
foreach ($storage
->loadMultiple($handler_settings['target_bundles']) as $bundle) {
if (isset($dependencies[$bundle
->getConfigDependencyKey()][$bundle
->getConfigDependencyName()])) {
unset($handler_settings['target_bundles'][$bundle
->id()]);
$auto_create_bundle = !empty($handler_settings['auto_create_bundle']) ? $handler_settings['auto_create_bundle'] : FALSE;
if ($auto_create_bundle && $auto_create_bundle == $bundle
->id()) {
$handler_settings['auto_create'] = FALSE;
$handler_settings['auto_create_bundle'] = NULL;
}
$bundles_changed = TRUE;
if ($handler_settings['target_bundles'] === []) {
\Drupal::logger('dynamic_entity_reference')
->critical('The %target_bundle bundle (entity type: %target_entity_type) was deleted. As a result, the %field_name dynamic entity reference field (entity_type: %entity_type, bundle: %bundle) no longer has any valid bundle it can reference. The field is not working correctly anymore and has to be adjusted.', [
'%target_bundle' => $bundle
->label(),
'%target_entity_type' => $bundle
->getEntityType()
->getBundleOf(),
'%field_name' => $field_definition
->getName(),
'%entity_type' => $field_definition
->getTargetEntityTypeId(),
'%bundle' => $field_definition
->getTargetBundle(),
]);
}
}
}
}
}
}
if ($bundles_changed) {
$settings[$target_type]['handler_settings'] = $handler_settings;
$field_definition
->setSettings($settings);
}
$changed |= $bundles_changed;
}
return $changed;
}
public static function getTargetTypes(array $settings) {
$labels = \Drupal::service('entity_type.repository')
->getEntityTypeLabels(TRUE);
$options = array_filter(array_keys($labels[(string) t('Content', [], [
'context' => 'Entity type group',
])]), function ($entity_type_id) {
return static::entityHasIntegerId($entity_type_id);
});
if (!empty($settings['exclude_entity_types'])) {
return array_diff($options, $settings['entity_type_ids'] ?: []);
}
else {
return array_intersect($options, $settings['entity_type_ids'] ?: []);
}
}
public static function entityHasIntegerId($entity_type_id) {
$entity_type = \Drupal::entityTypeManager()
->getDefinition($entity_type_id);
if (!$entity_type instanceof ContentEntityTypeInterface) {
return FALSE;
}
if (!$entity_type
->hasKey('id')) {
return FALSE;
}
$field_definitions = \Drupal::service('entity_field.manager')
->getBaseFieldDefinitions($entity_type_id);
$entity_type_id_definition = $field_definitions[$entity_type
->getKey('id')];
return $entity_type_id_definition
->getType() === 'integer';
}
public static function calculateStorageDependencies(FieldStorageDefinitionInterface $field_definition) {
$dependencies = FieldItemBase::calculateStorageDependencies($field_definition);
$entity_manager = \Drupal::entityTypeManager();
foreach (static::getTargetTypes($field_definition
->getSettings()) as $entity_type_id) {
if ($entity_manager
->hasDefinition($entity_type_id) && ($target_entity_type = $entity_manager
->getDefinition($entity_type_id))) {
$dependencies['module'][] = $target_entity_type
->getProvider();
}
}
return $dependencies;
}
public static function getPreconfiguredOptions() {
$options = [];
$entity_types = \Drupal::entityTypeManager()
->getDefinitions();
$common_references = array_filter($entity_types, function (EntityTypeInterface $entity_type) {
return $entity_type
->isCommonReferenceTarget();
});
foreach ($common_references as $entity_type) {
$options[$entity_type
->id()] = [
'label' => $entity_type
->getLabel(),
'field_storage_config' => [
'settings' => [
'exclude_entity_types' => FALSE,
'entity_type_ids' => [
$entity_type
->id(),
],
],
],
];
}
return $options;
}
}