diff --git a/core/core.services.yml b/core/core.services.yml index dacfcc7..0aba57e 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1354,3 +1354,6 @@ services: arguments: ['@controller_resolver', '@theme.manager', '@plugin.manager.element_info', '@request_stack', '@cache_factory', '@cache_contexts_manager', '%renderer.config%'] email.validator: class: Egulias\EmailValidator\EmailValidator + plugin.manager.entity.display_component_handler: + class: Drupal\Core\Entity\DisplayComponentHandlerPluginManager + parent: default_plugin_manager diff --git a/core/lib/Drupal/Core/Entity/Annotation/DisplayComponent.php b/core/lib/Drupal/Core/Entity/Annotation/DisplayComponent.php new file mode 100644 index 0000000..e2178c2 --- /dev/null +++ b/core/lib/Drupal/Core/Entity/Annotation/DisplayComponent.php @@ -0,0 +1,26 @@ +context = $context; + } + + /** + * {@inheritdoc} + */ + public function prepareDisplayComponents(array &$components, array &$hidden_components) { + } + + /** + * {@inheritdoc} + */ + public function hasElement($name) { + return FALSE; + } + + /** + * {@inheritdoc} + */ + public function massageIn($name, array $options) { + return $options; + } + + /** + * {@inheritdoc} + */ + public function massageOut($properties) { + return $properties; + } + + /** + * {@inheritdoc} + */ + public function getRenderer($name, array $options) { + } + +} diff --git a/core/lib/Drupal/Core/Entity/DisplayComponentHandlerInterface.php b/core/lib/Drupal/Core/Entity/DisplayComponentHandlerInterface.php new file mode 100644 index 0000000..fcb886d --- /dev/null +++ b/core/lib/Drupal/Core/Entity/DisplayComponentHandlerInterface.php @@ -0,0 +1,88 @@ +alterInfo('display_component_handler_info'); + $this->setCacheBackend($cache_backend, 'display_component_handlers'); + } + + /** + * {@inheritdoc} + */ + public function getInstance(array $options) { + $plugin_id = $options['type']; + + if (!isset($this->plugins[$plugin_id]) && !array_key_exists($plugin_id, $this->plugins)) { + $this->plugins[$plugin_id] = $this->discovery->getDefinition($plugin_id) ? $this->createInstance($plugin_id) : NULL; + } + + return $this->plugins[$plugin_id]; + } + +} diff --git a/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php b/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php index 882286a..9d3f5f0 100644 --- a/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php +++ b/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php @@ -130,33 +130,6 @@ public function __construct(array $values, $entity_type) { /** * {@inheritdoc} */ - public function getRenderer($field_name) { - if (isset($this->plugins[$field_name])) { - return $this->plugins[$field_name]; - } - - // Instantiate the widget object from the stored display properties. - if (($configuration = $this->getComponent($field_name)) && isset($configuration['type']) && ($definition = $this->getFieldDefinition($field_name))) { - $widget = $this->pluginManager->getInstance(array( - 'field_definition' => $definition, - 'form_mode' => $this->originalMode, - // No need to prepare, defaults have been merged in setComponent(). - 'prepare' => FALSE, - 'configuration' => $configuration - )); - } - else { - $widget = NULL; - } - - // Persist the widget object. - $this->plugins[$field_name] = $widget; - return $widget; - } - - /** - * {@inheritdoc} - */ public function buildForm(FieldableEntityInterface $entity, array &$form, FormStateInterface $form_state) { // Set #parents to 'top-level' by default. $form += array('#parents' => array()); diff --git a/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php b/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php index efffdbc..4a1336f 100644 --- a/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php +++ b/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php @@ -181,33 +181,6 @@ public function postSave(EntityStorageInterface $storage, $update = TRUE) { /** * {@inheritdoc} */ - public function getRenderer($field_name) { - if (isset($this->plugins[$field_name])) { - return $this->plugins[$field_name]; - } - - // Instantiate the formatter object from the stored display properties. - if (($configuration = $this->getComponent($field_name)) && isset($configuration['type']) && ($definition = $this->getFieldDefinition($field_name))) { - $formatter = $this->pluginManager->getInstance(array( - 'field_definition' => $definition, - 'view_mode' => $this->originalMode, - // No need to prepare, defaults have been merged in setComponent(). - 'prepare' => FALSE, - 'configuration' => $configuration - )); - } - else { - $formatter = NULL; - } - - // Persist the formatter object. - $this->plugins[$field_name] = $formatter; - return $formatter; - } - - /** - * {@inheritdoc} - */ public function build(FieldableEntityInterface $entity) { $build = $this->buildMultiple(array($entity)); return $build[0]; diff --git a/core/lib/Drupal/Core/Entity/EntityDisplayBase.php b/core/lib/Drupal/Core/Entity/EntityDisplayBase.php index 39e953f..17c1026 100644 --- a/core/lib/Drupal/Core/Entity/EntityDisplayBase.php +++ b/core/lib/Drupal/Core/Entity/EntityDisplayBase.php @@ -85,6 +85,11 @@ protected $hidden = array(); /** + * The renderer objects used for this display, keyed by component name. + */ + protected $renderers = array(); + + /** * The original view or form mode that was requested (case of view/form modes * being configured to fall back to the 'default' display). * @@ -113,6 +118,18 @@ */ protected $pluginManager; + /* + * A mapping of display elements and its corresponding handler. + */ + protected $handlers; + + /** + * The display component handler plugin manager. + * + * @var \Drupal\Core\Entity\DisplayComponentHandlerPluginManager + */ + protected $handlerManager; + /** * {@inheritdoc} */ @@ -135,6 +152,8 @@ public function __construct(array $values, $entity_type) { parent::__construct($values, $entity_type); + $this->handlerManager = \Drupal::service('plugin.manager.entity.display_component_handler'); + $this->originalMode = $this->mode; $this->init(); @@ -151,40 +170,10 @@ public function __construct(array $values, $entity_type) { protected function init() { // Only populate defaults for "official" view modes and form modes. if ($this->mode !== static::CUSTOM_MODE) { - // Fill in defaults for extra fields. - $context = $this->displayContext == 'view' ? 'display' : $this->displayContext; - $extra_fields = \Drupal::entityManager()->getExtraFields($this->targetEntityType, $this->bundle); - $extra_fields = isset($extra_fields[$context]) ? $extra_fields[$context] : array(); - foreach ($extra_fields as $name => $definition) { - if (!isset($this->content[$name]) && !isset($this->hidden[$name])) { - // Extra fields are visible by default unless they explicitly say so. - if (!isset($definition['visible']) || $definition['visible'] == TRUE) { - $this->content[$name] = array( - 'weight' => $definition['weight'] - ); - } - else { - $this->hidden[$name] = TRUE; - } - } - } - - // Fill in defaults for fields. - $fields = $this->getFieldDefinitions(); - foreach ($fields as $name => $definition) { - if (!$definition->isDisplayConfigurable($this->displayContext) || (!isset($this->content[$name]) && !isset($this->hidden[$name]))) { - $options = $definition->getDisplayOptions($this->displayContext); - - if (!empty($options['type']) && $options['type'] == 'hidden') { - $this->hidden[$name] = TRUE; - } - elseif ($options) { - $this->content[$name] = $this->pluginManager->prepareConfiguration($definition->getType(), $options); - } - // Note: (base) fields that do not specify display options are not - // tracked in the display at all, in order to avoid cluttering the - // configuration that gets saved back. - } + // Let the component handlers add missing components. + $handlers = $this->handlerManager->getDefinitions(); + foreach (array_keys($handlers) as $type) { + $this->getComponentHandler($type)->prepareDisplayComponents($this->content, $this->hidden); } } } @@ -288,15 +277,15 @@ public function calculateDependencies() { */ public function toArray() { $properties = parent::toArray(); - // Do not store options for fields whose display is not set to be - // configurable. - foreach ($this->getFieldDefinitions() as $field_name => $definition) { - if (!$definition->isDisplayConfigurable($this->displayContext)) { - unset($properties['content'][$field_name]); - unset($properties['hidden'][$field_name]); - } - } + // Let the component handlers add missing components. + if (!$this->handlerManager) { + $this->handlerManager = \Drupal::service('plugin.manager.entity.display_component_handler'); + } + $handlers = $this->handlerManager->getDefinitions(); + foreach (array_keys($handlers) as $type) { + $properties = $this->getComponentHandler($type)->massageOut($properties); + } return $properties; } @@ -333,9 +322,10 @@ public function setComponent($name, array $options = array()) { $options['weight'] = isset($max) ? $max + 1 : 0; } - // For a field, fill in default options. - if ($field_definition = $this->getFieldDefinition($name)) { - $options = $this->pluginManager->prepareConfiguration($field_definition->getType(), $options); + // Massage in some values. + $handler = $this->getComponentHandlerByElementName($name); + if ($handler) { + $options = $handler->massageIn($name, $options); } // Ensure we always have an empty settings and array. @@ -343,7 +333,7 @@ public function setComponent($name, array $options = array()) { $this->content[$name] = $options; unset($this->hidden[$name]); - unset($this->plugins[$name]); + unset($this->renderers[$name]); return $this; } @@ -443,6 +433,71 @@ public function onDependencyRemoval(array $dependencies) { } /** + * Finds component handler by element name. + * + * @param string $name + * The element name. + * + * @return \Drupal\Core\Entity\DisplayComponentHandlerInterface + */ + public function getComponentHandlerByElementName($name) { + if (!isset($this->handlers[$name])) { + $handlers = $this->handlerManager->getDefinitions(); + foreach (array_keys($handlers) as $type) { + $handler = $this->getComponentHandler($type); + if ($handler && $handler->hasElement($name)) { + break; + } + $handler = NULL; + } + $this->handlers[$name] = $handler; + } + + return $this->handlers[$name]; + } + + /** + * Instantiates component handler. + * + * @param string $type + * The type of component handler (field, extra_field). + * + * @return \Drupal\Core\Entity\DisplayComponentHandlerInterface + */ + public function getComponentHandler($type) { + $handler = $this->handlerManager->getInstance(array('type' => $type)); + if ($handler) { + $handler->setContext(array( + 'entity_type' => $this->targetEntityType, + 'bundle' => $this->bundle, + 'mode' => $this->originalMode, + 'display_context' => $this->displayContext, + )); + } + return $handler; + } + + /** + * {@inheritdoc} + */ + public function getRenderer($name) { + if (!isset($this->content[$name])) { + return NULL; + } + + if (!array_key_exists($name, $this->renderers)) { + if ($handler = $this->getComponentHandlerByElementName($name)) { + $options = $this->getComponent($name); + $this->renderers[$name] = $handler->getRenderer($name, $options); + } + else { + $this->renderers[$name] = NULL; + } + } + return $this->renderers[$name]; + } + + /** * {@inheritdoc} */ public function __sleep() { diff --git a/core/lib/Drupal/Core/Entity/Plugin/DisplayComponent/ExtraFieldDisplayComponentHandler.php b/core/lib/Drupal/Core/Entity/Plugin/DisplayComponent/ExtraFieldDisplayComponentHandler.php new file mode 100644 index 0000000..6db98e4 --- /dev/null +++ b/core/lib/Drupal/Core/Entity/Plugin/DisplayComponent/ExtraFieldDisplayComponentHandler.php @@ -0,0 +1,59 @@ +fetchExtraFields(); + foreach ($extra_fields as $name => $definition) { + if (!isset($components[$name]) && !isset($hidden_components[$name])) { + // Extra fields are visible by default unless they explicitly say so. + if (!isset($definition['visible']) || $definition['visible'] == TRUE) { + $components[$name] = array( + 'weight' => $definition['weight'] + ); + } + else { + $hidden_components[$name] = TRUE; + } + } + } + } + + /** + * {@inheritdoc} + */ + public function hasElement($name) { + $extra_fields = $this->fetchExtraFields(); + return isset($extra_fields[$name]); + } + + /** + * Fetches all the extra fields. + */ + protected function fetchExtraFields() { + $context = $this->context['display_context'] == 'view' ? 'display' : $this->context['display_context']; + $extra_fields = \Drupal::entityManager()->getExtraFields($this->context['entity_type'], $this->context['bundle']); + return isset($extra_fields[$context]) ? $extra_fields[$context] : array(); + } + +} diff --git a/core/lib/Drupal/Core/Entity/Plugin/DisplayComponent/FieldDisplayComponentHandler.php b/core/lib/Drupal/Core/Entity/Plugin/DisplayComponent/FieldDisplayComponentHandler.php new file mode 100644 index 0000000..f9d76e3 --- /dev/null +++ b/core/lib/Drupal/Core/Entity/Plugin/DisplayComponent/FieldDisplayComponentHandler.php @@ -0,0 +1,220 @@ +formatterPluginManager = $formatter_plugin_manager; + $this->widgetPluginManager = $widget_plugin_manager; + $this->entityManager = $entity_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static($configuration, $plugin_id, $plugin_definition, + $container->get('plugin.manager.field.formatter'), + $container->get('plugin.manager.field.widget'), + $container->get('entity.manager') + ); + } + + /** + * {@inheritdoc} + */ + public function massageIn($name, array $options) { + $field_definition = $this->getFieldDefinition($name); + if (!isset($field_definition)) { + // The field in process of removal from display. + return $options; + } + if ($this->context['display_context'] == 'view') { + return $this->formatterPluginManager->prepareConfiguration($field_definition->getType(), $options); + } + else { + return $this->widgetPluginManager->prepareConfiguration($field_definition->getType(), $options); + } + } + + /** + * {@inheritdoc} + */ + public function massageOut($properties) { + // Do not store options for fields whose display is not set to be + // configurable. + foreach ($this->getFieldDefinitions() as $field_name => $definition) { + if (!$definition->isDisplayConfigurable($this->context['display_context'])) { + unset($properties['content'][$field_name]); + unset($properties['hidden'][$field_name]); + } + } + + return $properties; + } + + /** + * {@inheritdoc} + */ + public function prepareDisplayComponents(array &$components, array &$hidden_components) { + if ($this->context['display_context'] == 'view') { + $plugin_manager = $this->formatterPluginManager; + } + else { + $plugin_manager = $this->widgetPluginManager; + } + + // Fill in defaults for fields. + $fields = $this->getFieldDefinitions(); + foreach ($fields as $name => $definition) { + if (!$definition->isDisplayConfigurable($this->context['display_context']) || (!isset($components[$name]) && !isset($hidden_components[$name]))) { + $options = $definition->getDisplayOptions($this->context['display_context']); + + if (!empty($options['type']) && $options['type'] == 'hidden') { + $hidden_components[$name] = TRUE; + } + elseif ($options) { + $components[$name] = $plugin_manager->prepareConfiguration($definition->getType(), $options); + } + // Note: (base) fields that do not specify display options are not + // tracked in the display at all, in order to avoid cluttering the + // configuration that gets saved back. + } + } + } + + /** + * {@inheritdoc} + */ + public function getRenderer($name, array $options) { + if (isset($options['type']) && ($definition = $this->getFieldDefinition($name))) { + if ($this->context['display_context'] == 'view') { + $plugin_manager = $this->formatterPluginManager; + $mode_key = 'view_mode'; + } + else { + $plugin_manager = $this->widgetPluginManager; + $mode_key = 'form_mode'; + } + + return $plugin_manager->getInstance(array( + 'field_definition' => $definition, + $mode_key => $this->context['mode'], + // No need to prepare, defaults have been merged when the options were + // written in the display. + 'prepare' => FALSE, + 'configuration' => $options, + )); + } + return NULL; + } + + /** + * {@inheritdoc} + */ + public function hasElement($name) { + $field_definition = $this->getFieldDefinition($name); + return isset($field_definition); + } + + /** + * Returns the field definition of a field. + */ + protected function getFieldDefinition($field_name) { + $definitions = $this->getFieldDefinitions(); + return isset($definitions[$field_name]) ? $definitions[$field_name] : NULL; + } + + /** + * Returns the definitions of the fields that are candidate for display. + */ + protected function getFieldDefinitions() { + $entity_type = $this->context['entity_type']; + $bundle = $this->context['bundle']; + $mode = $this->context['mode']; + + $definitions = \Drupal::entityManager()->getFieldDefinitions($entity_type, $bundle); + + // For "official" view modes and form modes, ignore fields whose + // definition states they should not be displayed. + if ($mode !== EntityDisplayBase::CUSTOM_MODE) { + $definitions = array_filter($definitions, array($this, 'fieldHasDisplayOptions')); + } + return $definitions; + } + + /** + * Determines if a field has options for a given display. + * + * @param FieldDefinitionInterface $definition + * A field definition. + * @return array|null + */ + private function fieldHasDisplayOptions(FieldDefinitionInterface $definition) { + // The display only cares about fields that specify display options. + // Discard base fields that are not rendered through formatters / widgets. + return $definition->getDisplayOptions($this->displayContext); + } + +} + diff --git a/core/modules/system/entity.api.php b/core/modules/system/entity.api.php index d188375..9807c79 100644 --- a/core/modules/system/entity.api.php +++ b/core/modules/system/entity.api.php @@ -1949,5 +1949,20 @@ function hook_entity_extra_field_info_alter(&$info) { } /** + * Modify the list of available component handler plugins. + * + * This hook may be used to modify plugin properties after they have been + * specified by other modules. + * + * @param $plugins + * An array of all the existing plugin definitions, passed by reference. + * + * @see DisplayComponentHandlerPluginManager + */ +function hook_display_component_handler_info_alter(array &$plugins) { + $plugins['someplugin']['label'] = t('Better name'); +} + +/** * @} End of "addtogroup hooks". */