diff --git a/core/includes/entity.inc b/core/includes/entity.inc index b8dfbd6..b65dfa3 100644 --- a/core/includes/entity.inc +++ b/core/includes/entity.inc @@ -670,7 +670,7 @@ function entity_get_display($entity_type, $bundle, $view_mode) { $display = entity_create('entity_display', array( 'targetEntityType' => $entity_type, 'bundle' => $bundle, - 'viewMode' => $view_mode, + 'displayMode' => $view_mode, )); } @@ -712,7 +712,7 @@ function entity_get_render_display(EntityInterface $entity, $view_mode) { $render_view_mode = !empty($view_mode_settings[$view_mode]['custom_settings']) ? $view_mode : 'default'; $display = entity_get_display($entity_type, $bundle, $render_view_mode); - $display->originalViewMode = $view_mode; + $display->originalDisplayMode = $view_mode; return $display; } @@ -770,7 +770,7 @@ function entity_get_form_display($entity_type, $bundle, $form_mode = 'default') $entity_form_display = entity_create('entity_form_display', array( 'targetEntityType' => $entity_type, 'bundle' => $bundle, - 'formMode' => $form_mode, + 'displayMode' => $form_mode, )); } diff --git a/core/modules/entity/entity.install b/core/modules/entity/entity.install index 4e95f12..cd28c73 100644 --- a/core/modules/entity/entity.install +++ b/core/modules/entity/entity.install @@ -37,7 +37,7 @@ function _update_8000_entity_get_display($entity_type, $bundle, $view_mode) { 'uuid' => $uuid->generate(), 'targetEntityType' => $entity_type, 'bundle' => $bundle, - 'viewMode' => $view_mode, + 'displayMode' => $view_mode, 'content' => array(), ); foreach ($properties as $key => $value) { @@ -76,7 +76,7 @@ function _update_8000_entity_get_form_display($entity_type, $bundle, $form_mode) 'uuid' => $uuid->generate(), 'targetEntityType' => $entity_type, 'bundle' => $bundle, - 'formMode' => $form_mode, + 'displayMode' => $form_mode, 'content' => array(), ); foreach ($properties as $key => $value) { diff --git a/core/modules/entity/entity.module b/core/modules/entity/entity.module index 09776fa..aa71c1d 100644 --- a/core/modules/entity/entity.module +++ b/core/modules/entity/entity.module @@ -22,7 +22,7 @@ function entity_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_ne foreach ($ids as $id) { $id = ConfigStorageController::getIDFromConfigName($id, $entity_info['config_prefix']); $form_display = entity_load('entity_form_display', $id); - $new_id = $entity_type . '.' . $bundle_new . '.' . $form_display->formMode; + $new_id = $entity_type . '.' . $bundle_new . '.' . $form_display->displayMode; $form_display->id = $new_id; $form_display->bundle = $bundle_new; $form_display->save(); diff --git a/core/modules/entity/lib/Drupal/entity/Plugin/Core/Entity/EntityDisplay.php b/core/modules/entity/lib/Drupal/entity/EntityDisplayBase.php similarity index 68% copy from core/modules/entity/lib/Drupal/entity/Plugin/Core/Entity/EntityDisplay.php copy to core/modules/entity/lib/Drupal/entity/EntityDisplayBase.php index f68ca6a..1130dd1 100644 --- a/core/modules/entity/lib/Drupal/entity/Plugin/Core/Entity/EntityDisplay.php +++ b/core/modules/entity/lib/Drupal/entity/EntityDisplayBase.php @@ -2,32 +2,18 @@ /** * @file - * Contains \Drupal\entity\Plugin\Core\Entity\EntityDisplay. + * Contains \Drupal\entity\EntityDisplayBase. */ -namespace Drupal\entity\Plugin\Core\Entity; +namespace Drupal\entity; use Drupal\Core\Config\Entity\ConfigEntityBase; -use Drupal\Component\Annotation\Plugin; -use Drupal\Core\Annotation\Translation; /** - * Configuration entity that contains display options for all components of a - * rendered entity in a given view mode.. - * - * @Plugin( - * id = "entity_display", - * label = @Translation("Entity display"), - * module = "entity", - * controller_class = "Drupal\Core\Config\Entity\ConfigStorageController", - * config_prefix = "entity.display", - * entity_keys = { - * "id" = "id", - * "uuid" = "uuid" - * } - * ) + * Base class for config entity types that store configuration for entity forms + * and displays. */ -class EntityDisplay extends ConfigEntityBase { +abstract class EntityDisplayBase extends ConfigEntityBase { /** * Unique ID for the config entity. @@ -58,11 +44,11 @@ class EntityDisplay extends ConfigEntityBase { public $bundle; /** - * View mode to be displayed. + * View or form mode to be displayed. * * @var string */ - public $viewMode; + public $displayMode; /** * List of component display options, keyed by component name. @@ -72,19 +58,33 @@ class EntityDisplay extends ConfigEntityBase { protected $content = array(); /** - * The original view mode that was requested (case of view modes being - * configured to fall back to the 'default' display). + * The original view or form mode that was requested (case of view/form modes + * being configured to fall back to the 'default' display). * * @var string */ - public $originalViewMode; + public $originalDisplayMode; /** - * The formatter objects used for this display, keyed by field name. + * The plugin objects used for this display, keyed by field name. * * @var array */ - protected $formatters = array(); + protected $plugins = array(); + + /** + * Context in which this entity will be used (e.g. 'display', 'form'). + * + * @var string + */ + protected $displayContext; + + /** + * The plugin manager used by this entity type. + * + * @var \Drupal\Component\Plugin\PluginManagerBase + */ + protected $pluginManager; /** * Overrides \Drupal\Core\Config\Entity\ConfigEntityBase::__construct(). @@ -94,19 +94,28 @@ public function __construct(array $values, $entity_type) { // currently produces invalid entities with a NULL bundle in some cases. // Add the validity checks back when http://drupal.org/node/1856556 is // fixed. - // if (!isset($values['targetEntityType']) || !isset($values['bundle']) || !isset($values['viewMode'])) { - // throw new \InvalidArgumentException('Missing required properties for an EntiyDisplay entity.'); + // if (!isset($values['targetEntityType']) || !isset($values['bundle']) || !isset($values['displayMode'])) { + // throw new \InvalidArgumentException('Missing required properties for an EntityDisplay entity.'); // } + + // A plugin manager and a context type needs to be set by extending classes. + if (!isset($this->pluginManager)) { + throw new \RuntimeException('Missing plugin manager.'); + } + if (!isset($this->displayContext)) { + throw new \RuntimeException('Missing display context type.'); + } + parent::__construct($values, $entity_type); - $this->originalViewMode = $this->viewMode; + $this->originalDisplayMode = $this->displayMode; } /** * Overrides \Drupal\Core\Entity\Entity::id(). */ public function id() { - return $this->targetEntityType . '.' . $this->bundle . '.' . $this->viewMode; + return $this->targetEntityType . '.' . $this->bundle . '.' . $this->displayMode; } /** @@ -129,7 +138,7 @@ public function getExportProperties() { 'uuid', 'targetEntityType', 'bundle', - 'viewMode', + 'displayMode', 'content', ); $properties = array(); @@ -140,20 +149,20 @@ public function getExportProperties() { } /** - * Creates a duplicate of the EntityDisplay object on a different view mode. + * Creates a duplicate object on a different display mode. * * The new object necessarily has the same $targetEntityType and $bundle * properties than the original one. * - * @param $view_mode - * The view mode for the new object. + * @param $display_mode + * The display mode for the new object. * - * @return \Drupal\entity\Plugin\Core\Entity\EntityDisplay + * @return \Drupal\entity\EntityDisplayBase * The new object. */ - public function createCopy($view_mode) { + public function createCopy($display_mode) { $display = $this->createDuplicate(); - $display->viewMode = $display->originalViewMode = $view_mode; + $display->displayMode = $display->originalDisplayMode = $display_mode; return $display; } @@ -186,7 +195,7 @@ public function getComponents() { */ public function getComponent($name) { // We always store 'extra fields', whether they are visible or hidden. - $extra_fields = field_info_extra_fields($this->targetEntityType, $this->bundle, 'display'); + $extra_fields = field_info_extra_fields($this->targetEntityType, $this->bundle, $this->displayContext); if (isset($extra_fields[$name])) { // If we have explicit settings, return an array or NULL depending on // visibility. @@ -239,14 +248,14 @@ public function setComponent($name, array $options = array()) { if ($instance = field_info_instance($this->targetEntityType, $name, $this->bundle)) { $field = field_info_field($instance['field_name']); - $options = drupal_container()->get('plugin.manager.field.formatter')->prepareConfiguration($field['type'], $options); + $options = $this->pluginManager->prepareConfiguration($field['type'], $options); // Clear the persisted formatter, if any. - unset($this->formatters[$name]); + unset($this->plugins[$name]); } // We always store 'extra fields', whether they are visible or hidden. - $extra_fields = field_info_extra_fields($this->targetEntityType, $this->bundle, 'display'); + $extra_fields = field_info_extra_fields($this->targetEntityType, $this->bundle, $this->displayContext); if (isset($extra_fields[$name])) { $options['visible'] = TRUE; } @@ -266,7 +275,7 @@ public function setComponent($name, array $options = array()) { * The EntityDisplay object. */ public function removeComponent($name) { - $extra_fields = field_info_extra_fields($this->targetEntityType, $this->bundle, 'display'); + $extra_fields = field_info_extra_fields($this->targetEntityType, $this->bundle, $this->displayContext); if (isset($extra_fields[$name])) { // 'Extra fields' are exposed in hooks and can appear at any given time. // Therefore we store extra fields that are explicitly being hidden, so @@ -278,7 +287,7 @@ public function removeComponent($name) { } else { unset($this->content[$name]); - unset($this->formatters[$name]); + unset($this->plugins[$name]); } return $this; @@ -302,44 +311,8 @@ public function getHighestWeight() { } // Let other modules feedback about their own additions. - $weights = array_merge($weights, module_invoke_all('field_info_max_weight', $this->targetEntityType, $this->bundle, 'display', $this->viewMode)); + $weights = array_merge($weights, module_invoke_all('field_info_max_weight', $this->targetEntityType, $this->bundle, $this->displayContext, $this->displayMode)); return $weights ? max($weights) : NULL; } - - /** - * Returns the Formatter plugin for a field. - * - * @param string $field_name - * The field name. - * - * @return \Drupal\field\Plugin\Type\Formatter\FormatterInterface - * If the field is not hidden, the Formatter plugin to use for rendering - * it. - */ - public function getFormatter($field_name) { - if (isset($this->formatters[$field_name])) { - return $this->formatters[$field_name]; - } - - // Instantiate the formatter object from the stored display properties. - if ($configuration = $this->getComponent($field_name)) { - $instance = field_info_instance($this->targetEntityType, $field_name, $this->bundle); - $formatter = drupal_container()->get('plugin.manager.field.formatter')->getInstance(array( - 'instance' => $instance, - 'view_mode' => $this->originalViewMode, - // No need to prepare, defaults have been merged in setComponent(). - 'prepare' => FALSE, - 'configuration' => $configuration - )); - } - else { - $formatter = NULL; - } - - // Persist the formatter object. - $this->formatters[$field_name] = $formatter; - return $formatter; - } - } diff --git a/core/modules/entity/lib/Drupal/entity/Plugin/Core/Entity/EntityDisplay.php b/core/modules/entity/lib/Drupal/entity/Plugin/Core/Entity/EntityDisplay.php index f68ca6a..9d59852 100644 --- a/core/modules/entity/lib/Drupal/entity/Plugin/Core/Entity/EntityDisplay.php +++ b/core/modules/entity/lib/Drupal/entity/Plugin/Core/Entity/EntityDisplay.php @@ -7,13 +7,14 @@ namespace Drupal\entity\Plugin\Core\Entity; -use Drupal\Core\Config\Entity\ConfigEntityBase; +use Drupal; use Drupal\Component\Annotation\Plugin; use Drupal\Core\Annotation\Translation; +use Drupal\entity\EntityDisplayBase; /** * Configuration entity that contains display options for all components of a - * rendered entity in a given view mode.. + * rendered entity in a given view mode. * * @Plugin( * id = "entity_display", @@ -27,284 +28,16 @@ * } * ) */ -class EntityDisplay extends ConfigEntityBase { +class EntityDisplay extends EntityDisplayBase { /** - * Unique ID for the config entity. - * - * @var string - */ - public $id; - - /** - * Unique UUID for the config entity. - * - * @var string - */ - public $uuid; - - /** - * Entity type to be displayed. - * - * @var string - */ - public $targetEntityType; - - /** - * Bundle to be displayed. - * - * @var string - */ - public $bundle; - - /** - * View mode to be displayed. - * - * @var string - */ - public $viewMode; - - /** - * List of component display options, keyed by component name. - * - * @var array - */ - protected $content = array(); - - /** - * The original view mode that was requested (case of view modes being - * configured to fall back to the 'default' display). - * - * @var string - */ - public $originalViewMode; - - /** - * The formatter objects used for this display, keyed by field name. - * - * @var array - */ - protected $formatters = array(); - - /** - * Overrides \Drupal\Core\Config\Entity\ConfigEntityBase::__construct(). + * Overrides \Drupal\entity\EntityDisplayBase::__construct(). */ public function __construct(array $values, $entity_type) { - // @todo See http://drupal.org/node/1825044#comment-6847792: contact.module - // currently produces invalid entities with a NULL bundle in some cases. - // Add the validity checks back when http://drupal.org/node/1856556 is - // fixed. - // if (!isset($values['targetEntityType']) || !isset($values['bundle']) || !isset($values['viewMode'])) { - // throw new \InvalidArgumentException('Missing required properties for an EntiyDisplay entity.'); - // } - parent::__construct($values, $entity_type); - - $this->originalViewMode = $this->viewMode; - } - - /** - * Overrides \Drupal\Core\Entity\Entity::id(). - */ - public function id() { - return $this->targetEntityType . '.' . $this->bundle . '.' . $this->viewMode; - } - - /** - * Overrides \Drupal\config\ConfigEntityBase::save(). - */ - public function save() { - // Build an ID if none is set. - if (empty($this->id)) { - $this->id = $this->id(); - } - return parent::save(); - } + $this->pluginManager = Drupal::service('plugin.manager.field.formatter'); + $this->displayContext = 'display'; - /** - * Overrides \Drupal\config\ConfigEntityBase::getExportProperties(); - */ - public function getExportProperties() { - $names = array( - 'id', - 'uuid', - 'targetEntityType', - 'bundle', - 'viewMode', - 'content', - ); - $properties = array(); - foreach ($names as $name) { - $properties[$name] = $this->get($name); - } - return $properties; - } - - /** - * Creates a duplicate of the EntityDisplay object on a different view mode. - * - * The new object necessarily has the same $targetEntityType and $bundle - * properties than the original one. - * - * @param $view_mode - * The view mode for the new object. - * - * @return \Drupal\entity\Plugin\Core\Entity\EntityDisplay - * The new object. - */ - public function createCopy($view_mode) { - $display = $this->createDuplicate(); - $display->viewMode = $display->originalViewMode = $view_mode; - return $display; - } - - /** - * Gets the display options for all components. - * - * @return array - * The array of display options, keyed by component name. - */ - public function getComponents() { - $result = array(); - foreach ($this->content as $name => $options) { - if (!isset($options['visible']) || $options['visible'] === TRUE) { - unset($options['visible']); - $result[$name] = $options; - } - } - return $result; - } - - /** - * Gets the display options set for a component. - * - * @param string $name - * The name of the component. - * - * @return array|null - * The display options for the component, or NULL if the component is not - * displayed. - */ - public function getComponent($name) { - // We always store 'extra fields', whether they are visible or hidden. - $extra_fields = field_info_extra_fields($this->targetEntityType, $this->bundle, 'display'); - if (isset($extra_fields[$name])) { - // If we have explicit settings, return an array or NULL depending on - // visibility. - if (isset($this->content[$name])) { - if ($this->content[$name]['visible']) { - return array( - 'weight' => $this->content[$name]['weight'], - ); - } - else { - return NULL; - } - } - - // If no explicit settings for the extra field, look at the default - // visibility in its definition. - $definition = $extra_fields[$name]; - if (!isset($definition['visible']) || $definition['visible'] == TRUE) { - return array( - 'weight' => $definition['weight'] - ); - } - else { - return NULL; - } - } - - if (isset($this->content[$name])) { - return $this->content[$name]; - } - } - - /** - * Sets the display options for a component. - * - * @param string $name - * The name of the component. - * @param array $options - * The display options. - * - * @return \Drupal\entity\Plugin\Core\Entity\EntityDisplay - * The EntityDisplay object. - */ - public function setComponent($name, array $options = array()) { - // If no weight specified, make sure the field sinks at the bottom. - if (!isset($options['weight'])) { - $max = $this->getHighestWeight(); - $options['weight'] = isset($max) ? $max + 1 : 0; - } - - if ($instance = field_info_instance($this->targetEntityType, $name, $this->bundle)) { - $field = field_info_field($instance['field_name']); - $options = drupal_container()->get('plugin.manager.field.formatter')->prepareConfiguration($field['type'], $options); - - // Clear the persisted formatter, if any. - unset($this->formatters[$name]); - } - - // We always store 'extra fields', whether they are visible or hidden. - $extra_fields = field_info_extra_fields($this->targetEntityType, $this->bundle, 'display'); - if (isset($extra_fields[$name])) { - $options['visible'] = TRUE; - } - - $this->content[$name] = $options; - - return $this; - } - - /** - * Sets a component to be hidden. - * - * @param string $name - * The name of the component. - * - * @return \Drupal\entity\Plugin\Core\Entity\EntityDisplay - * The EntityDisplay object. - */ - public function removeComponent($name) { - $extra_fields = field_info_extra_fields($this->targetEntityType, $this->bundle, 'display'); - if (isset($extra_fields[$name])) { - // 'Extra fields' are exposed in hooks and can appear at any given time. - // Therefore we store extra fields that are explicitly being hidden, so - // that we can differenciate with those that are simply not configured - // yet. - $this->content[$name] = array( - 'visible' => FALSE, - ); - } - else { - unset($this->content[$name]); - unset($this->formatters[$name]); - } - - return $this; - } - - /** - * Returns the highest weight of the components in the display. - * - * @return int|null - * The highest weight of the components in the display, or NULL if the - * display is empty. - */ - public function getHighestWeight() { - $weights = array(); - - // Collect weights for the components in the display. - foreach ($this->content as $options) { - if (isset($options['weight'])) { - $weights[] = $options['weight']; - } - } - - // Let other modules feedback about their own additions. - $weights = array_merge($weights, module_invoke_all('field_info_max_weight', $this->targetEntityType, $this->bundle, 'display', $this->viewMode)); - - return $weights ? max($weights) : NULL; + parent::__construct($values, $entity_type); } /** @@ -318,16 +51,16 @@ public function getHighestWeight() { * it. */ public function getFormatter($field_name) { - if (isset($this->formatters[$field_name])) { - return $this->formatters[$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)) { $instance = field_info_instance($this->targetEntityType, $field_name, $this->bundle); - $formatter = drupal_container()->get('plugin.manager.field.formatter')->getInstance(array( + $formatter = $this->pluginManager->getInstance(array( 'instance' => $instance, - 'view_mode' => $this->originalViewMode, + 'view_mode' => $this->originalDisplayMode, // No need to prepare, defaults have been merged in setComponent(). 'prepare' => FALSE, 'configuration' => $configuration @@ -338,8 +71,7 @@ public function getFormatter($field_name) { } // Persist the formatter object. - $this->formatters[$field_name] = $formatter; + $this->plugins[$field_name] = $formatter; return $formatter; } - } diff --git a/core/modules/entity/lib/Drupal/entity/Plugin/Core/Entity/EntityFormDisplay.php b/core/modules/entity/lib/Drupal/entity/Plugin/Core/Entity/EntityFormDisplay.php index ba86256..eb9a280 100644 --- a/core/modules/entity/lib/Drupal/entity/Plugin/Core/Entity/EntityFormDisplay.php +++ b/core/modules/entity/lib/Drupal/entity/Plugin/Core/Entity/EntityFormDisplay.php @@ -7,9 +7,10 @@ namespace Drupal\entity\Plugin\Core\Entity; +use Drupal; use Drupal\Component\Annotation\Plugin; use Drupal\Core\Annotation\Translation; -use Drupal\Core\Config\Entity\ConfigEntityBase; +use Drupal\entity\EntityDisplayBase; /** * Configuration entity that contains widget options for all components of a @@ -27,228 +28,16 @@ * } * ) */ -class EntityFormDisplay extends ConfigEntityBase { +class EntityFormDisplay extends EntityDisplayBase { /** - * Unique ID for the config entity. - * - * @var string - */ - public $id; - - /** - * Unique UUID for the config entity. - * - * @var string - */ - public $uuid; - - /** - * Entity type for which the form is built. - * - * @var string - */ - public $targetEntityType; - - /** - * Bundle for which the form is built. - * - * @var string - */ - public $bundle; - - /** - * Form mode for which the form is built. - * - * @var string - */ - public $formMode; - - /** - * List of component widget options, keyed by component name. - * - * @var array - */ - protected $content = array(); - - /** - * The original form mode that was requested (case of form modes being - * configured to fall back to the 'default' mode). - * - * @var string - */ - public $originalFormMode; - - /** - * The widget objects used for this form, keyed by field name. - * - * @var array - */ - protected $widgets = array(); - - /** - * Overrides \Drupal\Core\Config\Entity\ConfigEntityBase::__construct(). + * Overrides \Drupal\entity\EntityDisplayBase::__construct(). */ public function __construct(array $values, $entity_type) { - // @todo See http://drupal.org/node/1825044#comment-6847792: contact.module - // currently produces invalid entities with a NULL bundle in some cases. - // Add the validity checks back when http://drupal.org/node/1856556 is - // fixed. - //if (!isset($values['targetEntityType']) || !isset($values['bundle']) || !isset($values['formMode'])) { - // throw new \InvalidArgumentException('Missing required properties for an EntityFormDisplay entity.'); - //} - parent::__construct($values, $entity_type); - - $this->originalFormMode = $this->formMode; - } - - /** - * Overrides \Drupal\Core\Entity\Entity::id(). - */ - public function id() { - return $this->targetEntityType . '.' . $this->bundle . '.' . $this->formMode; - } - - /** - * Overrides \Drupal\config\ConfigEntityBase::save(). - */ - public function save() { - // Build an ID if none is set. - if (empty($this->id)) { - $this->id = $this->id(); - } - return parent::save(); - } - - /** - * Overrides \Drupal\config\ConfigEntityBase::getExportProperties(); - */ - public function getExportProperties() { - $names = array( - 'id', - 'uuid', - 'targetEntityType', - 'bundle', - 'formMode', - 'content', - ); - $properties = array(); - foreach ($names as $name) { - $properties[$name] = $this->get($name); - } - return $properties; - } + $this->pluginManager = Drupal::service('plugin.manager.field.widget'); + $this->displayContext = 'form'; - /** - * Creates a duplicate of the EntityFormDisplay object on a different form mode. - * - * The new object necessarily has the same $targetEntityType and $bundle - * properties than the original one. - * - * @param $form_mode - * The form mode for the new object. - * - * @return \Drupal\entity\Plugin\Core\Entity\EntityFormDisplay - * The new object. - */ - public function createCopy($form_mode) { - $entity = $this->createDuplicate(); - $entity->formMode = $entity->originalFormMode = $form_mode; - return $entity; - } - - /** - * Gets the widget options for all components. - * - * @return array - * The array of widget options, keyed by component name. - */ - public function getComponents() { - return $this->content; - } - - /** - * Gets the widget options set for a component. - * - * @param string $name - * The name of the component. - * - * @return array - * The widget options for the component. - */ - public function getComponent($name) { - if (isset($this->content[$name])) { - return $this->content[$name]; - } - } - - /** - * Sets the widget options for a component. - * - * @param string $name - * The name of the component. - * @param array $options - * The widget options. - * - * @return \Drupal\entity\Plugin\Core\Entity\EntityFormDisplay - * The EntityFormDisplay object. - */ - public function setComponent($name, array $options = array()) { - // If no weight specified, make sure the field sinks at the bottom. - if (!isset($options['weight'])) { - $max = $this->getHighestWeight(); - $options['weight'] = isset($max) ? $max + 1 : 0; - } - - if ($instance = field_info_instance($this->targetEntityType, $name, $this->bundle)) { - $field = field_info_field($instance['field_name']); - $options = drupal_container()->get('plugin.manager.field.widget')->prepareConfiguration($field['type'], $options); - - // Clear the persisted component, if any. - unset($this->widgets[$name]); - } - - $this->content[$name] = $options; - - return $this; - } - - /** - * Sets a component to be hidden. - * - * @param string $name - * The name of the component. - * - * @return \Drupal\entity\Plugin\Core\Entity\EntityFormDisplay - * The EntityFormDisplay object. - */ - public function removeComponent($name) { - // @todo This method can't do anything until http://drupal.org/node/1465774 - // is in. - return $this; - } - - /** - * Returns the highest weight of the components in the form. - * - * @return int|null - * The highest weight of the components in the form, or NULL if the form is - * empty. - */ - public function getHighestWeight() { - $weights = array(); - - // Collect weights for the components in the form. - foreach ($this->content as $options) { - if (isset($options['weight'])) { - $weights[] = $options['weight']; - } - } - - // Let other modules feedback about their own additions. - $weights = array_merge($weights, module_invoke_all('field_info_max_weight', $this->targetEntityType, $this->bundle, 'form', $this->formMode)); - - return $weights ? max($weights) : NULL; + parent::__construct($values, $entity_type); } /** @@ -261,16 +50,26 @@ public function getHighestWeight() { * A Widget plugin or NULL if the field does not exist. */ public function getWidget($field_name) { - if (isset($this->widgets[$field_name])) { - return $this->widgets[$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)) { - $instance = field_info_instance($this->targetEntityType, $field_name, $this->bundle); - $widget = drupal_container()->get('plugin.manager.field.widget')->getInstance(array( + // Check if this field actually exists. + if ($instance = field_info_instance($this->targetEntityType, $field_name, $this->bundle)) { + // Try to get the widget configuration from the stored display properties. + if (!$configuration = $this->getComponent($field_name)) { + // We don't store hidden fields but we need to use the 'Hidden' widget + // for them. + $configuration = array( + 'type' => 'hidden', + 'settings' => array(), + 'weight' => 0, + ); + } + + $widget = $this->pluginManager->getInstance(array( 'instance' => $instance, - 'form_mode' => $this->originalFormMode, + 'form_mode' => $this->originalDisplayMode, // No need to prepare, defaults have been merged in setComponent(). 'prepare' => FALSE, 'configuration' => $configuration @@ -281,7 +80,7 @@ public function getWidget($field_name) { } // Persist the widget object. - $this->widgets[$field_name] = $widget; + $this->plugins[$field_name] = $widget; return $widget; } } diff --git a/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php b/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php index 3396f89..7ea876a 100644 --- a/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php +++ b/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php @@ -37,7 +37,7 @@ public function testEntityDisplayCRUD() { $display = entity_create('entity_display', array( 'targetEntityType' => 'entity_test', 'bundle' => 'entity_test', - 'viewMode' => 'default', + 'displayMode' => 'default', )); $expected = array(); @@ -82,7 +82,7 @@ public function testEntityDisplayCRUD() { $new_display = entity_load('entity_display', $new_display->id()); $this->assertEqual($new_display->targetEntityType, $display->targetEntityType); $this->assertEqual($new_display->bundle, $display->bundle); - $this->assertEqual($new_display->viewMode, 'other_view_mode'); + $this->assertEqual($new_display->displayMode, 'other_view_mode'); $this->assertEqual($new_display->getComponents(), $display->getComponents()); } @@ -113,7 +113,7 @@ public function testExtraFieldComponent() { $display = entity_create('entity_display', array( 'targetEntityType' => 'entity_test', 'bundle' => 'entity_test', - 'viewMode' => 'default', + 'displayMode' => 'default', )); // Check that the default visibility taken into account for extra fields @@ -137,7 +137,7 @@ public function testFieldComponent() { $display = entity_create('entity_display', array( 'targetEntityType' => 'entity_test', 'bundle' => 'entity_test', - 'viewMode' => 'default', + 'displayMode' => 'default', )); // Create a field and an instance. diff --git a/core/modules/field/field.api.php b/core/modules/field/field.api.php index e21a4db..d50fc8a 100644 --- a/core/modules/field/field.api.php +++ b/core/modules/field/field.api.php @@ -24,7 +24,7 @@ * @see _field_extra_fields_pre_render() * @see hook_field_extra_fields_alter() * - * @return + * @return array * A nested array of 'pseudo-field' components. Each list is nested within the * following keys: entity type, bundle name, context (either 'form' or * 'display'). The keys are the name of the elements as appearing in the @@ -33,8 +33,7 @@ * - label: The human readable name of the component. * - description: A short description of the component contents. * - weight: The default weight of the element. - * - visible: The default visibility of the element. Only for 'display' - * context. + * - visible: The default visibility of the element. */ function hook_field_extra_fields() { $extra = array(); diff --git a/core/modules/field/field.attach.inc b/core/modules/field/field.attach.inc index f2ea5d6..20389e5 100644 --- a/core/modules/field/field.attach.inc +++ b/core/modules/field/field.attach.inc @@ -1440,7 +1440,7 @@ function field_attach_view(EntityInterface $entity, EntityDisplay $display, $lan $entity = $entity->getOriginalEntity(); // Let other modules alter the renderable array. - $view_mode = $display->originalViewMode; + $view_mode = $display->originalDisplayMode; $context = array( 'entity' => $entity, 'view_mode' => $view_mode, diff --git a/core/modules/field/field.info.inc b/core/modules/field/field.info.inc index 6aea80f..c144b37 100644 --- a/core/modules/field/field.info.inc +++ b/core/modules/field/field.info.inc @@ -155,23 +155,22 @@ function _field_info_collate_types_reset() { /** * Determines the behavior of a widget with respect to an operation. * - * @param $op - * The name of the operation. Currently supported: 'default value', 'multiple - * values'. - * @param $instance + * @param string $op + * The name of the operation. Currently supported: 'default_value', + * 'multiple_values'. + * @param array $instance * The field instance array. * - * @return + * @return int * One of these values: * - FIELD_BEHAVIOR_NONE: Do nothing for this operation. * - FIELD_BEHAVIOR_CUSTOM: Use the widget's callback function. * - FIELD_BEHAVIOR_DEFAULT: Use field.module default behavior. */ function field_behaviors_widget($op, $instance) { - // @todo Cleanup this crap. $info = array(); - if ($test = entity_get_form_display($instance['entity_type'], $instance['bundle'])->getWidget($instance['field_name'])) { - $info = $test->getDefinition(); + if ($component = entity_get_form_display($instance['entity_type'], $instance['bundle'])->getComponent($instance['field_name'])) { + $info = field_info_widget_types($component['type']); } return isset($info[$op]) ? $info[$op] : FIELD_BEHAVIOR_DEFAULT; } diff --git a/core/modules/field/lib/Drupal/field/FieldInfo.php b/core/modules/field/lib/Drupal/field/FieldInfo.php index f58d2a9..054e699 100644 --- a/core/modules/field/lib/Drupal/field/FieldInfo.php +++ b/core/modules/field/lib/Drupal/field/FieldInfo.php @@ -513,10 +513,9 @@ public function prepareInstance($instance, $field_type) { $instance['settings'] += field_info_instance_settings($field_type); // Set a default value for the instance. - // @todo Update this code. -// if (field_behaviors_widget('default value', $instance) == FIELD_BEHAVIOR_DEFAULT && !isset($instance['default_value'])) { -// $instance['default_value'] = NULL; -// } + if (!isset($instance['default_value']) && field_behaviors_widget('default_value', $instance) == FIELD_BEHAVIOR_DEFAULT) { + $instance['default_value'] = NULL; + } return $instance; } diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetPluginManager.php b/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetPluginManager.php index 7de723e..1f1a97c 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetPluginManager.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetPluginManager.php @@ -54,15 +54,15 @@ public function __construct($namespaces) { * - form_mode: (string) The form mode. * - prepare: (bool, optional) Whether default values should get merged in * the 'configuration' array. Defaults to TRUE. - * - configuration: (array) the configuration for the formatter. The + * - configuration: (array) the configuration for the widget. The * following key value pairs are allowed, and are all optional if * 'prepare' is TRUE: - * - type: (string) The formatter to use. Defaults to the - * 'default_formatter' for the field type, specified in - * hook_field_info(). The default formatter will also be used if the - * requested formatter is not available. - * - settings: (array) Settings specific to the formatter. Each setting - * defaults to the default value specified in the formatter definition. + * - type: (string) The widget to use. Defaults to the + * 'default_widget' for the field type, specified in + * hook_field_info(). The default widget will also be used if the + * requested widget is not available. + * - settings: (array) Settings specific to the widget. Each setting + * defaults to the default value specified in the widget definition. * * @return \Drupal\field\Plugin\Type\Widget\WidgetInterface * A Widget object. @@ -117,7 +117,7 @@ public function prepareConfiguration($field_type, array $configuration) { $field_type = field_info_field_types($field_type); $configuration['type'] = $field_type['default_widget']; } - // Fill in default settings values for the formatter. + // Fill in default settings values for the widget. $configuration['settings'] += field_info_widget_settings($configuration['type']); return $configuration; diff --git a/core/modules/field_ui/field_ui.admin.inc b/core/modules/field_ui/field_ui.admin.inc index 64fb8eb..74c0cb8 100644 --- a/core/modules/field_ui/field_ui.admin.inc +++ b/core/modules/field_ui/field_ui.admin.inc @@ -551,7 +551,7 @@ function field_ui_field_settings_form($form, &$form_state, $instance) { ), ), ); - if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_DEFAULT) { + if (field_behaviors_widget('multiple_values', $instance) == FIELD_BEHAVIOR_DEFAULT) { $form['field']['container']['#description'] = t('%unlimited will provide an %add-more button so users can add as many values as they like.', array( '%unlimited' => t('Unlimited'), '%add-more' => t('Add another item'), diff --git a/core/modules/simpletest/lib/Drupal/simpletest/Tests/DrupalUnitTestBaseTest.php b/core/modules/simpletest/lib/Drupal/simpletest/Tests/DrupalUnitTestBaseTest.php index 3aeab60..c9b84e5 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/Tests/DrupalUnitTestBaseTest.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/Tests/DrupalUnitTestBaseTest.php @@ -232,7 +232,7 @@ function testEnableModulesFixedList() { $display = entity_create('entity_display', array( 'targetEntityType' => 'entity_test', 'bundle' => 'entity_test', - 'viewMode' => 'default', + 'displayMode' => 'default', )); $field = array( 'field_name' => 'test_field', diff --git a/core/modules/user/user.install b/core/modules/user/user.install index 496c6cc..8b4b096 100644 --- a/core/modules/user/user.install +++ b/core/modules/user/user.install @@ -747,8 +747,7 @@ function user_update_8011() { $widget = 'image_image'; } else { - $formatter = 'hidden'; - $widget = 'field_hidden'; + $formatter = $widget = 'hidden'; } // Assign form settings for the 'default' form mode.