diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index 679f849..2990975 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -341,12 +341,18 @@ public function getBaseFieldDefinitions($entity_type_id) { * An array of field definitions, keyed by field name. * * @throws \LogicException - * Thrown if one of the entity keys is flagged as translatable. + * Thrown if a config entity type is given or if one of the entity keys is + * flagged as translatable. */ protected function buildBaseFieldDefinitions($entity_type_id) { $entity_type = $this->getDefinition($entity_type_id); $class = $entity_type->getClass(); + // Fail with an exception for config entity types. + if (in_array('Drupal\Core\Config\Entity\ConfigEntityInterface', class_implements($class))) { + throw new \LogicException(String::format('Getting the base fields is not supported for config entity type @type.', array('@type' => $entity_type->getLabel()))); + } + // Retrieve base field definitions and assign them the entity type provider. $base_field_definitions = $class::baseFieldDefinitions($entity_type); $provider = $entity_type->getProvider(); diff --git a/core/lib/Drupal/Core/Entity/TypedData/EntityDataDefinition.php b/core/lib/Drupal/Core/Entity/TypedData/EntityDataDefinition.php index 7962aef..07d844e 100644 --- a/core/lib/Drupal/Core/Entity/TypedData/EntityDataDefinition.php +++ b/core/lib/Drupal/Core/Entity/TypedData/EntityDataDefinition.php @@ -7,7 +7,6 @@ namespace Drupal\Core\Entity\TypedData; -use Drupal\Component\Utility\String; use Drupal\Core\TypedData\ComplexDataDefinitionBase; /** @@ -58,20 +57,21 @@ public static function createFromDataType($data_type) { public function getPropertyDefinitions() { if (!isset($this->propertyDefinitions)) { if ($entity_type_id = $this->getEntityTypeId()) { - // Fail with an exception for entity types which don't support type data. + // Return an empty array for entity types that don't support typed data. $entity_type_class = \Drupal::entityManager()->getDefinition($entity_type_id)->getClass(); if (!in_array('Drupal\Core\TypedData\TypedDataInterface', class_implements($entity_type_class))) { - throw new \InvalidArgumentException(String::format('The %entity_type entity type does not support data definitions.', array('%entity_type' => $entity_type_id))); - } - - // @todo: Add support for handling multiple bundles. - // See https://drupal.org/node/2169813. - $bundles = $this->getBundles(); - if (is_array($bundles) && count($bundles) == 1) { - $this->propertyDefinitions = \Drupal::entityManager()->getFieldDefinitions($entity_type_id, reset($bundles)); + $this->propertyDefinitions = array(); } else { - $this->propertyDefinitions = \Drupal::entityManager()->getBaseFieldDefinitions($entity_type_id); + // @todo: Add support for handling multiple bundles. + // See https://drupal.org/node/2169813. + $bundles = $this->getBundles(); + if (is_array($bundles) && count($bundles) == 1) { + $this->propertyDefinitions = \Drupal::entityManager()->getFieldDefinitions($entity_type_id, reset($bundles)); + } + else { + $this->propertyDefinitions = \Drupal::entityManager()->getBaseFieldDefinitions($entity_type_id); + } } } else { diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTypedDataDefinitionTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTypedDataDefinitionTest.php index f0de0b8..30eb212 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTypedDataDefinitionTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTypedDataDefinitionTest.php @@ -115,15 +115,9 @@ public function testEntities() { $this->assertEqual($this->typedDataManager->createDataDefinition('entity'), EntityDataDefinition::create()); $this->assertEqual($this->typedDataManager->createDataDefinition('entity:node'), EntityDataDefinition::create('node')); - // Test that creating a definition for an entity type which doesn't support - // typed data doesn't work. - try { - EntityDataDefinition::create('node_type'); - $this->fail("Creating data definition objects for entities which don't support typed data works."); - } - catch (\Exception $e) { - $this->pass("Creating data definition objects for entities which don't support typed data doesn't work."); - } + // Config entities don't support typed data. + $entity_definition = EntityDataDefinition::create('node_type'); + $this->assertEqual(array(), $entity_definition->getPropertyDefinitions()); } /**