commit c13a601c576e0043368cf09dfbde98ff042ce1d8 Author: fago Date: Wed May 22 15:03:41 2013 -0700 byConstraints() diff --git a/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php b/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php index 8d25eb3..5aa723f 100644 --- a/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php +++ b/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php @@ -237,7 +237,7 @@ protected function attachPropertyData(array &$entities, $load_revision = FALSE) $data = $query->execute(); // Fetch the field definitions to check which field is translatable. - $field_definition = \Drupal::entityManager()->getFieldDefinitions($this->entityType, array()); + $field_definition = \Drupal::entityManager()->getFieldDefinitions($this->entityType); $data_fields = array_flip(drupal_schema_fields_sql($this->entityInfo['data_table'])); foreach ($data as $values) { diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index 44526f5..28a7a74 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -288,16 +288,9 @@ public function getAdminPath($entity_type, $bundle) { * * @param string $entity_type * The entity type to get field definitions for. - * @param array $constraints - * An array of entity constraints as used for entities in typed data - * definitions, i.e. an array having an 'EntityType' and optionally a - * 'Bundle' key. For example: - * @code - * array( - * 'EntityType' => 'node', - * 'Bundle' => 'article', - * ) - * @endcode + * @param string $bundle + * (optional) The entity bundle for which to get field definitions. If FALSE + * is passed, no bundle-specific fields are included. Defaults to FALSE. * * @return array * An array of field definitions of entity fields, keyed by field @@ -309,10 +302,10 @@ public function getAdminPath($entity_type, $bundle) { * - configurable: A boolean indicating whether the field is configurable * via field.module. Defaults to FALSE. * - * @see Drupal\Core\TypedData\TypedDataManager::create() - * @see typed_data() + * @see \Drupal\Core\TypedData\TypedDataManager::create() + * @see \Drupal\Core\Entity\EntityManager::getFieldDefinitionsByConstraints() */ - public function getFieldDefinitions($entity_type, array $constraints) { + public function getFieldDefinitions($entity_type, $bundle = FALSE) { if (!isset($this->entityFieldInfo[$entity_type])) { // First, try to load from cache. $cid = 'entity_field_definitions:' . $entity_type . ':' . $this->languageManager->getLanguage(LANGUAGE_TYPE_INTERFACE)->langcode; @@ -349,17 +342,46 @@ public function getFieldDefinitions($entity_type, array $constraints) { } } - $bundle = !empty($constraints['Bundle']) ? $constraints['Bundle'] : FALSE; - - // Add in per-bundle fields. - if (!isset($this->fieldDefinitions[$entity_type][$bundle])) { - $this->fieldDefinitions[$entity_type][$bundle] = $this->entityFieldInfo[$entity_type]['definitions']; - - if ($bundle && isset($this->entityFieldInfo[$entity_type]['bundle map'][$bundle])) { - $this->fieldDefinitions[$entity_type][$bundle] += array_intersect_key($this->entityFieldInfo[$entity_type]['optional'], array_flip($this->entityFieldInfo[$entity_type]['bundle map'][$bundle])); + if (!$bundle) { + return $this->entityFieldInfo[$entity_type]['definitions']; + } + else { + // Add in per-bundle fields. + if (!isset($this->fieldDefinitions[$entity_type][$bundle])) { + $this->fieldDefinitions[$entity_type][$bundle] = $this->entityFieldInfo[$entity_type]['definitions']; + if (isset($this->entityFieldInfo[$entity_type]['bundle map'][$bundle])) { + $this->fieldDefinitions[$entity_type][$bundle] += array_intersect_key($this->entityFieldInfo[$entity_type]['optional'], array_flip($this->entityFieldInfo[$entity_type]['bundle map'][$bundle])); + } } + return $this->fieldDefinitions[$entity_type][$bundle]; } - return $this->fieldDefinitions[$entity_type][$bundle]; + } + + /** + * Gets an array of entity field definitions based on validation constraints. + * + * @param string $entity_type + * The entity type to get field definitions for. + * @param array $constraints + * An array of entity constraints as used for entities in typed data + * definitions, i.e. an array optionally including a 'Bundle' key. + * For example the constraints used by an entity reference could be: + * @code + * array( + * 'EntityType' => 'node', + * 'Bundle' => 'article', + * ) + * @endcode + * + * @return array + * An array of field definitions of entity fields, keyed by field + * name. See EntityManager::getFieldDefinitions(). + * + * @see \Drupal\Core\Entity\EntityManager::getFieldDefinitions() + */ + public function getFieldDefinitionsByConstraints($entity_type, array $constraints) { + // @todo: Add support for specifying multiple bundles. + return $this->getFieldDefinitions($entity_type, isset($constraints['Bundle']) ? $constraints['Bundle'] : FALSE); } } diff --git a/core/lib/Drupal/Core/Entity/EntityNG.php b/core/lib/Drupal/Core/Entity/EntityNG.php index e8d42bf..04d77b8 100644 --- a/core/lib/Drupal/Core/Entity/EntityNG.php +++ b/core/lib/Drupal/Core/Entity/EntityNG.php @@ -223,10 +223,8 @@ public function getPropertyDefinition($name) { */ public function getPropertyDefinitions() { if (!isset($this->fieldDefinitions)) { - $this->fieldDefinitions = \Drupal::entityManager()->getFieldDefinitions($this->entityType, array( - 'EntityType' => $this->entityType, - 'Bundle' => $this->bundle, - )); + $bundle = $this->bundle != $this->entityType ? $this->bundle : FALSE; + $this->fieldDefinitions = \Drupal::entityManager()->getFieldDefinitions($this->entityType, $bundle); } return $this->fieldDefinitions; } diff --git a/core/lib/Drupal/Core/Entity/Field/Type/EntityWrapper.php b/core/lib/Drupal/Core/Entity/Field/Type/EntityWrapper.php index d62ffb3..f85e7f1 100644 --- a/core/lib/Drupal/Core/Entity/Field/Type/EntityWrapper.php +++ b/core/lib/Drupal/Core/Entity/Field/Type/EntityWrapper.php @@ -178,7 +178,7 @@ public function getPropertyDefinition($name) { */ public function getPropertyDefinitions() { // @todo: Support getting definitions if multiple bundles are specified. - return \Drupal::entityManager()->getFieldDefinitions($this->entityType, $this->definition['constraints']); + return \Drupal::entityManager()->getFieldDefinitionsByConstraints($this->entityType, $this->definition['constraints']); } /**