diff --git a/core/config/schema/core.entity.schema.yml b/core/config/schema/core.entity.schema.yml index df2a2fd..174925d 100644 --- a/core/config/schema/core.entity.schema.yml +++ b/core/config/schema/core.entity.schema.yml @@ -362,3 +362,22 @@ field.formatter.settings.entity_reference_label: type: boolean label: 'Link label to the referenced entity' +field.formatter.settings.entity_reference_inline_settings: + type: mapping + label: 'Entity reference inline formatter display format settings' + mapping: + field_name: + label: 'Machine name of the field' + type: string + type: + label: 'Machine name of the formatter' + type: string + settings: + label: 'Formatter settings' + type: field.formatter.settings.[%parent.type] + label: + label: 'Target field label visibility setting' + type: string + show_basefields: + type: boolean + label: 'Whether basefields should be visible on the formatter settings' diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceInlineSettingsFormatter.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceInlineSettingsFormatter.php new file mode 100644 index 0000000..b87d62b --- /dev/null +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceInlineSettingsFormatter.php @@ -0,0 +1,494 @@ +get('entity_field.manager'), + $container->get('plugin.manager.field.formatter'), + $container->get('entity_type.manager'), + $container->get('entity_type.bundle.info') + ); + } + + /** + * Constructs a EntityReferenceInlineSettingsFormatter object. + * + * @param string $plugin_id + * The plugin_id for the formatter. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition + * The definition of the field to which the formatter is associated. + * @param array $settings + * The formatter settings. + * @param string $label + * The formatter label display setting. + * @param string $view_mode + * The view mode. + * @param array $third_party_settings + * Any third party settings. + * @param \Drupal\Core\Entity\EntityFieldManager $entity_field_manager + * The entity field manager. + * @param \Drupal\Core\Field\FormatterPluginManager $formatter_plugin_manager + * The formatter plugin manager. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * The entity type manager. + * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info + * The entity type bundle info. + */ + public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityFieldManager $entity_field_manager, FormatterPluginManager $formatter_plugin_manager, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info) { + parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); + $this->entityFieldManager = $entity_field_manager; + $this->formatterPluginManager = $formatter_plugin_manager; + $this->entityTypeManager = $entity_type_manager; + $this->entityTypeBundleInfo = $entity_type_bundle_info; + } + + /** + * {@inheritdoc} + */ + public static function defaultSettings() { + return [ + 'field_name' => '', + 'type' => '', + 'settings' => [], + 'label' => 'above', + 'show_basefields' => FALSE, + ]; + } + + /** + * Gets field definition for given field storage definition. + * + * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage_definition + * The field storage definition. + * + * @return \Drupal\Core\Field\BaseFieldDefinition + * The field definition. + */ + protected function getFieldDefinition(FieldStorageDefinitionInterface $field_storage_definition) { + return BaseFieldDefinition::createFromFieldStorageDefinition($field_storage_definition); + } + + /** + * {@inheritdoc} + */ + public function viewElements(FieldItemListInterface $items, $langcode) { + /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */ + $entities = $this->getEntitiesToView($items, $langcode); + + $build = []; + foreach ($entities as $delta => $entity) { + $build[$delta] = $this->getViewDisplay($entity->bundle())->build($entity); + } + return $build; + } + + /** + * Gets a list of supported fields. + * + * @param bool $show_basefields + * If FALSE, all basefields except for the label will be skipped. + * + * @return array + * List of fields that are supported keyed by field machine name. + */ + protected function getAvailableFieldNames($show_basefields) { + $entity_type_id = $this->fieldDefinition->getSetting('target_type'); + // We always show the entity label as an option to be selected. + $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); + $label_key = $entity_type->getKey('label'); + $field_names = [$label_key => $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id)[$label_key]->getLabel()]; + $target_bundles = $this->fieldDefinition->getSetting('handler_settings')['target_bundles'] === NULL ? array_keys($this->entityTypeBundleInfo->getBundleInfo($entity_type_id)) : $this->fieldDefinition->getSetting('handler_settings')['target_bundles']; + foreach ($target_bundles as $value) { + $bundle_field_names = array_map( + function (FieldDefinitionInterface $field_definition) use ($show_basefields) { + if ($show_basefields || !$field_definition->getFieldStorageDefinition()->isBaseField()) { + return $field_definition->getLabel(); + } + }, + $this->entityFieldManager->getFieldDefinitions($entity_type_id, $value) + ); + $field_names = array_merge($field_names, array_filter($bundle_field_names)); + } + return $field_names; + } + + /** + * {@inheritdoc} + */ + public static function isApplicable(FieldDefinitionInterface $field_definition) { + $entity_type = \Drupal::entityTypeManager() + ->getDefinition($field_definition->getTargetEntityTypeId()); + return $entity_type->isSubclassOf(FieldableEntityInterface::class); + } + + /** + * Get all available formatters for a field storage definition. + * + * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage_definition + * The field storage definition. + * + * @return string[] + * The field formatter labels keys by plugin ID. + */ + protected function getAvailableFormatterOptions(FieldStorageDefinitionInterface $field_storage_definition) { + $field_definition = $this->getFieldDefinition($field_storage_definition); + $formatters = $this->formatterPluginManager->getOptions($field_storage_definition->getType()); + $formatter_instances = array_map(function($formatter_id) use ($field_definition) { + $configuration = [ + 'field_definition' => $field_definition, + 'settings' => [], + 'label' => '', + 'view_mode' => '', + 'third_party_settings' => [], + ]; + return $this->formatterPluginManager->createInstance($formatter_id, $configuration); + }, array_combine(array_keys($formatters), array_keys($formatters))); + $filtered_formatter_instances = array_filter($formatter_instances, function (FormatterInterface $formatter) use ($field_definition) { + return $formatter->isApplicable($field_definition); + }); + $options = array_map(function (FormatterInterface $formatter) { + return $formatter->getPluginDefinition()['label']; + }, $filtered_formatter_instances); + return $options; + } + + /** + * Ajax callback for field name change. + * + * @param array $form + * The form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The form state. + * + * @return array + * The replaced form substructure. + */ + public static function onFieldNameChange(array $form, FormStateInterface $form_state) { + return $form['fields'][$form_state->getStorage()['plugin_settings_edit']]['plugin']['settings_edit_form']['settings']; + } + + /** + * Ajax callback for formatter type change. + * + * @param array $form + * The form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The form state. + * + * @return array + * The replaced form substructure. + */ + public static function onFormatterTypeChange(array $form, FormStateInterface $form_state) { + return $form['fields'][$form_state->getStorage()['plugin_settings_edit']]['plugin']['settings_edit_form']['settings']['settings']; + } + + /** + * Ajax callback for "show basefields" setting change. + * + * @param array $form + * The form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The form state. + * + * @return array + * The replaced form substructure. + */ + public static function onShowBasefieldsChange(array $form, FormStateInterface $form_state) { + return $form['fields'][$form_state->getStorage()['plugin_settings_edit']]['plugin']['settings_edit_form']['settings']; + } + + /** + * Rebuilds the form on select submit. + * + * @param array $form + * The form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The form state. + */ + public static function rebuildSubmit(array $form, FormStateInterface $form_state) { + $form_state->setRebuild(TRUE); + } + + /** + * {@inheritdoc} + */ + public function settingsForm(array $form, FormStateInterface $form_state) { + $form = parent::settingsForm($form, $form_state); + $target_entity_type_id = $this->fieldDefinition->getSetting('target_type'); + $show_basefields = $this->getSettingFromFormState($form_state, 'show_basefields'); + $field_name_options = $this->getAvailableFieldNames($show_basefields); + $field_storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions($target_entity_type_id); + // Field on the target entity this formatter is currently displaying. + $selected_field_name = $this->getSettingFromFormState($form_state, 'field_name'); + if (!$selected_field_name) { + // The first field is used as default in case of no value set. + $selected_field_name = key($field_name_options); + } + $field_storage = $field_storage_definitions[$selected_field_name]; + // If the field selected is a basefield but the show_basefields has just + // been unchecked, we need to reset the defaults. + if (!$show_basefields && $field_storage->isBaseField()) { + $selected_field_name = key($field_name_options); + $field_storage = $field_storage_definitions[$selected_field_name]; + } + + $form['#prefix'] = '