Commit d5847d3f authored by webchick's avatar webchick

Issue #1374116 by amateescu, swentel, Berdir, yched: Move bundle CRUD API out of Field API.

parent a098c6e1
......@@ -136,6 +136,65 @@ function hook_entity_bundle_info_alter(&$bundles) {
$bundles['user']['user']['label'] = t('Full account');
}
/**
* Act on entity_bundle_create().
*
* This hook is invoked after the operation has been performed.
*
* @param string $entity_type
* The type of $entity; e.g. 'node' or 'user'.
* @param string $bundle
* The name of the bundle.
*/
function hook_entity_bundle_create($entity_type, $bundle) {
// When a new bundle is created, the menu needs to be rebuilt to add the
// Field UI menu item tabs.
state()->set('menu_rebuild_needed', TRUE);
}
/**
* Act on entity_bundle_rename().
*
* This hook is invoked after the operation has been performed.
*
* @param string $entity_type
* The entity type to which the bundle is bound.
* @param string $bundle_old
* The previous name of the bundle.
* @param string $bundle_new
* The new name of the bundle.
*/
function hook_entity_bundle_rename($entity_type, $bundle_old, $bundle_new) {
// Update the settings associated with the bundle in my_module.settings.
$config = config('my_module.settings');
$bundle_settings = $config->get('bundle_settings');
if (isset($bundle_settings[$entity_type][$bundle_old])) {
$bundle_settings[$entity_type][$bundle_new] = $bundle_settings[$entity_type][$bundle_old];
unset($bundle_settings[$entity_type][$bundle_old]);
$config->set('bundle_settings', $bundle_settings);
}
}
/**
* Act on entity_bundle_delete().
*
* This hook is invoked after the operation has been performed.
*
* @param string $entity_type
* The type of entity; for example, 'node' or 'user'.
* @param string $bundle
* The bundle that was just deleted.
*/
function hook_entity_bundle_delete($entity_type, $bundle) {
// Remove the settings associated with the bundle in my_module.settings.
$config = config('my_module.settings');
$bundle_settings = $config->get('bundle_settings');
if (isset($bundle_settings[$entity_type][$bundle])) {
unset($bundle_settings[$entity_type][$bundle]);
$config->set('bundle_settings', $bundle_settings);
}
}
/**
* Alter the entity type definitions.
*
......
......@@ -85,6 +85,24 @@ function entity_get_bundles($entity_type = NULL) {
return array();
}
/**
* Notifies modules about an operation that was performed on a entity bundle.
*
* @param string $hook
* One of 'create', 'rename' or 'delete'.
* @param string $entity_type
* The entity type to which the bundle is bound.
* @param string $bundle
* The name of the bundle on which the operation was performed.
* @param string|null $bundle_new
* The new name of the bundle in case of a 'rename' operation. Defaults to
* NULL.
*/
function entity_invoke_bundle_hook($hook, $entity_type, $bundle, $bundle_new = NULL) {
entity_info_cache_clear();
module_invoke_all('entity_bundle_' . $hook, $entity_type, $bundle, $bundle_new);
}
/**
* Returns the entity view mode info.
*
......
......@@ -22,11 +22,11 @@ protected function postSave(EntityInterface $entity, $update) {
parent::postSave($entity, $update);
if (!$update) {
field_attach_create_bundle('custom_block', $entity->id());
entity_invoke_bundle_hook('create', 'custom_block', $entity->id());
custom_block_add_body_field($entity->id());
}
elseif ($entity->original->id() != $entity->id()) {
field_attach_rename_bundle('custom_block', $entity->original->id(), $entity->id());
entity_invoke_bundle_hook('rename', 'custom_block', $entity->original->id(), $entity->id());
}
}
......@@ -37,7 +37,7 @@ protected function postDelete($entities) {
parent::postDelete($entities);
foreach ($entities as $entity) {
field_attach_delete_bundle('custom_block', $entity->id());
entity_invoke_bundle_hook('delete', 'custom_block', $entity->id());
}
}
......
......@@ -16,7 +16,7 @@ function comment_uninstall() {
variable_del('comment_block_count');
$node_types = array_keys(node_type_get_types());
foreach ($node_types as $node_type) {
field_attach_delete_bundle('comment', 'comment_node_' . $node_type);
entity_invoke_bundle_hook('delete', 'comment', 'comment_node_' . $node_type);
variable_del('comment_' . $node_type);
variable_del('comment_anonymous_' . $node_type);
variable_del('comment_controls_' . $node_type);
......
......@@ -332,7 +332,7 @@ function comment_node_type_insert($info) {
*/
function comment_node_type_update($info) {
if (!empty($info->old_type) && $info->type != $info->old_type) {
field_attach_rename_bundle('comment', 'comment_node_' . $info->old_type, 'comment_node_' . $info->type);
entity_invoke_bundle_hook('rename', 'comment', 'comment_node_' . $info->old_type, 'comment_node_' . $info->type);
}
}
......@@ -340,7 +340,7 @@ function comment_node_type_update($info) {
* Implements hook_node_type_delete().
*/
function comment_node_type_delete($info) {
field_attach_delete_bundle('comment', 'comment_node_' . $info->type);
entity_invoke_bundle_hook('delete', 'comment', 'comment_node_' . $info->type);
$settings = array(
'comment',
'comment_default_mode',
......@@ -375,7 +375,7 @@ function _comment_body_field_create($info) {
}
// Create the instance if needed.
if (!field_read_instance('comment', 'comment_body', 'comment_node_' . $info->type, array('include_inactive' => TRUE))) {
field_attach_create_bundle('comment', 'comment_node_' . $info->type);
entity_invoke_bundle_hook('create', 'comment', 'comment_node_' . $info->type);
// Attaches the body field by default.
$instance = array(
'field_name' => 'comment_body',
......
......@@ -22,10 +22,10 @@ protected function postSave(EntityInterface $entity, $update) {
parent::postSave($entity, $update);
if (!$update) {
field_attach_create_bundle('contact_message', $entity->id());
entity_invoke_bundle_hook('create', 'contact_message', $entity->id());
}
elseif ($entity->original->id() != $entity->id()) {
field_attach_rename_bundle('contact_message', $entity->original->id(), $entity->id());
entity_invoke_bundle_hook('rename', 'contact_message', $entity->original->id(), $entity->id());
}
}
......@@ -36,7 +36,7 @@ protected function postDelete($entities) {
parent::postDelete($entities);
foreach ($entities as $entity) {
field_attach_delete_bundle('contact_message', $entity->id());
entity_invoke_bundle_hook('delete', 'contact_message', $entity->id());
}
}
......
......@@ -11,9 +11,9 @@
use Drupal\Core\Config\Entity\ConfigStorageController;
/**
* Implements hook_field_attach_rename_bundle().
* Implements hook_entity_bundle_rename().
*/
function entity_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
function entity_entity_bundle_rename($entity_type, $bundle_old, $bundle_new) {
$entity_info = entity_get_info('entity_display');
// Rename entity displays.
......@@ -31,9 +31,9 @@ function entity_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_ne
}
/**
* Implements hook_field_attach_delete_bundle().
* Implements hook_entity_bundle_delete().
*/
function entity_field_attach_delete_bundle($entity_type, $bundle, $instances) {
function entity_entity_bundle_delete($entity_type, $bundle) {
$entity_info = entity_get_info('entity_display');
// Remove entity displays of the deleted bundle.
......
......@@ -1166,68 +1166,6 @@ function hook_field_available_languages_alter(&$langcodes, $context) {
unset($langcodes[$index]);
}
/**
* Act on field_attach_create_bundle().
*
* This hook is invoked after the field module has performed the operation.
*
* @param string $entity_type
* The type of $entity; e.g. 'node' or 'user'.
* @param string $bundle
* The name of the bundle.
*/
function hook_field_attach_create_bundle($entity_type, $bundle) {
// When a new bundle is created, the menu needs to be rebuilt to add the
// Field UI menu item tabs.
state()->set('menu_rebuild_needed', TRUE);
}
/**
* Act on field_attach_rename_bundle().
*
* This hook is invoked after the field module has performed the operation.
*
* @param $entity_type
* The entity type to which the bundle is bound.
* @param $bundle_old
* The previous name of the bundle.
* @param $bundle_new
* The new name of the bundle.
*/
function hook_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
// Update the extra weights variable with new information.
if ($bundle_old !== $bundle_new) {
$extra_weights = variable_get('field_extra_weights', array());
if (isset($info[$entity_type][$bundle_old])) {
$extra_weights[$entity_type][$bundle_new] = $extra_weights[$entity_type][$bundle_old];
unset($extra_weights[$entity_type][$bundle_old]);
variable_set('field_extra_weights', $extra_weights);
}
}
}
/**
* Act on field_attach_delete_bundle.
*
* This hook is invoked after the field module has performed the operation.
*
* @param $entity_type
* The type of entity; for example, 'node' or 'user'.
* @param $bundle
* The bundle that was just deleted.
* @param $instances
* An array of all instances that existed for the bundle before it was
* deleted.
*/
function hook_field_attach_delete_bundle($entity_type, $bundle, $instances) {
// Remove the extra weights variable information for this bundle.
$extra_weights = variable_get('field_extra_weights', array());
if (isset($extra_weights[$entity_type][$bundle])) {
unset($extra_weights[$entity_type][$bundle]);
variable_set('field_extra_weights', $extra_weights);
}
}
/**
* @} End of "addtogroup field_attach".
*/
......
......@@ -1520,35 +1520,17 @@ function field_attach_prepare_translation(EntityInterface $entity, $langcode, En
}
/**
* Notifies field.module that a new bundle was created.
*
* The default SQL-based storage doesn't need to do anything about it, but
* others might.
*
* @param $entity_type
* The entity type to which the bundle is bound.
* @param $bundle
* The name of the newly created bundle.
* Implements hook_entity_bundle_create().
*/
function field_attach_create_bundle($entity_type, $bundle) {
function field_entity_bundle_create($entity_type, $bundle) {
// Clear the cache.
field_cache_clear();
// Let other modules act on creating the bundle.
module_invoke_all('field_attach_create_bundle', $entity_type, $bundle);
}
/**
* Notifies field.module that a bundle was renamed.
*
* @param $entity_type
* The entity type to which the bundle is bound.
* @param $bundle_old
* The previous name of the bundle.
* @param $bundle_new
* The new name of the bundle.
* Implements hook_entity_bundle_rename().
*/
function field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
function field_entity_bundle_rename($entity_type, $bundle_old, $bundle_new) {
db_update('field_config_instance')
->fields(array('bundle' => $bundle_new))
->condition('entity_type', $entity_type)
......@@ -1557,19 +1539,15 @@ function field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
// Clear the cache.
field_cache_clear();
entity_info_cache_clear();
// Update bundle settings.
$settings = variable_get('field_bundle_settings_' . $entity_type . '__' . $bundle_old, array());
variable_set('field_bundle_settings_' . $entity_type . '__' . $bundle_new, $settings);
variable_del('field_bundle_settings_' . $entity_type . '__' . $bundle_old);
// Let other modules act on renaming the bundle.
module_invoke_all('field_attach_rename_bundle', $entity_type, $bundle_old, $bundle_new);
}
/**
* Notifies field.module the a bundle was deleted.
* Implements hook_entity_bundle_delete().
*
* This deletes the data for the field instances as well as the field instances
* themselves. This function actually just marks the data and field instances as
......@@ -1577,16 +1555,11 @@ function field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
* not always possible to delete this much data in a single page request
* (particularly since for some field types, the deletion is more than just a
* simple DELETE query).
*
* @param $entity_type
* The entity type to which the bundle is bound.
* @param $bundle
* The bundle to delete.
*/
function field_attach_delete_bundle($entity_type, $bundle) {
// First, delete the instances themselves. field_read_instances() must be
// used here since field_info_instances() does not return instances for
// disabled entity types or bundles.
function field_entity_bundle_delete($entity_type, $bundle) {
// Get the instances on the bundle. field_read_instances() must be used
// here since field_info_instances() does not return instances for disabled
// entity types or bundles.
$instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle), array('include_inactive' => 1));
foreach ($instances as $instance) {
field_delete_instance($instance);
......@@ -1597,12 +1570,8 @@ function field_attach_delete_bundle($entity_type, $bundle) {
// Clear bundle display settings.
variable_del('field_bundle_settings_' . $entity_type . '__' . $bundle);
// Let other modules act on deleting the bundle.
module_invoke_all('field_attach_delete_bundle', $entity_type, $bundle, $instances);
}
/**
* @} End of "defgroup field_attach".
*/
......@@ -434,9 +434,9 @@ function testFieldAttachDelete() {
}
/**
* Test field_attach_create_bundle() and field_attach_rename_bundle().
* Test entity_bundle_create() and entity_bundle_rename().
*/
function testFieldAttachCreateRenameBundle() {
function testEntityCreateRenameBundle() {
// Create a new bundle.
$new_bundle = 'test_bundle_' . drupal_strtolower($this->randomName());
field_test_create_bundle($new_bundle);
......@@ -473,9 +473,9 @@ function testFieldAttachCreateRenameBundle() {
}
/**
* Test field_attach_delete_bundle().
* Test entity_bundle_delete().
*/
function testFieldAttachDeleteBundle() {
function testEntityDeleteBundle() {
// Create a new bundle.
$new_bundle = 'test_bundle_' . drupal_strtolower($this->randomName());
field_test_create_bundle($new_bundle);
......
......@@ -97,7 +97,7 @@ function field_test_create_bundle($bundle, $text = NULL) {
$info = entity_get_info();
foreach ($info as $type => $type_info) {
if ($type_info['module'] == 'field_test') {
field_attach_create_bundle($type, $bundle);
entity_invoke_bundle_hook('create', $type, $bundle);
}
}
}
......@@ -119,7 +119,7 @@ function field_test_rename_bundle($bundle_old, $bundle_new) {
$info = entity_get_info();
foreach ($info as $type => $type_info) {
if ($type_info['module'] == 'field_test') {
field_attach_rename_bundle($type, $bundle_old, $bundle_new);
entity_invoke_bundle_hook('rename', $type, $bundle_old, $bundle_new);
}
}
}
......@@ -138,7 +138,7 @@ function field_test_delete_bundle($bundle) {
$info = entity_get_info();
foreach ($info as $type => $type_info) {
if ($type_info['module'] == 'field_test') {
field_attach_delete_bundle($type, $bundle);
entity_invoke_bundle_hook('delete', $type, $bundle);
}
}
}
......
......@@ -416,16 +416,9 @@ function field_test_field_storage_delete_instance($instance) {
}
/**
* Implements hook_field_attach_create_bundle().
* Implements hook_entity_bundle_rename().
*/
function field_test_field_attach_create_bundle($bundle) {
// We don't need to do anything here.
}
/**
* Implements hook_field_attach_rename_bundle().
*/
function field_test_field_attach_rename_bundle($bundle_old, $bundle_new) {
function field_test_entity_bundle_rename($entity_type, $bundle_old, $bundle_new) {
$data = _field_test_storage_data();
// We need to account for deleted or inactive fields and instances.
......@@ -448,20 +441,18 @@ function field_test_field_attach_rename_bundle($bundle_old, $bundle_new) {
}
/**
* Implements hook_field_attach_delete_bundle().
* Implements hook_field_delete_instance().
*/
function field_test_field_attach_delete_bundle($entity_type, $bundle, $instances) {
function field_test_field_delete_instance($instance) {
$data = _field_test_storage_data();
foreach ($instances as $field_name => $instance) {
$field = field_info_field($field_name);
if ($field['storage']['type'] == 'field_test_storage') {
$field_data = &$data[$field['id']];
foreach (array('current', 'revisions') as $sub_table) {
foreach ($field_data[$sub_table] as &$row) {
if ($row->bundle == $bundle_old) {
$row->deleted = TRUE;
}
$field = field_info_field($instance['field_name']);
if ($field['storage']['type'] == 'field_test_storage') {
$field_data = &$data[$field['id']];
foreach (array('current', 'revisions') as $sub_table) {
foreach ($field_data[$sub_table] as &$row) {
if ($row->bundle == $instance['bundle']) {
$row->deleted = TRUE;
}
}
}
......
......@@ -569,9 +569,9 @@ function field_sql_storage_field_storage_delete_instance($instance) {
}
/**
* Implements hook_field_attach_rename_bundle().
* Implements hook_entity_bundle_rename().
*/
function field_sql_storage_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
function field_sql_storage_entity_bundle_rename($entity_type, $bundle_old, $bundle_new) {
// We need to account for deleted or inactive fields and instances.
$instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle_new), array('include_deleted' => TRUE, 'include_inactive' => TRUE));
foreach ($instances as $instance) {
......
......@@ -46,15 +46,6 @@ function field_ui_help($path, $arg) {
}
}
/**
* Implements hook_field_attach_rename_bundle().
*/
function field_ui_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
// The Field UI relies on entity_get_info() to build menu items for entity
// field administration pages. Ensure that the menu is rebuilt.
menu_router_rebuild();
}
/**
* Implements hook_menu().
*/
......@@ -322,14 +313,23 @@ function field_ui_element_info() {
}
/**
* Implements hook_field_attach_create_bundle().
* Implements hook_entity_bundle_create().
*/
function field_ui_field_attach_create_bundle($entity_type, $bundle) {
function field_ui_entity_bundle_create($entity_type, $bundle) {
// When a new bundle is created, the menu needs to be rebuilt to add our
// menu item tabs.
state()->set('menu_rebuild_needed', TRUE);
}
/**
* Implements hook_entity_bundle_rename().
*/
function field_ui_entity_bundle_rename($entity_type, $bundle_old, $bundle_new) {
// When a bundle is renamed, the menu needs to be rebuilt to add our
// menu item tabs.
state()->set('menu_rebuild_needed', TRUE);
}
/**
* Determines the adminstration path for a bundle.
*/
......
......@@ -537,7 +537,7 @@ function node_type_save($info) {
->execute();
if (!empty($type->old_type) && $type->old_type != $type->type) {
field_attach_rename_bundle('node', $type->old_type, $type->type);
entity_invoke_bundle_hook('rename', 'node', $type->old_type, $type->type);
}
module_invoke_all('node_type_update', $type);
$status = SAVED_UPDATED;
......@@ -548,7 +548,7 @@ function node_type_save($info) {
->fields($fields)
->execute();
field_attach_create_bundle('node', $type->type);
entity_invoke_bundle_hook('create', 'node', $type->type);
module_invoke_all('node_type_insert', $type);
$status = SAVED_NEW;
......@@ -665,7 +665,7 @@ function node_type_delete($name) {
db_delete('node_type')
->condition('type', $name)
->execute();
field_attach_delete_bundle('node', $name);
entity_invoke_bundle_hook('delete', 'node', $name);
module_invoke_all('node_type_delete', $type);
// Clear the node type cache.
......
......@@ -20,7 +20,7 @@ class VocabularyStorageController extends ConfigStorageController {
*/
protected function postSave(EntityInterface $entity, $update) {
if (!$update) {
field_attach_create_bundle('taxonomy_term', $entity->id());
entity_invoke_bundle_hook('create', 'taxonomy_term', $entity->id());
}
elseif ($entity->getOriginalID() != $entity->id()) {
// Reflect machine name changes in the definitions of existing 'taxonomy'
......@@ -41,7 +41,7 @@ protected function postSave(EntityInterface $entity, $update) {
}
}
// Update bundles.
field_attach_rename_bundle('taxonomy_term', $entity->getOriginalID(), $entity->id());
entity_invoke_bundle_hook('rename', 'taxonomy_term', $entity->getOriginalID(), $entity->id());
}
parent::postSave($entity, $update);
$this->resetCache($update ? array($entity->getOriginalID()) : array());
......
......@@ -90,7 +90,7 @@ function hook_taxonomy_vocabulary_update(Drupal\taxonomy\Plugin\Core\Entity\Voca
* Act before taxonomy vocabulary deletion.
*
* This hook is invoked from taxonomy_vocabulary_delete() before
* field_attach_delete_bundle() is called and before the vocabulary is actually
* entity_bundle_delete() is called and before the vocabulary is actually
* removed from the database.
*
* @param Drupal\taxonomy\Plugin\Core\Entity\Vocabulary $vocabulary
......@@ -109,7 +109,7 @@ function hook_taxonomy_vocabulary_predelete(Drupal\taxonomy\Plugin\Core\Entity\V
* Respond to taxonomy vocabulary deletion.
*
* This hook is invoked from taxonomy_vocabulary_delete() after
* field_attach_delete_bundle() has been called and after the vocabulary has
* entity_bundle_delete() has been called and after the vocabulary has
* been removed from the database.
*
* @param Drupal\taxonomy\Plugin\Core\Entity\Vocabulary $vocabulary
......
......@@ -15,7 +15,7 @@ function taxonomy_uninstall() {
$config_names = config_get_storage_names_with_prefix('taxonomy.vocabulary.');
foreach ($config_names as $config_name) {
$vid = substr($config_name, strlen('taxonomy.vocabulary.'));
field_attach_delete_bundle('taxonomy_term', $vid);
entity_invoke_bundle_hook('delete', 'taxonomy_term', $vid);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment