commit ce14b519c7be7f249d8bd869a1cdf5d35d86de79 Merge: eebcc56 e18b616 Author: fago Date: Mon Sep 29 01:25:05 2014 +0200 Merge branch '8.0.x' into fieldable-2 Conflicts: core/lib/Drupal/Core/Entity/ContentEntityInterface.php diff --cc core/lib/Drupal/Core/Entity/ContentEntityInterface.php index 551fc08,485068d..c67bea0 --- a/core/lib/Drupal/Core/Entity/ContentEntityInterface.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityInterface.php @@@ -37,4 -39,182 +37,5 @@@ interface ContentEntityInterface extend * regular fields. */ public function initTranslation($langcode); + - /** - * Provides base field definitions for an entity type. - * - * Implementations typically use the class - * \Drupal\Core\Field\BaseFieldDefinition for creating the field definitions; - * for example a 'name' field could be defined as the following: - * @code - * $fields['name'] = BaseFieldDefinition::create('string') - * ->setLabel(t('Name')); - * @endcode - * - * By definition, base fields are fields that exist for every bundle. To - * provide definitions for fields that should only exist on some bundles, use - * \Drupal\Core\Entity\ContentEntityInterface::bundleFieldDefinitions(). - * - * The definitions returned by this function can be overridden for all - * bundles by hook_entity_base_field_info_alter() or overridden on a - * per-bundle basis via 'base_field_override' configuration entities. - * - * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type - * The entity type definition. Useful when a single class is used for multiple, - * possibly dynamic entity types. - * - * @return \Drupal\Core\Field\FieldDefinitionInterface[] - * An array of base field definitions for the entity type, keyed by field - * name. - * - * @see \Drupal\Core\Entity\EntityManagerInterface::getFieldDefinitions() - * @see \Drupal\Core\Entity\ContentEntityInterface::bundleFieldDefinitions() - */ - public static function baseFieldDefinitions(EntityTypeInterface $entity_type); - - /** - * Provides field definitions for a specific bundle. - * - * This function can return definitions both for bundle fields (fields that - * are not defined in $base_field_definitions, and therefore might not exist - * on some bundles) as well as bundle-specific overrides of base fields - * (fields that are defined in $base_field_definitions, and therefore exist - * for all bundles). However, bundle-specific base field overrides can also - * be provided by 'base_field_override' configuration entities, and that is - * the recommended approach except in cases where an entity type needs to - * provide a bundle-specific base field override that is decoupled from - * configuration. Note that for most entity types, the bundles themselves are - * derived from configuration (e.g., 'node' bundles are managed via - * 'node_type' configuration entities), so decoupling bundle-specific base - * field overrides from configuration only makes sense for entity types that - * also decouple their bundles from configuration. In cases where both this - * function returns a bundle-specific override of a base field and a - * 'base_field_override' configuration entity exists, the latter takes - * precedence. - * - * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type - * The entity type definition. Useful when a single class is used for multiple, - * possibly dynamic entity types. - * @param string $bundle - * The bundle. - * @param \Drupal\Core\Field\FieldDefinitionInterface[] $base_field_definitions - * The list of base field definitions. - * - * @return \Drupal\Core\Field\FieldDefinitionInterface[] - * An array of bundle field definitions, keyed by field name. - * - * @see \Drupal\Core\Entity\EntityManagerInterface::getFieldDefinitions() - * @see \Drupal\Core\Entity\ContentEntityInterface::baseFieldDefinitions() - * - * @todo WARNING: This method will be changed in - * https://www.drupal.org/node/2346347. - */ - public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions); - - /** - * Returns whether the entity has a field with the given name. - * - * @param string $field_name - * The field name. - * - * @return bool - * TRUE if the entity has a field with the given name. FALSE otherwise. - */ - public function hasField($field_name); - - /** - * Gets the definition of a contained field. - * - * @param string $name - * The name of the field. - * - * @return \Drupal\Core\Field\FieldDefinitionInterface|null - * The definition of the field or null if the field does not exist. - */ - public function getFieldDefinition($name); - - /** - * Gets an array of field definitions of all contained fields. - * - * @return \Drupal\Core\Field\FieldDefinitionInterface[] - * An array of field definitions, keyed by field name. - * - * @see \Drupal\Core\Entity\EntityManagerInterface::getFieldDefinitions() - */ - public function getFieldDefinitions(); - - /** - * Returns an array of all field values. - * - * Gets an array of plain field values, including only non-computed values. - * Note that the structure varies by entity type and bundle. - * - * @return array - * An array of field values, keyed by field name. - */ - public function toArray(); - - /** - * Gets a field item list. - * - * @param string $field_name - * The name of the field to get; e.g., 'title' or 'name'. - * - * @throws \InvalidArgumentException - * If an invalid field name is given. - * - * @return \Drupal\Core\Field\FieldItemListInterface - * The field item list, containing the field items. - */ - public function get($field_name); - - /** - * Sets a field value. - * - * @param string $field_name - * The name of the field to set; e.g., 'title' or 'name'. - * @param mixed $value - * The value to set, or NULL to unset the field. - * @param bool $notify - * (optional) Whether to notify the entity of the change. Defaults to - * TRUE. If the update stems from the entity, set it to FALSE to avoid - * being notified again. - * - * @throws \InvalidArgumentException - * If the specified field does not exist. - * - * @return $this - */ - public function set($field_name, $value, $notify = TRUE); - - /** - * Gets an array of field item lists. - * - * @param bool $include_computed - * If set to TRUE, computed fields are included. Defaults to FALSE. - * - * @return \Drupal\Core\Field\FieldItemListInterface[] - * An array of field item lists implementing, keyed by field name. - */ - public function getFields($include_computed = FALSE); - - /** - * Reacts to changes to a field. - * - * Note that this is invoked after any changes have been applied. - * - * @param string $field_name - * The name of the field which is changed. - */ - public function onChange($field_name); - - /** - * Validates the currently set values. - * - * @return \Symfony\Component\Validator\ConstraintViolationListInterface - * A list of constraint violations. If the list is empty, validation - * succeeded. - */ - public function validate(); - } diff --cc core/lib/Drupal/Core/Entity/FieldableEntityInterface.php index 1c36c51,0000000..6c296f1 mode 100644,000000..100644 --- a/core/lib/Drupal/Core/Entity/FieldableEntityInterface.php +++ b/core/lib/Drupal/Core/Entity/FieldableEntityInterface.php @@@ -1,142 -1,0 +1,209 @@@ +setLabel(t('Name')); + * @endcode + * + * By definition, base fields are fields that exist for every bundle. To + * provide definitions for fields that should only exist on some bundles, use + * \Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions(). + * + * The definitions returned by this function can be overridden for all + * bundles by hook_entity_base_field_info_alter() or overridden on a + * per-bundle basis via 'base_field_override' configuration entities. + * + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * The entity type definition. Useful when a single class is used for multiple, + * possibly dynamic entity types. + * + * @return \Drupal\Core\Field\FieldDefinitionInterface[] + * An array of base field definitions for the entity type, keyed by field + * name. + * + * @see \Drupal\Core\Entity\EntityManagerInterface::getFieldDefinitions() + * @see \Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions() + */ + public static function baseFieldDefinitions(EntityTypeInterface $entity_type); + + /** + * Provides field definitions for a specific bundle. + * + * This function can return definitions both for bundle fields (fields that + * are not defined in $base_field_definitions, and therefore might not exist + * on some bundles) as well as bundle-specific overrides of base fields + * (fields that are defined in $base_field_definitions, and therefore exist + * for all bundles). However, bundle-specific base field overrides can also + * be provided by 'base_field_override' configuration entities, and that is + * the recommended approach except in cases where an entity type needs to + * provide a bundle-specific base field override that is decoupled from + * configuration. Note that for most entity types, the bundles themselves are + * derived from configuration (e.g., 'node' bundles are managed via + * 'node_type' configuration entities), so decoupling bundle-specific base + * field overrides from configuration only makes sense for entity types that + * also decouple their bundles from configuration. In cases where both this + * function returns a bundle-specific override of a base field and a + * 'base_field_override' configuration entity exists, the latter takes + * precedence. + * + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * The entity type definition. Useful when a single class is used for multiple, + * possibly dynamic entity types. + * @param string $bundle + * The bundle. + * @param \Drupal\Core\Field\FieldDefinitionInterface[] $base_field_definitions + * The list of base field definitions. + * + * @return \Drupal\Core\Field\FieldDefinitionInterface[] + * An array of bundle field definitions, keyed by field name. + * + * @see \Drupal\Core\Entity\EntityManagerInterface::getFieldDefinitions() + * @see \Drupal\Core\Entity\FieldableEntityInterface::baseFieldDefinitions() ++ * ++ * @todo WARNING: This method will be changed in ++ * https://www.drupal.org/node/2346347. + */ + public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions); + + /** + * Returns whether the entity has a field with the given name. + * + * @param string $field_name + * The field name. + * + * @return bool + * TRUE if the entity has a field with the given name. FALSE otherwise. + */ + public function hasField($field_name); + + /** + * Gets the definition of a contained field. + * + * @param string $name + * The name of the field. + * + * @return \Drupal\Core\Field\FieldDefinitionInterface|null + * The definition of the field or null if the field does not exist. + */ + public function getFieldDefinition($name); + + /** + * Gets an array of field definitions of all contained fields. + * + * @return \Drupal\Core\Field\FieldDefinitionInterface[] + * An array of field definitions, keyed by field name. + * + * @see \Drupal\Core\Entity\EntityManagerInterface::getFieldDefinitions() + */ + public function getFieldDefinitions(); + + /** + * Returns an array of all field values. + * + * Gets an array of plain field values, including only non-computed values. + * Note that the structure varies by entity type and bundle. + * + * @return array + * An array of field values, keyed by field name. + */ + public function toArray(); ++ ++ /** ++ * Gets a field item list. ++ * ++ * @param string $field_name ++ * The name of the field to get; e.g., 'title' or 'name'. ++ * ++ * @throws \InvalidArgumentException ++ * If an invalid field name is given. ++ * ++ * @return \Drupal\Core\Field\FieldItemListInterface ++ * The field item list, containing the field items. ++ */ ++ public function get($field_name); ++ ++ /** ++ * Sets a field value. ++ * ++ * @param string $field_name ++ * The name of the field to set; e.g., 'title' or 'name'. ++ * @param mixed $value ++ * The value to set, or NULL to unset the field. ++ * @param bool $notify ++ * (optional) Whether to notify the entity of the change. Defaults to ++ * TRUE. If the update stems from the entity, set it to FALSE to avoid ++ * being notified again. ++ * ++ * @throws \InvalidArgumentException ++ * If the specified field does not exist. ++ * ++ * @return $this ++ */ ++ public function set($field_name, $value, $notify = TRUE); ++ ++ /** ++ * Gets an array of field item lists. ++ * ++ * @param bool $include_computed ++ * If set to TRUE, computed fields are included. Defaults to FALSE. ++ * ++ * @return \Drupal\Core\Field\FieldItemListInterface[] ++ * An array of field item lists implementing, keyed by field name. ++ */ ++ public function getFields($include_computed = FALSE); ++ ++ /** ++ * Reacts to changes to a field. ++ * ++ * Note that this is invoked after any changes have been applied. ++ * ++ * @param string $field_name ++ * The name of the field which is changed. ++ */ ++ public function onChange($field_name); ++ ++ /** ++ * Validates the currently set values. ++ * ++ * @return \Symfony\Component\Validator\ConstraintViolationListInterface ++ * A list of constraint violations. If the list is empty, validation ++ * succeeded. ++ */ ++ public function validate(); ++ +} diff --cc core/lib/Drupal/Core/Field/BaseFieldDefinition.php index a8f11f6,7eb0d8a..a51333f --- a/core/lib/Drupal/Core/Field/BaseFieldDefinition.php +++ b/core/lib/Drupal/Core/Field/BaseFieldDefinition.php @@@ -434,6 -435,19 +435,19 @@@ class BaseFieldDefinition extends ListD /** * {@inheritdoc} */ - public function getOptionsProvider($property_name, ContentEntityInterface $entity) { ++ public function getOptionsProvider($property_name, FieldableEntityInterface $entity) { + // If the field item class implements the interface, proxy it through. + $item = $entity->get($this->getName())->first(); + if ($item instanceof OptionsProviderInterface) { + return $item; + } + // @todo: Allow setting custom options provider, see + // https://www.drupal.org/node/2002138. + } + + /** + * {@inheritdoc} + */ public function getPropertyDefinition($name) { if (!isset($this->propertyDefinitions)) { $this->getPropertyDefinitions(); diff --cc core/lib/Drupal/Core/Field/FieldStorageDefinitionInterface.php index 6ace3fa,7df3c83..578a6da --- a/core/lib/Drupal/Core/Field/FieldStorageDefinitionInterface.php +++ b/core/lib/Drupal/Core/Field/FieldStorageDefinitionInterface.php @@@ -7,6 -7,8 +7,8 @@@ namespace Drupal\Core\Field; -use Drupal\Core\Entity\ContentEntityInterface; ++use Drupal\Core\Entity\FieldableEntityInterface; + /** * Defines an interface for entity field storage definitions. * @@@ -131,6 -133,19 +133,19 @@@ interface FieldStorageDefinitionInterfa public function getDescription(); /** + * Gets an options provider for the given field item property. + * + * @param string $property_name + * The name of the property to get options for; e.g., 'value'. - * @param \Drupal\Core\Entity\ContentEntityInterface $entity ++ * @param \Drupal\Core\Entity\FieldableEntityInterface $entity + * The entity for which the options should be provided. + * + * @return \Drupal\Core\TypedData\OptionsProviderInterface|null + * An options provider, or NULL if no options are defined. + */ - public function getOptionsProvider($property_name, ContentEntityInterface $entity); ++ public function getOptionsProvider($property_name, FieldableEntityInterface $entity); + + /** * Returns whether the field can contain multiple items. * * @return bool diff --cc core/modules/field/src/Entity/FieldStorageConfig.php index 7f9d5d9,c6e9bd1..29794c7 --- a/core/modules/field/src/Entity/FieldStorageConfig.php +++ b/core/modules/field/src/Entity/FieldStorageConfig.php @@@ -10,9 -10,11 +10,11 @@@ namespace Drupal\field\Entity use Drupal\Component\Utility\String; use Drupal\Component\Utility\Unicode; use Drupal\Core\Config\Entity\ConfigEntityBase; -use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityStorageInterface; ++use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Field\FieldException; use Drupal\Core\Field\FieldStorageDefinitionInterface; + use Drupal\Core\TypedData\OptionsProviderInterface; use Drupal\field\FieldStorageConfigInterface; /** @@@ -583,6 -585,19 +585,19 @@@ class FieldStorageConfig extends Config /** * {@inheritdoc} */ - public function getOptionsProvider($property_name, ContentEntityInterface $entity) { ++ public function getOptionsProvider($property_name, FieldableEntityInterface $entity) { + // If the field item class implements the interface, proxy it through. + $item = $entity->get($this->getName())->first(); + if ($item instanceof OptionsProviderInterface) { + return $item; + } + // @todo: Allow setting custom options provider, see + // https://www.drupal.org/node/2002138. + } + + /** + * {@inheritdoc} + */ public function isMultiple() { $cardinality = $this->getCardinality(); return ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) || ($cardinality > 1); diff --cc core/modules/system/entity.api.php index a3f23dc,8979ce8..b9f60fe --- a/core/modules/system/entity.api.php +++ b/core/modules/system/entity.api.php @@@ -5,9 -5,10 +5,10 @@@ * Hooks provided the Entity module. */ -use Drupal\Component\Utility\String; +use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Access\AccessResult; use Drupal\Core\Entity\ContentEntityInterface; + use Drupal\Core\Entity\DynamicallyFieldableEntityStorageInterface; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Render\Element;