From 5ff3e98c4b44d873f45f21713000e7d5919639a9 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Tue, 25 Jun 2013 23:02:08 +0100 Subject: [PATCH] Issue #2013939 by swentel, amateescu, pcambra: Remove hook_field_CRUD_() in favor of hook_entity_TYPE_CRUD() calls. --- .../entity_reference/entity_reference.module | 17 +- core/modules/field/field.api.php | 110 -------- .../Drupal/field/Plugin/Core/Entity/Field.php | 19 +- .../Plugin/Core/Entity/FieldInstance.php | 20 +- .../field/lib/Drupal/field/Tests/CrudTest.php | 4 +- .../modules/field_test/field_test.module | 9 +- .../modules/field_test/field_test.storage.inc | 13 +- .../field_sql_storage.module | 10 - core/modules/image/image.module | 246 +++++++++--------- core/modules/options/options.module | 5 +- core/modules/views/views.module | 24 +- 11 files changed, 181 insertions(+), 296 deletions(-) diff --git a/core/modules/entity_reference/entity_reference.module b/core/modules/entity_reference/entity_reference.module index 6940452e7b83..b5d70a5fa651 100644 --- a/core/modules/entity_reference/entity_reference.module +++ b/core/modules/entity_reference/entity_reference.module @@ -9,6 +9,7 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Core\Database\Query\AlterableInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\Field\FieldDefinitionInterface; +use Drupal\field\FieldInterface; /** * Implements hook_field_info(). @@ -157,29 +158,29 @@ function entity_reference_field_settings_form($field, $instance) { } /** - * Implements hook_field_update_field(). + * Implements hook_ENTITY_TYPE_update() for 'field_entity'. * * Reset the instance handler settings, when the target type is changed. */ -function entity_reference_field_update_field($field, $prior_field) { - if ($field['type'] != 'entity_reference') { - // Not an entity reference field. +function entity_reference_field_entity_update(FieldInterface $field) { + if ($field->type != 'entity_reference') { + // Only act on entity reference fields. return; } - if ($field['settings']['target_type'] == $prior_field['settings']['target_type']) { + if ($field->settings['target_type'] == $field->original->settings['target_type']) { // Target type didn't change. return; } - if (empty($field['bundles'])) { + if (empty($field->bundles)) { // Field has no instances. return; } - $field_name = $field['field_name']; + $field_name = $field->id(); - foreach ($field['bundles'] as $entity_type => $bundles) { + foreach ($field->bundles() as $entity_type => $bundles) { foreach ($bundles as $bundle) { $instance = field_info_instance($entity_type, $field_name, $bundle); $instance->settings['handler_settings'] = array(); diff --git a/core/modules/field/field.api.php b/core/modules/field/field.api.php index 057027dd9f16..20e9301c9a2f 100644 --- a/core/modules/field/field.api.php +++ b/core/modules/field/field.api.php @@ -1217,33 +1217,6 @@ function hook_field_info_max_weight($entity_type, $bundle, $context, $context_mo * @{ */ -/** - * Act on a field being created. - * - * This hook lets modules react to the creation of a field. It is called after - * the definition is saved, so it cannot be used to modify the field itself. - * - * @param $field - * The field just created. - */ -function hook_field_create_field($field) { - // @todo Needs function body. -} - -/** - * Act on a field instance being created. - * - * This hook lets modules react to the creation of a field instance. It is - * called after the definition is saved, so it cannot be used to modify the - * instance itself. - * - * @param $instance - * The instance just created. - */ -function hook_field_create_instance($instance) { - // @todo Needs function body. -} - /** * Forbid a field update from occurring. * @@ -1282,89 +1255,6 @@ function hook_field_update_forbid($field, $prior_field) { } } -/** - * Act on a field being updated. - * - * This hook lets modules react to the update of an existing field. It is - * called after the definition is saved, so it cannot be used to modify the - * field itself. - * - * @param $field - * The field as it is post-update. - * @param $prior_field - * The field as it was pre-update. - */ -function hook_field_update_field($field, $prior_field) { - // Reset the static value that keeps track of allowed values for list fields. - drupal_static_reset('list_allowed_values'); -} - -/** - * Act on a field being deleted. - * - * This hook lets modules react to the deletion of an existing field. It is - * called after the definition is deleted. - * - * @param $field - * The field just deleted. - */ -function hook_field_delete_field($field) { - // @todo Needs function body. -} - -/** - * Act on a field instance being updated. - * - * This hook lets modules react to the update of an existing field instance. It - * is called after the definition is saved, so it cannot be used to modify the - * instance itself. - * - * @param $instance - * The instance as it is post-update. - * @param $prior_$instance - * The instance as it was pre-update. - */ -function hook_field_update_instance($instance, $prior_instance) { - // @todo Needs function body. -} - -/** - * Act on a field instance being deleted. - * - * This hook lets modules react to the deletion of an existing field instance. - * It is called after the definition is deleted. - * - * @param $instance - * The instance just deleted. - */ -function hook_field_delete_instance($instance) { - // @todo Needs function body. -} - -/** - * Act on field records being read from the database. - * - * This hook is invoked from field_read_fields() on each field being read. - * - * @param $field - * The field record just read from the database. - */ -function hook_field_read_field($field) { - // @todo Needs function body. -} - -/** - * Act on a field record being read from the database. - * - * This hook is invoked from field_read_instances() on each instance being read. - * - * @param $instance - * The instance record just read from the database. - */ -function hook_field_read_instance($instance) { - // @todo Needs function body. -} - /** * Acts when a field record is being purged. * diff --git a/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/Field.php b/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/Field.php index c2fe6c05a9dc..4af0fc2872f1 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/Field.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/Field.php @@ -207,6 +207,13 @@ class Field extends ConfigEntityBase implements FieldInterface { */ protected $storageDetails; + /** + * The original field. + * + * @var \Drupal\field\Plugin\Core\Entity\Field + */ + public $original = NULL; + /** * Constructs a Field object. * @@ -378,10 +385,6 @@ class Field extends ConfigEntityBase implements FieldInterface { $result = parent::save(); field_cache_clear(); - // Invoke hook_field_create_field() after the cache is cleared for API - // consistency. - $module_handler->invokeAll('field_create_field', array($this)); - return $result; } @@ -401,6 +404,7 @@ class Field extends ConfigEntityBase implements FieldInterface { $storage_controller = \Drupal::entityManager()->getStorageController($this->entityType); $original = $storage_controller->loadUnchanged($this->id()); + $this->original = $original; // Some updates are always disallowed. if ($this->type != $original->type) { @@ -432,10 +436,6 @@ class Field extends ConfigEntityBase implements FieldInterface { $result = parent::save(); field_cache_clear(); - // Invoke hook_field_update_field() after the cache is cleared for API - // consistency. - $module_handler->invokeAll('field_update_field', array($this, $original)); - return $result; } @@ -481,9 +481,6 @@ class Field extends ConfigEntityBase implements FieldInterface { // Clear the cache. field_cache_clear(); - - // Invoke hook_field_delete_field(). - $module_handler->invokeAll('field_delete_field', array($this)); } } diff --git a/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/FieldInstance.php b/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/FieldInstance.php index 449cb68c44da..5f7e435d76ea 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/FieldInstance.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/FieldInstance.php @@ -200,6 +200,13 @@ class FieldInstance extends ConfigEntityBase implements FieldInstanceInterface { */ protected $bundle_rename_allowed = FALSE; + /** + * The original instance. + * + * @var \Drupal\field\Plugin\Core\Entity\FieldInstance + */ + public $original = NULL; + /** * Constructs a FieldInstance object. * @@ -371,10 +378,6 @@ class FieldInstance extends ConfigEntityBase implements FieldInstanceInterface { $result = parent::save(); field_cache_clear(); - // Invoke hook_field_create_instance() after the cache is cleared for API - // consistency. - $module_handler->invokeAll('field_create_instance', array($this)); - return $result; } @@ -395,6 +398,7 @@ class FieldInstance extends ConfigEntityBase implements FieldInstanceInterface { $instance_controller = \Drupal::entityManager()->getStorageController($this->entityType); $original = $instance_controller->loadUnchanged($this->getOriginalID()); + $this->original = $original; // Some updates are always disallowed. if ($this->entity_type != $original->entity_type) { @@ -414,10 +418,6 @@ class FieldInstance extends ConfigEntityBase implements FieldInstanceInterface { $result = parent::save(); field_cache_clear(); - // Invoke hook_field_update_instance() after the cache is cleared for API - // consistency. - $module_handler->invokeAll('field_update_instance', array($this, $original)); - return $result; } @@ -462,10 +462,6 @@ class FieldInstance extends ConfigEntityBase implements FieldInstanceInterface { // hook_field_storage_delete_instance(). $module_handler->invoke($this->field->storage['module'], 'field_storage_delete_instance', array($this)); - // Let modules react to the deletion of the instance with - // hook_field_delete_instance(). - $module_handler->invokeAll('field_delete_instance', array($this)); - // Remove the instance from the entity form displays. if ($form_display = entity_load('entity_form_display', $this->entity_type . '.' . $this->bundle . '.default')) { $form_display->removeComponent($this->field->id())->save(); diff --git a/core/modules/field/lib/Drupal/field/Tests/CrudTest.php b/core/modules/field/lib/Drupal/field/Tests/CrudTest.php index 3f9846f968ea..72f9abe53d92 100644 --- a/core/modules/field/lib/Drupal/field/Tests/CrudTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/CrudTest.php @@ -44,8 +44,8 @@ class CrudTest extends FieldUnitTestBase { $field = entity_create('field_entity', $field_definition); $field->save(); $mem = field_test_memorize(); - $this->assertIdentical($mem['field_test_field_create_field'][0][0]['field_name'], $field_definition['field_name'], 'hook_field_create_field() called with correct arguments.'); - $this->assertIdentical($mem['field_test_field_create_field'][0][0]['type'], $field_definition['type'], 'hook_field_create_field() called with correct arguments.'); + $this->assertIdentical($mem['field_test_field_entity_create'][0][0]['field_name'], $field_definition['field_name'], 'hook_entity_create() called with correct arguments.'); + $this->assertIdentical($mem['field_test_field_entity_create'][0][0]['type'], $field_definition['type'], 'hook_entity_create() called with correct arguments.'); // Read the configuration. Check against raw configuration data rather than // the loaded ConfigEntity, to be sure we check that the defaults are diff --git a/core/modules/field/tests/modules/field_test/field_test.module b/core/modules/field/tests/modules/field_test/field_test.module index 7a2a535b8615..0f111de8e231 100644 --- a/core/modules/field/tests/modules/field_test/field_test.module +++ b/core/modules/field/tests/modules/field_test/field_test.module @@ -1,6 +1,7 @@ getField(); + if ($field->storage['type'] == 'field_test_storage') { + $field_data = &$data[$field->uuid]; foreach (array('current', 'revisions') as $sub_table) { foreach ($field_data[$sub_table] as &$row) { - if ($row->bundle == $instance['bundle']) { + if ($row->bundle == $field_instance->bundle) { $row->deleted = TRUE; } } diff --git a/core/modules/field_sql_storage/field_sql_storage.module b/core/modules/field_sql_storage/field_sql_storage.module index ef8a67f0b078..5e3e0ddfcb8a 100644 --- a/core/modules/field_sql_storage/field_sql_storage.module +++ b/core/modules/field_sql_storage/field_sql_storage.module @@ -250,16 +250,6 @@ function field_sql_storage_field_storage_create_field($field) { // been saved yet. This will be done in hook_field_create_field(). } -/** - * Implements hook_field_create_field(). - */ -function field_sql_storage_field_create_field($field) { - // Rebuild the schema now that the field has been saved. - if ($field['storage']['type'] == 'field_sql_storage') { - drupal_get_schema(NULL, TRUE); - } -} - /** * Implements hook_field_update_forbid(). * diff --git a/core/modules/image/image.module b/core/modules/image/image.module index 53cb813dab09..5eda365f0d59 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -17,6 +17,8 @@ use Drupal\Component\Utility\Crypt; use Drupal\Component\Uuid\Uuid; use Drupal\file\Plugin\Core\Entity\File; use Drupal\image\Plugin\Core\Entity\ImageStyle; +use Drupal\field\FieldInterface; +use Drupal\field\FieldInstanceInterface; /** * Image style constant for user presets in the database. @@ -346,125 +348,6 @@ function image_file_predelete(File $file) { image_path_flush($file->getFileUri()); } -/** - * Implements hook_field_delete_field(). - */ -function image_field_delete_field($field) { - if ($field['type'] != 'image') { - return; - } - - // The value of a managed_file element can be an array if #extended == TRUE. - $fid = (isset($field['settings']['default_image']['fids']) ? $field['settings']['default_image']['fids'] : $field['settings']['default_image']); - if ($fid && ($file = file_load($fid[0]))) { - file_usage()->delete($file, 'image', 'default_image', $field['id']); - } -} - -/** - * Implements hook_field_update_field(). - */ -function image_field_update_field($field, $prior_field) { - if ($field['type'] != 'image') { - return; - } - - // The value of a managed_file element can be an array if #extended == TRUE. - $fid_new = (isset($field['settings']['default_image']['fids']) ? $field['settings']['default_image']['fids'] : $field['settings']['default_image']); - $fid_old = (isset($prior_field['settings']['default_image']['fids']) ? $prior_field['settings']['default_image']['fids'] : $prior_field['settings']['default_image']); - - $file_new = $fid_new ? file_load($fid_new) : FALSE; - - if ($fid_new != $fid_old) { - - // Is there a new file? - if ($file_new) { - $file_new->status = FILE_STATUS_PERMANENT; - $file_new->save(); - file_usage()->add($file_new, 'image', 'default_image', $field['uuid']); - } - - // Is there an old file? - if ($fid_old && ($file_old = file_load($fid_old[0]))) { - file_usage()->delete($file_old, 'image', 'default_image', $field['uuid']); - } - } - - // If the upload destination changed, then move the file. - if ($file_new && (file_uri_scheme($file_new->getFileUri()) != $field['settings']['uri_scheme'])) { - $directory = $field['settings']['uri_scheme'] . '://default_images/'; - file_prepare_directory($directory, FILE_CREATE_DIRECTORY); - file_move($file_new, $directory . $file_new->filename); - } -} - -/** - * Implements hook_field_delete_instance(). - */ -function image_field_delete_instance($instance) { - // Only act on image fields. - $field = field_read_field($instance['field_name']); - if ($field['type'] != 'image') { - return; - } - - // The value of a managed_file element can be an array if the #extended - // property is set to TRUE. - $fid = $instance['settings']['default_image']; - if (is_array($fid)) { - $fid = $fid['fid']; - } - - // Remove the default image when the instance is deleted. - if ($fid && ($file = file_load($fid))) { - file_usage()->delete($file, 'image', 'default_image', $instance['id']); - } -} - -/** - * Implements hook_field_update_instance(). - */ -function image_field_update_instance($instance, $prior_instance) { - // Only act on image fields. - $field = field_read_field($instance['field_name']); - if ($field['type'] != 'image') { - return; - } - - // The value of a managed_file element can be an array if the #extended - // property is set to TRUE. - $fid_new = $instance['settings']['default_image']; - if (isset($fid_new['fids'])) { - $fid_new = $fid_new['fids']; - } - $fid_old = $prior_instance['settings']['default_image']; - if (isset($fid_old['fids'])) { - $fid_old = $fid_old['fids']; - } - - // If the old and new files do not match, update the default accordingly. - $file_new = $fid_new ? file_load($fid_new[0]) : FALSE; - if ($fid_new != $fid_old) { - // Save the new file, if present. - if ($file_new) { - $file_new->status = FILE_STATUS_PERMANENT; - $file_new->save(); - file_usage()->add($file_new, 'image', 'default_image', $instance['uuid']); - } - // Delete the old file, if present. - if ($fid_old && ($file_old = file_load($fid_old[0]))) { - file_usage()->delete($file_old, 'image', 'default_image', $instance['uuid']); - } - } - - // If the upload destination changed, then move the file. - if ($file_new && (file_uri_scheme($file_new->getFileUri()) != $field['settings']['uri_scheme'])) { - $directory = $field['settings']['uri_scheme'] . '://default_images/'; - file_prepare_directory($directory, FILE_CREATE_DIRECTORY); - file_move($file_new, $directory . $file_new->filename); - } -} - /** * Clears cached versions of a specific file in all styles. * @@ -1082,3 +965,128 @@ function image_entity_presave(EntityInterface $entity, $type) { } } } + +/** + * Implements hook_ENTITY_TYPE_update() for 'field_entity'. + */ +function image_field_entity_update(FieldInterface $field) { + if ($field->type != 'image') { + // Only act on image fields. + return; + } + + $prior_field = $field->original; + + // The value of a managed_file element can be an array if #extended == TRUE. + $fid_new = (isset($field->settings['default_image']['fids']) ? $field->settings['default_image']['fids'] : $field->settings['default_image']); + $fid_old = (isset($prior_field->settings['default_image']['fids']) ? $prior_field->settings['default_image']['fids'] : $prior_field->settings['default_image']); + + $file_new = $fid_new ? file_load($fid_new) : FALSE; + + if ($fid_new != $fid_old) { + + // Is there a new file? + if ($file_new) { + $file_new->status = FILE_STATUS_PERMANENT; + $file_new->save(); + file_usage()->add($file_new, 'image', 'default_image', $field->uuid); + } + + // Is there an old file? + if ($fid_old && ($file_old = file_load($fid_old[0]))) { + file_usage()->delete($file_old, 'image', 'default_image', $field->uuid); + } + } + + // If the upload destination changed, then move the file. + if ($file_new && (file_uri_scheme($file_new->getFileUri()) != $field->settings['uri_scheme'])) { + $directory = $field->settings['uri_scheme'] . '://default_images/'; + file_prepare_directory($directory, FILE_CREATE_DIRECTORY); + file_move($file_new, $directory . $file_new->filename); + } +} + +/** + * Implements hook_ENTITY_TYPE_update() for 'field_instance'. + */ +function image_field_instance_update(FieldInstanceInterface $field_instance) { + $field = $field_instance->getField(); + if ($field->type != 'image') { + // Only act on image fields. + return; + } + + $prior_instance = $field_instance->original; + + // The value of a managed_file element can be an array if the #extended + // property is set to TRUE. + $fid_new = $field_instance->settings['default_image']; + if (isset($fid_new['fids'])) { + $fid_new = $fid_new['fids']; + } + $fid_old = $prior_instance->settings['default_image']; + if (isset($fid_old['fids'])) { + $fid_old = $fid_old['fids']; + } + + // If the old and new files do not match, update the default accordingly. + $file_new = $fid_new ? file_load($fid_new[0]) : FALSE; + if ($fid_new != $fid_old) { + // Save the new file, if present. + if ($file_new) { + $file_new->status = FILE_STATUS_PERMANENT; + $file_new->save(); + file_usage()->add($file_new, 'image', 'default_image', $field_instance->uuid); + } + // Delete the old file, if present. + if ($fid_old && ($file_old = file_load($fid_old[0]))) { + file_usage()->delete($file_old, 'image', 'default_image', $field_instance->uuid); + } + } + + // If the upload destination changed, then move the file. + if ($file_new && (file_uri_scheme($file_new->getFileUri()) != $field->settings['uri_scheme'])) { + $directory = $field->settings['uri_scheme'] . '://default_images/'; + file_prepare_directory($directory, FILE_CREATE_DIRECTORY); + file_move($file_new, $directory . $file_new->filename); + } +} + +/** + * Implements hook_ENTITY_TYPE_delete() for 'field_entity'. + */ +function image_field_entity_delete(FieldInterface $field) { + if ($field->type != 'image') { + // Only act on image fields. + return; + } + + // The value of a managed_file element can be an array if #extended == TRUE. + $fid = (isset($field->settings['default_image']['fids']) ? $field->settings['default_image']['fids'] : $field->settings['default_image']); + if ($fid && ($file = file_load($fid[0]))) { + file_usage()->delete($file, 'image', 'default_image', $field->uuid); + } +} + +/** + * Implements hook_ENTITY_TYPE_delete() for 'field_instance'. + */ +function image_field_instance_delete(FieldInstanceInterface $field_instance) { + $field = $field_instance->getField(); + if ($field->type != 'image') { + // Only act on image fields. + return; + } + + // The value of a managed_file element can be an array if the #extended + // property is set to TRUE. + $fid = $field_instance->settings['default_image']; + if (is_array($fid)) { + $fid = $fid['fid']; + } + + // Remove the default image when the instance is deleted. + if ($fid && ($file = file_load($fid))) { + file_usage()->delete($file, 'image', 'default_image', $field_instance->uuid); + } +} diff --git a/core/modules/options/options.module b/core/modules/options/options.module index d95846e27b47..e6ebdbedc1f6 100644 --- a/core/modules/options/options.module +++ b/core/modules/options/options.module @@ -10,7 +10,6 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\Field\FieldDefinitionInterface; use Drupal\field\FieldInterface; use Drupal\field\FieldUpdateForbiddenException; -use Drupal\options\Plugin\field\widget\OptionsWidgetBase; /** * Implements hook_help(). @@ -215,9 +214,9 @@ function options_field_settings_form_value_boolean_allowed_values($element, $inp } /** - * Implements hook_field_update_field(). + * Implements hook_ENTITY_TYPE_update() for 'field_entity'. */ -function options_field_update_field($field, $prior_field) { +function options_field_entity_update(FieldInterface $field) { drupal_static_reset('options_allowed_values'); } diff --git a/core/modules/views/views.module b/core/modules/views/views.module index 0e5922b69b2a..5daa3313a44e 100644 --- a/core/modules/views/views.module +++ b/core/modules/views/views.module @@ -16,6 +16,7 @@ use Drupal\views\ViewExecutable; use Drupal\Component\Plugin\Exception\PluginException; use Drupal\views\Plugin\Core\Entity\View; use Drupal\views\Views; +use Drupal\field\FieldInstanceInterface; /** * Implements hook_forms(). @@ -646,24 +647,25 @@ function views_language_list($field = 'name', $flags = Language::STATE_ALL) { } /** - * Implements hook_field_create_instance. + * Implements hook_ENTITY_TYPE_create() for 'field_instance'. */ -function views_field_create_instance($instance) { - cache('views_info')->deleteAll(); - cache('views_results')->deleteAll(); -} -/** - * Implements hook_field_update_instance. - */ -function views_field_update_instance($instance, $prior_instance) { +function views_field_instance_create(FieldInstanceInterface $field_instance) { cache('views_info')->deleteAll(); cache('views_results')->deleteAll(); } /** - * Implements hook_field_delete_instance. + * Implements hook_ENTITY_TYPE_update() for 'field_instance'. */ -function views_field_delete_instance($instance) { +function views_field_instance_update(FieldInstanceInterface $field_instance) { + cache('views_info')->deleteAll(); + cache('views_results')->deleteAll(); +} + +/** + * Implements hook_ENTITY_TYPE_delete() for 'field_instance'. + */ +function views_field_instance_delete(FieldInstanceInterface $field_instance) { cache('views_info')->deleteAll(); cache('views_results')->deleteAll(); }