diff --git a/core/lib/Drupal/Core/Entity/ContentEntityDatabaseStorage.php b/core/lib/Drupal/Core/Entity/ContentEntityDatabaseStorage.php index a650fce..0cfb7c4 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityDatabaseStorage.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityDatabaseStorage.php @@ -1141,8 +1141,7 @@ public function onFieldCreate(FieldStorageDefinitionInterface $storage_definitio /** * {@inheritdoc} */ - public function onFieldUpdate(FieldStorageDefinitionInterface $storage_definition) { - $original = $storage_definition->original; + public function onFieldUpdate(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) { if (!$storage_definition->hasData()) { // There is no data. Re-create the tables completely. @@ -1252,11 +1251,11 @@ public function onInstanceDelete(FieldDefinitionInterface $field_definition) { $revision_name = static::_fieldRevisionTableName($field_definition); $this->database->update($table_name) ->fields(array('deleted' => 1)) - ->condition('bundle', $field_definition->bundle) + ->condition('bundle', $field_definition->getBundle()) ->execute(); $this->database->update($revision_name) ->fields(array('deleted' => 1)) - ->condition('bundle', $field_definition->bundle) + ->condition('bundle', $field_definition->getBundle()) ->execute(); } @@ -1292,7 +1291,7 @@ protected function readFieldItemsToPurge(FieldDefinitionInterface $field_definit $table_name = static::_fieldTableName($field_definition, $is_deleted); $query = $this->database->select($table_name, 't', array('fetch' => \PDO::FETCH_ASSOC)) ->fields('t') - ->condition('bundle', $field_definition->bundle) + ->condition('bundle', $field_definition->getBundle()) ->orderBy('entity_id') ->orderBy('revision_id') ->orderBy('delta') @@ -1396,17 +1395,8 @@ public function countFieldData($storage_definition, $as_bool = FALSE) { * @see hook_schema() */ public static function _fieldSqlSchema(FieldStorageDefinitionInterface $storage_definition, array $schema = NULL, $deleted = FALSE) { - if ($storage_definition->deleted && !$deleted) { - throw new \Exception("Field schema for deleted field to be returned."); - } - if ($deleted) { - $description_current = "Data storage for deleted field {$storage_definition->getUniqueStorageIdentifier()} ({$storage_definition->getTargetEntityTypeId()}, {$storage_definition->getName()})."; - $description_revision = "Revision archive storage for deleted field {$storage_definition->getUniqueStorageIdentifier()} ({$storage_definition->getTargetEntityTypeId()}, {$storage_definition->getName()})."; - } - else { - $description_current = "Data storage for {$storage_definition->getTargetEntityTypeId()} field {$storage_definition->getName()}."; - $description_revision = "Revision archive storage for {$storage_definition->getTargetEntityTypeId()} field {$storage_definition->getName()}."; - } + $description_current = "Data storage for {$storage_definition->getTargetEntityTypeId()} field {$storage_definition->getName()}."; + $description_revision = "Revision archive storage for {$storage_definition->getTargetEntityTypeId()} field {$storage_definition->getName()}."; $entity_type_id = $storage_definition->getTargetEntityTypeId(); $entity_manager = \Drupal::entityManager(); diff --git a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php index 23e1ffa..c72f7de 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php @@ -273,7 +273,7 @@ public function onFieldCreate(FieldStorageDefinitionInterface $storage_definitio /** * {@inheritdoc} */ - public function onFieldUpdate(FieldStorageDefinitionInterface $storage_definition) { } + public function onFieldUpdate(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) { } /** * {@inheritdoc} diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index 93e9fcf..f27c8d4 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -391,6 +391,7 @@ protected function buildBaseFieldDefinitions($entity_type_id) { if ($base_field_definition instanceof FieldDefinition) { $base_field_definition->setName($field_name); $base_field_definition->setTargetEntityTypeId($entity_type_id); + $base_field_definition->setBundle(NULL); } } @@ -488,6 +489,7 @@ protected function buildBundleFieldDefinitions($entity_type_id, $bundle, array $ if ($field_definition instanceof FieldDefinition) { $field_definition->setName($field_name); $field_definition->setTargetEntityTypeId($entity_type_id); + $field_definition->setBundle($bundle); } } diff --git a/core/lib/Drupal/Core/Entity/FieldableEntityStorageInterface.php b/core/lib/Drupal/Core/Entity/FieldableEntityStorageInterface.php index 5245d8f..4e27be2 100644 --- a/core/lib/Drupal/Core/Entity/FieldableEntityStorageInterface.php +++ b/core/lib/Drupal/Core/Entity/FieldableEntityStorageInterface.php @@ -25,8 +25,10 @@ public function onFieldCreate(FieldStorageDefinitionInterface $storage_definitio * * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition * The field being updated. + * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $original + * The original storage definition; i.e., the definition before the update. */ - public function onFieldUpdate(FieldStorageDefinitionInterface $storage_definition); + public function onFieldUpdate(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original); /** * Allows reaction to the deletion of a field. diff --git a/core/lib/Drupal/Core/Field/FieldDefinition.php b/core/lib/Drupal/Core/Field/FieldDefinition.php index a7837a0..6eadbda 100644 --- a/core/lib/Drupal/Core/Field/FieldDefinition.php +++ b/core/lib/Drupal/Core/Field/FieldDefinition.php @@ -460,8 +460,7 @@ public function getTargetEntityTypeId() { * @param string $entity_type_id * The name of the target entity type to set. * - * @return static - * The object itself for chaining. + * @return $this */ public function setTargetEntityTypeId($entity_type_id) { $this->definition['entity_type'] = $entity_type_id; @@ -471,6 +470,26 @@ public function setTargetEntityTypeId($entity_type_id) { /** * {@inheritdoc} */ + public function getBundle() { + return isset($this->definition['bundle']) ? $this->definition['bundle'] : NULL; + } + + /** + * Sets the bundle this field is defined for. + * + * @param string|null $bundle + * The bundle, or NULL if the field is not bundle-specific. + * + * @return $this + */ + public function setBundle($bundle) { + $this->definition['bundle'] = $bundle; + return $this; + } + + /** + * {@inheritdoc} + */ public function getSchema() { if (!isset($this->schema)) { // Get the schema from the field item class. @@ -550,5 +569,4 @@ public function getUniqueStorageIdentifier() { return $this->getTargetEntityTypeId() . '-' . $this->getName(); } - } diff --git a/core/lib/Drupal/Core/Field/FieldDefinitionInterface.php b/core/lib/Drupal/Core/Field/FieldDefinitionInterface.php index a6c9190..194f77b 100644 --- a/core/lib/Drupal/Core/Field/FieldDefinitionInterface.php +++ b/core/lib/Drupal/Core/Field/FieldDefinitionInterface.php @@ -55,6 +55,15 @@ interface FieldDefinitionInterface extends FieldStorageDefinitionInterface, ListDataDefinitionInterface { /** + * Gets the bundle the field is defined for. + * + * @return string|null + * The bundle the field is defined for, or NULL if it is base field; i.e., + * it is not bundle-specific. + */ + public function getBundle(); + + /** * Returns whether the display for the field can be configured. * * @param string $display_context diff --git a/core/lib/Drupal/Core/Field/FieldStorageDefinitionInterface.php b/core/lib/Drupal/Core/Field/FieldStorageDefinitionInterface.php index b8961dc..6adfb1d 100644 --- a/core/lib/Drupal/Core/Field/FieldStorageDefinitionInterface.php +++ b/core/lib/Drupal/Core/Field/FieldStorageDefinitionInterface.php @@ -286,7 +286,8 @@ public function getProvider(); public function hasCustomStorage(); /** - * @todo + * Returns a unique identifier for the field. + * * @return string */ public function getUniqueStorageIdentifier(); diff --git a/core/modules/field/src/Entity/FieldConfig.php b/core/modules/field/src/Entity/FieldConfig.php index 838488d..66aa577 100644 --- a/core/modules/field/src/Entity/FieldConfig.php +++ b/core/modules/field/src/Entity/FieldConfig.php @@ -368,7 +368,7 @@ protected function preSaveUpdated(EntityStorageInterface $storage) { // Notify the storage. The controller can reject the definition // update as invalid by raising an exception, which stops execution before // the definition is written to config. - $entity_manager->getStorage($this->entity_type)->onFieldUpdate($this); + $entity_manager->getStorage($this->entity_type)->onFieldUpdate($this, $this->original); } /** @@ -744,7 +744,6 @@ public function getUniqueStorageIdentifier() { return $this->uuid(); } - /** * Helper to retrieve the field item class. */ diff --git a/core/modules/field/src/Entity/FieldInstanceConfig.php b/core/modules/field/src/Entity/FieldInstanceConfig.php index 2b4ff56..25d511f 100644 --- a/core/modules/field/src/Entity/FieldInstanceConfig.php +++ b/core/modules/field/src/Entity/FieldInstanceConfig.php @@ -630,6 +630,13 @@ public function getTargetEntityTypeId() { /** * {@inheritdoc} */ + public function getBundle() { + return $this->bundle; + } + + /** + * {@inheritdoc} + */ public function isQueryable() { return TRUE; }