Commit 5ff3e98c authored by alexpott's avatar alexpott

Issue #2013939 by swentel, amateescu, pcambra: Remove hook_field_CRUD_() in...

Issue #2013939 by swentel, amateescu, pcambra: Remove hook_field_CRUD_() in favor of hook_entity_TYPE_CRUD() calls.
parent 795e1f83
......@@ -9,6 +9,7 @@
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();
......
......@@ -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.
*
......
......@@ -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 @@ protected function saveNew() {
$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 @@ protected function saveUpdated() {
$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 @@ protected function saveUpdated() {
$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 @@ public function delete() {
// Clear the cache.
field_cache_clear();
// Invoke hook_field_delete_field().
$module_handler->invokeAll('field_delete_field', array($this));
}
}
......
......@@ -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 @@ protected function saveNew() {
$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 @@ protected function saveUpdated() {
$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 @@ protected function saveUpdated() {
$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 @@ public function delete($field_cleanup = TRUE) {
// 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();
......
......@@ -44,8 +44,8 @@ function testCreateField() {
$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
......
<?php
use Drupal\Core\Entity\EntityInterface;
use Drupal\field\FieldInterface;
/**
* @file
......@@ -125,8 +126,8 @@ function field_test_field_language_alter(&$display_langcode, $context) {
* $mem = field_test_memorize();
*
* // make sure hook_field_create_field() is invoked correctly
* assertEqual(count($mem['field_test_field_create_field']), 1);
* assertEqual($mem['field_test_field_create_field'][0], array($field));
* assertEqual(count($mem['field_test_field_entity_create']), 1);
* assertEqual($mem['field_test_field_entity_create'][0], array($field));
* @endcode
*
* @param $key
......@@ -151,9 +152,9 @@ function field_test_memorize($key = NULL, $value = NULL) {
}
/**
* Memorize calls to hook_field_create_field().
* Memorize calls to field_test_field_entity_create() for field creation.
*/
function field_test_field_create_field($field) {
function field_test_field_entity_create(FieldInterface $field) {
$args = func_get_args();
field_test_memorize(__FUNCTION__, $args);
}
......
......@@ -6,6 +6,7 @@
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\field\FieldInstanceInterface;
/**
* Implements hook_field_storage_info().
......@@ -441,17 +442,17 @@ function field_test_entity_bundle_rename($entity_type, $bundle_old, $bundle_new)
}
/**
* Implements hook_field_delete_instance().
* Implements hook_ENTITY_TYPE_delete() for 'field_instance'.
*/
function field_test_field_delete_instance($instance) {
function field_test_field_instance_delete(FieldInstanceInterface $field_instance) {
$data = _field_test_storage_data();
$field = field_info_field($instance['field_name']);
if ($field['storage']['type'] == 'field_test_storage') {
$field_data = &$data[$field['uuid']];
$field = $field_instance->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;
}
}
......
......@@ -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().
*
......
......@@ -17,6 +17,8 @@
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);
}
}
......@@ -10,7 +10,6 @@
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');
}
......
......@@ -16,6 +16,7 @@
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) {
function views_field_instance_create(FieldInstanceInterface $field_instance) {
cache('views_info')->deleteAll();
cache('views_results')->deleteAll();
}