diff --git a/dynamic_entity_reference.module b/dynamic_entity_reference.module index be53516..6f987b9 100644 --- a/dynamic_entity_reference.module +++ b/dynamic_entity_reference.module @@ -54,3 +54,14 @@ function dynamic_entity_reference_module_implements_alter(&$implementations, $ho $implementations['dynamic_entity_reference'] = $group; } } + +/** + * Implements hook_form_FORM_ID_alter() for 'field_ui_field_storage_add_form'. + */ +function dynamic_entity_reference_form_field_ui_field_storage_add_form_alter(array &$form) { + $optgroup = (string) t('Dynamic Reference'); + // Move the "Dynamic entity reference" option to the end of the list and + // rename it to "Other". + unset($form['add']['new_storage_type']['#options'][$optgroup]['dynamic_entity_reference']); + $form['add']['new_storage_type']['#options'][$optgroup]['dynamic_entity_reference'] = t('Other…'); +} diff --git a/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php b/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php index 388092b..f69d4d3 100644 --- a/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php +++ b/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php @@ -31,7 +31,7 @@ use Drupal\dynamic_entity_reference\DataDynamicReferenceDefinition; * id = "dynamic_entity_reference", * label = @Translation("Dynamic entity reference"), * description = @Translation("An entity field containing a dynamic entity reference."), - * category = @Translation("Reference"), + * category = @Translation("Dynamic Reference"), * no_ui = FALSE, * list_class = "\Drupal\dynamic_entity_reference\Plugin\Field\FieldType\DynamicEntityReferenceFieldItemList", * default_widget = "dynamic_entity_reference_default", @@ -572,4 +572,33 @@ class DynamicEntityReferenceItem extends EntityReferenceItem { return $dependencies; } + /** + * {@inheritdoc} + */ + public static function getPreconfiguredOptions() { + $options = []; + + // Add all the commonly referenced entity types as distinct pre-configured + // options. + $entity_types = \Drupal::entityTypeManager()->getDefinitions(); + $common_references = array_filter($entity_types, function (EntityTypeInterface $entity_type) { + return $entity_type->isCommonReferenceTarget(); + }); + + /** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */ + 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; + } + }