From ae0984f8711db81fd2157d189bbddb1105a9cc19 Mon Sep 17 00:00:00 2001 From: catch <catch@35733.no-reply.drupal.org> Date: Mon, 22 Apr 2013 22:57:38 +0100 Subject: [PATCH] Issue #1942346 by swentel: Convert Field API variables to CMI. --- .../lib/Drupal/edit/Tests/EditTestBase.php | 5 +-- .../Drupal/entity/Tests/EntityDisplayTest.php | 5 +++ core/modules/field/config/field.settings.yml | 2 + .../field/config/schema/field.schema.yml | 6 +++ core/modules/field/field.api.php | 2 +- core/modules/field/field.attach.inc | 5 ++- core/modules/field/field.crud.inc | 4 +- core/modules/field/field.install | 13 ++++++ core/modules/field/field.module | 2 +- core/modules/field/field.multilingual.inc | 44 ++++--------------- .../Drupal/field/Plugin/Core/Entity/Field.php | 2 +- .../Drupal/field/Plugin/views/field/Field.php | 2 +- .../field/lib/Drupal/field/Tests/CrudTest.php | 2 +- .../lib/Drupal/field/Tests/FieldTestBase.php | 11 ----- .../Drupal/field/Tests/FieldUnitTestBase.php | 3 +- .../Drupal/hal/Tests/NormalizerTestBase.php | 1 + .../Tests/EntitySerializationTest.php | 2 + .../Tests/NormalizerTestBase.php | 1 + .../Tests/Entity/EntityUnitTestBase.php | 1 + .../system/Tests/Entity/FieldAccessTest.php | 2 + .../Tests/Formatter/TextPlainUnitTest.php | 2 + 21 files changed, 55 insertions(+), 62 deletions(-) diff --git a/core/modules/edit/lib/Drupal/edit/Tests/EditTestBase.php b/core/modules/edit/lib/Drupal/edit/Tests/EditTestBase.php index bdf56c54a45a..d3737bb5a9f6 100644 --- a/core/modules/edit/lib/Drupal/edit/Tests/EditTestBase.php +++ b/core/modules/edit/lib/Drupal/edit/Tests/EditTestBase.php @@ -13,7 +13,6 @@ * Parent class for Edit tests. */ class EditTestBase extends DrupalUnitTestBase { - var $default_storage = 'field_sql_storage'; /** * Modules to enable. @@ -29,9 +28,7 @@ function setUp() { $this->installSchema('system', 'variable'); $this->installSchema('entity_test', array('entity_test', 'entity_test_rev')); - - // Set default storage backend. - variable_set('field_storage_default', $this->default_storage); + $this->installConfig(array('field')); } /** diff --git a/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php b/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php index 1257f48cf3d4..11f6da792fff 100644 --- a/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php +++ b/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php @@ -24,6 +24,11 @@ public static function getInfo() { ); } + protected function setUp() { + parent::setUp(); + $this->installConfig(array('field')); + } + /** * Tests basic CRUD operations on EntityDisplay objects. */ diff --git a/core/modules/field/config/field.settings.yml b/core/modules/field/config/field.settings.yml index b6172c13a698..5ca00001aa7b 100644 --- a/core/modules/field/config/field.settings.yml +++ b/core/modules/field/config/field.settings.yml @@ -1 +1,3 @@ +default_storage: field_sql_storage +language_fallback: '1' purge_batch_size: 10 diff --git a/core/modules/field/config/schema/field.schema.yml b/core/modules/field/config/schema/field.schema.yml index b9e51d61b5b3..238409edb265 100644 --- a/core/modules/field/config/schema/field.schema.yml +++ b/core/modules/field/config/schema/field.schema.yml @@ -4,6 +4,12 @@ field.settings: type: mapping label: 'Field settings' mapping: + default_storage: + type: string + label: 'The default storage backend for a field' + language_fallback: + type: boolean + label: 'Whether the field display falls back to global language fallback configuration' purge_batch_size: type: integer label: 'Maximum number of field data records to purge' diff --git a/core/modules/field/field.api.php b/core/modules/field/field.api.php index e1b5904540bb..5ba15c958b8b 100644 --- a/core/modules/field/field.api.php +++ b/core/modules/field/field.api.php @@ -1140,7 +1140,7 @@ function hook_field_attach_prepare_translation_alter(\Drupal\Core\Entity\EntityI function hook_field_language_alter(&$display_langcode, $context) { // Do not apply core language fallback rules if they are disabled or if Locale // is not registered as a translation handler. - if (variable_get('field_language_fallback', TRUE) && field_has_translation_handler($context['entity']->entityType())) { + if (field_language_fallback_enabled() && field_has_translation_handler($context['entity']->entityType())) { field_language_fallback($display_langcode, $context['entity'], $context['langcode']); } } diff --git a/core/modules/field/field.attach.inc b/core/modules/field/field.attach.inc index 51d2d575f009..cde15f492c83 100644 --- a/core/modules/field/field.attach.inc +++ b/core/modules/field/field.attach.inc @@ -22,8 +22,9 @@ * the data in SQL differently or use a completely different storage mechanism * such as a cloud-based database. * - * Each field defines which storage backend it uses. The Drupal system variable - * 'field_storage_default' identifies the storage backend used by default. + * Each field defines which storage backend it uses. The Drupal configuration + * 'field.settings.default_storage' identifies the storage backend used by + * default. * * See @link field Field API @endlink for information about the other parts of * the Field API. diff --git a/core/modules/field/field.crud.inc b/core/modules/field/field.crud.inc index ff331a08c71d..dba30fa38050 100644 --- a/core/modules/field/field.crud.inc +++ b/core/modules/field/field.crud.inc @@ -45,8 +45,8 @@ * - settings: each omitted setting is given the default value defined in * hook_field_info(). * - storage: - * - type: the storage backend specified in the 'field_storage_default' - * system variable. + * - type: the storage backend specified in the + * 'field.settings.default_storage' configuration. * - settings: each omitted setting is given the default value specified in * hook_field_storage_info(). * diff --git a/core/modules/field/field.install b/core/modules/field/field.install index f485baaf3825..37da7976f8d9 100644 --- a/core/modules/field/field.install +++ b/core/modules/field/field.install @@ -470,6 +470,19 @@ function field_update_8003() { $state->set('field.instance.deleted', $deleted_instances); } +/** + * Moves field_storage_default and field_language_fallback to config. + * + * @ingroup config_upgrade + */ +function field_update_8004() { + update_variable_set('field_language_fallback', TRUE); + update_variables_to_config('field.settings', array( + 'field_storage_default' => 'default_storage', + 'field_language_fallback' => 'language_fallback', + )); +} + /** * @} End of "addtogroup updates-7.x-to-8.x". * The next series of updates should start at 9000. diff --git a/core/modules/field/field.module b/core/modules/field/field.module index 1e314846c9cf..8f459a352bfb 100644 --- a/core/modules/field/field.module +++ b/core/modules/field/field.module @@ -333,7 +333,7 @@ function field_field_widget_info_alter(&$info) { * otherwise all the fallback candidates are inspected to see if there is a * field translation available in another language. * By default this is called by field_field_language_alter(), but this - * behavior can be disabled by setting the 'field_language_fallback' + * behavior can be disabled by setting the 'field.settings.language_fallback' * variable to FALSE. * * @param $field_langcodes diff --git a/core/modules/field/field.multilingual.inc b/core/modules/field/field.multilingual.inc index 8e5ee396f821..55b364ebf885 100644 --- a/core/modules/field/field.multilingual.inc +++ b/core/modules/field/field.multilingual.inc @@ -63,41 +63,6 @@ * the Field API. */ -/** - * Implements hook_language_insert(). - */ -function field_language_insert() { - field_info_cache_clear(); - // If the number of languages is bigger than 1, enable the core language - // fallback rules. - // Because the language_count is updated only after the hook is invoked, we - // check if the language_count is bigger or equal with 1 at the current time. - if (variable_get('language_count', 1) >= 1) { - variable_set('field_language_fallback', TRUE); - } -} - -/** - * Implements hook_language_update(). - */ -function field_language_update() { - field_info_cache_clear(); -} - -/** - * Implements hook_language_delete(). - */ -function field_language_delete() { - field_info_cache_clear(); - // If the number of languages is less than 2, disable the core language - // fallback rules. - // Because the language_count is updated after the hook is invoked, we check - // if the language_count is less or equal with 2 at the current time. - if (variable_get('language_count', 1) <= 2) { - variable_set('field_language_fallback', FALSE); - } -} - /** * Collects the available language codes for the given entity type and field. * @@ -185,6 +150,13 @@ function field_content_languages() { return array_keys(language_list(LANGUAGE_ALL)); } +/** + * Checks whether field language fallback is enabled. + */ +function field_language_fallback_enabled() { + return language_multilingual() && config('field.settings')->get('language_fallback'); +} + /** * Checks whether a field has language support. * @@ -317,7 +289,7 @@ function field_language(EntityInterface $entity, $field_name = NULL, $langcode = ); // Do not apply core language fallback rules if they are disabled or if // the entity does not have a translation handler registered. - if (variable_get('field_language_fallback', FALSE) && field_has_translation_handler($entity_type)) { + if (field_language_fallback_enabled() && field_has_translation_handler($entity_type)) { field_language_fallback($display_langcode, $context['entity'], $context['langcode']); } drupal_alter('field_language', $display_langcode, $context); 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 14d02e84d322..b89953351649 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 @@ -323,7 +323,7 @@ public function save() { // Provide default storage. $this->storage += array( - 'type' => variable_get('field_storage_default', 'field_sql_storage'), + 'type' => config('field.settings')->get('default_storage'), 'settings' => array(), ); // Check that the storage type is known. diff --git a/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php b/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php index c28875066014..2e91a31efa66 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php +++ b/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php @@ -193,7 +193,7 @@ public function query($use_groupby = FALSE) { $this->view->display_handler->options['field_langcode']); $placeholder = $this->placeholder(); $langcode_fallback_candidates = array($langcode); - if (variable_get('locale_field_language_fallback', TRUE)) { + if (field_language_fallback_enabled()) { require_once DRUPAL_ROOT . '/includes/language.inc'; $langcode_fallback_candidates = array_merge($langcode_fallback_candidates, language_fallback_get_candidates()); } diff --git a/core/modules/field/lib/Drupal/field/Tests/CrudTest.php b/core/modules/field/lib/Drupal/field/Tests/CrudTest.php index a0afa3f3629c..9ee6019ee218 100644 --- a/core/modules/field/lib/Drupal/field/Tests/CrudTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/CrudTest.php @@ -63,7 +63,7 @@ function testCreateField() { $this->assertEqual($field_config['settings'], $field_type['settings'], 'Default field settings have been written.'); // Ensure that default storage was set. - $this->assertEqual($field_config['storage']['type'], variable_get('field_storage_default'), 'The field type is properly saved.'); + $this->assertEqual($field_config['storage']['type'], config('field.settings')->get('default_storage'), 'The field type is properly saved.'); // Guarantee that the name is unique. try { diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldTestBase.php b/core/modules/field/lib/Drupal/field/Tests/FieldTestBase.php index a34586e61fc0..3944639a8d09 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldTestBase.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldTestBase.php @@ -14,17 +14,6 @@ * Parent class for Field API tests. */ abstract class FieldTestBase extends WebTestBase { - var $default_storage = 'field_sql_storage'; - - /** - * Set the default field storage backend for fields created during tests. - */ - function setUp() { - parent::setUp(); - - // Set default storage backend. - variable_set('field_storage_default', $this->default_storage); - } /** * Generate random values for a field_test field. diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldUnitTestBase.php b/core/modules/field/lib/Drupal/field/Tests/FieldUnitTestBase.php index 3e1bf38b3ea6..223e0acc6f8a 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldUnitTestBase.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldUnitTestBase.php @@ -14,7 +14,6 @@ * Parent class for Field API unit tests. */ abstract class FieldUnitTestBase extends DrupalUnitTestBase { - var $default_storage = 'field_sql_storage'; /** * Modules to enable. @@ -40,7 +39,7 @@ function setUp() { $this->installSchema('field_test', array('test_entity', 'test_entity_revision', 'test_entity_bundle')); // Set default storage backend. - variable_set('field_storage_default', $this->default_storage); + $this->installConfig(array('field')); } /** diff --git a/core/modules/hal/lib/Drupal/hal/Tests/NormalizerTestBase.php b/core/modules/hal/lib/Drupal/hal/Tests/NormalizerTestBase.php index 8391d0e7c078..b40bffadcf96 100644 --- a/core/modules/hal/lib/Drupal/hal/Tests/NormalizerTestBase.php +++ b/core/modules/hal/lib/Drupal/hal/Tests/NormalizerTestBase.php @@ -62,6 +62,7 @@ function setUp() { $this->installSchema('user', array('users')); $this->installSchema('language', array('language')); $this->installSchema('entity_test', array('entity_test')); + $this->installConfig(array('field')); // Add English as a language. $english = new Language(array( diff --git a/core/modules/serialization/lib/Drupal/serialization/Tests/EntitySerializationTest.php b/core/modules/serialization/lib/Drupal/serialization/Tests/EntitySerializationTest.php index 5afc882b0b2f..f6c106ee533f 100644 --- a/core/modules/serialization/lib/Drupal/serialization/Tests/EntitySerializationTest.php +++ b/core/modules/serialization/lib/Drupal/serialization/Tests/EntitySerializationTest.php @@ -63,6 +63,8 @@ protected function setUp() { $this->entity->save(); $this->serializer = $this->container->get('serializer'); + + $this->installConfig(array('field')); } /** diff --git a/core/modules/serialization/lib/Drupal/serialization/Tests/NormalizerTestBase.php b/core/modules/serialization/lib/Drupal/serialization/Tests/NormalizerTestBase.php index d485a5b05802..1d9bff660526 100644 --- a/core/modules/serialization/lib/Drupal/serialization/Tests/NormalizerTestBase.php +++ b/core/modules/serialization/lib/Drupal/serialization/Tests/NormalizerTestBase.php @@ -23,6 +23,7 @@ protected function setUp() { $this->installSchema('entity_test', array('entity_test_mulrev', 'entity_test_mulrev_property_revision', 'entity_test_mulrev_property_data')); $this->installSchema('system', array('url_alias')); + $this->installConfig(array('field')); // Auto-create a field for testing. field_create_field(array( diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUnitTestBase.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUnitTestBase.php index c3f236b17844..41918d3b2710 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUnitTestBase.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUnitTestBase.php @@ -26,6 +26,7 @@ public function setUp() { $this->installSchema('user', 'users'); $this->installSchema('system', 'sequences'); $this->installSchema('entity_test', 'entity_test'); + $this->installConfig(array('field')); } /** diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/FieldAccessTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/FieldAccessTest.php index 5e08eed64db1..a460b6f70482 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/FieldAccessTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/FieldAccessTest.php @@ -41,6 +41,8 @@ public static function getInfo() { protected function setUp() { parent::setUp(); + // Install field configuration. + $this->installConfig(array('field')); // The users table is needed for creating dummy user accounts. $this->installSchema('user', array('users')); // Register entity_test text field. diff --git a/core/modules/text/lib/Drupal/text/Tests/Formatter/TextPlainUnitTest.php b/core/modules/text/lib/Drupal/text/Tests/Formatter/TextPlainUnitTest.php index 2000ffd1a0e8..712638430acf 100644 --- a/core/modules/text/lib/Drupal/text/Tests/Formatter/TextPlainUnitTest.php +++ b/core/modules/text/lib/Drupal/text/Tests/Formatter/TextPlainUnitTest.php @@ -45,6 +45,8 @@ public static function getInfo() { function setUp() { parent::setUp(); + $this->installConfig(array('field')); + // @todo Add helper methods for all of the following. $this->entity_type = 'test_entity'; -- GitLab