diff --git a/core/lib/Drupal/Core/Entity/EntityFormController.php b/core/lib/Drupal/Core/Entity/EntityFormController.php index 4a03975..42f4843 100644 --- a/core/lib/Drupal/Core/Entity/EntityFormController.php +++ b/core/lib/Drupal/Core/Entity/EntityFormController.php @@ -286,13 +286,13 @@ protected function submitEntityLanguage(array $form, array &$form_state) { $current_langcode = $entity->language()->langcode == $form_langcode ? $form_state['values']['langcode'] : $form_langcode; foreach (field_info_instances($entity_type, $entity->bundle()) as $instance) { - $field_name = $instance['field_name']; + $field_name = $instance->field_name; $field = field_info_field($field_name); $previous_langcode = $form[$field_name]['#language']; // Handle a possible language change: new language values are inserted, // previous ones are deleted. - if ($field['translatable'] && $previous_langcode != $current_langcode) { + if ($field->translatable && $previous_langcode != $current_langcode) { $form_state['values'][$field_name][$current_langcode] = $form_state['values'][$field_name][$previous_langcode]; $form_state['values'][$field_name][$previous_langcode] = array(); } diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index 42e4d77..7f0b84f 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -195,7 +195,7 @@ public function processDefinition(&$definition, $plugin_id) { // Prepare entity schema fields SQL info for // Drupal\Core\Entity\DatabaseStorageControllerInterface::buildQuery(). - if (isset($definition['base_table'])) { + if (isset($definition['base_table']) && drupal_get_schema($definition['base_table']) !== FALSE) { $definition['schema_fields_sql']['base_table'] = drupal_schema_fields_sql($definition['base_table']); if (isset($definition['data_table'])) { $definition['schema_fields_sql']['data_table'] = drupal_schema_fields_sql($definition['data_table']); diff --git a/core/modules/comment/comment.install b/core/modules/comment/comment.install index 48bb5a4..2c6d416 100644 --- a/core/modules/comment/comment.install +++ b/core/modules/comment/comment.install @@ -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); + field_attach_delete_bundle('comment', 'comment_node_' . $node_type, FALSE); variable_del('comment_' . $node_type); variable_del('comment_anonymous_' . $node_type); variable_del('comment_controls_' . $node_type); diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentLanguageTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentLanguageTest.php index 35a935e..c644660 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentLanguageTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentLanguageTest.php @@ -71,7 +71,7 @@ function setUp() { // Make comment body translatable. $field = field_info_field('comment_body'); - $field['translatable'] = TRUE; + $field->translatable = TRUE; field_update_field($field); $this->assertTrue(field_is_translatable('comment', $field), 'Comment body is translatable.'); } diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentTranslationUITest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentTranslationUITest.php index c67e2b9..7c97769 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentTranslationUITest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentTranslationUITest.php @@ -64,7 +64,7 @@ protected function getTranslatorPermissions() { function setupTestFields() { parent::setupTestFields(); $field = field_info_field('comment_body'); - $field['translatable'] = TRUE; + $field->translatable = TRUE; field_update_field($field); } diff --git a/core/modules/contact/lib/Drupal/contact/Tests/Views/ContactFieldsTest.php b/core/modules/contact/lib/Drupal/contact/Tests/Views/ContactFieldsTest.php index 7170030..a9e151c 100644 --- a/core/modules/contact/lib/Drupal/contact/Tests/Views/ContactFieldsTest.php +++ b/core/modules/contact/lib/Drupal/contact/Tests/Views/ContactFieldsTest.php @@ -58,7 +58,7 @@ protected function setUp() { * Tests the views data generation. */ public function testViewsData() { - $field_name = $this->field['field_name']; + $field_name = $this->field->field_name; $table_name = _field_sql_storage_tablename($this->field); $data = drupal_container()->get('views.views_data')->get($table_name); diff --git a/core/modules/edit/lib/Drupal/edit/EditorBase.php b/core/modules/edit/lib/Drupal/edit/EditorBase.php index d2e4d09..a0844d1 100644 --- a/core/modules/edit/lib/Drupal/edit/EditorBase.php +++ b/core/modules/edit/lib/Drupal/edit/EditorBase.php @@ -9,7 +9,7 @@ use Drupal\Component\Plugin\PluginBase; use Drupal\edit\EditorInterface; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Defines a base editor (Create.js PropertyEditor widget) implementation. diff --git a/core/modules/edit/lib/Drupal/edit/EditorInterface.php b/core/modules/edit/lib/Drupal/edit/EditorInterface.php index 251233a..df4c7f9 100644 --- a/core/modules/edit/lib/Drupal/edit/EditorInterface.php +++ b/core/modules/edit/lib/Drupal/edit/EditorInterface.php @@ -8,7 +8,7 @@ namespace Drupal\edit; use Drupal\Component\Plugin\PluginInspectionInterface; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Defines an interface for in-place editors (Create.js PropertyEditor widgets). diff --git a/core/modules/edit/lib/Drupal/edit/EditorSelector.php b/core/modules/edit/lib/Drupal/edit/EditorSelector.php index 131eea2..fe71340 100644 --- a/core/modules/edit/lib/Drupal/edit/EditorSelector.php +++ b/core/modules/edit/lib/Drupal/edit/EditorSelector.php @@ -9,7 +9,7 @@ use Drupal\Component\Plugin\PluginManagerInterface; use Drupal\Component\Utility\NestedArray; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Selects an in-place editor (an Editor plugin) for a field. diff --git a/core/modules/edit/lib/Drupal/edit/EditorSelectorInterface.php b/core/modules/edit/lib/Drupal/edit/EditorSelectorInterface.php index 2c180cd..926c7a6 100644 --- a/core/modules/edit/lib/Drupal/edit/EditorSelectorInterface.php +++ b/core/modules/edit/lib/Drupal/edit/EditorSelectorInterface.php @@ -7,7 +7,7 @@ namespace Drupal\edit; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Interface for selecting an in-place editor (an Editor plugin) for a field. diff --git a/core/modules/edit/lib/Drupal/edit/Form/EditFieldForm.php b/core/modules/edit/lib/Drupal/edit/Form/EditFieldForm.php index 1959ae7..dd1259b 100644 --- a/core/modules/edit/lib/Drupal/edit/Form/EditFieldForm.php +++ b/core/modules/edit/lib/Drupal/edit/Form/EditFieldForm.php @@ -89,7 +89,7 @@ protected function buildEntity(array $form, array &$form_state) { // types: http://drupal.org/node/1678002. if ($entity->entityType() == 'node' && $entity->isNewRevision() && !isset($entity->log)) { $instance = field_info_instance($entity->entityType(), $form_state['field_name'], $entity->bundle()); - $entity->log = t('Updated the %field-name field through in-place editing.', array('%field-name' => $instance['label'])); + $entity->log = t('Updated the %field-name field through in-place editing.', array('%field-name' => $instance->label)); } return $entity; diff --git a/core/modules/edit/lib/Drupal/edit/MetadataGenerator.php b/core/modules/edit/lib/Drupal/edit/MetadataGenerator.php index 3ec8ce2..0e6a3b3 100644 --- a/core/modules/edit/lib/Drupal/edit/MetadataGenerator.php +++ b/core/modules/edit/lib/Drupal/edit/MetadataGenerator.php @@ -9,7 +9,7 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Component\Plugin\PluginManagerInterface; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; use Drupal\edit\Access\EditEntityFieldAccessCheckInterface; @@ -59,7 +59,7 @@ public function __construct(EditEntityFieldAccessCheckInterface $access_checker, * Implements \Drupal\edit\MetadataGeneratorInterface::generate(). */ public function generate(EntityInterface $entity, FieldInstance $instance, $langcode, $view_mode) { - $field_name = $instance['field_name']; + $field_name = $instance->field_name; // Early-return if user does not have access. $access = $this->accessChecker->accessEditEntityField($entity, $field_name); @@ -68,7 +68,7 @@ public function generate(EntityInterface $entity, FieldInstance $instance, $lang } // Early-return if no editor is available. - $formatter_id = entity_get_render_display($entity, $view_mode)->getFormatter($instance['field_name'])->getPluginId(); + $formatter_id = entity_get_render_display($entity, $view_mode)->getFormatter($instance->field_name)->getPluginId(); $items = $entity->get($field_name); $items = $items[$langcode]; $editor_id = $this->editorSelector->getEditor($formatter_id, $instance, $items); @@ -77,7 +77,7 @@ public function generate(EntityInterface $entity, FieldInstance $instance, $lang } // Gather metadata, allow the editor to add additional metadata of its own. - $label = $instance['label']; + $label = $instance->label; $editor = $this->editorManager->createInstance($editor_id); $metadata = array( 'label' => check_plain($label), diff --git a/core/modules/edit/lib/Drupal/edit/MetadataGeneratorInterface.php b/core/modules/edit/lib/Drupal/edit/MetadataGeneratorInterface.php index 2b6b1d8..71e447d 100644 --- a/core/modules/edit/lib/Drupal/edit/MetadataGeneratorInterface.php +++ b/core/modules/edit/lib/Drupal/edit/MetadataGeneratorInterface.php @@ -8,7 +8,7 @@ namespace Drupal\edit; use Drupal\Core\Entity\EntityInterface; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Interface for generating in-place editing metadata for an entity field. diff --git a/core/modules/edit/lib/Drupal/edit/Plugin/edit/editor/DirectEditor.php b/core/modules/edit/lib/Drupal/edit/Plugin/edit/editor/DirectEditor.php index 0a386c5..7251e82 100644 --- a/core/modules/edit/lib/Drupal/edit/Plugin/edit/editor/DirectEditor.php +++ b/core/modules/edit/lib/Drupal/edit/Plugin/edit/editor/DirectEditor.php @@ -9,7 +9,7 @@ use Drupal\edit\EditorBase; use Drupal\Core\Annotation\Plugin; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Defines the "direct" Create.js PropertyEditor widget. @@ -29,14 +29,14 @@ class DirectEditor extends EditorBase { * how to generalize to other textual field types. */ function isCompatible(FieldInstance $instance, array $items) { - $field = field_info_field($instance['field_name']); + $field = field_info_field($instance->field_name); // This editor is incompatible with multivalued fields. - if ($field['cardinality'] != 1) { + if ($field->cardinality != 1) { return FALSE; } // This editor is incompatible with processed ("rich") text fields. - elseif (!empty($instance['settings']['text_processing'])) { + elseif (!empty($instance->settings['text_processing'])) { return FALSE; } else { diff --git a/core/modules/edit/lib/Drupal/edit/Plugin/edit/editor/FormEditor.php b/core/modules/edit/lib/Drupal/edit/Plugin/edit/editor/FormEditor.php index 59e8d67..d1bec24 100644 --- a/core/modules/edit/lib/Drupal/edit/Plugin/edit/editor/FormEditor.php +++ b/core/modules/edit/lib/Drupal/edit/Plugin/edit/editor/FormEditor.php @@ -9,7 +9,7 @@ use Drupal\edit\EditorBase; use Drupal\Core\Annotation\Plugin; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Defines the "form" Create.js PropertyEditor widget. diff --git a/core/modules/edit/lib/Drupal/edit/Tests/EditTestBase.php b/core/modules/edit/lib/Drupal/edit/Tests/EditTestBase.php index 18d92d8..4216fe9 100644 --- a/core/modules/edit/lib/Drupal/edit/Tests/EditTestBase.php +++ b/core/modules/edit/lib/Drupal/edit/Tests/EditTestBase.php @@ -58,12 +58,12 @@ function setUp() { */ function createFieldWithInstance($field_name, $type, $cardinality, $label, $instance_settings, $widget_type, $widget_settings, $formatter_type, $formatter_settings) { $field = $field_name . '_field'; - $this->$field = array( + $field_definition = array( 'field_name' => $field_name, 'type' => $type, 'cardinality' => $cardinality, ); - $this->$field_name = field_create_field($this->$field); + $this->$field = field_create_field($field_definition); $instance = $field_name . '_instance'; $this->$instance = array( diff --git a/core/modules/edit/lib/Drupal/edit/Tests/EditorSelectionTest.php b/core/modules/edit/lib/Drupal/edit/Tests/EditorSelectionTest.php index 199a525..d34fc6d 100644 --- a/core/modules/edit/lib/Drupal/edit/Tests/EditorSelectionTest.php +++ b/core/modules/edit/lib/Drupal/edit/Tests/EditorSelectionTest.php @@ -79,23 +79,23 @@ function testText() { $this->assertEqual('direct', $this->getSelectedEditor($items, $field_name), "Without text processing, cardinality 1, the 'direct' editor is selected."); // Editor selection with text processing, cardinality 1. - $this->field_text_instance['settings']['text_processing'] = 1; + $this->field_text_instance->settings['text_processing'] = 1; field_update_instance($this->field_text_instance); $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With text processing, cardinality 1, the 'form' editor is selected."); // Editor selection without text processing, cardinality 1 (again). - $this->field_text_instance['settings']['text_processing'] = 0; + $this->field_text_instance->settings['text_processing'] = 0; field_update_instance($this->field_text_instance); $this->assertEqual('direct', $this->getSelectedEditor($items, $field_name), "Without text processing again, cardinality 1, the 'direct' editor is selected."); // Editor selection without text processing, cardinality >1 - $this->field_text_field['cardinality'] = 2; + $this->field_text_field->cardinality = 2; field_update_field($this->field_text_field); $items[] = array('value' => 'Hallo, wereld!', 'format' => 'full_html'); $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "Without text processing, cardinality >1, the 'form' editor is selected."); // Editor selection with text processing, cardinality >1 - $this->field_text_instance['settings']['text_processing'] = 1; + $this->field_text_instance->settings['text_processing'] = 1; field_update_instance($this->field_text_instance); $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With text processing, cardinality >1, the 'form' editor is selected."); } @@ -134,7 +134,7 @@ function testTextWysiwyg() { $this->assertEqual('wysiwyg', $this->getSelectedEditor($items, $field_name), "With cardinality 1, and the full_html text format, the 'wysiwyg' editor is selected."); // Editor selection with text processing, cardinality >1 - $this->field_textarea_field['cardinality'] = 2; + $this->field_textarea_field->cardinality = 2; field_update_field($this->field_textarea_field); $items[] = array('value' => 'Hallo, wereld!', 'format' => 'full_html'); $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With cardinality >1, and both items using the full_html text format, the 'form' editor is selected."); @@ -164,7 +164,7 @@ function testNumber() { $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With cardinality 1, the 'form' editor is selected."); // Editor selection with cardinality >1. - $this->field_nr_field['cardinality'] = 2; + $this->field_nr_field->cardinality = 2; field_update_field($this->field_nr_field); $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With cardinality >1, the 'form' editor is selected."); } diff --git a/core/modules/edit/tests/modules/lib/Drupal/edit_test/Plugin/edit/editor/WysiwygEditor.php b/core/modules/edit/tests/modules/lib/Drupal/edit_test/Plugin/edit/editor/WysiwygEditor.php index 943848f..14e33e4 100644 --- a/core/modules/edit/tests/modules/lib/Drupal/edit_test/Plugin/edit/editor/WysiwygEditor.php +++ b/core/modules/edit/tests/modules/lib/Drupal/edit_test/Plugin/edit/editor/WysiwygEditor.php @@ -9,7 +9,7 @@ use Drupal\edit\EditorBase; use Drupal\Core\Annotation\Plugin; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Defines the "wysiwyg" Create.js PropertyEditor widget. @@ -27,16 +27,16 @@ class WysiwygEditor extends EditorBase { * Implements \Drupal\edit\EditorInterface::isCompatible(). */ function isCompatible(FieldInstance $instance, array $items) { - $field = field_info_field($instance['field_name']); + $field = field_info_field($instance->field_name); // This editor is incompatible with multivalued fields. - if ($field['cardinality'] != 1) { + if ($field->cardinality != 1) { return FALSE; } // This editor is compatible with processed ("rich") text fields; but only // if there is a currently active text format and that text format is the // 'full_html' text format. - elseif (!empty($instance['settings']['text_processing'])) { + elseif (!empty($instance->settings['text_processing'])) { $format_id = $items[0]['format']; if (isset($format_id) && $format_id === 'full_html') { return TRUE; diff --git a/core/modules/email/lib/Drupal/email/Tests/EmailFieldTest.php b/core/modules/email/lib/Drupal/email/Tests/EmailFieldTest.php index c719c97..9bc4dcd 100644 --- a/core/modules/email/lib/Drupal/email/Tests/EmailFieldTest.php +++ b/core/modules/email/lib/Drupal/email/Tests/EmailFieldTest.php @@ -49,9 +49,9 @@ function testEmailField() { 'field_name' => drupal_strtolower($this->randomName()), 'type' => 'email', ); - field_create_field($this->field); + $this->field = field_create_field($this->field); $this->instance = array( - 'field_name' => $this->field['field_name'], + 'field_name' => $this->field->field_name, 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', 'widget' => array( @@ -64,7 +64,7 @@ function testEmailField() { field_create_instance($this->instance); // Create a display for the full view mode. entity_get_display('test_entity', 'test_bundle', 'full') - ->setComponent($this->field['field_name'], array( + ->setComponent($this->field->field_name, array( 'type' => 'email_mailto', )) ->save(); @@ -72,13 +72,13 @@ function testEmailField() { // Display creation form. $this->drupalGet('test-entity/add/test_bundle'); $langcode = LANGUAGE_NOT_SPECIFIED; - $this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value]", '', 'Widget found.'); + $this->assertFieldByName("{$this->field->field_name}[$langcode][0][value]", '', 'Widget found.'); $this->assertRaw('placeholder="example@example.com"'); // Submit a valid e-mail address and ensure it is accepted. $value = 'test@example.com'; $edit = array( - "{$this->field['field_name']}[$langcode][0][value]" => $value, + "{$this->field->field_name}[$langcode][0][value]" => $value, ); $this->drupalPost(NULL, $edit, t('Save')); preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match); diff --git a/core/modules/entity/lib/Drupal/entity/Plugin/Core/Entity/EntityDisplay.php b/core/modules/entity/lib/Drupal/entity/Plugin/Core/Entity/EntityDisplay.php index 9048788..5bba3d8 100644 --- a/core/modules/entity/lib/Drupal/entity/Plugin/Core/Entity/EntityDisplay.php +++ b/core/modules/entity/lib/Drupal/entity/Plugin/Core/Entity/EntityDisplay.php @@ -238,8 +238,8 @@ public function setComponent($name, array $options = array()) { } if ($instance = field_info_instance($this->targetEntityType, $name, $this->bundle)) { - $field = field_info_field($instance['field_name']); - $options = drupal_container()->get('plugin.manager.field.formatter')->prepareConfiguration($field['type'], $options); + $field = field_info_field($instance->field_name); + $options = drupal_container()->get('plugin.manager.field.formatter')->prepareConfiguration($field->type, $options); // Clear the persisted formatter, if any. unset($this->formatters[$name]); diff --git a/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php b/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php index 6d213f6..771339b 100644 --- a/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php +++ b/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php @@ -145,17 +145,17 @@ public function testFieldComponent() { 'field_name' => 'test_field', 'type' => 'test_field' ); - field_create_field($field); + $field = field_create_field($field); $instance = array( - 'field_name' => $field['field_name'], + 'field_name' => $field->field_name, 'entity_type' => 'entity_test', 'bundle' => 'entity_test', ); field_create_instance($instance); // Check that providing no options results in default values being used. - $display->setComponent($field['field_name']); - $field_type_info = field_info_field_types($field['type']); + $display->setComponent($field->field_name); + $field_type_info = field_info_field_types($field->type); $default_formatter = $field_type_info['default_formatter']; $default_settings = field_info_formatter_settings($default_formatter); $expected = array( @@ -164,10 +164,10 @@ public function testFieldComponent() { 'type' => $default_formatter, 'settings' => $default_settings, ); - $this->assertEqual($display->getComponent($field['field_name']), $expected); + $this->assertEqual($display->getComponent($field->field_name), $expected); // Check that the getFormatter() method returns the correct formatter plugin. - $formatter = $display->getFormatter($field['field_name']); + $formatter = $display->getFormatter($field->field_name); $this->assertEqual($formatter->getPluginId(), $default_formatter); $this->assertEqual($formatter->getSettings(), $default_settings); @@ -175,26 +175,26 @@ public function testFieldComponent() { // arbitrary property and reading it back. $random_value = $this->randomString(); $formatter->randomValue = $random_value; - $formatter = $display->getFormatter($field['field_name']); + $formatter = $display->getFormatter($field->field_name); $this->assertEqual($formatter->randomValue, $random_value ); // Check that changing the definition creates a new formatter. - $display->setComponent($field['field_name'], array( + $display->setComponent($field->field_name, array( 'type' => 'field_test_multiple', )); - $formatter = $display->getFormatter($field['field_name']); + $formatter = $display->getFormatter($field->field_name); $this->assertEqual($formatter->getPluginId(), 'field_test_multiple'); $this->assertFalse(isset($formatter->randomValue)); // Check that specifying an unknown formatter (e.g. case of a disabled // module) gets stored as is in the display, but results in the default // formatter being used. - $display->setComponent($field['field_name'], array( + $display->setComponent($field->field_name, array( 'type' => 'unknown_formatter', )); - $options = $display->getComponent($field['field_name']); + $options = $display->getComponent($field->field_name); $this->assertEqual($options['type'], 'unknown_formatter'); - $formatter = $display->getFormatter($field['field_name']); + $formatter = $display->getFormatter($field->field_name); $this->assertEqual($formatter->getPluginId(), $default_formatter); } diff --git a/core/modules/entity_reference/entity_reference.install b/core/modules/entity_reference/entity_reference.install index 1de70ca..5c18a84 100644 --- a/core/modules/entity_reference/entity_reference.install +++ b/core/modules/entity_reference/entity_reference.install @@ -34,7 +34,7 @@ function entity_reference_field_schema($field) { // Create a foreign key to the target entity type base type. // @todo It's still not safe to call entity_get_info() in here. // see http://drupal.org/node/1847582 - // $entity_type = $field['settings']['target_type']; + // $entity_type = $field->settings['target_type']; // $entity_info = entity_get_info($entity_type); // // $base_table = $entity_info['base_table']; diff --git a/core/modules/entity_reference/entity_reference.module b/core/modules/entity_reference/entity_reference.module index 4e98d15..695aed0 100644 --- a/core/modules/entity_reference/entity_reference.module +++ b/core/modules/entity_reference/entity_reference.module @@ -46,10 +46,10 @@ function entity_reference_entity_field_info($entity_type) { foreach (field_info_instances($entity_type) as $bundle_name => $instances) { foreach ($instances as $field_name => $instance) { $field = field_info_field($field_name); - if ($field['type'] != 'entity_reference') { + if ($field->type != 'entity_reference') { continue; } - $property_info['definitions'][$field_name]['settings']['target_type'] = $field['settings']['target_type']; + $property_info['definitions'][$field_name]['settings']['target_type'] = $field->settings['target_type']; } } return $property_info; @@ -113,13 +113,13 @@ function entity_reference_field_is_empty($item, $field) { */ function entity_reference_field_presave(EntityInterface $entity, $field, $instance, $langcode, &$items) { global $user; - $target_type = $field['settings']['target_type']; + $target_type = $field->settings['target_type']; $entity_info = entity_get_info($target_type); $bundles = entity_get_bundles($target_type); // Get the bundle. - if (!empty($instance['settings']['handler_settings']['target_bundles']) && count($instance['settings']['handler_settings']['target_bundles']) == 1) { - $bundle = reset($instance['settings']['handler_settings']['target_bundles']); + if (!empty($instance->settings['handler_settings']['target_bundles']) && count($instance->settings['handler_settings']['target_bundles']) == 1) { + $bundle = reset($instance->settings['handler_settings']['target_bundles']); } else { $bundle = reset($bundles); @@ -160,9 +160,9 @@ function entity_reference_field_validate(EntityInterface $entity = NULL, $field, $invalid_entities = array_diff_key($ids, array_flip($valid_ids)); if ($invalid_entities) { foreach ($invalid_entities as $id => $delta) { - $errors[$field['field_name']][$langcode][$delta][] = array( + $errors[$field->field_name][$langcode][$delta][] = array( 'error' => 'entity_reference_invalid_entity', - 'message' => t('The referenced entity (@type: @id) does not exist.', array('@type' => $field['settings']['target_type'], '@id' => $id)), + 'message' => t('The referenced entity (@type: @id) does not exist.', array('@type' => $field->settings['target_type'], '@id' => $id)), ); } } @@ -188,7 +188,7 @@ function entity_reference_field_settings_form($field, $instance, $has_data) { '#type' => 'select', '#title' => t('Target type'), '#options' => $entity_type_options, - '#default_value' => $field['settings']['target_type'], + '#default_value' => $field->settings['target_type'], '#required' => TRUE, '#description' => t('The entity type that can be referenced through this field.'), '#disabled' => $has_data, @@ -204,27 +204,27 @@ function entity_reference_field_settings_form($field, $instance, $has_data) { * Reset the instance handler settings, when the target type is changed. */ function entity_reference_field_update_field($field, $prior_field, $has_data) { - if ($field['type'] != 'entity_reference') { + if ($field->type != 'entity_reference') { // Not an entity reference field. return; } - if ($field['settings']['target_type'] == $prior_field['settings']['target_type']) { + if ($field->settings['target_type'] == $prior_field->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->field_name; - 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(); + $instance->settings['handler_settings'] = array(); field_update_instance($instance); } } @@ -237,11 +237,11 @@ function entity_reference_field_instance_settings_form($field, $instance, $form_ $field = isset($form_state['entity_reference']['field']) ? $form_state['entity_reference']['field'] : $field; $instance = isset($form_state['entity_reference']['instance']) ? $form_state['entity_reference']['instance'] : $instance; - $settings = $instance['settings']; + $settings = $instance->settings; $settings += array('handler' => 'default'); // Get all selection plugins for this entity type. - $selection_plugins = drupal_container()->get('plugin.manager.entity_reference.selection')->getSelectionGroups($field['settings']['target_type']); + $selection_plugins = drupal_container()->get('plugin.manager.entity_reference.selection')->getSelectionGroups($field->settings['target_type']); $handler_groups = array_keys($selection_plugins); $handlers = drupal_container()->get('plugin.manager.entity_reference.selection')->getDefinitions(); @@ -364,7 +364,7 @@ function _entity_reference_element_validate_filter(&$element, &$form_state) { function _entity_reference_field_instance_settings_validate($form, &$form_state) { $instance = $form['#instance']; if (isset($form_state['values']['instance'])) { - $instance['settings'] = $form_state['values']['instance']['settings']; + $instance->settings = $form_state['values']['instance']['settings']; } $form_state['entity_reference']['instance'] = $instance; @@ -399,7 +399,7 @@ function entity_reference_options_list($field, $instance, $entity_type = NULL, $ } // Rebuild the array by changing the bundle key into the bundle label. - $target_type = $field['settings']['target_type']; + $target_type = $field->settings['target_type']; $bundles = entity_get_bundles($target_type); $return = array(); @@ -442,7 +442,7 @@ function entity_reference_autocomplete_access_callback($type, $field_name, $enti return FALSE; } - if ($field['type'] != 'entity_reference' || !field_access('edit', $field, $entity_type)) { + if ($field->type != 'entity_reference' || !field_access('edit', $field, $entity_type)) { return FALSE; } @@ -503,7 +503,7 @@ function entity_reference_autocomplete_callback($type, $field_name, $entity_type * @see entity_reference_autocomplete_callback() */ function entity_reference_autocomplete_callback_get_matches($type, $field, $instance, $entity_type, $entity_id = '', $string = '') { - $target_type = $field['settings']['target_type']; + $target_type = $field->settings['target_type']; $matches = array(); $entity = NULL; @@ -534,7 +534,7 @@ function entity_reference_autocomplete_callback_get_matches($type, $field, $inst if (isset($tag_last)) { // Get an array of matching entities. - $match_operator = !empty($instance['widget']['settings']['match_operator']) ? $instance['widget']['settings']['match_operator'] : 'CONTAINS'; + $match_operator = !empty($instance->widget_settings['settings']['match_operator']) ? $instance->widget_settings['settings']['match_operator'] : 'CONTAINS'; $entity_labels = $handler->getReferencableEntities($tag_last, $match_operator, 10); // Loop through the products and convert them into autocomplete output. diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/SelectionPluginManager.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/SelectionPluginManager.php index af91a2d..b2abc72 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/SelectionPluginManager.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/SelectionPluginManager.php @@ -47,7 +47,7 @@ public function createInstance($plugin_id, array $configuration = array()) { */ public function getInstance(array $options) { $selection_handler = $options['instance']['settings']['handler']; - $target_entity_type = $options['field']['settings']['target_type']; + $target_entity_type = $options['field']->settings['target_type']; // Get all available selection plugins for this entity type. $selection_handler_groups = $this->getSelectionGroups($target_entity_type); diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/SelectionBase.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/SelectionBase.php index 205df06..b761e07 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/SelectionBase.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/SelectionBase.php @@ -63,14 +63,14 @@ public function __construct($field, $instance, EntityInterface $entity = NULL) { * Implements SelectionInterface::settingsForm(). */ public static function settingsForm(&$field, &$instance) { - $entity_info = entity_get_info($field['settings']['target_type']); - $bundles = entity_get_bundles($field['settings']['target_type']); + $entity_info = entity_get_info($field->settings['target_type']); + $bundles = entity_get_bundles($field->settings['target_type']); // Merge-in default values. - if (!isset($instance['settings']['handler_settings'])) { - $instance['settings']['handler_settings'] = array(); + if (!isset($instance->settings['handler_settings'])) { + $instance->settings['handler_settings'] = array(); } - $instance['settings']['handler_settings'] += array( + $instance->settings['handler_settings'] += array( 'target_bundles' => array(), 'sort' => array( 'field' => '_none', @@ -86,10 +86,10 @@ public static function settingsForm(&$field, &$instance) { $target_bundles_title = t('Bundles'); // Default core entity types with sensible labels. - if ($field['settings']['target_type'] == 'node') { + if ($field->settings['target_type'] == 'node') { $target_bundles_title = t('Content types'); } - elseif ($field['settings']['target_type'] == 'taxonomy_term') { + elseif ($field->settings['target_type'] == 'taxonomy_term') { $target_bundles_title = t('Vocabularies'); } @@ -97,7 +97,7 @@ public static function settingsForm(&$field, &$instance) { '#type' => 'checkboxes', '#title' => $target_bundles_title, '#options' => $bundle_options, - '#default_value' => (!empty($instance['settings']['handler_settings']['target_bundles'])) ? $instance['settings']['handler_settings']['target_bundles'] : array_keys($bundle_options), + '#default_value' => (!empty($instance->settings['handler_settings']['target_bundles'])) ? $instance->settings['handler_settings']['target_bundles'] : array_keys($bundle_options), '#size' => 6, '#multiple' => TRUE, '#element_validate' => array('_entity_reference_element_validate_filter'), @@ -113,10 +113,10 @@ public static function settingsForm(&$field, &$instance) { // @todo Use Entity::getPropertyDefinitions() when all entity types are // converted to the new Field API. $fields = drupal_map_assoc($entity_info['schema_fields_sql']['base_table']); - foreach (field_info_instances($field['settings']['target_type']) as $bundle_instances) { + foreach (field_info_instances($field->settings['target_type']) as $bundle_instances) { foreach ($bundle_instances as $instance_name => $instance_info) { $field_info = field_info_field($instance_name); - foreach ($field_info['columns'] as $column_name => $column_info) { + foreach ($field_info->columns as $column_name => $column_info) { $fields[$instance_name . '.' . $column_name] = t('@label (@column)', array('@label' => $instance_info['label'], '@column' => $column_name)); } } @@ -130,7 +130,7 @@ public static function settingsForm(&$field, &$instance) { ) + $fields, '#ajax' => TRUE, '#limit_validation_errors' => array(), - '#default_value' => $instance['settings']['handler_settings']['sort']['field'], + '#default_value' => $instance->settings['handler_settings']['sort']['field'], ); $form['sort']['settings'] = array( @@ -139,9 +139,9 @@ public static function settingsForm(&$field, &$instance) { '#process' => array('_entity_reference_form_process_merge_parent'), ); - if ($instance['settings']['handler_settings']['sort']['field'] != '_none') { + if ($instance->settings['handler_settings']['sort']['field'] != '_none') { // Merge-in default values. - $instance['settings']['handler_settings']['sort'] += array( + $instance->settings['handler_settings']['sort'] += array( 'direction' => 'ASC', ); @@ -153,7 +153,7 @@ public static function settingsForm(&$field, &$instance) { 'ASC' => t('Ascending'), 'DESC' => t('Descending'), ), - '#default_value' => $instance['settings']['handler_settings']['sort']['direction'], + '#default_value' => $instance->settings['handler_settings']['sort']['direction'], ); } @@ -164,7 +164,7 @@ public static function settingsForm(&$field, &$instance) { * Implements SelectionInterface::getReferencableEntities(). */ public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) { - $target_type = $this->field['settings']['target_type']; + $target_type = $this->field->settings['target_type']; $query = $this->buildEntityQuery($match, $match_operator); if ($limit > 0) { @@ -203,7 +203,7 @@ public function countReferencableEntities($match = NULL, $match_operator = 'CONT public function validateReferencableEntities(array $ids) { $result = array(); if ($ids) { - $target_type = $this->field['settings']['target_type']; + $target_type = $this->field->settings['target_type']; $entity_info = entity_get_info($target_type); $query = $this->buildEntityQuery(); $result = $query @@ -263,12 +263,12 @@ public function validateAutocompleteInput($input, &$element, &$form_state, $form * it. */ public function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') { - $target_type = $this->field['settings']['target_type']; + $target_type = $this->field->settings['target_type']; $entity_info = entity_get_info($target_type); $query = entity_query($target_type); - if (!empty($this->instance['settings']['handler_settings']['target_bundles'])) { - $query->condition($entity_info['entity_keys']['bundle'], $this->instance['settings']['handler_settings']['target_bundles'], 'IN'); + if (!empty($this->instance->settings['handler_settings']['target_bundles'])) { + $query->condition($entity_info['entity_keys']['bundle'], $this->instance->settings['handler_settings']['target_bundles'], 'IN'); } if (isset($match) && isset($entity_info['entity_keys']['label'])) { @@ -276,7 +276,7 @@ public function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') { } // Add entity-access tag. - $query->addTag($this->field['settings']['target_type'] . '_access'); + $query->addTag($this->field->settings['target_type'] . '_access'); // Add the Selection handler for // entity_reference_query_entity_reference_alter(). @@ -285,8 +285,8 @@ public function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') { $query->addMetaData('entity_reference_selection_handler', $this); // Add the sort option. - if (!empty($this->instance['settings']['handler_settings']['sort'])) { - $sort_settings = $this->instance['settings']['handler_settings']['sort']; + if (!empty($this->instance->settings['handler_settings']['sort'])) { + $sort_settings = $this->instance->settings['handler_settings']['sort']; if ($sort_settings['field'] != '_none') { $query->sort($sort_settings['field'], $sort_settings['direction']); } diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceEntityFormatter.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceEntityFormatter.php index 98cc69c..4d470b4 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceEntityFormatter.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceEntityFormatter.php @@ -36,7 +36,7 @@ class EntityReferenceEntityFormatter extends EntityReferenceFormatterBase { * Overrides \Drupal\entity_reference\Plugin\field\formatter\EntityReferenceFormatterBase::settingsForm(). */ public function settingsForm(array $form, array &$form_state) { - $view_modes = entity_get_view_modes($this->field['settings']['target_type']); + $view_modes = entity_get_view_modes($this->field->settings['target_type']); $options = array(); foreach ($view_modes as $view_mode => $view_mode_settings) { $options[$view_mode] = $view_mode_settings['label']; @@ -65,7 +65,7 @@ public function settingsForm(array $form, array &$form_state) { public function settingsSummary() { $summary = array(); - $view_modes = entity_get_view_modes($this->field['settings']['target_type']); + $view_modes = entity_get_view_modes($this->field->settings['target_type']); $view_mode = $this->getSetting('view_mode'); $summary[] = t('Rendered as @mode', array('@mode' => isset($view_modes[$view_mode]['label']) ? $view_modes[$view_mode]['label'] : $view_mode)); $summary[] = $this->getSetting('links') ? t('Display links') : t('Do not display links'); @@ -83,7 +83,7 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) { $view_mode = $this->getSetting('view_mode'); $links = $this->getSetting('links'); - $target_type = $this->field['settings']['target_type']; + $target_type = $this->field->settings['target_type']; $elements = array(); diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceFormatterBase.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceFormatterBase.php index 76fc029..05b4ba0 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceFormatterBase.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceFormatterBase.php @@ -40,7 +40,7 @@ public function prepareView(array $entities, $langcode, array &$items) { } } - $target_type = $this->field['settings']['target_type']; + $target_type = $this->field->settings['target_type']; $target_entities = array(); diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteTagsWidget.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteTagsWidget.php index 2887af6..a668438 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteTagsWidget.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteTagsWidget.php @@ -40,8 +40,8 @@ public function elementValidate($element, &$form_state, $form) { $value = array(); // If a value was entered into the autocomplete. $handler = entity_reference_get_selection_handler($this->field, $this->instance); - $bundles = entity_get_bundles($this->field['settings']['target_type']); - $auto_create = isset($this->instance['settings']['handler_settings']['auto_create']) ? $this->instance['settings']['handler_settings']['auto_create'] : FALSE; + $bundles = entity_get_bundles($this->field->settings['target_type']); + $auto_create = isset($this->instance->settings['handler_settings']['auto_create']) ? $this->instance->settings['handler_settings']['auto_create'] : FALSE; if (!empty($element['#value'])) { $entities = drupal_explode_tags($element['#value']); @@ -62,7 +62,7 @@ public function elementValidate($element, &$form_state, $form) { if ($match) { $value[] = array('target_id' => $match); } - elseif ($auto_create && (count($this->instance['settings']['handler_settings']['target_bundles']) == 1 || count($bundles) == 1)) { + elseif ($auto_create && (count($this->instance->settings['handler_settings']['target_bundles']) == 1 || count($bundles) == 1)) { // Auto-create item. see entity_reference_field_presave(). $value[] = array('target_id' => 'auto_create', 'label' => $entity); } diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidget.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidget.php index 5896259..3032e50 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidget.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidget.php @@ -57,7 +57,7 @@ public function formElement(array $items, $delta, array $element, $langcode, arr * Overrides \Drupal\entity_reference\Plugin\field\widget\AutocompleteWidgetBase::elementValidate() */ public function elementValidate($element, &$form_state, $form) { - $auto_create = isset($this->instance['settings']['handler_settings']['auto_create']) ? $this->instance['settings']['handler_settings']['auto_create'] : FALSE; + $auto_create = isset($this->instance->settings['handler_settings']['auto_create']) ? $this->instance->settings['handler_settings']['auto_create'] : FALSE; // If a value was entered into the autocomplete. $value = ''; @@ -73,7 +73,7 @@ public function elementValidate($element, &$form_state, $form) { $value = $handler->validateAutocompleteInput($element['#value'], $element, $form_state, $form, !$auto_create); } - if (!$value && $auto_create && (count($this->instance['settings']['handler_settings']['target_bundles']) == 1)) { + if (!$value && $auto_create && (count($this->instance->settings['handler_settings']['target_bundles']) == 1)) { // Auto-create item. see entity_reference_field_presave(). $value = array( 'target_id' => 'auto_create', diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php index fe30b11..92fa6dd 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php @@ -59,7 +59,7 @@ public function formElement(array $items, $delta, array $element, $langcode, arr // Prepare the autocomplete path. $autocomplete_path = $this->getSetting('autocomplete_path'); - $autocomplete_path .= '/' . $field['field_name'] . '/' . $instance['entity_type'] . '/' . $instance['bundle'] . '/'; + $autocomplete_path .= '/' . $field->field_name . '/' . $instance->entity_type . '/' . $instance->bundle . '/'; // Use as a placeholder in the URL when we don't have an entity. // Most web servers collapse two consecutive slashes. @@ -107,7 +107,7 @@ protected function getLabels(array $items) { } // Load those entities and loop through them to extract their labels. - $entities = entity_load_multiple($this->field['settings']['target_type'], $entity_ids); + $entities = entity_load_multiple($this->field->settings['target_type'], $entity_ids); foreach ($entities as $entity_id => $entity_item) { $label = $entity_item->label(); diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutocompleteTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutocompleteTest.php index 3d94c04..e8344c6 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutocompleteTest.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutocompleteTest.php @@ -59,7 +59,7 @@ function setUp() { ); field_create_instance($this->instance); entity_get_display('node', 'article', 'default') - ->setComponent($this->instance['field_name'], array( + ->setComponent($this->instance->field_name, array( 'type' => 'entity_reference_label', )) ->save(); diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionAccessTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionAccessTest.php index cf1c619..053961f 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionAccessTest.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionAccessTest.php @@ -58,7 +58,7 @@ protected function assertReferencable($field, $instance, $tests, $handler_name) */ public function testNodeHandler() { // Build a fake field instance. - $field = array( + $field = entity_create('field_entity', array( 'translatable' => FALSE, 'entity_types' => array(), 'settings' => array( @@ -67,7 +67,7 @@ public function testNodeHandler() { 'field_name' => 'test_field', 'type' => 'entity_reference', 'cardinality' => '1', - ); + )); $instance = array( 'settings' => array( 'handler' => 'default', @@ -196,7 +196,7 @@ public function testNodeHandler() { */ public function testUserHandler() { // Build a fake field instance. - $field = array( + $field = entity_create('field_entity', array( 'translatable' => FALSE, 'entity_types' => array(), 'settings' => array( @@ -205,7 +205,7 @@ public function testUserHandler() { 'field_name' => 'test_field', 'type' => 'entity_reference', 'cardinality' => '1', - ); + )); $instance = array( 'settings' => array( 'handler' => 'default', @@ -336,7 +336,7 @@ public function testUserHandler() { */ public function testCommentHandler() { // Build a fake field instance. - $field = array( + $field = entity_create('field_entity', array( 'translatable' => FALSE, 'entity_types' => array(), 'settings' => array( @@ -345,7 +345,7 @@ public function testCommentHandler() { 'field_name' => 'test_field', 'type' => 'entity_reference', 'cardinality' => '1', - ); + )); $instance = array( 'settings' => array( 'handler' => 'default', diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionSortTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionSortTest.php index cc768e2..8589b76 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionSortTest.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionSortTest.php @@ -55,7 +55,7 @@ public function testSort() { // Build a fake field instance. - $field = array( + $field = entity_create('field_entity', array( 'translatable' => FALSE, 'entity_types' => array(), 'settings' => array( @@ -64,7 +64,7 @@ public function testSort() { 'field_name' => 'test_field', 'type' => 'entity_reference', 'cardinality' => 1, - ); + )); $instance = array( 'settings' => array( @@ -135,7 +135,7 @@ public function testSort() { $this->assertIdentical($result['article'], $expected_result, 'Query sorted by field returned expected values.'); // Assert sort by property. - $instance['settings']['handler_settings']['sort'] = array( + $instance->settings['handler_settings']['sort'] = array( 'field' => 'nid', 'direction' => 'ASC', ); diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/Views/SelectionTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/Views/SelectionTest.php index f11f5a4..136c69b 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/Views/SelectionTest.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/Views/SelectionTest.php @@ -40,7 +40,7 @@ public function testSelectionHandler() { } // Build a fake field instance. - $field = array( + $field = entity_create('field_entity', array( 'translatable' => FALSE, 'entity_types' => array(), 'settings' => array( @@ -49,7 +49,7 @@ public function testSelectionHandler() { 'field_name' => 'test_field', 'type' => 'entity_reference', 'cardinality' => '1', - ); + )); $instance = array( 'settings' => array( 'handler' => 'views', diff --git a/core/modules/field/field.api.php b/core/modules/field/field.api.php index 29112ef..b5d17f4 100644 --- a/core/modules/field/field.api.php +++ b/core/modules/field/field.api.php @@ -230,7 +230,7 @@ function hook_field_info_alter(&$info) { * - foreign keys: (optional) An array of Schema API foreign key definitions. */ function hook_field_schema($field) { - if ($field['type'] == 'text_long') { + if ($field->type == 'text_long') { $columns = array( 'value' => array( 'type' => 'text', @@ -243,7 +243,7 @@ function hook_field_schema($field) { $columns = array( 'value' => array( 'type' => 'varchar', - 'length' => $field['settings']['max_length'], + 'length' => $field->settings['max_length'], 'not null' => FALSE, ), ); @@ -313,7 +313,7 @@ function hook_field_load($entity_type, $entities, $field, $instances, $langcode, // by formatters if needed. if (empty($instances[$id]['settings']['text_processing']) || filter_format_allowcache($item['format'])) { $items[$id][$delta]['safe_value'] = isset($item['value']) ? _text_sanitize($instances[$id], $langcode, $item, 'value') : ''; - if ($field['type'] == 'text_with_summary') { + if ($field->type == 'text_with_summary') { $items[$id][$delta]['safe_summary'] = isset($item['summary']) ? _text_sanitize($instances[$id], $langcode, $item, 'summary') : ''; } } @@ -348,14 +348,14 @@ function hook_field_load($entity_type, $entities, $field, $instances, $langcode, * @param $langcode * The language associated with $items. * @param $items - * $entity->{$field['field_name']}, or an empty array if unset. + * $entity->{$field->field_name}, or an empty array if unset. */ function hook_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) { // Sample code from image.module: if there are no images specified at all, // use the default image. foreach ($entities as $id => $entity) { - if (empty($items[$id]) && $field['settings']['default_image']) { - if ($file = file_load($field['settings']['default_image'])) { + if (empty($items[$id]) && $field->settings['default_image']) { + if ($file = file_load($field->settings['default_image'])) { $items[$id][0] = (array) $file + array( 'is_default' => TRUE, 'alt' => '', @@ -381,7 +381,7 @@ function hook_field_prepare_view($entity_type, $entities, $field, $instances, $l * @param $langcode * The language associated with $items. * @param $items - * $entity->{$field['field_name']}[$langcode], or an empty array if unset. + * $entity->{$field->field_name}[$langcode], or an empty array if unset. * @param $errors * The array of errors (keyed by field name, language code, and delta) that * have already been reported for the entity. The function should add its @@ -393,10 +393,10 @@ function hook_field_prepare_view($entity_type, $entities, $field, $instances, $l function hook_field_validate(\Drupal\Core\Entity\EntityInterface $entity = NULL, $field, $instance, $langcode, $items, &$errors) { foreach ($items as $delta => $item) { if (!empty($item['value'])) { - if (!empty($field['settings']['max_length']) && drupal_strlen($item['value']) > $field['settings']['max_length']) { - $errors[$field['field_name']][$langcode][$delta][] = array( + if (!empty($field->settings['max_length']) && drupal_strlen($item['value']) > $field->settings['max_length']) { + $errors[$field->field_name][$langcode][$delta][] = array( 'error' => 'text_max_length', - 'message' => t('%name: the value may not be longer than %max characters.', array('%name' => $instance['label'], '%max' => $field['settings']['max_length'])), + 'message' => t('%name: the value may not be longer than %max characters.', array('%name' => $instance->label, '%max' => $field->settings['max_length'])), ); } } @@ -418,15 +418,15 @@ function hook_field_validate(\Drupal\Core\Entity\EntityInterface $entity = NULL, * @param $langcode * The language associated with $items. * @param $items - * $entity->{$field['field_name']}[$langcode], or an empty array if unset. + * $entity->{$field->field_name}[$langcode], or an empty array if unset. */ function hook_field_presave(\Drupal\Core\Entity\EntityInterface $entity, $field, $instance, $langcode, &$items) { - if ($field['type'] == 'number_decimal') { + if ($field->type == 'number_decimal') { // Let PHP round the value to ensure consistent behavior across storage // backends. foreach ($items as $delta => $item) { if (isset($item['value'])) { - $items[$delta]['value'] = round($item['value'], $field['settings']['scale']); + $items[$delta]['value'] = round($item['value'], $field->settings['scale']); } } } @@ -451,7 +451,7 @@ function hook_field_presave(\Drupal\Core\Entity\EntityInterface $entity, $field, * @param $langcode * The language associated with $items. * @param $items - * $entity->{$field['field_name']}[$langcode], or an empty array if unset. + * $entity->{$field->field_name}[$langcode], or an empty array if unset. * * @see hook_field_update() * @see hook_field_delete() @@ -490,7 +490,7 @@ function hook_field_insert(\Drupal\Core\Entity\EntityInterface $entity, $field, * @param $langcode * The language associated with $items. * @param $items - * $entity->{$field['field_name']}[$langcode], or an empty array if unset. + * $entity->{$field->field_name}[$langcode], or an empty array if unset. * * @see hook_field_insert() * @see hook_field_delete() @@ -577,7 +577,7 @@ function hook_field_storage_update_field($field, $prior_field, $has_data) { * @param $langcode * The language associated with $items. * @param $items - * $entity->{$field['field_name']}[$langcode], or an empty array if unset. + * $entity->{$field->field_name}[$langcode], or an empty array if unset. * * @see hook_field_insert() * @see hook_field_update() @@ -605,7 +605,7 @@ function hook_field_delete(\Drupal\Core\Entity\EntityInterface $entity, $field, * @param $langcode * The language associated with $items. * @param $items - * $entity->{$field['field_name']}[$langcode], or an empty array if unset. + * $entity->{$field->field_name}[$langcode], or an empty array if unset. */ function hook_field_delete_revision(\Drupal\Core\Entity\EntityInterface $entity, $field, $instance, $langcode, &$items) { foreach ($items as $delta => $item) { @@ -626,7 +626,7 @@ function hook_field_delete_revision(\Drupal\Core\Entity\EntityInterface $entity, * @param $langcode * The language associated with $items. * @param $items - * $entity->{$field['field_name']}[$langcode], or an empty array if unset. + * $entity->{$field->field_name}[$langcode], or an empty array if unset. * @param $source_entity * The source entity from which field values are being copied. * @param $source_langcode @@ -635,7 +635,7 @@ function hook_field_delete_revision(\Drupal\Core\Entity\EntityInterface $entity, function hook_field_prepare_translation(\Drupal\Core\Entity\EntityInterface $entity, $field, $instance, $langcode, &$items, $source_entity, $source_langcode) { // If the translating user is not permitted to use the assigned text format, // we must not expose the source values. - $field_name = $field['field_name']; + $field_name = $field->field_name; $formats = filter_formats(); $format_id = $source_entity->{$field_name}[$source_langcode][0]['format']; if (!filter_access($formats[$format_id])) { @@ -802,7 +802,7 @@ function hook_field_widget_WIDGET_TYPE_form_alter(&$element, &$form_state, $cont function hook_field_widget_properties_alter(array &$widget_properties, array $context) { // Change a widget's type according to the time of day. $field = $context['field']; - if ($context['entity_type'] == 'node' && $field['field_name'] == 'field_foo') { + if ($context['entity_type'] == 'node' && $field->field_name == 'field_foo') { $time = date('H'); $widget_properties['type'] = $time < 12 ? 'widget_am' : 'widget_pm'; } @@ -1296,8 +1296,8 @@ function hook_field_storage_details($field) { $details = array(); // Add field columns. - foreach ((array) $field['columns'] as $column_name => $attributes) { - $real_name = _field_sql_storage_columnname($field['field_name'], $column_name); + foreach ((array) $field->columns as $column_name => $attributes) { + $real_name = _field_sql_storage_columnname($field->field_name, $column_name); $columns[$column_name] = $real_name; } return array( @@ -1324,9 +1324,9 @@ function hook_field_storage_details($field) { * @see hook_field_storage_details() */ function hook_field_storage_details_alter(&$details, $field) { - if ($field['field_name'] == 'field_of_interest') { + if ($field->field_name == 'field_of_interest') { $columns = array(); - foreach ((array) $field['columns'] as $column_name => $attributes) { + foreach ((array) $field->columns as $column_name => $attributes) { $columns[$column_name] = $column_name; } $details['drupal_variables'] = array( @@ -1375,7 +1375,7 @@ function hook_field_storage_load($entity_type, $entities, $age, $fields, $option foreach ($fields as $field_id => $ids) { $field = field_info_field_by_id($field_id); - $field_name = $field['field_name']; + $field_name = $field->field_name; $table = $load_current ? _field_sql_storage_tablename($field) : _field_sql_storage_revision_tablename($field); $query = db_select($table, 't') @@ -1397,11 +1397,11 @@ function hook_field_storage_load($entity_type, $entities, $age, $fields, $option $delta_count[$row->entity_id][$row->langcode] = 0; } - if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->langcode] < $field['cardinality']) { + if ($field->cardinality == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->langcode] < $field->cardinality) { $item = array(); // For each column declared by the field, populate the item // from the prefixed database column. - foreach ($field['columns'] as $column => $attributes) { + foreach ($field->columns as $column => $attributes) { $column_name = _field_sql_storage_columnname($field_name, $column); $item[$column] = $row->$column_name; } @@ -1439,7 +1439,7 @@ function hook_field_storage_write(\Drupal\Core\Entity\EntityInterface $entity, $ foreach ($fields as $field_id) { $field = field_info_field_by_id($field_id); - $field_name = $field['field_name']; + $field_name = $field->field_name; $table_name = _field_sql_storage_tablename($field); $revision_name = _field_sql_storage_revision_tablename($field); @@ -1469,7 +1469,7 @@ function hook_field_storage_write(\Drupal\Core\Entity\EntityInterface $entity, $ // Prepare the multi-insert query. $do_insert = FALSE; $columns = array('entity_type', 'entity_id', 'revision_id', 'bundle', 'delta', 'langcode'); - foreach ($field['columns'] as $column => $attributes) { + foreach ($field->columns as $column => $attributes) { $columns[] = _field_sql_storage_columnname($field_name, $column); } $query = db_insert($table_name)->fields($columns); @@ -1489,7 +1489,7 @@ function hook_field_storage_write(\Drupal\Core\Entity\EntityInterface $entity, $ 'delta' => $delta, 'langcode' => $langcode, ); - foreach ($field['columns'] as $column => $attributes) { + foreach ($field->columns as $column => $attributes) { $record[_field_sql_storage_columnname($field_name, $column)] = isset($item[$column]) ? $item[$column] : NULL; } $query->values($record); @@ -1497,7 +1497,7 @@ function hook_field_storage_write(\Drupal\Core\Entity\EntityInterface $entity, $ $revision_query->values($record); } - if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && ++$delta_count == $field['cardinality']) { + if ($field->cardinality != FIELD_CARDINALITY_UNLIMITED && ++$delta_count == $field->cardinality) { break; } } @@ -1525,8 +1525,8 @@ function hook_field_storage_write(\Drupal\Core\Entity\EntityInterface $entity, $ */ function hook_field_storage_delete(\Drupal\Core\Entity\EntityInterface $entity, $fields) { foreach (field_info_instances($entity->entityType(), $entity->bundle()) as $instance) { - if (isset($fields[$instance['field_id']])) { - $field = field_info_field_by_id($instance['field_id']); + if (isset($fields[$instance->field_id])) { + $field = field_info_field_by_id($instance->field_id); field_sql_storage_field_storage_purge($entity, $field, $instance); } } @@ -1603,7 +1603,7 @@ function hook_field_storage_query($query) { $select_query->fields($table_alias, array('entity_type', 'entity_id', 'revision_id', 'bundle')); $field_base_table = $table_alias; } - if ($field['cardinality'] != 1) { + if ($field->cardinality != 1) { $select_query->distinct(); } } @@ -1613,7 +1613,7 @@ function hook_field_storage_query($query) { $table_alias = $table_aliases[$key]; $field = $condition['field']; // Add the specified condition. - $sql_field = "$table_alias." . _field_sql_storage_columnname($field['field_name'], $condition['column']); + $sql_field = "$table_alias." . _field_sql_storage_columnname($field->field_name, $condition['column']); $query->addCondition($select_query, $sql_field, $condition); // Add delta / language group conditions. foreach (array('delta', 'langcode') as $column) { @@ -1666,7 +1666,7 @@ function hook_field_storage_query($query) { $specifier = $order['specifier']; $field = $specifier['field']; $table_alias = $table_aliases[$specifier['index']]; - $sql_field = "$table_alias." . _field_sql_storage_columnname($field['field_name'], $specifier['column']); + $sql_field = "$table_alias." . _field_sql_storage_columnname($field->field_name, $specifier['column']); $select_query->orderBy($sql_field, $order['direction']); } elseif ($order['type'] == 'property') { @@ -1706,7 +1706,7 @@ function hook_field_storage_create_field($field) { */ function hook_field_storage_delete_field($field) { // Mark all data associated with the field for deletion. - $field['deleted'] = 0; + $field->deleted = 0; $table = _field_sql_storage_tablename($field); $revision_table = _field_sql_storage_revision_tablename($field); db_update($table) @@ -1714,7 +1714,7 @@ function hook_field_storage_delete_field($field) { ->execute(); // Move the table to a unique name while the table contents are being deleted. - $field['deleted'] = 1; + $field->deleted = 1; $new_table = _field_sql_storage_tablename($field); $revision_new_table = _field_sql_storage_revision_tablename($field); db_rename_table($table, $new_table); @@ -1732,18 +1732,18 @@ function hook_field_storage_delete_field($field) { * The instance being deleted. */ function hook_field_storage_delete_instance($instance) { - $field = field_info_field($instance['field_name']); + $field = field_info_field($instance->field_name); $table_name = _field_sql_storage_tablename($field); $revision_name = _field_sql_storage_revision_tablename($field); db_update($table_name) ->fields(array('deleted' => 1)) - ->condition('entity_type', $instance['entity_type']) - ->condition('bundle', $instance['bundle']) + ->condition('entity_type', $instance->entity_type) + ->condition('bundle', $instance->bundle) ->execute(); db_update($revision_name) ->fields(array('deleted' => 1)) - ->condition('entity_type', $instance['entity_type']) - ->condition('bundle', $instance['bundle']) + ->condition('entity_type', $instance->entity_type) + ->condition('bundle', $instance->bundle) ->execute(); } @@ -1921,7 +1921,7 @@ function hook_field_info_max_weight($entity_type, $bundle, $context) { function hook_field_widget_properties_ENTITY_TYPE_alter(array &$widget_properties, array $context) { // Change a widget's type according to the time of day. $field = $context['field']; - if ($field['field_name'] == 'field_foo') { + if ($field->field_name == 'field_foo') { $time = date('H'); $widget_properties['type'] = $time < 12 ? 'widget_am' : 'widget_pm'; } @@ -1987,13 +1987,13 @@ function hook_field_update_forbid($field, $prior_field, $has_data) { // the new field will have fewer values, and any data exists for the // abandoned keys, the field will have no way to display them. So, // forbid such an update. - if ($has_data && count($field['settings']['allowed_values']) < count($prior_field['settings']['allowed_values'])) { + if ($has_data && count($field->settings['allowed_values']) < count($prior_field->settings['allowed_values'])) { // Identify the keys that will be lost. - $lost_keys = array_diff(array_keys($field['settings']['allowed_values']), array_keys($prior_field['settings']['allowed_values'])); + $lost_keys = array_diff(array_keys($field->settings['allowed_values']), array_keys($prior_field->settings['allowed_values'])); // If any data exist for those keys, forbid the update. $query = new EntityFieldQuery(); $found = $query - ->fieldCondition($prior_field['field_name'], 'value', $lost_keys) + ->fieldCondition($prior_field->field_name, 'value', $lost_keys) ->range(0, 1) ->execute(); if ($found) { @@ -2097,7 +2097,7 @@ function hook_field_read_instance($instance) { */ function hook_field_purge_field($field) { db_delete('my_module_field_info') - ->condition('id', $field['id']) + ->condition('id', $field->field_name) ->execute(); } @@ -2201,7 +2201,7 @@ function hook_field_storage_purge(\Drupal\Core\Entity\EntityInterface $entity, $ * TRUE if the operation is allowed, and FALSE if the operation is denied. */ function hook_field_access($op, $field, $entity_type, $entity, $account) { - if ($field['field_name'] == 'field_of_interest' && $op == 'edit') { + if ($field->field_name == 'field_of_interest' && $op == 'edit') { return user_access('edit field of interest', $account); } return TRUE; diff --git a/core/modules/field/field.attach.inc b/core/modules/field/field.attach.inc index 69102f4..b349a2d 100644 --- a/core/modules/field/field.attach.inc +++ b/core/modules/field/field.attach.inc @@ -169,8 +169,8 @@ function field_invoke_method($method, $target_function, EntityInterface $entity, $target = call_user_func($target_function, $instance); if (method_exists($target, $method)) { - $field = field_info_field_by_id($instance['field_id']); - $field_name = $field['field_name']; + $field = field_info_field_by_id($instance->field_id); + $field_name = $field->field_name; // Determine the list of languages to iterate on. $available_langcodes = field_available_languages($entity_type, $field); @@ -264,8 +264,8 @@ function field_invoke_method_multiple($method, $target_function, array $entities $entity_instances = _field_invoke_get_instances($entity_type, $entity->bundle(), $options); foreach ($entity_instances as $instance) { - $instance_id = $instance['id']; - $field_name = $instance['field_name']; + $instance_id = $instance->id; + $field_name = $instance->field_name; // Let the closure determine the target object on which the method should // be called. @@ -281,7 +281,7 @@ function field_invoke_method_multiple($method, $target_function, array $entities // Unless a language code suggestion is provided we iterate on all the // available language codes. - $field = field_info_field_by_id($instance['field_id']); + $field = field_info_field_by_id($instance->field_id); $available_langcodes = field_available_languages($entity_type, $field); $langcode = !empty($options['langcode'][$id]) ? $options['langcode'][$id] : $options['langcode']; $langcodes = _field_language_suggestion($available_langcodes, $langcode, $field_name); @@ -298,7 +298,7 @@ function field_invoke_method_multiple($method, $target_function, array $entities // For each instance, invoke the method and collect results. foreach ($instances as $instance_id => $instance) { - $field_name = $instance['field_name']; + $field_name = $instance->field_name; // Iterate over all the field translations. foreach ($grouped_items[$instance_id] as $langcode => &$items) { @@ -399,9 +399,9 @@ function _field_invoke($op, EntityInterface $entity, &$a = NULL, &$b = NULL, $op foreach ($instances as $instance) { // field_info_field() is not available for deleted fields, so use // field_info_field_by_id(). - $field = field_info_field_by_id($instance['field_id']); - $field_name = $field['field_name']; - $function = $options['default'] ? 'field_default_' . $op : $field['module'] . '_field_' . $op; + $field = field_info_field_by_id($instance->field_id); + $field_name = $field->field_name; + $function = $options['default'] ? 'field_default_' . $op : $field->module . '_field_' . $op; if (function_exists($function)) { // Determine the list of languages to iterate on. $available_langcodes = field_available_languages($entity->entityType(), $field); @@ -507,10 +507,10 @@ function _field_invoke_multiple($op, $entity_type, $entities, &$a = NULL, &$b = $id = $entity->id(); foreach ($instances as $instance) { - $field_id = $instance['field_id']; - $field_name = $instance['field_name']; + $field_id = $instance->field_id; + $field_name = $instance->field_name; $field = field_info_field_by_id($field_id); - $function = $options['default'] ? 'field_default_' . $op : $field['module'] . '_field_' . $op; + $function = $options['default'] ? 'field_default_' . $op : $field->module . '_field_' . $op; if (function_exists($function)) { // Add the field to the list of fields to invoke the hook on. if (!isset($fields[$field_id])) { @@ -538,8 +538,8 @@ function _field_invoke_multiple($op, $entity_type, $entities, &$a = NULL, &$b = // For each field, invoke the field hook and collect results. foreach ($fields as $field_id => $field) { - $field_name = $field['field_name']; - $function = $options['default'] ? 'field_default_' . $op : $field['module'] . '_field_' . $op; + $field_name = $field->field_name; + $function = $options['default'] ? 'field_default_' . $op : $field->module . '_field_' . $op; // Iterate over all the field translations. foreach ($grouped_items[$field_id] as $langcode => &$items) { $entities = $grouped_entities[$field_id][$langcode]; @@ -688,7 +688,7 @@ function _field_invoke_get_instances($entity_type, $bundle, $options) { // Single-field mode by field id: we need to loop on each instance to // find the right one. foreach ($instances as $instance) { - if ($instance['field_id'] == $options['field_id']) { + if ($instance->field_id == $options['field_id']) { $instances = array($instance); break; } @@ -785,8 +785,8 @@ function _field_invoke_widget_target() { * * // Only for multiple widgets: * '#entity_type' => The name of the entity type, - * '#bundle' => $instance['bundle'], - * '#columns' => array_keys($field['columns']), + * '#bundle' => $instance->bundle, + * '#columns' => array_keys($field->columns), * // The remaining elements in the sub-array depend on the widget. * '#type' => The type of the widget, * ... @@ -942,8 +942,8 @@ function field_attach_load($entity_type, $entities, $age = FIELD_LOAD_CURRENT, $ $id = $entity->id(); $vid = $entity->getRevisionId(); foreach ($instances as $instance) { - $field_name = $instance['field_name']; - $field_id = $instance['field_id']; + $field_name = $instance->field_name; + $field_id = $instance->field_id; // Make sure all fields are present at least as empty arrays. if (!isset($queried_entities[$id]->{$field_name})) { $queried_entities[$id]->{$field_name} = array(); @@ -951,7 +951,7 @@ function field_attach_load($entity_type, $entities, $age = FIELD_LOAD_CURRENT, $ // Collect the storage backend if the field has not been loaded yet. if (!isset($skip_fields[$field_id])) { $field = field_info_field_by_id($field_id); - $storages[$field['storage']['type']][$field_id][] = $load_current ? $id : $vid; + $storages[$field->storage['type']][$field_id][] = $load_current ? $id : $vid; } } } @@ -976,7 +976,7 @@ function field_attach_load($entity_type, $entities, $age = FIELD_LOAD_CURRENT, $ $data = array(); $instances = field_info_instances($entity_type, $entity->bundle()); foreach ($instances as $instance) { - $data[$instance['field_name']] = $queried_entities[$id]->{$instance['field_name']}; + $data[$instance->field_name] = $queried_entities[$id]->{$instance->field_name}; } $cid = "field:$entity_type:$id"; cache('field')->set($cid, $data); @@ -1169,13 +1169,13 @@ function field_attach_insert(EntityInterface $entity) { // Collect the storage backends used by the remaining fields in the entities. $storages = array(); foreach (field_info_instances($entity->entityType(), $entity->bundle()) as $instance) { - $field = field_info_field_by_id($instance['field_id']); - $field_id = $field['id']; - $field_name = $field['field_name']; + $field = field_info_field_by_id($instance->field_id); + $field_id = $field->id(); + $field_name = $field->field_name; if (!empty($entity->$field_name)) { // Collect the storage backend if the field has not been written yet. if (!isset($skip_fields[$field_id])) { - $storages[$field['storage']['type']][$field_id] = $field_id; + $storages[$field->storage['type']][$field_id] = $field_id; } } } @@ -1210,9 +1210,9 @@ function field_attach_update(EntityInterface $entity) { // Collect the storage backends used by the remaining fields in the entities. $storages = array(); foreach (field_info_instances($entity->entityType(), $entity->bundle()) as $instance) { - $field = field_info_field_by_id($instance['field_id']); - $field_id = $field['id']; - $field_name = $field['field_name']; + $field = field_info_field_by_id($instance->field_id); + $field_id = $field->id(); + $field_name = $field->field_name; // Leave the field untouched if $entity comes with no $field_name property, // but empty the field if it comes as a NULL value or an empty array. // Function property_exists() is slower, so we catch the more frequent @@ -1220,7 +1220,7 @@ function field_attach_update(EntityInterface $entity) { if (isset($entity->$field_name) || property_exists($entity, $field_name)) { // Collect the storage backend if the field has not been written yet. if (!isset($skip_fields[$field_id])) { - $storages[$field['storage']['type']][$field_id] = $field_id; + $storages[$field->storage['type']][$field_id] = $field_id; } } } @@ -1253,9 +1253,9 @@ function field_attach_delete(EntityInterface $entity) { // Collect the storage backends used by the fields in the entities. $storages = array(); foreach (field_info_instances($entity->entityType(), $entity->bundle()) as $instance) { - $field = field_info_field_by_id($instance['field_id']); - $field_id = $field['id']; - $storages[$field['storage']['type']][$field_id] = $field_id; + $field = field_info_field_by_id($instance->field_id); + $field_id = $field->id(); + $storages[$field->storage['type']][$field_id] = $field_id; } // Field storage backends delete their data. @@ -1286,9 +1286,9 @@ function field_attach_delete_revision(EntityInterface $entity) { // Collect the storage backends used by the fields in the entities. $storages = array(); foreach (field_info_instances($entity->entityType(), $entity->bundle()) as $instance) { - $field = field_info_field_by_id($instance['field_id']); - $field_id = $field['id']; - $storages[$field['storage']['type']][$field_id] = $field_id; + $field = field_info_field_by_id($instance->field_id); + $field_id = $field->id(); + $storages[$field->storage['type']][$field_id] = $field_id; } // Field storage backends delete their data. @@ -1359,8 +1359,8 @@ function field_attach_prepare_view($entity_type, array $entities, array $display // instance, call the prepareView() method on the formatter object handed by // the entity display. $target_function = function ($instance) use ($displays) { - if (isset($displays[$instance['bundle']])) { - return $displays[$instance['bundle']]->getFormatter($instance['field_name']); + if (isset($displays[$instance->bundle])) { + return $displays[$instance->bundle]->getFormatter($instance->field_name); } }; field_invoke_method_multiple('prepareView', $target_function, $prepare, $null, $null, $options); @@ -1423,7 +1423,7 @@ function field_attach_view(EntityInterface $entity, EntityDisplay $display, $lan // For each instance, call the view() method on the formatter object handed // by the entity display. $target_function = function ($instance) use ($display) { - return $display->getFormatter($instance['field_name']); + return $display->getFormatter($instance->field_name); }; $null = NULL; $output = field_invoke_method('view', $target_function, $entity, $null, $null, $options); @@ -1468,7 +1468,7 @@ function field_attach_preprocess(EntityInterface $entity, $element, &$variables) // Enable BC if necessary. $entity = $entity->getBCEntity(); foreach (field_info_instances($entity->entityType(), $entity->bundle()) as $instance) { - $field_name = $instance['field_name']; + $field_name = $instance->field_name; if (isset($element[$field_name]['#language'])) { $langcode = $element[$field_name]['#language']; $variables[$field_name] = isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : NULL; @@ -1549,11 +1549,18 @@ function field_attach_create_bundle($entity_type, $bundle) { * The new name of the bundle. */ function field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) { - db_update('field_config_instance') - ->fields(array('bundle' => $bundle_new)) - ->condition('entity_type', $entity_type) - ->condition('bundle', $bundle_old) - ->execute(); + $instances = field_read_instances(); + foreach ($instances as $id => $instance) { + if ($instance->entity_type == $entity_type && $instance->bundle == $bundle_old) { + $new_instance_id = $instance->entity_type . '.' . $bundle_new . '.' . $instance->field_name; + config('field.instance.' . $instance->entity_type . '.' . $bundle_old . '.' . $instance->field_name)->rename('field.instance.' . $new_instance_id); + config('field.instance.' . $instance->entity_type . '.' . $bundle_new . '.' . $instance->field_name) + ->set('data.bundle', $bundle_new) + ->set('id', $new_instance_id) + ->set('label', $new_instance_id) + ->save(); + } + } // Clear the cache. field_cache_clear(); @@ -1569,7 +1576,7 @@ function field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) { } /** - * Notifies field.module the a bundle was deleted. + * Notifies field.module that a bundle was deleted. * * 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 @@ -1582,14 +1589,18 @@ function field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) { * The entity type to which the bundle is bound. * @param $bundle * The bundle to delete. + * @param $field_cleanup + * If TRUE, the field will be deleted as well if its last instance is being + * deleted. If FALSE, it is the caller's responsibility to handle the case of + * fields left without instances. Defaults to TRUE. */ -function field_attach_delete_bundle($entity_type, $bundle) { +function field_attach_delete_bundle($entity_type, $bundle, $field_cleanup = TRUE) { // 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. $instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle), array('include_inactive' => 1)); foreach ($instances as $instance) { - field_delete_instance($instance); + field_delete_instance($instance, $field_cleanup); } // Clear the cache. diff --git a/core/modules/field/field.crud.inc b/core/modules/field/field.crud.inc index f6ee8a2..625de75 100644 --- a/core/modules/field/field.crud.inc +++ b/core/modules/field/field.crud.inc @@ -29,7 +29,7 @@ * field_create_instance() for that. * * @param $field - * A field definition array. The field_name and type properties are required. + * A field definition. The field_name and type properties are required. * Other properties, if omitted, will be given the following default values: * - cardinality: 1 * - locked: FALSE @@ -49,41 +49,48 @@ * hook_field_storage_info(). * * @return - * The $field array with the id property filled in. + * The $field entity. * * @throws Drupal\field\FieldException * * See: @link field Field API data structures @endlink. */ function field_create_field($field) { + + // Module developers can still pass in an array of properties + // into field_create_field(). + if (is_array($field)) { + $field = entity_create('field_entity', $field); + } + // Field name is required. - if (empty($field['field_name'])) { + if (empty($field->field_name)) { throw new FieldException('Attempt to create an unnamed field.'); } // Field type is required. - if (empty($field['type'])) { + if (empty($field->type)) { throw new FieldException('Attempt to create a field with no type.'); } // Field name cannot contain invalid characters. - if (!preg_match('/^[_a-z]+[_a-z0-9]*$/', $field['field_name'])) { + if (!preg_match('/^[_a-z]+[_a-z0-9]*$/', $field->field_name)) { throw new FieldException('Attempt to create a field with invalid characters. Only lowercase alphanumeric characters and underscores are allowed, and only lowercase letters and underscore are allowed as the first character'); } // Field name cannot be longer than 32 characters. We use drupal_strlen() // because the DB layer assumes that column widths are given in characters, // not bytes. - if (drupal_strlen($field['field_name']) > 32) { + if (drupal_strlen($field->field_name) > 32) { throw new FieldException(t('Attempt to create a field with a name longer than 32 characters: %name', - array('%name' => $field['field_name']))); + array('%name' => $field->field_name))); } // Ensure the field name is unique over active and disabled fields. // We do not care about deleted fields. - $prior_field = field_read_field($field['field_name'], array('include_inactive' => TRUE)); + $prior_field = field_read_field($field->field_name, array('include_inactive' => TRUE)); if (!empty($prior_field)) { - $message = $prior_field['active']? - t('Attempt to create field name %name which already exists and is active.', array('%name' => $field['field_name'])): - t('Attempt to create field name %name which already exists, although it is inactive.', array('%name' => $field['field_name'])); + $message = $prior_field->active ? + t('Attempt to create field name %name which already exists and is active.', array('%name' => $field->field_name)): + t('Attempt to create field name %name which already exists, although it is inactive.', array('%name' => $field->field_name)); throw new FieldException($message); } @@ -91,110 +98,97 @@ function field_create_field($field) { // collisions with existing entity properties, but some is better // than none. foreach (entity_get_info() as $type => $info) { - if (in_array($field['field_name'], $info['entity_keys'])) { - throw new FieldException(t('Attempt to create field name %name which is reserved by entity type %type.', array('%name' => $field['field_name'], '%type' => $type))); + if (in_array($field->field_name, $info['entity_keys'])) { + throw new FieldException(t('Attempt to create field name %name which is reserved by entity type %type.', array('%name' => $field->field_name, '%type' => $type))); } } - $field += array( + $defaults = array( 'entity_types' => array(), 'cardinality' => 1, 'translatable' => FALSE, - 'locked' => FALSE, 'settings' => array(), - 'storage' => array(), + 'bundles' => array(), + 'entity_types' => array(), + 'locked' => FALSE, 'deleted' => 0, + 'storage' => array(), + 'indexes' => array(), + 'foreign_keys' => array(), ); + foreach ($defaults as $key => $value) { + if (!isset($field->{$key})) { + $field->{$key} = $value; + } + } + // Check that the field type is known. - $field_type = field_info_field_types($field['type']); + $field_type = field_info_field_types($field->type); if (!$field_type) { - throw new FieldException(t('Attempt to create a field of unknown type %type.', array('%type' => $field['type']))); + throw new FieldException(t('Attempt to create a field of unknown type %type.', array('%type' => $field->type))); } // Create all per-field-type properties (needed here as long as we have // settings that impact column definitions). - $field['settings'] += field_info_field_settings($field['type']); - $field['module'] = $field_type['module']; - $field['active'] = 1; + $field->settings += field_info_field_settings($field->type); + $field->module = $field_type['module']; + $field->active = 1; // Provide default storage. - $field['storage'] += array( + $field->storage += array( 'type' => variable_get('field_storage_default', 'field_sql_storage'), 'settings' => array(), ); // Check that the storage type is known. - $storage_type = field_info_storage_types($field['storage']['type']); + $storage_type = field_info_storage_types($field->storage['type']); if (!$storage_type) { - throw new FieldException(t('Attempt to create a field with unknown storage type %type.', array('%type' => $field['storage']['type']))); + throw new FieldException(t('Attempt to create a field with unknown storage type %type.', array('%type' => $field->storage['type']))); } // Provide default storage settings. - $field['storage']['settings'] += field_info_storage_settings($field['storage']['type']); - $field['storage']['module'] = $storage_type['module']; - $field['storage']['active'] = 1; + $field->storage['settings'] += field_info_storage_settings($field->storage['type']); + $field->storage['module'] = $storage_type['module']; + $field->storage['active'] = 1; + $field->storage_type = $field->storage['type']; + $field->storage_module = $storage_type['module']; + $field->storage_active = 1; // Collect storage information. - module_load_install($field['module']); - $schema = (array) module_invoke($field['module'], 'field_schema', $field); + module_load_install($field->module); + $schema = (array) module_invoke($field->module, 'field_schema', $field); $schema += array('columns' => array(), 'indexes' => array(), 'foreign keys' => array()); // 'columns' are hardcoded in the field type. - $field['columns'] = $schema['columns']; - if (array_intersect(array_keys($field['columns']), field_reserved_columns())) { + $field->columns = $schema['columns']; + if (array_intersect(array_keys($field->columns), field_reserved_columns())) { throw new FieldException(t('Illegal field type columns.')); } // 'foreign keys' are hardcoded in the field type. - $field['foreign keys'] = $schema['foreign keys']; + $field->foreign_keys = $schema['foreign keys']; // 'indexes' can be both hardcoded in the field type, and specified in the // incoming $field definition. - $field += array( - 'indexes' => array(), - ); - $field['indexes'] += $schema['indexes']; + $field->indexes += $schema['indexes']; - // The serialized 'data' column contains everything from $field that does not - // have its own column and is not automatically populated when the field is - // read. - $data = $field; - unset($data['columns'], $data['field_name'], $data['type'], $data['active'], $data['module'], $data['storage_type'], $data['storage_active'], $data['storage_module'], $data['locked'], $data['cardinality'], $data['deleted']); // Additionally, do not save the 'bundles' property populated by // field_info_field(). - unset($data['bundles']); - - $record = array( - 'field_name' => $field['field_name'], - 'type' => $field['type'], - 'module' => $field['module'], - 'active' => $field['active'], - 'storage_type' => $field['storage']['type'], - 'storage_module' => $field['storage']['module'], - 'storage_active' => $field['storage']['active'], - 'locked' => $field['locked'], - 'data' => $data, - 'cardinality' => $field['cardinality'], - 'translatable' => $field['translatable'], - 'deleted' => $field['deleted'], - ); + $field->bundles = array(); - // Store the field and get the id back. - drupal_write_record('field_config', $record); - $field['id'] = $record['id']; + // Save the configuration. + $field->id = $field->field_name; + $field->save(); // Invoke hook_field_storage_create_field after the field is // complete (e.g. it has its id). try { - // Invoke hook_field_storage_create_field after - // drupal_write_record() sets the field id. + // Invoke hook_field_storage_create_field. module_invoke($storage_type['module'], 'field_storage_create_field', $field); } catch (Exception $e) { // If storage creation failed, remove the field_config record before // rethrowing the exception. - db_delete('field_config') - ->condition('id', $field['id']) - ->execute(); + entity_delete_multiple('field_entity', array($field->field_name)); throw $e; } // Clear caches - field_cache_clear(TRUE); + field_cache_clear(); // Invoke external hooks after the cache is cleared for API consistency. module_invoke_all('field_create_field', $field); @@ -213,7 +207,7 @@ function field_create_field($field) { * that cannot be updated. * * @param $field - * A field structure. $field['field_name'] must provided; it + * A field structure. $field->field_name must provided; it * identifies the field that will be updated to match this * structure. Any other properties of the field that are not * specified in $field will be left unchanged, so it is not @@ -224,41 +218,66 @@ function field_create_field($field) { * @see field_create_field() */ function field_update_field($field) { + + // Module developers can still pass in an array of properties + // into field_update_field(). + if (is_array($field)) { + $field_loaded = entity_load('field_entity', $field['field_name']); + if (empty($field_loaded)) { + throw new FieldException('Attempt to update a non-existent field.'); + } + // Override settings + // @todo should we make updating a field completely different ? + foreach ($field as $key => $value) { + if (is_array($value)) { + if ($key == 'indexes') { + $field_loaded->{$key} = $value; + } + else { + $field_loaded->{$key} += $value; + } + } + else { + $field_loaded->{$key} = $value; + } + } + $field = $field_loaded; + } + // Check that the specified field exists. - $prior_field = field_read_field($field['field_name']); + $prior_field = field_read_field($field->field_name); if (empty($prior_field)) { throw new FieldException('Attempt to update a non-existent field.'); } // Use the prior field values for anything not specifically set by the new - // field to be sure that all values are set. - $field += $prior_field; - $field['settings'] += $prior_field['settings']; + // field to be sure that all values are set. // @todo remove ? + $field->settings += $prior_field->settings; // Some updates are always disallowed. - if ($field['type'] != $prior_field['type']) { + if ($field->type != $prior_field->type) { throw new FieldException("Cannot change an existing field's type."); } - if ($field['entity_types'] != $prior_field['entity_types']) { + if ($field->entity_types != $prior_field->entity_types) { throw new FieldException("Cannot change an existing field's entity_types property."); } - if ($field['storage']['type'] != $prior_field['storage']['type']) { + if ($field->storage['type'] != $prior_field->storage['type']) { throw new FieldException("Cannot change an existing field's storage type."); } // Collect the new storage information, since what is in // $prior_field may no longer be right. - module_load_install($field['module']); - $schema = (array) module_invoke($field['module'], 'field_schema', $field); + module_load_install($field->module); + $schema = (array) module_invoke($field->module, 'field_schema', $field); $schema += array('columns' => array(), 'indexes' => array()); // 'columns' are hardcoded in the field type. - $field['columns'] = $schema['columns']; + $field->columns = $schema['columns']; // 'indexes' can be both hardcoded in the field type, and specified in the // incoming $field definition. - $field += array( - 'indexes' => array(), - ); - $field['indexes'] += $schema['indexes']; + if (!isset($field->indexes)) { + $field->indexes = array(); + } + $field->indexes += $schema['indexes']; $has_data = field_has_data($field); @@ -270,29 +289,21 @@ function field_update_field($field) { // Tell the storage engine to update the field. Do this before // saving the new definition since it still might fail. - $storage_type = field_info_storage_types($field['storage']['type']); + $storage_type = field_info_storage_types($field->storage['type']); module_invoke($storage_type['module'], 'field_storage_update_field', $field, $prior_field, $has_data); // Save the new field definition. @todo: refactor with // field_create_field. - // The serialized 'data' column contains everything from $field that does not - // have its own column and is not automatically populated when the field is - // read. - $data = $field; - unset($data['columns'], $data['field_name'], $data['type'], $data['locked'], $data['module'], $data['cardinality'], $data['active'], $data['deleted']); // Additionally, do not save the 'bundles' property populated by // field_info_field(). - unset($data['bundles']); + $field->bundles = array(); - $field['data'] = $data; - - // Store the field and create the id. - $primary_key = array('id'); - drupal_write_record('field_config', $field, $primary_key); + // Save the configuration. + $field->save(); // Clear caches - field_cache_clear(TRUE); + field_cache_clear(); // Invoke external hooks after the cache is cleared for API consistency. module_invoke_all('field_update_field', $field, $prior_field, $has_data); @@ -342,64 +353,49 @@ function field_read_field($field_name, $include_additional = array()) { * field ID, otherwise it is keyed by field name. */ function field_read_fields($params = array(), $include_additional = array()) { - $query = db_select('field_config', 'fc', array('fetch' => PDO::FETCH_ASSOC)); - $query->fields('fc'); - - // Turn the conditions into a query. - foreach ($params as $key => $value) { - // Allow filtering on the 'entity_type' and 'bundle' columns of the - // field_config_instance table. - if ($key == 'entity_type' || $key == 'bundle') { - if (empty($fci_join)) { - $fci_join = $query->join('field_config_instance', 'fci', 'fc.id = fci.field_id'); - } - $key = 'fci.' . $key; - } - else { - $key = 'fc.' . $key; - } + $fields = array(); - $query->condition($key, $value); + // Check if we need to retrieve deleted fields. + $include_deleted = (isset($include_additional['include_deleted']) && $include_additional['include_deleted']) || (isset($params['deleted']) && $params['deleted']); + if ($include_deleted) { + $deleted_fields = state()->get('field.field.deleted') ?: array(); } + // Add active and storage active parameters. if (!isset($include_additional['include_inactive']) || !$include_additional['include_inactive']) { - $query - ->condition('fc.active', 1) - ->condition('fc.storage_active', 1); + $params['active'] = 1; + $params['storage_active'] = 1; } - $include_deleted = (isset($include_additional['include_deleted']) && $include_additional['include_deleted']); - if (!$include_deleted) { - $query->condition('fc.deleted', 0); + + // Get configuration fields. + $config_fields = entity_load_multiple('field_entity'); + + // Merge deleted fields. + if (!empty($deleted_fields)) { + $config_fields += $deleted_fields; } - $fields = array(); - $results = $query->execute(); - foreach ($results as $record) { - $field = unserialize($record['data']); - $field['id'] = $record['id']; - $field['field_name'] = $record['field_name']; - $field['type'] = $record['type']; - $field['module'] = $record['module']; - $field['active'] = $record['active']; - $field['storage']['type'] = $record['storage_type']; - $field['storage']['module'] = $record['storage_module']; - $field['storage']['active'] = $record['storage_active']; - $field['locked'] = $record['locked']; - $field['cardinality'] = $record['cardinality']; - $field['translatable'] = $record['translatable']; - $field['deleted'] = $record['deleted']; + foreach ($config_fields as $field) { + // Conditions. + foreach ($params as $key => $value) { + if ($field->{$key} != $value) { + continue 2; + } + } + + // Invoke read field. module_invoke_all('field_read_field', $field); // Populate storage information. - module_load_install($field['module']); - $schema = (array) module_invoke($field['module'], 'field_schema', $field); + module_load_install($field->module); + $schema = (array) module_invoke($field->module, 'field_schema', $field); $schema += array('columns' => array(), 'indexes' => array()); - $field['columns'] = $schema['columns']; + $field->columns = $schema['columns']; - $field_name = $field['field_name']; + $field_name = $field->field_name; if ($include_deleted) { - $field_name = $field['id']; + $field_name = $field->uuid; } $fields[$field_name] = $field; } @@ -415,8 +411,9 @@ function field_read_fields($params = array(), $include_additional = array()) { function field_delete_field($field_name) { // Delete all non-deleted instances. $field = field_info_field($field_name); - if (isset($field['bundles'])) { - foreach ($field['bundles'] as $entity_type => $bundles) { + + if (isset($field->bundles)) { + foreach ($field->bundles as $entity_type => $bundles) { foreach ($bundles as $bundle) { $instance = field_info_instance($entity_type, $field_name, $bundle); field_delete_instance($instance, FALSE); @@ -425,16 +422,19 @@ function field_delete_field($field_name) { } // Mark field data for deletion. - module_invoke($field['storage']['module'], 'field_storage_delete_field', $field); + module_invoke($field->storage['module'], 'field_storage_delete_field', $field); - // Mark the field for deletion. - db_update('field_config') - ->fields(array('deleted' => 1)) - ->condition('field_name', $field_name) - ->execute(); + // Delete the configuration of this field and save the field configuration + // in the key_value table so we can use it later during field_purge_batch(). + // This makes sure a new field can be created immediately with the same name. + $deleted_fields = state()->get('field.field.deleted') ?: array(); + $field->deleted = TRUE; + $deleted_fields[$field->id()] = $field; + state()->set('field.field.deleted', $deleted_fields); + entity_delete_multiple('field_entity', array($field->field_name)); // Clear the cache. - field_cache_clear(TRUE); + field_cache_clear(); module_invoke_all('field_delete_field', $field); } @@ -474,25 +474,32 @@ function field_delete_field($field_name) { * * See: @link field Field API data structures @endlink. */ -function field_create_instance(&$instance) { - $field = field_read_field($instance['field_name']); +function field_create_instance($instance) { + + // Module developers can still pass in an array of properties + // into field_create_instance(). + if (is_array($instance)) { + $instance = entity_create('field_instance', $instance); + } + + $field = field_read_field($instance->field_name); if (empty($field)) { - throw new FieldException(t("Attempt to create an instance of a field @field_name that doesn't exist or is currently inactive.", array('@field_name' => $instance['field_name']))); + throw new FieldException(t("Attempt to create an instance of a field @field_name that doesn't exist or is currently inactive.", array('@field_name' => $instance->field_name))); } // Check that the required properties exists. - if (empty($instance['entity_type'])) { - throw new FieldException(t('Attempt to create an instance of field @field_name without an entity type.', array('@field_name' => $instance['field_name']))); + if (empty($instance->entity_type)) { + throw new FieldException(t('Attempt to create an instance of field @field_name without an entity type.', array('@field_name' => $instance->field_name))); } - if (empty($instance['bundle'])) { - throw new FieldException(t('Attempt to create an instance of field @field_name without a bundle.', array('@field_name' => $instance['field_name']))); + if (empty($instance->bundle)) { + throw new FieldException(t('Attempt to create an instance of field @field_name without a bundle.', array('@field_name' => $instance->field_name))); } // Check that the field can be attached to this entity type. - if (!empty($field['entity_types']) && !in_array($instance['entity_type'], $field['entity_types'])) { - throw new FieldException(t('Attempt to create an instance of field @field_name on forbidden entity type @entity_type.', array('@field_name' => $instance['field_name'], '@entity_type' => $instance['entity_type']))); + if (!empty($field->entity_types) && !in_array($instance->entity_type, $field->entity_types)) { + throw new FieldException(t('Attempt to create an instance of field @field_name on forbidden entity type @entity_type.', array('@field_name' => $instance->field_name, '@entity_type' => $instance->entity_type))); } // Set the field id. - $instance['field_id'] = $field['id']; + $instance->field_id = $field->uuid; // Note that we do *not* prevent creating a field on non-existing bundles, // because that would break the 'Body as field' upgrade for contrib @@ -507,9 +514,9 @@ function field_create_instance(&$instance) { // Ensure the field instance is unique within the bundle. // We only check for instances of active fields, since adding an instance of // a disabled field is not supported. - $prior_instance = field_read_instance($instance['entity_type'], $instance['field_name'], $instance['bundle']); + $prior_instance = field_read_instance($instance->entity_type, $instance->field_name, $instance->bundle); if (!empty($prior_instance)) { - $message = t('Attempt to create an instance of field @field_name on bundle @bundle that already has an instance of that field.', array('@field_name' => $instance['field_name'], '@bundle' => $instance['bundle'])); + $message = t('Attempt to create an instance of field @field_name on bundle @bundle that already has an instance of that field.', array('@field_name' => $instance->field_name, '@bundle' => $instance->bundle)); throw new FieldException($message); } @@ -542,23 +549,41 @@ function field_create_instance(&$instance) { * @see field_create_instance() */ function field_update_instance($instance) { + + // Module developers can still pass in an array of properties + // into field_update_instance(). + if (is_array($instance)) { + $instance_loaded = entity_load('field_entity', $field['field_name']); + if (empty($instance_loaded)) { + throw new FieldException('Attempt to update a non-existent instance.'); + } + // Override settings + // @todo should we make updating a instance completely different ? + foreach ($instance as $key => $value) { + if (is_array($value)) { + $instance_loaded->{$key} += $value; + } + else { + $instance_loaded->{$key} = $value; + } + } + $instance = $instance_loaded; + } + // Check that the specified field exists. - $field = field_read_field($instance['field_name']); + $field = field_read_field($instance->field_name); if (empty($field)) { - throw new FieldException(t('Attempt to update an instance of a nonexistent field @field.', array('@field' => $instance['field_name']))); + throw new FieldException(t('Attempt to update an instance of a nonexistent field @field.', array('@field' => $instance->field_name))); } // Check that the field instance exists (even if it is inactive, since we // want to be able to replace inactive widgets with new ones). - $prior_instance = field_read_instance($instance['entity_type'], $instance['field_name'], $instance['bundle'], array('include_inactive' => TRUE)); + $prior_instance = field_read_instance($instance->entity_type, $instance->field_name, $instance->bundle, array('include_inactive' => TRUE)); if (empty($prior_instance)) { - throw new FieldException(t("Attempt to update an instance of field @field on bundle @bundle that doesn't exist.", array('@field' => $instance['field_name'], '@bundle' => $instance['bundle']))); + throw new FieldException(t("Attempt to update an instance of field @field on bundle @bundle that doesn't exist.", array('@field' => $instance->field_name, '@bundle' => $instance->bundle))); } - $instance['id'] = $prior_instance['id']; - $instance['field_id'] = $prior_instance['field_id']; - - _field_write_instance($instance, TRUE); + _field_write_instance($instance); // Clear caches. field_cache_clear(); @@ -571,74 +596,51 @@ function field_update_instance($instance) { * * @param $instance * An instance structure. - * @param $update - * Whether this is a new or existing instance. */ -function _field_write_instance(&$instance, $update = FALSE) { - $field = field_read_field($instance['field_name']); - $field_type = field_info_field_types($field['type']); - - // Temporary workaround to allow incoming $instance as arrays or classed - // objects. - // @todo remove once the external APIs have been converted to use - // FieldInstance objects. - if (is_object($instance) && get_class($instance) == 'Drupal\field\FieldInstance') { - $instance = $instance->getArray(); - } +function _field_write_instance($instance) { + $field = field_read_field($instance->field_name); + $field_type = field_info_field_types($field->type); // Set defaults. - $instance += array( + $defaults = array( 'settings' => array(), - 'widget' => array(), + 'widget_settings' => array(), 'required' => FALSE, - 'label' => $instance['field_name'], + 'label' => $instance->field_name, 'description' => '', 'deleted' => 0, ); + // @todo check if these are set by entity_create() (I guess so). + foreach ($defaults as $key => $value) { + if (!isset($instance->{$key})) { + $instance->{$key} = $value; + } + } // Set default instance settings. - $instance['settings'] += field_info_instance_settings($field['type']); - + $instance->settings += field_info_instance_settings($field->type); // Set default widget and settings. - $instance['widget'] += array( + $instance->widget_settings += array( // TODO: what if no 'default_widget' specified ? 'type' => $field_type['default_widget'], 'settings' => array(), ); // If no weight specified, make sure the field sinks at the bottom. - if (!isset($instance['widget']['weight'])) { - $max_weight = field_info_max_weight($instance['entity_type'], $instance['bundle'], 'form'); - $instance['widget']['weight'] = isset($max_weight) ? $max_weight + 1 : 0; + if (!isset($instance->widget_settings['weight'])) { + $max_weight = field_info_max_weight($instance->entity_type, $instance->bundle, 'form'); + $instance->widget_settings['weight'] = isset($max_weight) ? $max_weight + 1 : 0; } // Check widget module. - $widget_type = field_info_widget_types($instance['widget']['type']); - $instance['widget']['module'] = $widget_type['module']; - $instance['widget']['settings'] += field_info_widget_settings($instance['widget']['type']); - - // The serialized 'data' column contains everything from $instance that does - // not have its own column and is not automatically populated when the - // instance is read. - $data = $instance; - unset($data['id'], $data['field_id'], $data['field_name'], $data['entity_type'], $data['bundle'], $data['deleted']); - - $record = array( - 'field_id' => $instance['field_id'], - 'field_name' => $instance['field_name'], - 'entity_type' => $instance['entity_type'], - 'bundle' => $instance['bundle'], - 'data' => $data, - 'deleted' => $instance['deleted'], - ); - // We need to tell drupal_update_record() the primary keys to trigger an - // update. - if ($update) { - $record['id'] = $instance['id']; - drupal_write_record('field_config_instance', $record, array('id')); - } - else { - drupal_write_record('field_config_instance', $record); - $instance['id'] = $record['id']; + $widget_type = field_info_widget_types($instance->widget_settings['type']); + $instance->widget_settings['module'] = $widget_type['module']; + $instance->widget_settings['settings'] += field_info_widget_settings($instance->widget_settings['type']); + + if (!isset($instance->id)) { + $instance->id = $instance->entity_type . '.' . $instance->bundle . '.' . $instance->field_name; } + + // Save into config. + $instance->save(); } /** @@ -673,8 +675,8 @@ function field_read_instance($entity_type, $field_name, $bundle, $include_additi * * @param $param * An array of properties to use in selecting a field instance. Valid keys - * include any column of the field_config_instance table. If NULL, all - * instances will be returned. + * include any property of the instance config object, except for data. + * If NULL, all instances will be returned. * @param $include_additional * The default behavior of this function is to not return field instances that * have been marked deleted, or whose field is inactive. Setting @@ -685,44 +687,66 @@ function field_read_instance($entity_type, $field_name, $bundle, $include_additi * An array of instances matching the arguments. */ function field_read_instances($params = array(), $include_additional = array()) { - $include_inactive = isset($include_additional['include_inactive']) && $include_additional['include_inactive']; - $include_deleted = isset($include_additional['include_deleted']) && $include_additional['include_deleted']; + $instances = array(); - $query = db_select('field_config_instance', 'fci', array('fetch' => PDO::FETCH_ASSOC)); - $query->join('field_config', 'fc', 'fc.id = fci.field_id'); - $query->fields('fci'); + $deleted_fields = state()->get('field.field.deleted'); + $include_inactive = isset($include_additional['include_inactive']) && $include_additional['include_inactive']; + $include_deleted = (isset($include_additional['include_deleted']) && $include_additional['include_deleted']) || (isset($params['deleted']) && $params['deleted']); - // Turn the conditions into a query. - foreach ($params as $key => $value) { - $query->condition('fci.' . $key, $value); + if ($include_deleted) { + $deleted_instances = state()->get('field.instance.deleted') ?: array(); } - if (!$include_inactive) { - $query - ->condition('fc.active', 1) - ->condition('fc.storage_active', 1); - } - if (!$include_deleted) { - $query->condition('fc.deleted', 0); - $query->condition('fci.deleted', 0); + + // Get configuration instances. + $config_instances = entity_load_multiple('field_instance'); + // Merge deleted instances. + if (!empty($deleted_instances)) { + $config_instances += $deleted_instances; } - $instances = array(); - $results = $query->execute(); + foreach ($config_instances as $instance) { + + $entity_info = entity_get_info($instance->entity_type); + + // Get data from the field. If the field is marked as deleted, we + // need to get it from the state storage. + $field = entity_load('field_entity', $instance->field_name); + if ($include_deleted) { + if (empty($field) && isset($deleted_fields[$instance->field_id])) { + $field = $deleted_fields[$instance->field_id]; + } + } + if (empty($field)) { + continue; + } + + // @todo do we really need this ? + $instance->active = $field->active; + $instance->locked = $field->locked; + $instance->type = $field->type; + $instance->module = $field->module; + $instance->storage_type = $field->storage_type; + $instance->storage_active = $field->storage_active; + $instance->storage_module = $field->storage_module; - foreach ($results as $record) { - // Filter out instances on unknown entity types (for instance because the - // module exposing them was disabled). - $entity_info = entity_get_info($record['entity_type']); if ($include_inactive || $entity_info) { - $instance = unserialize($record['data']); - $instance['id'] = $record['id']; - $instance['field_id'] = $record['field_id']; - $instance['field_name'] = $record['field_name']; - $instance['entity_type'] = $record['entity_type']; - $instance['bundle'] = $record['bundle']; - $instance['deleted'] = $record['deleted']; + // Conditions. + if (!$include_inactive) { + $params['active'] = 1; + $params['storage_active'] = 1; + } + if (!$include_deleted) { + $params['deleted'] = 0; + } + foreach ($params as $key => $value) { + if ($instance->{$key} != $value) { + continue 2; + } + } + // Invoke read instance. module_invoke_all('field_read_instance', $instance); + $instances[] = $instance; } } @@ -740,27 +764,28 @@ function field_read_instances($params = array(), $include_additional = array()) * fields left without instances. Defaults to TRUE. */ function field_delete_instance($instance, $field_cleanup = TRUE) { - // Mark the field instance for deletion. - db_update('field_config_instance') - ->fields(array('deleted' => 1)) - ->condition('field_name', $instance['field_name']) - ->condition('entity_type', $instance['entity_type']) - ->condition('bundle', $instance['bundle']) - ->execute(); + + // Delete the configuration of this instance and save the configuration + // in the key_value table so we can use it later during field_purge_batch(). + $deleted_instances = state()->get('field.instance.deleted') ?: array(); + $instance['deleted'] = TRUE; + $deleted_instances[$instance['id']] = $instance; + state()->set('field.instance.deleted', $deleted_instances); + entity_load('field_instance', $instance->entity_type . '.' . $instance->bundle . '.' . $instance->field_name)->delete(); // Clear the cache. field_cache_clear(); // Mark instance data for deletion. - $field = field_info_field($instance['field_name']); - module_invoke($field['storage']['module'], 'field_storage_delete_instance', $instance); + $field = field_info_field($instance->field_name); + module_invoke($field->storage['module'], 'field_storage_delete_instance', $instance); // Let modules react to the deletion of the instance. module_invoke_all('field_delete_instance', $instance); // Delete the field itself if we just deleted its last instance. - if ($field_cleanup && count($field['bundles']) == 0) { - field_delete_field($field['field_name']); + if ($field_cleanup && count($field->bundles) == 0) { + field_delete_field($field->field_name); } } @@ -857,16 +882,16 @@ function field_purge_batch($batch_size) { $factory = drupal_container()->get('entity.query'); $info = entity_get_info(); foreach ($instances as $instance) { - $entity_type = $instance['entity_type']; + $entity_type = $instance->entity_type; $ids = (object) array( 'entity_type' => $entity_type, - 'bundle' => $instance['bundle'], + 'bundle' => $instance->bundle, ); // field_purge_data() will need the field array. - $field = field_info_field_by_id($instance['field_id']); + $field = field_info_field_by_id($instance->field_id); // Retrieve some entities. $query = $factory->get($entity_type) - ->condition('id:' . $field['id'] . '.deleted', 1) + ->condition('id:' . $field->id() . '.deleted', 1) ->range(0, $batch_size); // If there's no bundle key, all results will have the same bundle. if (!empty($info[$entity_type]['entity_keys']['bundle'])) { @@ -884,7 +909,7 @@ function field_purge_batch($batch_size) { $ids->entity_id = $entity_id; $entities[$entity_id] = _field_create_entity_from_ids($ids); } - field_attach_load($entity_type, $entities, FIELD_LOAD_CURRENT, array('field_id' => $field['id'], 'deleted' => 1)); + field_attach_load($entity_type, $entities, FIELD_LOAD_CURRENT, array('field_id' => $field->id(), 'deleted' => 1)); foreach ($entities as $entity) { // Purge the data for the entity. field_purge_data($entity, $field, $instance); @@ -897,9 +922,9 @@ function field_purge_batch($batch_size) { } // Retrieve all deleted fields. Any that have no instances can be purged. - $fields = field_read_fields(array('deleted' => 1), array('include_deleted' => 1)); - foreach ($fields as $field) { - $instances = field_read_instances(array('field_id' => $field['id']), array('include_deleted' => 1)); + $deleted_fields = state()->get('field.field.deleted') ?: array(); + foreach ($deleted_fields as $field) { + $instances = field_read_instances(array('field_id' => $field->id()), array('include_deleted' => 1)); if (empty($instances)) { field_purge_field($field); } @@ -923,11 +948,11 @@ function field_purge_batch($batch_size) { function field_purge_data(EntityInterface $entity, $field, $instance) { // Each field type's hook_field_delete() only expects to operate on a single // field at a time, so we can use it as-is for purging. - $options = array('field_id' => $instance['field_id'], 'deleted' => TRUE); + $options = array('field_id' => $instance->field_id, 'deleted' => TRUE); _field_invoke('delete', $entity, $dummy, $dummy, $options); // Tell the field storage system to purge the data. - module_invoke($field['storage']['module'], 'field_storage_purge', $entity, $field, $instance); + module_invoke($field->storage['module'], 'field_storage_purge', $entity, $field, $instance); // Let other modules act on purging the data. foreach (module_implements('field_attach_purge') as $module) { @@ -946,13 +971,13 @@ function field_purge_data(EntityInterface $entity, $field, $instance) { * The instance record to purge. */ function field_purge_instance($instance) { - db_delete('field_config_instance') - ->condition('id', $instance['id']) - ->execute(); - // Notify the storage engine. - $field = field_info_field_by_id($instance['field_id']); - module_invoke($field['storage']['module'], 'field_storage_purge_instance', $instance); + $field = field_info_field_by_id($instance->field_id); + module_invoke($field->storage['module'], 'field_storage_purge_instance', $instance); + + $deleted_instances = state()->get('field.instance.deleted'); + unset($deleted_instances[$instance['id']]); + state()->set('field.instance.deleted', $deleted_instances); // Clear the cache. field_info_cache_clear(); @@ -971,17 +996,17 @@ function field_purge_instance($instance) { * The field record to purge. */ function field_purge_field($field) { - $instances = field_read_instances(array('field_id' => $field['id']), array('include_deleted' => 1)); + $instances = field_read_instances(array('field_id' => $field->id()), array('include_deleted' => 1)); if (count($instances) > 0) { - throw new FieldException(t('Attempt to purge a field @field_name that still has instances.', array('@field_name' => $field['field_name']))); + throw new FieldException(t('Attempt to purge a field @field_name that still has instances.', array('@field_name' => $field->field_name))); } - db_delete('field_config') - ->condition('id', $field['id']) - ->execute(); + $deleted_fields = state()->get('field.field.deleted'); + unset($deleted_fields[$field->id()]); + state()->set('field.field.deleted', $deleted_fields); // Notify the storage engine. - module_invoke($field['storage']['module'], 'field_storage_purge_field', $field); + module_invoke($field->storage['module'], 'field_storage_purge_field', $field); // Clear the cache. field_info_cache_clear(); diff --git a/core/modules/field/field.default.inc b/core/modules/field/field.default.inc index 27ba32c..9c1301e 100644 --- a/core/modules/field/field.default.inc +++ b/core/modules/field/field.default.inc @@ -29,7 +29,7 @@ * @param $langcode * The language associated with $items. * @param $items - * $entity->{$field['field_name']}[$langcode], or an empty array if unset. + * $entity->{$field->field_name}[$langcode], or an empty array if unset. * @param $errors * The array of errors, keyed by field name and by value delta, that have * already been reported for the entity. The function should add its errors to @@ -45,10 +45,10 @@ function field_default_validate(EntityInterface $entity, $field, $instance, $lan // Check that the number of values doesn't exceed the field cardinality. // For form submitted values, this can only happen with 'multiple value' // widgets. - if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && count($items) > $field['cardinality']) { - $errors[$field['field_name']][$langcode][0][] = array( + if ($field->cardinality != FIELD_CARDINALITY_UNLIMITED && count($items) > $field->cardinality) { + $errors[$field->field_name][$langcode][0][] = array( 'error' => 'field_cardinality', - 'message' => t('%name: this field cannot hold more than @count values.', array('%name' => $instance['label'], '@count' => $field['cardinality'])), + 'message' => t('%name: this field cannot hold more than @count values.', array('%name' => $instance->label, '@count' => $field->cardinality)), ); } } @@ -65,14 +65,14 @@ function field_default_validate(EntityInterface $entity, $field, $instance, $lan * @param $langcode * The language the entity has to be translated to. * @param $items - * $entity->{$field['field_name']}[$langcode], or an empty array if unset. + * $entity->{$field->field_name}[$langcode], or an empty array if unset. * @param \Drupal\Core\Entity\EntityInterface $source_entity * The source entity holding the field values to be translated. * @param $source_langcode * The source language from which to translate. */ function field_default_prepare_translation(EntityInterface $entity, $field, $instance, $langcode, &$items, EntityInterface $source_entity, $source_langcode) { - $field_name = $field['field_name']; + $field_name = $field->field_name; // If the field is untranslatable keep using LANGUAGE_NOT_SPECIFIED. if ($langcode == LANGUAGE_NOT_SPECIFIED) { $source_langcode = LANGUAGE_NOT_SPECIFIED; diff --git a/core/modules/field/field.form.inc b/core/modules/field/field.form.inc index 203a112..7ac7ef1 100644 --- a/core/modules/field/field.form.inc +++ b/core/modules/field/field.form.inc @@ -159,7 +159,7 @@ function field_add_more_js($form, $form_state) { $field_state = field_form_get_state($parents, $field_name, $langcode, $form_state); $field = $field_state['field']; - if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED) { + if ($field->cardinality != FIELD_CARDINALITY_UNLIMITED) { return; } diff --git a/core/modules/field/field.info b/core/modules/field/field.info index c74da0e..3c6c5fb 100644 --- a/core/modules/field/field.info +++ b/core/modules/field/field.info @@ -3,5 +3,4 @@ description = Field API to add fields to entities like nodes and users. package = Core version = VERSION core = 8.x -dependencies[] = field_sql_storage required = TRUE diff --git a/core/modules/field/field.info.inc b/core/modules/field/field.info.inc index b422b47..44a7c42 100644 --- a/core/modules/field/field.info.inc +++ b/core/modules/field/field.info.inc @@ -5,7 +5,7 @@ * Field Info API, providing information about available fields and field types. */ -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; use Drupal\field\FieldInfo; /** @@ -166,7 +166,7 @@ function _field_info_collate_types_reset() { * - FIELD_BEHAVIOR_DEFAULT: Use field.module default behavior. */ function field_behaviors_widget($op, $instance) { - $info = field_info_widget_types($instance['widget']['type']); + $info = field_info_widget_types($instance->widget_settings['type']); return isset($info[$op]) ? $info[$op] : FIELD_BEHAVIOR_DEFAULT; } @@ -299,8 +299,8 @@ function field_info_fields() { $fields = array(); foreach ($info as $key => $field) { - if (!$field['deleted']) { - $fields[$field['field_name']] = $field; + if (!$field->deleted) { + $fields[$field->field_name] = $field; } } @@ -516,7 +516,7 @@ function field_info_max_weight($entity_type, $bundle) { // Collect weights for fields. foreach (field_info_instances($entity_type, $bundle) as $instance) { - $weights[] = $instance['widget']['weight']; + $weights[] = $instance->widget_settings['weight']; } // Collect weights for extra fields. foreach (field_info_extra_fields($entity_type, $bundle, 'form') as $extra) { diff --git a/core/modules/field/field.install b/core/modules/field/field.install index 695fd89..17d0d7c 100644 --- a/core/modules/field/field.install +++ b/core/modules/field/field.install @@ -9,158 +9,7 @@ * Implements hook_schema(). */ function field_schema() { - // Static (meta) tables. - $schema['field_config'] = array( - 'fields' => array( - 'id' => array( - 'type' => 'serial', - 'not null' => TRUE, - 'description' => 'The primary identifier for a field', - ), - 'field_name' => array( - 'type' => 'varchar', - 'length' => 32, - 'not null' => TRUE, - 'description' => 'The name of this field. Non-deleted field names are unique, but multiple deleted fields can have the same name.', - ), - 'type' => array( - 'type' => 'varchar', - 'length' => 128, - 'not null' => TRUE, - 'description' => 'The type of this field.', - ), - 'module' => array( - 'type' => 'varchar', - 'length' => 128, - 'not null' => TRUE, - 'default' => '', - 'description' => 'The module that implements the field type.', - ), - 'active' => array( - 'type' => 'int', - 'size' => 'tiny', - 'not null' => TRUE, - 'default' => 0, - 'description' => 'Boolean indicating whether the module that implements the field type is enabled.', - ), - 'storage_type' => array( - 'type' => 'varchar', - 'length' => 128, - 'not null' => TRUE, - 'description' => 'The storage backend for the field.', - ), - 'storage_module' => array( - 'type' => 'varchar', - 'length' => 128, - 'not null' => TRUE, - 'default' => '', - 'description' => 'The module that implements the storage backend.', - ), - 'storage_active' => array( - 'type' => 'int', - 'size' => 'tiny', - 'not null' => TRUE, - 'default' => 0, - 'description' => 'Boolean indicating whether the module that implements the storage backend is enabled.', - ), - 'locked' => array( - 'type' => 'int', - 'size' => 'tiny', - 'not null' => TRUE, - 'default' => 0, - 'description' => '@TODO', - ), - 'data' => array( - 'type' => 'blob', - 'size' => 'big', - 'not null' => TRUE, - 'serialize' => TRUE, - 'description' => 'Serialized data containing the field properties that do not warrant a dedicated column.', - ), - 'cardinality' => array( - 'type' => 'int', - 'size' => 'tiny', - 'not null' => TRUE, - 'default' => 0, - ), - 'translatable' => array( - 'type' => 'int', - 'size' => 'tiny', - 'not null' => TRUE, - 'default' => 0, - ), - 'deleted' => array( - 'type' => 'int', - 'size' => 'tiny', - 'not null' => TRUE, - 'default' => 0, - ), - ), - 'primary key' => array('id'), - 'indexes' => array( - 'field_name' => array('field_name'), - // Used by field_read_fields(). - 'active' => array('active'), - 'storage_active' => array('storage_active'), - 'deleted' => array('deleted'), - // Used by field_sync_field_status(). - 'module' => array('module'), - 'storage_module' => array('storage_module'), - 'type' => array('type'), - 'storage_type' => array('storage_type'), - ), - ); - $schema['field_config_instance'] = array( - 'fields' => array( - 'id' => array( - 'type' => 'serial', - 'not null' => TRUE, - 'description' => 'The primary identifier for a field instance', - ), - 'field_id' => array( - 'type' => 'int', - 'not null' => TRUE, - 'description' => 'The identifier of the field attached by this instance', - ), - 'field_name' => array( - 'type' => 'varchar', - 'length' => 32, - 'not null' => TRUE, - 'default' => '' - ), - 'entity_type' => array( - 'type' => 'varchar', - 'length' => 32, - 'not null' => TRUE, - 'default' => '' - ), - 'bundle' => array( - 'type' => 'varchar', - 'length' => 128, - 'not null' => TRUE, - 'default' => '' - ), - 'data' => array( - 'type' => 'blob', - 'size' => 'big', - 'not null' => TRUE, - 'serialize' => TRUE, - ), - 'deleted' => array( - 'type' => 'int', - 'size' => 'tiny', - 'not null' => TRUE, - 'default' => 0, - ), - ), - 'primary key' => array('id'), - 'indexes' => array( - // Used by field_delete_instance(). - 'field_name_bundle' => array('field_name', 'entity_type', 'bundle'), - // Used by field_read_instances(). - 'deleted' => array('deleted'), - ), - ); + $schema['cache_field'] = drupal_get_schema_unprocessed('system', 'cache'); $schema['cache_field']['description'] = 'Cache table for the Field module to store already built field informations.'; @@ -172,7 +21,7 @@ function field_schema() { * * @ingroup update_api */ -function _update_7000_field_create_field(&$field) { +function _update_7000_field_create_field($field) { // Merge in default values.` $field += array( 'entity_types' => array(), @@ -229,12 +78,25 @@ function _update_7000_field_create_field(&$field) { 'deleted' => (int) $field['deleted'], ); // We don't use drupal_write_record() here because it depends on the schema. - $field['id'] = db_insert('field_config') + $record['id'] = db_insert('field_config') ->fields($record) ->execute(); - // Create storage for the field. - field_sql_storage_field_storage_create_field($field); + // Extra keys - @todo clean this up. + $record['settings'] = $data['settings']; + $record['columns'] = $field['columns']; + $record['indexes'] = isset($data['indexes']) ? $data['indexes'] : array(); + if (!isset($record['foreign_keys'])) { + $record['foreign_keys'] = array(); + } + if (isset($data['foreign keys'])) { + $record['foreign_keys'] += $data['foreign keys']; + } + $record['storage'] = $data['storage']; + unset($record['data']); + field_sql_storage_field_storage_create_field((object) $record); + + return $record; } /** @@ -271,7 +133,6 @@ function _update_7000_field_delete_field($field_name) { ->execute(); } - /** * Deletes an instance and all its data of a field stored in SQL Storage. * @@ -364,10 +225,10 @@ function _update_7000_field_create_instance($field, &$instance) { unset($data['id'], $data['field_id'], $data['field_name'], $data['entity_type'], $data['bundle'], $data['deleted']); $record = array( - 'field_id' => $instance['field_id'], - 'field_name' => $instance['field_name'], - 'entity_type' => $instance['entity_type'], - 'bundle' => $instance['bundle'], + 'field_id' => $instance->field_id, + 'field_name' => $instance->field_name, + 'entity_type' => $instance->entity_type, + 'bundle' => $instance->bundle, 'data' => serialize($data), 'deleted' => (int) $instance['deleted'], ); @@ -382,15 +243,14 @@ function _update_7000_field_create_instance($field, &$instance) { */ /** - * Reassign all list.module fields to be controlled by options.module. + * Implements hook_update_dependencies(). */ -function field_update_8001() { - db_update('field_config') - ->fields(array( - 'module' => 'options', - )) - ->condition('module', 'list') - ->execute(); +function field_update_dependencies() { + // Convert to config after SQL storage has been updated. + $dependencies['field_sql_storage'][8000] = array( + 'field' => 8002, + ); + return $dependencies; } /** @@ -398,7 +258,7 @@ function field_update_8001() { * * @ingroup config_upgrade */ -function field_update_8002() { +function field_update_8001() { $displays = array(); module_load_install('entity'); @@ -442,7 +302,7 @@ function field_update_8002() { // Migration of 'extra_fields' display settings. Avoid calling // entity_get_info() by fetching the relevant variables directly in the - // cariables table. + // variable table. $variables = array_map('unserialize', db_query("SELECT name, value FROM {variable} WHERE name LIKE '%field_bundle_settings_%'")->fetchAllKeyed()); foreach ($variables as $variable_name => $variable_value) { if (preg_match('/field_bundle_settings_(.*)__(.*)/', $variable_name, $matches)) { @@ -484,6 +344,103 @@ function field_update_8002() { } /** + * Convert fields and instances to config. + */ +function field_update_8002() { + $manifest_ids = array('fields' => array(), 'instances' => array()); + + $deleted_fields = state()->get('field.field.deleted') ?: array(); + $deleted_instances = state()->get('field.instance.deleted') ?: array(); + + $field_ids = array(); + $fields = db_query("SELECT * FROM {field_config}")->fetchAll(PDO::FETCH_ASSOC); + foreach ($fields as $field) { + $field['data'] = unserialize($field['data']); + + // Keep old id for the field. + $old_id = $field['id']; + $field['id'] = $field['field_name']; + $field['settings'] = $field['data']['settings']; + + // @todo clean this up. + $field['storage'] = $field['data']['storage']; + if (!isset($field['indexes'])) { + $field['indexes'] = array(); + } + $field['foreign_keys'] = array(); + if (isset($field['data']['foreign keys'])) { + $field['foreign_keys'] += $field['data']['foreign keys']; + } + $schema = (array) module_invoke($field['module'], 'field_schema', (object) $field); + $schema += array('columns' => array(), 'indexes' => array()); + // 'columns' are hardcoded in the field type. + $field['columns'] = $schema['columns']; + // 'indexes' can be both hardcoded in the field type, and specified in the + // incoming $field definition. + $field['indexes'] += $schema['indexes']; + + // Remove data key. + unset($field['data']); + + // Reassign all list.module fields to be controlled by options.module. + if ($field['module'] == 'list') { + $field['module'] = 'options'; + } + + if (!$field['deleted']) { + $e_field = entity_create('field_entity', $field); + $e_field->save(); + $manifest_ids['fields'][] = $field['field_name']; + } + else { + $deleted_fields[$field->field_name] = $field; + } + $field_ids[$old_id] = array( + 'id' => $e_field->id(), + ); + } + + $instances = db_query("SELECT * FROM {field_config_instance}")->fetchAll(PDO::FETCH_ASSOC); + foreach ($instances as $instance) { + $instance += unserialize($instance['data']); + unset($instance['data']); + + // Map old field id to new UUID. + $old_id = $instance['field_id']; + $instance['field_id'] = $field_ids[$old_id]['id']; + + if (!$instance['deleted']) { + $field_instance = entity_create('field_instance', $instance)->save(); + /*array( + 'id' => $instance->entity_type . '.' . $instance->bundle . '.' . $instance->field_name, + 'label' => $instance->entity_type . '.' . $instance->bundle . '.' . $instance->field_name, + 'data' => $instance, + ))->save();*/ + $manifest_ids['instances'][] = $instance->entity_type . '.' . $instance->bundle . '.' . $instance->field_name; + } + else { + $deleted_instances[$instance['id']] = $instance; + } + + // Update file_usage table in case this instance has a default image. + if (!empty($instance->settings['default_image'])) { + db_update('file_usage') + ->fields(array('id' => $instance->field_id)) + ->condition('fid', $instance->settings['default_image']) + ->execute(); + } + } + + // Create the manifest files. + update_config_manifest_add('field.field', $manifest_ids['fields']); + update_config_manifest_add('field.instance', $manifest_ids['instances']); + + // Save the deleted fields and instances in state. + state()->set('field.field.deleted', $deleted_fields); + state()->set('field.instance.deleted', $deleted_instances); +} + +/** * @} 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 36bd18a..8d57768 100644 --- a/core/modules/field/field.module +++ b/core/modules/field/field.module @@ -334,7 +334,9 @@ function field_cron() { * required if there are any active fields of that type. */ function field_system_info_alter(&$info, $file, $type) { - if ($type == 'module' && module_hook($file->name, 'field_info')) { + // During a Drupal 7 to Drupal 8 upgrade it is only safe to call + // field_read_fields() once field_update_8002() has finished. + if ($type == 'module' && module_hook($file->name, 'field_info') && drupal_get_installed_schema_version('field') >= 8002) { $fields = field_read_fields(array('module' => $file->name), array('include_deleted' => TRUE)); if ($fields) { $info['required'] = TRUE; @@ -343,7 +345,7 @@ function field_system_info_alter(&$info, $file, $type) { // remains no actual, non-deleted fields) $non_deleted = FALSE; foreach ($fields as $field) { - if (empty($field['deleted'])) { + if (empty($field->deleted)) { $non_deleted = TRUE; break; } @@ -436,14 +438,14 @@ function field_entity_field_info($entity_type) { foreach ($instances as $field_name => $instance) { $field = field_info_field($field_name); - if (!empty($field_types[$field['type']]['field item class'])) { + if (!empty($field_types[$field->type]['field item class'])) { // @todo: Allow for adding field type settings. $definition = array( 'label' => t('Field !name', array('!name' => $field_name)), - 'type' => $field['type'] . '_field', + 'type' => $field->type . '_field', 'configurable' => TRUE, - 'translatable' => !empty($field['translatable']) + 'translatable' => !empty($field->translatable) ); if ($optional) { @@ -536,23 +538,41 @@ function field_modules_disabled($modules) { } /** - * Refreshes the 'active' and 'storage_active' columns for fields. + * Refreshes the 'active' and 'storage_active' values for fields. */ function field_sync_field_status() { - // Refresh the 'active' and 'storage_active' columns according to the current + + $deleted_fields = state()->get('field.field.deleted') ?: array(); + $fields = field_read_fields(array(), array('include_deleted' => 1 ,'include_inactive' => 1)); + // Refresh the 'active' and 'storage_active' values according to the current // set of enabled modules. $modules = array_keys(drupal_container()->get('module_handler')->getModuleList()); foreach ($modules as $module_name) { - field_associate_fields($module_name); + $fields = field_associate_fields($module_name, $fields); + } + + foreach ($fields as $id => $field) { + if (!in_array($field->module, $modules)) { + $fields[$id]->active = 0; + } + if (!in_array($field->storage_module, $modules)) { + $fields[$id]->storage_active = 0; + } } - db_update('field_config') - ->fields(array('active' => 0)) - ->condition('module', $modules, 'NOT IN') - ->execute(); - db_update('field_config') - ->fields(array('storage_active' => 0)) - ->condition('storage_module', $modules, 'NOT IN') - ->execute(); + + foreach ($fields as $id => $field) { + if (!$field->deleted) { + $field->save(); + } + else { + $deleted_fields[$field->id()] = $field; + } + } + + // Save the deleted fields. + state()->set('field.field.deleted', $deleted_fields); + + field_cache_clear(); } /** @@ -560,24 +580,37 @@ function field_sync_field_status() { * * @param $module * The name of the module to update on. + * @param $fields + * A collection of fields. */ -function field_associate_fields($module) { +function field_associate_fields($module, $fields) { + // Associate field types. $field_types = (array) module_invoke($module, 'field_info'); + if ($field_types) { - db_update('field_config') - ->fields(array('module' => $module, 'active' => 1)) - ->condition('type', array_keys($field_types)) - ->execute(); + $field_types = array_keys($field_types); + foreach ($fields as $id => $field) { + if (in_array($field->type, $field_types)) { + $fields[$id]->module = $module; + $fields[$id]->active = TRUE; + } + } } + // Associate storage backends. $storage_types = (array) module_invoke($module, 'field_storage_info'); if ($storage_types) { - db_update('field_config') - ->fields(array('storage_module' => $module, 'storage_active' => 1)) - ->condition('storage_type', array_keys($storage_types)) - ->execute(); + $storage_types = array_keys($storage_types); + foreach ($fields as $id => $field) { + if (in_array($field->storage_type, $storage_types)) { + $fields[$id]->storage_module = $module; + $fields[$id]->storage_active = TRUE; + } + } } + + return $fields; } /** @@ -594,12 +627,12 @@ function field_associate_fields($module) { */ function field_get_default_value(EntityInterface $entity, $field, $instance, $langcode = NULL) { $items = array(); - if (!empty($instance['default_value_function'])) { - $function = $instance['default_value_function']; + if (!empty($instance->default_value_function)) { + $function = $instance->default_value_function; $items = $function($entity, $field, $instance, $langcode); } - elseif (!empty($instance['default_value'])) { - $items = $instance['default_value']; + elseif (!empty($instance->default_value)) { + $items = $instance->default_value; } return $items; } @@ -617,7 +650,7 @@ function field_get_default_value(EntityInterface $entity, $field, $instance, $la * the array keys to ensure sequential deltas. */ function _field_filter_items($field, $items) { - $function = $field['module'] . '_field_is_empty'; + $function = $field->module . '_field_is_empty'; foreach ((array) $items as $delta => $item) { // Explicitly break if the function is undefined. if ($function($item, $field)) { @@ -639,7 +672,7 @@ function _field_filter_items($field, $items) { * The sorted array of field items. */ function _field_sort_items($field, $items) { - if (($field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) && isset($items[0]['_weight'])) { + if (($field->cardinality > 1 || $field->cardinality == FIELD_CARDINALITY_UNLIMITED) && isset($items[0]['_weight'])) { usort($items, '_field_sort_items_helper'); foreach ($items as $delta => $item) { if (is_array($items[$delta])) { @@ -933,7 +966,7 @@ function field_view_field(EntityInterface $entity, $field_name, $display_options // $display_options, so we cannot let preparation happen internally. $field = field_info_field($field_name); $formatter_manager = drupal_container()->get('plugin.manager.field.formatter'); - $display_options = $formatter_manager->prepareConfiguration($field['type'], $display_options); + $display_options = $formatter_manager->prepareConfiguration($field->type, $display_options); $formatter = $formatter_manager->getInstance(array( 'instance' => $instance, 'view_mode' => $view_mode, @@ -1010,10 +1043,10 @@ function field_get_items(EntityInterface $entity, $field_name, $langcode = NULL) * TRUE if the field has data for any entity; FALSE otherwise. */ function field_has_data($field) { - $field = field_info_field_by_id($field['id']); - $columns = array_keys($field['columns']); + $field = field_info_field_by_id($field->id()); + $columns = array_keys($field->columns); $factory = drupal_container()->get('entity.query'); - foreach ($field['bundles'] as $entity_type => $bundle) { + foreach ($field->bundles as $entity_type => $bundle) { // Entity Query throws an exception if there is no base table. $entity_info = entity_get_info($entity_type); if (!isset($entity_info['base_table'])) { @@ -1022,7 +1055,7 @@ function field_has_data($field) { $query = $factory->get($entity_type); $group = $query->orConditionGroup(); foreach ($columns as $column) { - $group->exists($field['field_name'] . '.' . $column); + $group->exists($field->field_name . '.' . $column); } $result = $query ->condition($group) diff --git a/core/modules/field/field.multilingual.inc b/core/modules/field/field.multilingual.inc index a9b33ac..1ba113b 100644 --- a/core/modules/field/field.multilingual.inc +++ b/core/modules/field/field.multilingual.inc @@ -28,7 +28,7 @@ * * The available language codes for a particular field are returned by * field_available_languages(). Whether a field is translatable is determined by - * calling field_is_translatable(), which checks the $field['translatable'] + * calling field_is_translatable(), which checks the $field->translatable * property returned by field_info_field(), and whether there is at least one * translation handler available for the field. A translation handler is a * module registering itself via hook_entity_info_alter() to handle field @@ -129,7 +129,7 @@ function field_available_languages($entity_type, $field) { $drupal_static_fast['field_langcodes'] = &drupal_static(__FUNCTION__); } $field_langcodes = &$drupal_static_fast['field_langcodes']; - $field_name = $field['field_name']; + $field_name = $field->field_name; if (!isset($field_langcodes[$entity_type][$field_name])) { // If the field has language support enabled we retrieve an (alterable) list @@ -208,7 +208,10 @@ function field_content_languages() { * TRUE if the field can be translated. */ function field_is_translatable($entity_type, $field) { - return $field['translatable'] && field_has_translation_handler($entity_type); + // See http://drupal.org/node/1862758#comment-7035586 + if (!empty($field)) { + return $field->translatable && field_has_translation_handler($entity_type); + } } /** @@ -312,17 +315,17 @@ function field_language(EntityInterface $entity, $field_name = NULL, $langcode = // if the field translation is not available. It is up to translation // handlers to implement language fallback rules. foreach (field_info_instances($entity_type, $bundle) as $instance) { - if (isset($entity->{$instance['field_name']}[$langcode])) { - $display_langcode[$instance['field_name']] = $langcode; + if (isset($entity->{$instance->field_name}[$langcode])) { + $display_langcode[$instance->field_name] = $langcode; } else { // If the field has a value for one of the locked languages, then use // that language for display. If not, the default one will be // LANGUAGE_NOT_SPECIFIED. - $display_langcode[$instance['field_name']] = LANGUAGE_NOT_SPECIFIED; + $display_langcode[$instance->field_name] = LANGUAGE_NOT_SPECIFIED; foreach (language_list(LANGUAGE_LOCKED) as $language_locked) { - if (isset($entity->{$instance['field_name']}[$language_locked->langcode])) { - $display_langcode[$instance['field_name']] = $language_locked->langcode; + if (isset($entity->{$instance->field_name}[$language_locked->langcode])) { + $display_langcode[$instance->field_name] = $language_locked->langcode; break; } } diff --git a/core/modules/field/field.views.inc b/core/modules/field/field.views.inc index 6c769cd..5da80e2 100644 --- a/core/modules/field/field.views.inc +++ b/core/modules/field/field.views.inc @@ -18,11 +18,11 @@ function field_views_data() { $data = array(); foreach (field_info_fields() as $field) { - if ($field['storage']['type'] != 'field_sql_storage') { + if ($field->storage['type'] != 'field_sql_storage') { continue; } - $module = $field['module']; + $module = $field->module; $result = (array) module_invoke($module, 'field_views_data', $field); if (empty($result)) { @@ -48,11 +48,11 @@ function field_views_data() { */ function field_views_data_alter(&$data) { foreach (field_info_fields() as $field) { - if ($field['storage']['type'] != 'field_sql_storage') { + if ($field->storage['type'] != 'field_sql_storage') { continue; } - $function = $field['module'] . '_field_views_data_views_data_alter'; + $function = $field->module . '_field_views_data_views_data_alter'; if (function_exists($function)) { $function($data, $field); } @@ -72,8 +72,8 @@ function field_views_field_label($field_name) { foreach ($instances as $entity_name => $entity_type) { foreach ($entity_type as $bundle) { if (isset($bundle[$field_name])) { - $label_counter[$bundle[$field_name]['label']] = isset($label_counter[$bundle[$field_name]['label']]) ? ++$label_counter[$bundle[$field_name]['label']] : 1; - $all_labels[$entity_name][$bundle[$field_name]['label']] = TRUE; + $label_counter[$bundle[$field_name]->label] = isset($label_counter[$bundle[$field_name]->label]) ? ++$label_counter[$bundle[$field_name]->label] : 1; + $all_labels[$entity_name][$bundle[$field_name]->label] = TRUE; } } } @@ -93,7 +93,7 @@ function field_views_field_default_views_data($field) { $field_types = field_info_field_types(); // Check the field module is available. - if (!isset($field_types[$field['type']])) { + if (!isset($field_types[$field->type])) { return; } @@ -110,10 +110,10 @@ function field_views_field_default_views_data($field) { $revision_tables = array(); $groups = array(); - $group_name = count($field['bundles']) > 1 ? t('Field') : NULL; + $group_name = count($field->bundles) > 1 ? t('Field') : NULL; // Build the relationships between the field table and the entity tables. - foreach ($field['bundles'] as $entity => $bundles) { + foreach ($field->bundles as $entity => $bundles) { $entity_info = entity_get_info($entity); $groups[$entity] = $entity_info['label']; @@ -171,25 +171,25 @@ function field_views_field_default_views_data($field) { } $add_fields = array('delta', 'langcode', 'bundle'); - foreach ($field['columns'] as $column_name => $attributes) { - $add_fields[] = _field_sql_storage_columnname($field['field_name'], $column_name); + foreach ($field->columns as $column_name => $attributes) { + $add_fields[] = _field_sql_storage_columnname($field->field_name, $column_name); } // Note: we don't have a label available here, because we are at the field // level, not at the instance level. So we just go through all instances // and take the one which is used the most frequently. - $field_name = $field['field_name']; + $field_name = $field->field_name; list($label, $all_labels) = field_views_field_label($field_name); foreach ($tables as $type => $table) { if ($type == FIELD_LOAD_CURRENT) { $group = $group_name; $old_column = 'entity_id'; - $column = $field['field_name']; + $column = $field->field_name; } else { $group = t('@group (historical data)', array('@group' => $group_name)); $old_column = 'revision_id'; - $column = $field['field_name'] . '-' . $old_column; + $column = $field->field_name . '-' . $old_column; } $data[$table][$column] = array( @@ -237,12 +237,12 @@ function field_views_field_default_views_data($field) { $data[$table][$column]['help'] .= ' ' . t('Also known as: !also.', array('!also' => implode(', ', $also_known))); } - $keys = array_keys($field['columns']); + $keys = array_keys($field->columns); $real_field = reset($keys); $data[$table][$column]['field'] = array( 'table' => $table, 'id' => 'field', - 'field_name' => $field['field_name'], + 'field_name' => $field->field_name, // Provide a real field for group by. 'real field' => $column . '_' . $real_field, 'additional fields' => $add_fields, @@ -253,7 +253,7 @@ function field_views_field_default_views_data($field) { ); } - foreach ($field['columns'] as $column => $attributes) { + foreach ($field->columns as $column => $attributes) { $allow_sort = TRUE; // Identify likely filters and arguments for each column based on field type. @@ -280,12 +280,12 @@ function field_views_field_default_views_data($field) { break; } - if (count($field['columns']) == 1 || $column == 'value') { - $title = t('@label (!name)', array('@label' => $label, '!name' => $field['field_name'])); + if (count($field->columns) == 1 || $column == 'value') { + $title = t('@label (!name)', array('@label' => $label, '!name' => $field->field_name)); $title_short = $label; } else { - $title = t('@label (!name:!column)', array('@label' => $label, '!name' => $field['field_name'], '!column' => $column)); + $title = t('@label (!name:!column)', array('@label' => $label, '!name' => $field->field_name, '!column' => $column)); $title_short = t('@label:!column', array('@label' => $label, '!column' => $column)); } @@ -296,10 +296,10 @@ function field_views_field_default_views_data($field) { else { $group = t('@group (historical data)', array('@group' => $group_name)); } - $column_real_name = $field['storage']['details']['sql'][$type][$table][$column]; + $column_real_name = $field->storage['details']['sql'][$type][$table][$column]; // Load all the fields from the table by default. - $additional_fields = array_values($field['storage']['details']['sql'][$type][$table]); + $additional_fields = array_values($field->storage['details']['sql'][$type][$table]); $data[$table][$column_real_name] = array( 'group' => $group, @@ -315,11 +315,11 @@ function field_views_field_default_views_data($field) { foreach ($all_labels as $entity_name => $labels) { foreach ($labels as $label_name => $true) { if ($group_name != $groups[$entity_name] || $label != $label_name) { - if (count($field['columns']) == 1 || $column == 'value') { - $alias_title = t('@label (!name)', array('@label' => $label_name, '!name' => $field['field_name'])); + if (count($field->columns) == 1 || $column == 'value') { + $alias_title = t('@label (!name)', array('@label' => $label_name, '!name' => $field->field_name)); } else { - $alias_title = t('@label (!name:!column)', array('@label' => $label_name, '!name' => $field['field_name'], '!column' => $column)); + $alias_title = t('@label (!name:!column)', array('@label' => $label_name, '!name' => $field->field_name, '!column' => $column)); } $aliases[] = array( 'group' => $groups[$entity_name], @@ -340,7 +340,7 @@ function field_views_field_default_views_data($field) { 'table' => $table, 'id' => $argument, 'additional fields' => $additional_fields, - 'field_name' => $field['field_name'], + 'field_name' => $field->field_name, 'empty field name' => t('- No value -'), ); $data[$table][$column_real_name]['filter'] = array( @@ -348,7 +348,7 @@ function field_views_field_default_views_data($field) { 'table' => $table, 'id' => $filter, 'additional fields' => $additional_fields, - 'field_name' => $field['field_name'], + 'field_name' => $field->field_name, 'allow empty' => TRUE, ); if (!empty($allow_sort)) { @@ -357,13 +357,13 @@ function field_views_field_default_views_data($field) { 'table' => $table, 'id' => $sort, 'additional fields' => $additional_fields, - 'field_name' => $field['field_name'], + 'field_name' => $field->field_name, ); } // Expose additional delta column for multiple value fields. - if ($field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) { - $title_delta = t('@label (!name:delta)', array('@label' => $label, '!name' => $field['field_name'])); + if ($field->cardinality > 1 || $field->cardinality == FIELD_CARDINALITY_UNLIMITED) { + $title_delta = t('@label (!name:delta)', array('@label' => $label, '!name' => $field->field_name)); $title_short_delta = t('@label:delta', array('@label' => $label)); $data[$table]['delta'] = array( @@ -381,14 +381,14 @@ function field_views_field_default_views_data($field) { 'id' => 'numeric', 'additional fields' => $additional_fields, 'empty field name' => t('- No value -'), - 'field_name' => $field['field_name'], + 'field_name' => $field->field_name, ); $data[$table]['delta']['filter'] = array( 'field' => 'delta', 'table' => $table, 'id' => 'numeric', 'additional fields' => $additional_fields, - 'field_name' => $field['field_name'], + 'field_name' => $field->field_name, 'allow empty' => TRUE, ); $data[$table]['delta']['sort'] = array( @@ -396,13 +396,13 @@ function field_views_field_default_views_data($field) { 'table' => $table, 'id' => 'standard', 'additional fields' => $additional_fields, - 'field_name' => $field['field_name'], + 'field_name' => $field->field_name, ); } // Expose additional language column for translatable fields. - if (!empty($field['translatable'])) { - $title_language = t('@label (!name:language)', array('@label' => $label, '!name' => $field['field_name'])); + if (!empty($field->translatable)) { + $title_language = t('@label (!name:language)', array('@label' => $label, '!name' => $field->field_name)); $title_short_language = t('@label:language', array('@label' => $label)); $data[$table]['language'] = array( @@ -420,14 +420,14 @@ function field_views_field_default_views_data($field) { 'id' => 'language', 'additional fields' => $additional_fields, 'empty field name' => t(''), - 'field_name' => $field['field_name'], + 'field_name' => $field->field_name, ); $data[$table]['language']['filter'] = array( 'field' => 'language', 'table' => $table, 'id' => 'language', 'additional fields' => $additional_fields, - 'field_name' => $field['field_name'], + 'field_name' => $field->field_name, 'allow empty' => TRUE, ); $data[$table]['language']['sort'] = array( @@ -435,7 +435,7 @@ function field_views_field_default_views_data($field) { 'table' => $table, 'id' => 'standard', 'additional fields' => $additional_fields, - 'field_name' => $field['field_name'], + 'field_name' => $field->field_name, ); } } @@ -455,7 +455,7 @@ function list_field_views_data($field) { $data[$table_name][$field_name]['filter']['id'] = 'field_list'; } if (isset($field_data['argument']) && $field_name != 'delta') { - if ($field['type'] == 'list_text') { + if ($field->type == 'list_text') { $data[$table_name][$field_name]['argument']['id'] = 'field_list_string'; } else { diff --git a/core/modules/field/lib/Drupal/field/FieldInfo.php b/core/modules/field/lib/Drupal/field/FieldInfo.php index 7ff6850..2d5e31f 100644 --- a/core/modules/field/lib/Drupal/field/FieldInfo.php +++ b/core/modules/field/lib/Drupal/field/FieldInfo.php @@ -142,17 +142,11 @@ public function getFieldMap() { $map = array(); - $query = db_select('field_config_instance', 'fci'); - $query->join('field_config', 'fc', 'fc.id = fci.field_id'); - $query->fields('fc', array('type')); - $query->fields('fci', array('field_name', 'entity_type', 'bundle')) - ->condition('fc.active', 1) - ->condition('fc.storage_active', 1) - ->condition('fc.deleted', 0) - ->condition('fci.deleted', 0); - foreach ($query->execute() as $row) { - $map[$row->field_name]['bundles'][$row->entity_type][] = $row->bundle; - $map[$row->field_name]['type'] = $row->type; + $instances = field_read_instances(); + foreach ($instances as $key => $instance) { + $map[$instance->field_name]['bundles'][$instance->entity_type][] = $instance->bundle; + // @todo what is this type ?! + $map[$instance->field_name]['type'] = $instance->type; } // Save in "static" and persistent caches. @@ -181,7 +175,7 @@ public function getFields() { else { // Collect and prepare fields. foreach (field_read_fields(array(), array('include_deleted' => TRUE)) as $field) { - $this->fieldsById[$field['id']] = $this->prepareField($field); + $this->fieldsById[$field->uuid] = $this->prepareField($field); } // Store in persistent cache. @@ -190,8 +184,8 @@ public function getFields() { // Fill the name/ID map. foreach ($this->fieldsById as $field) { - if (!$field['deleted']) { - $this->fieldIdsByName[$field['field_name']] = $field['id']; + if (!$field->deleted) { + $this->fieldIdsByName[$field->field_name] = $field->uuid; } } @@ -227,9 +221,9 @@ public function getInstances($entity_type = NULL) { $this->getFields(); foreach (field_read_instances() as $instance) { - $field = $this->getField($instance['field_name']); - $instance = $this->prepareInstance($instance, $field['type']); - $this->bundleInstances[$instance['entity_type']][$instance['bundle']][$instance['field_name']] = new FieldInstance($instance); + $field = $this->getField($instance->field_name); + $instance = $this->prepareInstance($instance, $field->type); + $this->bundleInstances[$instance->entity_type][$instance->bundle][$instance->field_name] = $instance; } // Store in persistent cache. @@ -275,8 +269,8 @@ public function getField($field_name) { $field = $this->prepareField($field); // Save in the "static" cache. - $this->fieldsById[$field['id']] = $field; - $this->fieldIdsByName[$field['field_name']] = $field['id']; + $this->fieldsById[$field->uuid] = $field; + $this->fieldIdsByName[$field->field_name] = $field->uuid; return $field; } @@ -309,14 +303,14 @@ public function getFieldById($field_id) { // bundle. // Cache miss: read from definition. - if ($fields = field_read_fields(array('id' => $field_id), array('include_deleted' => TRUE))) { + if ($fields = field_read_fields(array('field_name' => $field_id), array('include_deleted' => TRUE))) { $field = current($fields); $field = $this->prepareField($field); // Store in the static cache. - $this->fieldsById[$field['id']] = $field; - if (!$field['deleted']) { - $this->fieldIdsByName[$field['field_name']] = $field['id']; + $this->fieldsById[$field->uuid] = $field; + if (!$field->deleted) { + $this->fieldIdsByName[$field->field_name] = $field->uuid; } return $field; @@ -355,10 +349,10 @@ public function getBundleInstances($entity_type, $bundle) { // Extract the field definitions and save them in the "static" cache. foreach ($info['fields'] as $field) { - if (!isset($this->fieldsById[$field['id']])) { - $this->fieldsById[$field['id']] = $field; - if (!$field['deleted']) { - $this->fieldIdsByName[$field['field_name']] = $field['id']; + if (!isset($this->fieldsById[$field->uuid])) { + $this->fieldsById[$field->uuid] = $field; + if (!$field->deleted) { + $this->fieldIdsByName[$field->field_name] = $field->uuid; } } } @@ -382,22 +376,22 @@ public function getBundleInstances($entity_type, $bundle) { // Collect the fields in the bundle. $params = array('entity_type' => $entity_type, 'bundle' => $bundle); - $fields = field_read_fields($params); + $fields = field_read_fields(); // This iterates on non-deleted instances, so deleted fields are kept out of // the persistent caches. foreach (field_read_instances($params) as $instance) { - $field = $fields[$instance['field_name']]; + $field = $fields[$instance->field_name]; - $instance = $this->prepareInstance($instance, $field['type']); - $instances[$field['field_name']] = new FieldInstance($instance); + $instance = $this->prepareInstance($instance, $field->type); + $instances[$field->field_name] = $instance; // If the field is not in our global "static" list yet, add it. - if (!isset($this->fieldsById[$field['id']])) { + if (!isset($this->fieldsById[$field->uuid])) { $field = $this->prepareField($field); - $this->fieldsById[$field['id']] = $field; - $this->fieldIdsByName[$field['field_name']] = $field['id']; + $this->fieldsById[$field->uuid] = $field; + $this->fieldIdsByName[$field->field_name] = $field->uuid; } } @@ -413,12 +407,13 @@ public function getBundleInstances($entity_type, $bundle) { // The persistent cache additionally contains the definitions of the fields // involved in the bundle. + uasort($instances, array($this, '_orderInstances')); $cache = array( 'instances' => $instances, 'fields' => array() ); foreach ($instances as $instance) { - $cache['fields'][] = $this->fieldsById[$instance['field_id']]; + $cache['fields'][] = $this->fieldsById[$instance->field_id]; } cache('field')->set("field_info:bundle:$entity_type:$bundle", $cache, CacheBackendInterface::CACHE_PERMANENT, array('field_info' => TRUE)); @@ -477,20 +472,20 @@ public function getBundleExtraFields($entity_type, $bundle) { */ public function prepareField($field) { // Make sure all expected field settings are present. - $field['settings'] += field_info_field_settings($field['type']); - $field['storage']['settings'] += field_info_storage_settings($field['storage']['type']); + $field->settings += field_info_field_settings($field->type); + $field->storage['settings'] += field_info_storage_settings($field->storage['type']); // Add storage details. - $details = (array) module_invoke($field['storage']['module'], 'field_storage_details', $field); + $details = (array) module_invoke($field->storage['module'], 'field_storage_details', $field); drupal_alter('field_storage_details', $details, $field); - $field['storage']['details'] = $details; + $field->storage['details'] = $details; // Populate the list of bundles using the field. - $field['bundles'] = array(); - if (!$field['deleted']) { + $field->bundles = array(); + if (!$field->deleted) { $map = $this->getFieldMap(); - if (isset($map[$field['field_name']])) { - $field['bundles'] = $map[$field['field_name']]['bundles']; + if (isset($map[$field->field_name])) { + $field->bundles = $map[$field->field_name]['bundles']; } } @@ -510,11 +505,11 @@ public function prepareField($field) { */ public function prepareInstance($instance, $field_type) { // Make sure all expected instance settings are present. - $instance['settings'] += field_info_instance_settings($field_type); + $instance->settings += field_info_instance_settings($field_type); // Set a default value for the instance. - if (field_behaviors_widget('default value', $instance) == FIELD_BEHAVIOR_DEFAULT && !isset($instance['default_value'])) { - $instance['default_value'] = NULL; + if (field_behaviors_widget('default value', $instance) == FIELD_BEHAVIOR_DEFAULT && !isset($instance->default_value)) { + $instance->default_value = NULL; } return $instance; @@ -553,4 +548,11 @@ public function prepareExtraFields($extra_fields, $entity_type, $bundle) { return $result; } + + /** + * Helper function to sort the instances on the widget weight. + */ + function _orderInstances($a, $b) { + return ($a->widget_settings['weight'] < $b->widget_settings['weight']) ? -1 : 1; + } } diff --git a/core/modules/field/lib/Drupal/field/FieldInstance.php b/core/modules/field/lib/Drupal/field/FieldInstance.php deleted file mode 100644 index ba6ea44..0000000 --- a/core/modules/field/lib/Drupal/field/FieldInstance.php +++ /dev/null @@ -1,135 +0,0 @@ -definition = $definition; - } - - /** - * Returns the Widget plugin for the instance. - * - * @return Drupal\field\Plugin\Type\Widget\WidgetInterface - * The Widget plugin to be used for the instance. - */ - public function getWidget() { - if (empty($this->widget)) { - $widget_properties = $this->definition['widget']; - - // Let modules alter the widget properties. - $context = array( - 'entity_type' => $this->definition['entity_type'], - 'bundle' => $this->definition['bundle'], - 'field' => field_info_field($this->definition['field_name']), - 'instance' => $this, - ); - drupal_alter(array('field_widget_properties', 'field_widget_properties_' . $this->definition['entity_type']), $widget_properties, $context); - - $options = array( - 'instance' => $this, - 'type' => $widget_properties['type'], - 'settings' => $widget_properties['settings'], - 'weight' => $widget_properties['weight'], - ); - $this->widget = drupal_container()->get('plugin.manager.field.widget')->getInstance($options); - } - - return $this->widget; - } - - /** - * Implements ArrayAccess::offsetExists(). - */ - public function offsetExists($offset) { - return isset($this->definition[$offset]) || array_key_exists($offset, $this->definition); - } - - /** - * Implements ArrayAccess::offsetGet(). - */ - public function &offsetGet($offset) { - return $this->definition[$offset]; - } - - /** - * Implements ArrayAccess::offsetSet(). - */ - public function offsetSet($offset, $value) { - if (!isset($offset)) { - // Do nothing; $array[] syntax is not supported by this temporary wrapper. - return; - } - $this->definition[$offset] = $value; - - // If the widget or formatter properties changed, the corrsponding plugins - // need to be re-instanciated. - if ($offset == 'widget') { - unset($this->widget); - } - } - - /** - * Implements ArrayAccess::offsetUnset(). - */ - public function offsetUnset($offset) { - unset($this->definition[$offset]); - - // If the widget or formatter properties changed, the corrsponding plugins - // need to be re-instanciated. - if ($offset == 'widget') { - unset($this->widget); - } - } - - /** - * Returns the instance definition as a regular array. - * - * This is used as a temporary BC layer. - * @todo Remove once the external APIs have been converted to use - * FieldInstance objects. - * - * @return array - * The instance definition as a regular array. - */ - public function getArray() { - return $this->definition; - } - - /** - * Handles serialization of Drupal\field\FieldInstance objects. - */ - public function __sleep() { - return array('definition'); - } - -} diff --git a/core/modules/field/lib/Drupal/field/FieldInstanceStorageController.php b/core/modules/field/lib/Drupal/field/FieldInstanceStorageController.php new file mode 100644 index 0000000..8a4d0e9 --- /dev/null +++ b/core/modules/field/lib/Drupal/field/FieldInstanceStorageController.php @@ -0,0 +1,46 @@ +get('data'); + field_create_instance($config); + } + + /** + * Overrides \Drupal\Core\Config\Entity\ConfigStorageController::importChange(). + */ + public function importChange($name, Config $new_config, Config $old_config) { + $config = $new_config->get('data'); + field_update_instance($config); + } + + /** + * Overrides \Drupal\Core\Config\Entity\ConfigStorageController::importDelete(). + */ + public function importDelete($name, Config $new_config, Config $old_config) { + $config = $old_config->get('data'); + // In case the field has been deleted through this import hook, + // the instance will be deleted by then already. + if (!empty($config)) { + field_delete_instance($config); + } + } + +} diff --git a/core/modules/field/lib/Drupal/field/FieldStorageController.php b/core/modules/field/lib/Drupal/field/FieldStorageController.php new file mode 100644 index 0000000..a86c39c --- /dev/null +++ b/core/modules/field/lib/Drupal/field/FieldStorageController.php @@ -0,0 +1,39 @@ +get()); + } + + /** + * Overrides \Drupal\Core\Config\Entity\ConfigStorageController::importChange(). + */ + public function importChange($name, Config $new_config, Config $old_config) { + field_update_field($new_config->get()); + } + + /** + * Overrides \Drupal\Core\Config\Entity\ConfigStorageController::importDelete(). + */ + public function importDelete($name, Config $new_config, Config $old_config) { + field_delete_field($old_config->get('field_name')); + } + +} diff --git a/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/FieldEntity.php b/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/FieldEntity.php new file mode 100644 index 0000000..a30e48b --- /dev/null +++ b/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/FieldEntity.php @@ -0,0 +1,198 @@ +field_name; + } + + /** + * Generate an a table id for deleted fields. + * + * When a field is a deleted, the tables are renamed to {field_data_field_id} + * and {field_revision_field_id}. To make sure we don't end up with table + * names longer than 64 characters, we hash the uuid and return the first + * 6 characters so we end up with a short unique id. + */ + function generate_table_id() { + return substr(hash('sha256', $this->uuid), 0, 6); + } + +} 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 new file mode 100644 index 0000000..f88614d --- /dev/null +++ b/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/FieldInstance.php @@ -0,0 +1,167 @@ +widget)) { + $widget_properties = $this->widget_settings; + + // Let modules alter the widget properties. + $context = array( + 'entity_type' => $this->entity_type, + 'bundle' => $this->bundle, + 'field' => field_info_field($this->field_name), + 'instance' => $this, + ); + drupal_alter(array('field_widget_properties', 'field_widget_properties_' . $this->entity_type), $widget_properties, $context); + + $options = array( + 'instance' => $this, + 'type' => $widget_properties['type'], + 'settings' => $widget_properties['settings'], + 'weight' => $widget_properties['weight'], + ); + $this->widget = drupal_container()->get('plugin.manager.field.widget')->getInstance($options); + } + + return $this->widget; + } + +} diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterBase.php b/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterBase.php index 04a038d..6d25725 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterBase.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterBase.php @@ -10,7 +10,7 @@ use Drupal\Component\Plugin\Discovery\DiscoveryInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\field\Plugin\PluginSettingsBase; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Base class for 'Field formatter' plugin implementations. @@ -73,7 +73,7 @@ public function __construct($plugin_id, DiscoveryInterface $discovery, $instance parent::__construct(array(), $plugin_id, $discovery); $this->instance = $instance; - $this->field = field_info_field($instance['field_name']); + $this->field = field_info_field($instance->field_name); $this->settings = $settings; $this->label = $label; $this->viewMode = $view_mode; @@ -93,14 +93,14 @@ public function view(EntityInterface $entity, $langcode, array $items) { $entity_type = $entity->entityType(); $info = array( '#theme' => 'field', - '#title' => $instance['label'], + '#title' => $instance->label, '#access' => field_access('view', $field, $entity->entityType(), $entity), '#label_display' => $this->label, '#view_mode' => $this->viewMode, '#language' => $langcode, - '#field_name' => $field['field_name'], - '#field_type' => $field['type'], - '#field_translatable' => $field['translatable'], + '#field_name' => $field->field_name, + '#field_type' => $field->type, + '#field_translatable' => $field->translatable, '#entity_type' => $entity_type, '#bundle' => $entity->bundle(), '#object' => $entity, @@ -108,7 +108,7 @@ public function view(EntityInterface $entity, $langcode, array $items) { '#formatter' => $this->getPluginId(), ); - $addition[$field['field_name']] = array_merge($info, $elements); + $addition[$field->field_name] = array_merge($info, $elements); } return $addition; diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterInterface.php b/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterInterface.php index 36693b0..4058810 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterInterface.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterInterface.php @@ -8,7 +8,7 @@ namespace Drupal\field\Plugin\Type\Formatter; use Drupal\Core\Entity\EntityInterface; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; use Drupal\field\Plugin\PluginSettingsInterface; /** diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterPluginManager.php b/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterPluginManager.php index c935c44..d7ff1b3 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterPluginManager.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterPluginManager.php @@ -13,7 +13,7 @@ use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery; use Drupal\Core\Plugin\Discovery\AlterDecorator; use Drupal\field\Plugin\Type\Formatter\FormatterLegacyDiscoveryDecorator; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Plugin type manager for field formatters. @@ -69,11 +69,11 @@ public function __construct() { public function getInstance(array $options) { $configuration = $options['configuration']; $instance = $options['instance']; - $field = field_info_field($instance['field_name']); + $field = field_info_field($instance->field_name); // Fill in default configuration if needed. if (!isset($options['prepare']) || $options['prepare'] == TRUE) { - $configuration = $this->prepareConfiguration($field['type'], $configuration); + $configuration = $this->prepareConfiguration($field->type, $configuration); } $plugin_id = $configuration['type']; @@ -82,9 +82,9 @@ public function getInstance(array $options) { // - $type_info doesn't exist (the widget type is unknown), // - the field type is not allowed for the widget. $definition = $this->getDefinition($configuration['type']); - if (!isset($definition['class']) || !in_array($field['type'], $definition['field_types'])) { + if (!isset($definition['class']) || !in_array($field->type, $definition['field_types'])) { // Grab the default widget for the field type. - $field_type_definition = field_info_field_types($field['type']); + $field_type_definition = field_info_field_types($field->type); $plugin_id = $field_type_definition['default_formatter']; } diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetBase.php b/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetBase.php index 12118f8..59a4b70 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetBase.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetBase.php @@ -11,7 +11,7 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Core\Entity\EntityInterface; use Drupal\field\Plugin\PluginSettingsBase; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Base class for 'Field widget' plugin implementations. @@ -65,7 +65,7 @@ public function __construct($plugin_id, DiscoveryInterface $discovery, FieldInst parent::__construct(array(), $plugin_id, $discovery); $this->instance = $instance; - $this->field = field_info_field($instance['field_name']); + $this->field = field_info_field($instance->field_name); $this->settings = $settings; $this->weight = $weight; } @@ -76,7 +76,7 @@ public function __construct($plugin_id, DiscoveryInterface $discovery, FieldInst public function form(EntityInterface $entity, $langcode, array $items, array &$form, array &$form_state, $get_delta = NULL) { $field = $this->field; $instance = $this->instance; - $field_name = $field['field_name']; + $field_name = $field->field_name; $parents = $form['#parents']; @@ -106,8 +106,8 @@ public function form(EntityInterface $entity, $langcode, array $items, array &$f if (isset($get_delta) || $definition['multiple_values']) { $delta = isset($get_delta) ? $get_delta : 0; $element = array( - '#title' => check_plain($instance['label']), - '#description' => field_filter_xss(token_replace($instance['description'])), + '#title' => check_plain($instance->label), + '#description' => field_filter_xss(token_replace($instance->description)), ); $element = $this->formSingleElement($entity, $items, $delta, $langcode, $element, $form, $form_state); @@ -137,7 +137,7 @@ public function form(EntityInterface $entity, $langcode, array $items, array &$f '#type' => 'container', '#attributes' => array( 'class' => array( - 'field-type-' . drupal_html_class($field['type']), + 'field-type-' . drupal_html_class($field->type), 'field-name-' . drupal_html_class($field_name), 'field-widget-' . drupal_html_class($this->getPluginId()), ), @@ -176,12 +176,12 @@ public function form(EntityInterface $entity, $langcode, array $items, array &$f protected function formMultipleElements(EntityInterface $entity, array $items, $langcode, array &$form, array &$form_state) { $field = $this->field; $instance = $this->instance; - $field_name = $field['field_name']; + $field_name = $field->field_name; $parents = $form['#parents']; // Determine the number of widgets to display. - switch ($field['cardinality']) { + switch ($field->cardinality) { case FIELD_CARDINALITY_UNLIMITED: $field_state = field_form_get_state($parents, $field_name, $langcode, $form_state); $max = $field_state['items_count']; @@ -189,16 +189,16 @@ protected function formMultipleElements(EntityInterface $entity, array $items, $ break; default: - $max = $field['cardinality'] - 1; - $is_multiple = ($field['cardinality'] > 1); + $max = $field->cardinality - 1; + $is_multiple = ($field->cardinality > 1); break; } $id_prefix = implode('-', array_merge($parents, array($field_name))); $wrapper_id = drupal_html_id($id_prefix . '-add-more-wrapper'); - $title = check_plain($instance['label']); - $description = field_filter_xss(token_replace($instance['description'])); + $title = check_plain($instance->label); + $description = field_filter_xss(token_replace($instance->description)); $elements = array(); @@ -234,9 +234,9 @@ protected function formMultipleElements(EntityInterface $entity, array $items, $ if ($elements) { $elements += array( '#theme' => 'field_multiple_value_form', - '#field_name' => $field['field_name'], - '#cardinality' => $field['cardinality'], - '#required' => $instance['required'], + '#field_name' => $field->field_name, + '#cardinality' => $field->cardinality, + '#required' => $instance->required, '#title' => $title, '#description' => $description, '#prefix' => '
', @@ -245,7 +245,7 @@ protected function formMultipleElements(EntityInterface $entity, array $items, $ ); // Add 'add more' button, if not working with a programmed form. - if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED && empty($form_state['programmed'])) { + if ($field->cardinality == FIELD_CARDINALITY_UNLIMITED && empty($form_state['programmed'])) { $elements['add_more'] = array( '#type' => 'submit', '#name' => strtr($id_prefix, '-', '_') . '_add_more', @@ -276,12 +276,12 @@ protected function formSingleElement(EntityInterface $entity, array $items, $del '#entity_type' => $entity->entityType(), '#bundle' => $entity->bundle(), '#entity' => $entity, - '#field_name' => $field['field_name'], + '#field_name' => $field->field_name, '#language' => $langcode, '#field_parents' => $form['#parents'], - '#columns' => array_keys($field['columns']), + '#columns' => array_keys($field->columns), // Only the first widget should be required. - '#required' => $delta == 0 && $instance['required'], + '#required' => $delta == 0 && $instance->required, '#delta' => $delta, '#weight' => $delta, ); @@ -309,7 +309,7 @@ protected function formSingleElement(EntityInterface $entity, array $items, $del * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::extractFormValues(). */ public function extractFormValues(EntityInterface $entity, $langcode, array &$items, array $form, array &$form_state) { - $field_name = $this->field['field_name']; + $field_name = $this->field->field_name; // Extract the values from $form_state['values']. $path = array_merge($form['#parents'], array($field_name, $langcode)); @@ -360,7 +360,7 @@ public function extractFormValues(EntityInterface $entity, $langcode, array &$it * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::flagErrors(). */ public function flagErrors(EntityInterface $entity, $langcode, array $items, array $form, array &$form_state) { - $field_name = $this->field['field_name']; + $field_name = $this->field->field_name; $field_state = field_form_get_state($form['#parents'], $field_name, $langcode, $form_state); @@ -423,7 +423,7 @@ public function massageFormValues(array $values, array $form, array &$form_state * The field values. */ protected function sortItems(array &$items) { - $is_multiple = ($this->field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) || ($this->field['cardinality'] > 1); + $is_multiple = ($this->field->cardinality == FIELD_CARDINALITY_UNLIMITED) || ($this->field->cardinality > 1); if ($is_multiple && isset($items[0]['_weight'])) { usort($items, function ($a, $b) { $a_weight = (is_array($a) ? $a['_weight'] : 0); diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetInterface.php b/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetInterface.php index c0ffc95..8711bbd 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetInterface.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetInterface.php @@ -8,7 +8,7 @@ namespace Drupal\field\Plugin\Type\Widget; use Drupal\Core\Entity\EntityInterface; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Interface definition for field widget plugins. diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetPluginManager.php b/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetPluginManager.php index 2d2c6e2..ec5cbd3 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetPluginManager.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetPluginManager.php @@ -50,14 +50,14 @@ public function getInstance(array $options) { $type = $options['type']; $definition = $this->getDefinition($type); - $field = field_info_field($instance['field_name']); + $field = field_info_field($instance->field_name); // Switch back to default widget if either: // - $type_info doesn't exist (the widget type is unknown), // - the field type is not allowed for the widget. - if (!isset($definition['class']) || !in_array($field['type'], $definition['field_types'])) { + if (!isset($definition['class']) || !in_array($field->type, $definition['field_types'])) { // Grab the default widget for the field type. - $field_type_definition = field_info_field_types($field['type']); + $field_type_definition = field_info_field_types($field->type); $type = $field_type_definition['default_widget']; } diff --git a/core/modules/field/lib/Drupal/field/Plugin/field/widget/LegacyWidget.php b/core/modules/field/lib/Drupal/field/Plugin/field/widget/LegacyWidget.php index ee66f3f..9564b83 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/field/widget/LegacyWidget.php +++ b/core/modules/field/lib/Drupal/field/Plugin/field/widget/LegacyWidget.php @@ -51,8 +51,8 @@ public function formElement(array $items, $delta, array $element, $langcode, arr // from $instance. Put the actual properties we use here, which might have // been altered by hook_field_widget_property(). $instance = clone $this->instance; - $instance['widget']['type'] = $this->getPluginId(); - $instance['widget']['settings'] = $this->getSettings(); + $instance->widget_settings['type'] = $this->getPluginId(); + $instance->widget_settings['settings'] = $this->getSettings(); return $function($form, $form_state, $this->field, $instance, $langcode, $items, $delta, $element); } @@ -68,7 +68,7 @@ public function formElement(array $items, $delta, array $element, $langcode, arr * code in field_default_form_errors(). */ public function flagErrors(EntityInterface $entity, $langcode, array $items, array $form, array &$form_state) { - $field_name = $this->field['field_name']; + $field_name = $this->field->field_name; $field_state = field_form_get_state($form['#parents'], $field_name, $langcode, $form_state); 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 30836a3..5e6edee 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 @@ -78,7 +78,7 @@ public function init(ViewExecutable $view, DisplayPluginBase $display, array &$o $this->multiple = FALSE; $this->limit_values = FALSE; - if ($field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) { + if ($field->cardinality > 1 || $field->cardinality == FIELD_CARDINALITY_UNLIMITED) { $this->multiple = TRUE; // If "Display all values in the same row" is FALSE, then we always limit @@ -94,7 +94,7 @@ public function init(ViewExecutable $view, DisplayPluginBase $display, array &$o // Otherwise, we only limit values if the user hasn't selected "all", 0, or // the value matching field cardinality. - if ((intval($this->options['delta_limit']) && ($this->options['delta_limit'] != $field['cardinality'])) || intval($this->options['delta_offset'])) { + if ((intval($this->options['delta_limit']) && ($this->options['delta_limit'] != $field->cardinality)) || intval($this->options['delta_offset'])) { $this->limit_values = TRUE; } } @@ -262,8 +262,8 @@ protected function defineOptions() { // defineOptions runs before init/construct, so no $this->field_info $field = field_info_field($this->definition['field_name']); - $field_type = field_info_field_types($field['type']); - $column_names = array_keys($field['columns']); + $field_type = field_info_field_types($field->type); + $column_names = array_keys($field->columns); $default_column = ''; // Try to determine a sensible default. if (count($column_names) == 1) { @@ -298,7 +298,7 @@ protected function defineOptions() { // If we know the exact number of allowed values, then that can be // the default. Otherwise, default to 'all'. $options['delta_limit'] = array( - 'default' => ($field['cardinality'] > 1) ? $field['cardinality'] : 'all', + 'default' => ($field->cardinality > 1) ? $field->cardinality : 'all', ); $options['delta_offset'] = array( 'default' => 0, @@ -331,8 +331,8 @@ public function buildOptionsForm(&$form, &$form_state) { parent::buildOptionsForm($form, $form_state); $field = $this->field_info; - $formatters = _field_view_formatter_options($field['type']); - $column_names = array_keys($field['columns']); + $formatters = _field_view_formatter_options($field->type); + $column_names = array_keys($field->columns); // If this is a multiple value field, add its options. if ($this->multiple) { @@ -340,7 +340,7 @@ public function buildOptionsForm(&$form, &$form_state) { } // No need to ask the user anything if the field has only one column. - if (count($field['columns']) == 1) { + if (count($field->columns) == 1) { $form['click_sort_column'] = array( '#type' => 'value', '#value' => isset($column_names[0]) ? $column_names[0] : '', @@ -426,7 +426,7 @@ function fakeFieldInstance($formatter, $formatter_settings) { $field_name = $this->definition['field_name']; $field = field_read_field($field_name); - $field_type = field_info_field_types($field['type']); + $field_type = field_info_field_types($field->type); return array( // Build a fake entity type and bundle. @@ -435,7 +435,7 @@ function fakeFieldInstance($formatter, $formatter_settings) { 'bundle' => 'views_fake', // Use the default field settings for settings and widget. - 'settings' => field_info_instance_settings($field['type']), + 'settings' => field_info_instance_settings($field->type), 'widget' => array( 'type' => $field_type['default_widget'], 'settings' => array(), @@ -483,14 +483,14 @@ function multiple_options_form(&$form, &$form_state) { // translating prefix and suffix separately. list($prefix, $suffix) = explode('@count', t('Display @count value(s)')); - if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) { + if ($field->cardinality == FIELD_CARDINALITY_UNLIMITED) { $type = 'textfield'; $options = NULL; $size = 5; } else { $type = 'select'; - $options = drupal_map_assoc(range(1, $field['cardinality'])); + $options = drupal_map_assoc(range(1, $field->cardinality)); $size = 1; } $form['multi_type'] = array( @@ -810,14 +810,14 @@ function render_item($count, $item) { function document_self_tokens(&$tokens) { $field = $this->field_info; - foreach ($field['columns'] as $id => $column) { + foreach ($field->columns as $id => $column) { $tokens['[' . $this->options['id'] . '-' . $id . ']'] = t('Raw @column', array('@column' => $id)); } } function add_self_tokens(&$tokens, $item) { $field = $this->field_info; - foreach ($field['columns'] as $id => $column) { + foreach ($field->columns as $id => $column) { // Use filter_xss_admin because it's user data and we can't be sure it is safe. // We know nothing about the data, though, so we can't really do much else. diff --git a/core/modules/field/lib/Drupal/field/Tests/BulkDeleteTest.php b/core/modules/field/lib/Drupal/field/Tests/BulkDeleteTest.php index c365961..9ea8654 100644 --- a/core/modules/field/lib/Drupal/field/Tests/BulkDeleteTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/BulkDeleteTest.php @@ -7,6 +7,8 @@ namespace Drupal\field\Tests; +use Drupal\field\Plugin\Core\Entity\FieldInstance; + /** * Unit test class for field bulk delete and batch purge functionality. */ @@ -119,7 +121,7 @@ function setUp() { foreach ($this->bundles as $bundle) { foreach ($this->fields as $field) { $instance = array( - 'field_name' => $field['field_name'], + 'field_name' => $field->field_name, 'entity_type' => $this->entity_type, 'bundle' => $bundle, 'widget' => array( @@ -132,7 +134,7 @@ function setUp() { for ($i = 0; $i < 10; $i++) { $entity = field_test_create_entity($id, $id, $bundle); foreach ($this->fields as $field) { - $entity->{$field['field_name']}[LANGUAGE_NOT_SPECIFIED] = $this->_generateTestFieldValues($field['cardinality']); + $entity->{$field->field_name}[LANGUAGE_NOT_SPECIFIED] = $this->_generateTestFieldValues($field->cardinality); } $entity->save(); $id++; @@ -157,7 +159,7 @@ function setUp() { function testDeleteFieldInstance() { $bundle = reset($this->bundles); $field = reset($this->fields); - $field_name = $field['field_name']; + $field_name = $field->field_name; $factory = drupal_container()->get('entity.query'); // There are 10 entities of this bundle. @@ -167,11 +169,11 @@ function testDeleteFieldInstance() { $this->assertEqual(count($found), 10, 'Correct number of entities found before deleting'); // Delete the instance. - $instance = field_info_instance($this->entity_type, $field['field_name'], $bundle); + $instance = field_info_instance($this->entity_type, $field->field_name, $bundle); field_delete_instance($instance); // The instance still exists, deleted. - $instances = field_read_instances(array('field_id' => $field['id'], 'deleted' => 1), array('include_deleted' => 1, 'include_inactive' => 1)); + $instances = field_read_instances(array('field_id' => $field->id(), 'deleted' => 1), array('include_deleted' => 1, 'include_inactive' => 1)); $this->assertEqual(count($instances), 1, 'There is one deleted instance'); $this->assertEqual($instances[0]['bundle'], $bundle, 'The deleted instance is for the correct bundle'); @@ -198,10 +200,10 @@ function testDeleteFieldInstance() { $ids->entity_id = $entity_id; $entities[$entity_id] = _field_create_entity_from_ids($ids); } - field_attach_load($this->entity_type, $entities, FIELD_LOAD_CURRENT, array('field_id' => $field['id'], 'deleted' => 1)); + field_attach_load($this->entity_type, $entities, FIELD_LOAD_CURRENT, array('field_id' => $field->id(), 'deleted' => 1)); $this->assertEqual(count($found), 10, 'Correct number of entities found after deleting'); foreach ($entities as $id => $entity) { - $this->assertEqual($this->entities[$id]->{$field['field_name']}, $entity->{$field['field_name']}, "Entity $id with deleted data loaded correctly"); + $this->assertEqual($this->entities[$id]->{$field->field_name}, $entity->{$field->field_name}, "Entity $id with deleted data loaded correctly"); } } @@ -217,7 +219,7 @@ function testPurgeInstance() { $field = reset($this->fields); // Delete the instance. - $instance = field_info_instance($this->entity_type, $field['field_name'], $bundle); + $instance = field_info_instance($this->entity_type, $field->field_name, $bundle); field_delete_instance($instance); // No field hooks were called. @@ -232,7 +234,7 @@ function testPurgeInstance() { // There are $count deleted entities left. $found = entity_query('test_entity') ->condition('fttype', $bundle) - ->condition($field['field_name'] . '.deleted', 1) + ->condition($field->field_name . '.deleted', 1) ->execute(); $this->assertEqual(count($found), $count, 'Correct number of entities found after purging 2'); } @@ -244,7 +246,7 @@ function testPurgeInstance() { // bundle. $actual_hooks = field_test_memorize(); $hooks = array(); - $entities = $this->convertToPartialEntities($this->entities_by_bundles[$bundle], $field['field_name']); + $entities = $this->convertToPartialEntities($this->entities_by_bundles[$bundle], $field->field_name); foreach (array_chunk($entities, $batch_size, TRUE) as $chunk_entity) { $hooks['field_test_field_load'][] = $chunk_entity; } @@ -254,19 +256,19 @@ function testPurgeInstance() { $this->checkHooksInvocations($hooks, $actual_hooks); // The instance still exists, deleted. - $instances = field_read_instances(array('field_id' => $field['id'], 'deleted' => 1), array('include_deleted' => 1, 'include_inactive' => 1)); + $instances = field_read_instances(array('field_id' => $field->id(), 'deleted' => 1), array('include_deleted' => 1, 'include_inactive' => 1)); $this->assertEqual(count($instances), 1, 'There is one deleted instance'); // Purge the instance. field_purge_batch($batch_size); // The instance is gone. - $instances = field_read_instances(array('field_id' => $field['id'], 'deleted' => 1), array('include_deleted' => 1, 'include_inactive' => 1)); + $instances = field_read_instances(array('field_id' => $field->id(), 'deleted' => 1), array('include_deleted' => 1, 'include_inactive' => 1)); $this->assertEqual(count($instances), 0, 'The instance is gone'); // The field still exists, not deleted, because it has a second instance. - $fields = field_read_fields(array('id' => $field['id']), array('include_deleted' => 1, 'include_inactive' => 1)); - $this->assertTrue(isset($fields[$field['id']]), 'The field exists and is not deleted'); + $fields = field_read_fields(array('uuid' => $field->id()), array('include_deleted' => 1, 'include_inactive' => 1)); + $this->assertTrue(isset($fields[$field->id()]), 'The field exists and is not deleted'); } /** @@ -281,7 +283,7 @@ function testPurgeField() { // Delete the first instance. $bundle = reset($this->bundles); - $instance = field_info_instance($this->entity_type, $field['field_name'], $bundle); + $instance = field_info_instance($this->entity_type, $field->field_name, $bundle); field_delete_instance($instance); // Assert that hook_field_delete() was not called yet. @@ -298,7 +300,7 @@ function testPurgeField() { // bundle. $actual_hooks = field_test_memorize(); $hooks = array(); - $entities = $this->convertToPartialEntities($this->entities_by_bundles[$bundle], $field['field_name']); + $entities = $this->convertToPartialEntities($this->entities_by_bundles[$bundle], $field->field_name); $hooks['field_test_field_load'][] = $entities; $hooks['field_test_field_delete'] = $entities; $this->checkHooksInvocations($hooks, $actual_hooks); @@ -307,12 +309,12 @@ function testPurgeField() { field_purge_batch(0); // The field still exists, not deleted. - $fields = field_read_fields(array('id' => $field['id']), array('include_deleted' => 1)); - $this->assertTrue(isset($fields[$field['id']]) && !$fields[$field['id']]['deleted'], 'The field exists and is not deleted'); + $fields = field_read_fields(array('uuid' => $field->id()), array('include_deleted' => 1)); + $this->assertTrue(isset($fields[$field->id()]) && !$fields[$field->id()]->deleted, 'The field exists and is not deleted'); // Delete the second instance. $bundle = next($this->bundles); - $instance = field_info_instance($this->entity_type, $field['field_name'], $bundle); + $instance = field_info_instance($this->entity_type, $field->field_name, $bundle); field_delete_instance($instance); // Assert that hook_field_delete() was not called yet. @@ -325,20 +327,20 @@ function testPurgeField() { // Check hooks invocations (same as above, for the 2nd bundle). $actual_hooks = field_test_memorize(); $hooks = array(); - $entities = $this->convertToPartialEntities($this->entities_by_bundles[$bundle], $field['field_name']); + $entities = $this->convertToPartialEntities($this->entities_by_bundles[$bundle], $field->field_name); $hooks['field_test_field_load'][] = $entities; $hooks['field_test_field_delete'] = $entities; $this->checkHooksInvocations($hooks, $actual_hooks); // The field still exists, deleted. - $fields = field_read_fields(array('id' => $field['id']), array('include_deleted' => 1)); - $this->assertTrue(isset($fields[$field['id']]) && $fields[$field['id']]['deleted'], 'The field exists and is deleted'); + $fields = field_read_fields(array('uuid' => $field->id()), array('include_deleted' => 1)); + $this->assertTrue(isset($fields[$field->id()]) && $fields[$field->id()]->deleted, 'The field exists and is deleted'); // Purge again to purge the instance and the field. field_purge_batch(0); // The field is gone. - $fields = field_read_fields(array('id' => $field['id']), array('include_deleted' => 1, 'include_inactive' => 1)); + $fields = field_read_fields(array('uuid' => $field->id()), array('include_deleted' => 1, 'include_inactive' => 1)); $this->assertEqual(count($fields), 0, 'The field is purged.'); } } diff --git a/core/modules/field/lib/Drupal/field/Tests/CrudTest.php b/core/modules/field/lib/Drupal/field/Tests/CrudTest.php index 1c539da..4bc419c 100644 --- a/core/modules/field/lib/Drupal/field/Tests/CrudTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/CrudTest.php @@ -41,28 +41,27 @@ function testCreateField() { 'type' => 'test_field', ); field_test_memorize(); - $field_definition = field_create_field($field_definition); + $create_field = field_create_field($field_definition); $mem = field_test_memorize(); - $this->assertIdentical($mem['field_test_field_create_field'][0][0], $field_definition, 'hook_field_create_field() called with correct arguments.'); + $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.'); - // Read the raw record from the {field_config_instance} table. - $result = db_query('SELECT * FROM {field_config} WHERE field_name = :field_name', array(':field_name' => $field_definition['field_name'])); - $record = $result->fetchAssoc(); - $record['data'] = unserialize($record['data']); + // Read the configuration. + $record = entity_load('field_entity', $field_definition['field_name']); // Ensure that basic properties are preserved. - $this->assertEqual($record['field_name'], $field_definition['field_name'], 'The field name is properly saved.'); - $this->assertEqual($record['type'], $field_definition['type'], 'The field type is properly saved.'); + $this->assertEqual($record->field_name, $field_definition['field_name'], 'The field name is properly saved.'); + $this->assertEqual($record->type, $field_definition['type'], 'The field type is properly saved.'); // Ensure that cardinality defaults to 1. - $this->assertEqual($record['cardinality'], 1, 'Cardinality defaults to 1.'); + $this->assertEqual($record->cardinality, 1, 'Cardinality defaults to 1.'); // Ensure that default settings are present. $field_type = field_info_field_types($field_definition['type']); - $this->assertIdentical($record['data']['settings'], $field_type['settings'], 'Default field settings have been written.'); + $this->assertEqual($record->settings, $field_type['settings'], 'Default field settings have been written.'); // Ensure that default storage was set. - $this->assertEqual($record['storage_type'], variable_get('field_storage_default'), 'The field type is properly saved.'); + $this->assertEqual($record->storage_type, variable_get('field_storage_default'), 'The field type is properly saved.'); // Guarantee that the name is unique. try { @@ -157,11 +156,10 @@ function testCreateField() { function testCreateFieldFail() { $field_name = 'duplicate'; $field_definition = array('field_name' => $field_name, 'type' => 'test_field', 'storage' => array('type' => 'field_test_storage_failure')); - $query = db_select('field_config')->condition('field_name', $field_name)->countQuery(); + $field = entity_load('field_entity', $field_name); - // The field does not appear in field_config. - $count = $query->execute()->fetchField(); - $this->assertEqual($count, 0, 'A field_config row for the field does not exist.'); + // The field does not exist. + $this->assertFalse($field, 'The field does not exist.'); // Try to create the field. try { @@ -172,9 +170,10 @@ function testCreateFieldFail() { $this->assertTrue(TRUE, 'Field creation (correctly) fails.'); } - // The field does not appear in field_config. - $count = $query->execute()->fetchField(); - $this->assertEqual($count, 0, 'A field_config row for the field does not exist.'); + // The field does not exist. + // $field = config('field.field.' . $field_name)->get(); + $field = entity_load('field_entity', $field_name); + $this->assertFalse($field, 'The field does not exist.'); } /** @@ -221,10 +220,11 @@ function testReadFields() { field_create_instance($instance_definition); // Check that criteria spanning over the field_config_instance table work. - $fields = field_read_fields(array('entity_type' => $instance_definition['entity_type'], 'bundle' => $instance_definition['bundle'])); + // @todo do we still want to support this ? + /*$fields = field_read_fields(array('entity_type' => $instance_definition['entity_type'], 'bundle' => $instance_definition['bundle'])); $this->assertTrue(count($fields) == 1 && isset($fields[$field_definition['field_name']]), 'The field was properly read.'); $fields = field_read_fields(array('entity_type' => $instance_definition['entity_type'], 'field_name' => $instance_definition['field_name'])); - $this->assertTrue(count($fields) == 1 && isset($fields[$field_definition['field_name']]), 'The field was properly read.'); + $this->assertTrue(count($fields) == 1 && isset($fields[$field_definition['field_name']]), 'The field was properly read.');*/ } /** @@ -239,7 +239,7 @@ function testFieldIndexes() { field_create_field($field_definition); $field = field_read_field($field_definition['field_name']); $expected_indexes = array('value' => array('value')); - $this->assertEqual($field['indexes'], $expected_indexes, 'Field type indexes saved by default'); + $this->assertEqual($field->indexes, $expected_indexes, 'Field type indexes saved by default'); // Check that indexes specified by the field definition override the field // type indexes. @@ -253,7 +253,7 @@ function testFieldIndexes() { field_create_field($field_definition); $field = field_read_field($field_definition['field_name']); $expected_indexes = array('value' => array()); - $this->assertEqual($field['indexes'], $expected_indexes, 'Field definition indexes override field type indexes'); + $this->assertEqual($field->indexes, $expected_indexes, 'Field definition indexes override field type indexes'); // Check that indexes specified by the field definition add to the field // type indexes. @@ -267,7 +267,7 @@ function testFieldIndexes() { field_create_field($field_definition); $field = field_read_field($field_definition['field_name']); $expected_indexes = array('value' => array('value'), 'value_2' => array('value')); - $this->assertEqual($field['indexes'], $expected_indexes, 'Field definition indexes are merged with field type indexes'); + $this->assertEqual($field->indexes, $expected_indexes, 'Field definition indexes are merged with field type indexes'); } /** @@ -278,13 +278,13 @@ function testDeleteField() { // Create two fields (so we can test that only one is deleted). $this->field = array('field_name' => 'field_1', 'type' => 'test_field'); - field_create_field($this->field); + $this->field = field_create_field($this->field); $this->another_field = array('field_name' => 'field_2', 'type' => 'test_field'); - field_create_field($this->another_field); + $this->another_field = field_create_field($this->another_field); // Create instances for each. $this->instance_definition = array( - 'field_name' => $this->field['field_name'], + 'field_name' => $this->field->field_name, 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', 'widget' => array( @@ -293,18 +293,18 @@ function testDeleteField() { ); field_create_instance($this->instance_definition); $this->another_instance_definition = $this->instance_definition; - $this->another_instance_definition['field_name'] = $this->another_field['field_name']; + $this->another_instance_definition['field_name'] = $this->another_field->field_name; field_create_instance($this->another_instance_definition); // Test that the first field is not deleted, and then delete it. - $field = field_read_field($this->field['field_name'], array('include_deleted' => TRUE)); - $this->assertTrue(!empty($field) && empty($field['deleted']), 'A new field is not marked for deletion.'); - field_delete_field($this->field['field_name']); + $field = field_read_field($this->field->field_name, array('include_deleted' => TRUE)); + $this->assertTrue(!empty($field) && empty($field->deleted), 'A new field is not marked for deletion.'); + field_delete_field($this->field->field_name); // Make sure that the field is marked as deleted when it is specifically // loaded. - $field = field_read_field($this->field['field_name'], array('include_deleted' => TRUE)); - $this->assertTrue(!empty($field['deleted']), 'A deleted field is marked for deletion.'); + $deleted_fields = state()->get('field.field.deleted'); + $this->assertTrue(isset($deleted_fields[$field->id()]), 'A deleted field is marked for deletion.'); // Make sure that this field's instance is marked as deleted when it is // specifically loaded. @@ -312,7 +312,7 @@ function testDeleteField() { $this->assertTrue(!empty($instance['deleted']), 'An instance for a deleted field is marked for deletion.'); // Try to load the field normally and make sure it does not show up. - $field = field_read_field($this->field['field_name']); + $field = field_read_field($this->field->field_name); $this->assertTrue(empty($field), 'A deleted field is not loaded by default.'); // Try to load the instance normally and make sure it does not show up. @@ -320,8 +320,8 @@ function testDeleteField() { $this->assertTrue(empty($instance), 'An instance for a deleted field is not loaded by default.'); // Make sure the other field (and its field instance) are not deleted. - $another_field = field_read_field($this->another_field['field_name']); - $this->assertTrue(!empty($another_field) && empty($another_field['deleted']), 'A non-deleted field is not marked for deletion.'); + $another_field = field_read_field($this->another_field->field_name); + $this->assertTrue(!empty($another_field) && empty($another_field->deleted), 'A non-deleted field is not marked for deletion.'); $another_instance = field_read_instance('test_entity', $this->another_instance_definition['field_name'], $this->another_instance_definition['bundle']); $this->assertTrue(!empty($another_instance) && empty($another_instance['deleted']), 'An instance of a non-deleted field is not marked for deletion.'); @@ -329,25 +329,25 @@ function testDeleteField() { // write data into it. field_create_field($this->field); field_create_instance($this->instance_definition); - $field = field_read_field($this->field['field_name']); - $this->assertTrue(!empty($field) && empty($field['deleted']), 'A new field with a previously used name is created.'); + $field = field_read_field($this->field->field_name); + $this->assertTrue(!empty($field) && empty($field->deleted), 'A new field with a previously used name is created.'); $instance = field_read_instance('test_entity', $this->instance_definition['field_name'], $this->instance_definition['bundle']); $this->assertTrue(!empty($instance) && empty($instance['deleted']), 'A new instance for a previously used field name is created.'); // Save an entity with data for the field - $entity = field_test_create_entity(0, 0, $instance['bundle']); + $entity = field_test_create_entity(0, 0, $instance->bundle); $langcode = LANGUAGE_NOT_SPECIFIED; $values[0]['value'] = mt_rand(1, 127); - $entity->{$field['field_name']}[$langcode] = $values; + $entity->{$field->field_name}[$langcode] = $values; $entity_type = 'test_entity'; field_attach_insert($entity); // Verify the field is present on load $entity = field_test_create_entity(0, 0, $this->instance_definition['bundle']); field_attach_load($entity_type, array(0 => $entity)); - $this->assertIdentical(count($entity->{$field['field_name']}[$langcode]), count($values), "Data in previously deleted field saves and loads correctly"); + $this->assertIdentical(count($entity->{$field->field_name}[$langcode]), count($values), "Data in previously deleted field saves and loads correctly"); foreach ($values as $delta => $value) { - $this->assertEqual($entity->{$field['field_name']}[$langcode][$delta]['value'], $values[$delta]['value'], "Data in previously deleted field saves and loads correctly"); + $this->assertEqual($entity->{$field->field_name}[$langcode][$delta]['value'], $values[$delta]['value'], "Data in previously deleted field saves and loads correctly"); } } @@ -389,7 +389,7 @@ function testUpdateField() { 'type' => 'test_field', 'cardinality' => $cardinality, ); - $field_definition = field_create_field($field_definition); + field_create_field($field_definition); $instance = array( 'field_name' => 'field_update', 'entity_type' => 'test_entity', @@ -400,7 +400,7 @@ function testUpdateField() { do { // We need a unique ID for our entity. $cardinality will do. $id = $cardinality; - $entity = field_test_create_entity($id, $id, $instance['bundle']); + $entity = field_test_create_entity($id, $id, $instance->bundle); // Fill in the entity with more values than $cardinality. for ($i = 0; $i < 20; $i++) { $entity->field_update[LANGUAGE_NOT_SPECIFIED][$i]['value'] = $i; @@ -408,7 +408,7 @@ function testUpdateField() { // Save the entity. field_attach_insert($entity); // Load back and assert there are $cardinality number of values. - $entity = field_test_create_entity($id, $id, $instance['bundle']); + $entity = field_test_create_entity($id, $id, $instance->bundle); field_attach_load('test_entity', array($id => $entity)); $this->assertEqual(count($entity->field_update[LANGUAGE_NOT_SPECIFIED]), $field_definition['cardinality'], 'Cardinality is kept'); // Now check the values themselves. @@ -427,7 +427,7 @@ function testUpdateField() { function testUpdateFieldForbid() { $field = array('field_name' => 'forbidden', 'type' => 'test_field', 'settings' => array('changeable' => 0, 'unchangeable' => 0)); $field = field_create_field($field); - $field['settings']['changeable']++; + $field->settings['changeable']++; try { field_update_field($field); $this->pass(t("A changeable setting can be updated.")); @@ -435,7 +435,7 @@ function testUpdateFieldForbid() { catch (FieldException $e) { $this->fail(t("An unchangeable setting cannot be updated.")); } - $field['settings']['unchangeable']++; + $field->settings['unchangeable']++; try { field_update_field($field); $this->fail(t("An unchangeable setting can be updated.")); @@ -505,6 +505,6 @@ function _testActiveHelper($field_definition, $modules) { // Check that the field is active again after all modules have been // enabled. $field = field_read_field($field_name); - $this->assertTrue($field_definition <= $field, 'The field was was marked active.'); + $this->assertTrue($field_definition <= $field, 'The field was marked active.'); } } diff --git a/core/modules/field/lib/Drupal/field/Tests/DisplayApiTest.php b/core/modules/field/lib/Drupal/field/Tests/DisplayApiTest.php index 8c28727..7f2807f 100644 --- a/core/modules/field/lib/Drupal/field/Tests/DisplayApiTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/DisplayApiTest.php @@ -62,11 +62,11 @@ function setUp() { field_create_field($this->field); field_create_instance($this->instance); // Create a display for the default view mode. - entity_get_display($this->instance['entity_type'], $this->instance['bundle'], 'default') + entity_get_display($this->instance->entity_type, $this->instance->bundle, 'default') ->setComponent($this->field_name, $this->display_options['default']) ->save(); // Create a display for the teaser view mode. - entity_get_display($this->instance['entity_type'], $this->instance['bundle'], 'teaser') + entity_get_display($this->instance->entity_type, $this->instance->bundle, 'teaser') ->setComponent($this->field_name, $this->display_options['teaser']) ->save(); diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldAccessTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldAccessTest.php index bc1a380..8f237bb 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldAccessTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldAccessTest.php @@ -37,13 +37,13 @@ function setUp() { $this->content_type_info = $this->drupalCreateContentType(); $this->content_type = $this->content_type_info->type; - $this->field = array( + $field = array( 'field_name' => 'test_view_field', 'type' => 'text', ); - field_create_field($this->field); + $this->field = field_create_field($field); $this->instance = array( - 'field_name' => $this->field['field_name'], + 'field_name' => $this->field->field_name, 'entity_type' => 'node', 'bundle' => $this->content_type, 'widget' => array( @@ -55,7 +55,7 @@ function setUp() { // Assign display properties for the 'default' and 'teaser' view modes. foreach (array('default', 'teaser') as $view_mode) { entity_get_display('node', $this->content_type, $view_mode) - ->setComponent($this->field['field_name']) + ->setComponent($this->field->field_name) ->save(); } diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php index 89e07c6..0d4dfea 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php @@ -33,9 +33,9 @@ function testFieldAttachView() { $options = array('field_name' => $this->field_name_2); // Populate values to be displayed. - $values = $this->_generateTestFieldValues($this->field['cardinality']); + $values = $this->_generateTestFieldValues($this->field->cardinality); $entity_init->{$this->field_name}[$langcode] = $values; - $values_2 = $this->_generateTestFieldValues($this->field_2['cardinality']); + $values_2 = $this->_generateTestFieldValues($this->field_2->cardinality); $entity_init->{$this->field_name_2}[$langcode] = $values_2; // Simple formatter, label displayed. @@ -51,7 +51,7 @@ function testFieldAttachView() { 'test_formatter_setting' => $formatter_setting, ), ); - $display->setComponent($this->field['field_name'], $display_options); + $display->setComponent($this->field->field_name, $display_options); $formatter_setting_2 = $this->randomName(); $display_options_2 = array( @@ -61,14 +61,14 @@ function testFieldAttachView() { 'test_formatter_setting' => $formatter_setting_2, ), ); - $display->setComponent($this->field_2['field_name'], $display_options_2); + $display->setComponent($this->field_2->field_name, $display_options_2); // View all fields. field_attach_prepare_view($entity_type, array($entity->ftid => $entity), $displays); $entity->content = field_attach_view($entity, $display); $output = drupal_render($entity->content); $this->content = $output; - $this->assertRaw($this->instance['label'], "First field's label is displayed."); + $this->assertRaw($this->instance->label, "First field's label is displayed."); foreach ($values as $delta => $value) { $this->content = $output; $this->assertRaw("$formatter_setting|{$value['value']}", "Value $delta is displayed, formatter settings are applied."); @@ -83,7 +83,7 @@ function testFieldAttachView() { $entity->content = field_attach_view($entity, $display, $langcode, $options); $output = drupal_render($entity->content); $this->content = $output; - $this->assertNoRaw($this->instance['label'], "First field's label is not displayed."); + $this->assertNoRaw($this->instance->label, "First field's label is not displayed."); foreach ($values as $delta => $value) { $this->content = $output; $this->assertNoRaw("$formatter_setting|{$value['value']}", "Value $delta is displayed, formatter settings are applied."); @@ -97,21 +97,21 @@ function testFieldAttachView() { // Label hidden. $entity = clone($entity_init); $display_options['label'] = 'hidden'; - $display->setComponent($this->field['field_name'], $display_options); + $display->setComponent($this->field->field_name, $display_options); field_attach_prepare_view($entity_type, array($entity->ftid => $entity), $displays); $entity->content = field_attach_view($entity, $display); $output = drupal_render($entity->content); $this->content = $output; - $this->assertNoRaw($this->instance['label'], "Hidden label: label is not displayed."); + $this->assertNoRaw($this->instance->label, "Hidden label: label is not displayed."); // Field hidden. $entity = clone($entity_init); - $display->removeComponent($this->field['field_name']); + $display->removeComponent($this->field->field_name); field_attach_prepare_view($entity_type, array($entity->ftid => $entity), $displays); $entity->content = field_attach_view($entity, $display); $output = drupal_render($entity->content); $this->content = $output; - $this->assertNoRaw($this->instance['label'], "Hidden field: label is not displayed."); + $this->assertNoRaw($this->instance->label, "Hidden field: label is not displayed."); foreach ($values as $delta => $value) { $this->assertNoRaw("$formatter_setting|{$value['value']}", "Hidden field: value $delta is not displayed."); } @@ -119,7 +119,7 @@ function testFieldAttachView() { // Multiple formatter. $entity = clone($entity_init); $formatter_setting = $this->randomName(); - $display->setComponent($this->field['field_name'], array( + $display->setComponent($this->field->field_name, array( 'label' => 'above', 'type' => 'field_test_multiple', 'settings' => array( @@ -139,7 +139,7 @@ function testFieldAttachView() { // Test a formatter that uses hook_field_formatter_prepare_view(). $entity = clone($entity_init); $formatter_setting = $this->randomName(); - $display->setComponent($this->field['field_name'], array( + $display->setComponent($this->field->field_name, array( 'label' => 'above', 'type' => 'field_test_with_prepare_view', 'settings' => array( @@ -180,7 +180,7 @@ function testFieldAttachPrepareViewMultiple() { // Set the instance to be hidden. $display = entity_get_display('test_entity', 'test_bundle', 'full') - ->removeComponent($this->field['field_name']); + ->removeComponent($this->field->field_name); // Set up a second instance on another bundle, with a formatter that uses // hook_field_formatter_prepare_view(). @@ -191,7 +191,7 @@ function testFieldAttachPrepareViewMultiple() { field_create_instance($this->instance2); $display_2 = entity_get_display('test_entity', 'test_bundle_2', 'full') - ->setComponent($this->field['field_name'], array( + ->setComponent($this->field->field_name, array( 'type' => 'field_test_with_prepare_view', 'settings' => array( 'test_formatter_setting_additional' => $formatter_setting, @@ -202,11 +202,11 @@ function testFieldAttachPrepareViewMultiple() { // Create one entity in each bundle. $entity1_init = field_test_create_entity(1, 1, 'test_bundle'); - $values1 = $this->_generateTestFieldValues($this->field['cardinality']); + $values1 = $this->_generateTestFieldValues($this->field->cardinality); $entity1_init->{$this->field_name}[$langcode] = $values1; $entity2_init = field_test_create_entity(2, 2, 'test_bundle_2'); - $values2 = $this->_generateTestFieldValues($this->field['cardinality']); + $values2 = $this->_generateTestFieldValues($this->field->cardinality); $entity2_init->{$this->field_name}[$langcode] = $values2; // Run prepare_view, and check that the entities come out as expected. @@ -231,9 +231,9 @@ function testFieldAttachPrepareViewMultiple() { */ function testFieldAttachCache() { // Initialize random values and a test entity. - $entity_init = field_test_create_entity(1, 1, $this->instance['bundle']); + $entity_init = field_test_create_entity(1, 1, $this->instance->bundle); $langcode = LANGUAGE_NOT_SPECIFIED; - $values = $this->_generateTestFieldValues($this->field['cardinality']); + $values = $this->_generateTestFieldValues($this->field->cardinality); // Non-cacheable entity type. $entity_type = 'test_entity'; @@ -259,11 +259,11 @@ function testFieldAttachCache() { $entity_init = entity_create($entity_type, array( 'ftid' => 1, 'ftvid' => 1, - 'fttype' => $this->instance['bundle'], + 'fttype' => $this->instance->bundle, )); $cid = "field:$entity_type:{$entity_init->ftid}"; $instance = $this->instance; - $instance['entity_type'] = $entity_type; + $instance->entity_type = $entity_type; field_create_instance($instance); // Check that no initial cache entry is present. @@ -288,7 +288,7 @@ function testFieldAttachCache() { $this->assertEqual($cache->data[$this->field_name][$langcode], $values, 'Cached: correct cache entry on load'); // Update with different values, and check that the cache entry is wiped. - $values = $this->_generateTestFieldValues($this->field['cardinality']); + $values = $this->_generateTestFieldValues($this->field->cardinality); $entity = clone($entity_init); $entity->{$this->field_name}[$langcode] = $values; field_attach_update($entity); @@ -304,9 +304,9 @@ function testFieldAttachCache() { $entity_init = entity_create($entity_type, array( 'ftid' => 1, 'ftvid' => 2, - 'fttype' => $this->instance['bundle'], + 'fttype' => $this->instance->bundle, )); - $values = $this->_generateTestFieldValues($this->field['cardinality']); + $values = $this->_generateTestFieldValues($this->field->cardinality); $entity = clone($entity_init); $entity->{$this->field_name}[$langcode] = $values; field_attach_update($entity); @@ -334,12 +334,12 @@ function testFieldAttachValidate() { $this->createFieldWithInstance('_2'); $entity_type = 'test_entity'; - $entity = field_test_create_entity(0, 0, $this->instance['bundle']); + $entity = field_test_create_entity(0, 0, $this->instance->bundle); $langcode = LANGUAGE_NOT_SPECIFIED; // Set up all but one values of the first field to generate errors. $values = array(); - for ($delta = 0; $delta < $this->field['cardinality']; $delta++) { + for ($delta = 0; $delta < $this->field->cardinality; $delta++) { $values[$delta]['value'] = -1; } // Arrange for item 1 not to generate an error. @@ -348,7 +348,7 @@ function testFieldAttachValidate() { // Set up all values of the second field to generate errors. $values_2 = array(); - for ($delta = 0; $delta < $this->field_2['cardinality']; $delta++) { + for ($delta = 0; $delta < $this->field_2->cardinality; $delta++) { $values_2[$delta]['value'] = -1; } $entity->{$this->field_name_2}[$langcode] = $values_2; @@ -397,7 +397,7 @@ function testFieldAttachValidate() { $this->assertEqual(count($errors[$this->field_name_2][$langcode]), 0, 'No extraneous errors set for second field'); // Check that cardinality is validated. - $entity->{$this->field_name_2}[$langcode] = $this->_generateTestFieldValues($this->field_2['cardinality'] + 1); + $entity->{$this->field_name_2}[$langcode] = $this->_generateTestFieldValues($this->field_2->cardinality + 1); // When validating all fields. try { field_attach_validate($entity); @@ -426,7 +426,7 @@ function testFieldAttachForm() { $this->createFieldWithInstance('_2'); $entity_type = 'test_entity'; - $entity = field_test_create_entity(0, 0, $this->instance['bundle']); + $entity = field_test_create_entity(0, 0, $this->instance->bundle); $langcode = LANGUAGE_NOT_SPECIFIED; // When generating form for all fields. @@ -434,13 +434,13 @@ function testFieldAttachForm() { $form_state = form_state_defaults(); field_attach_form($entity, $form, $form_state); - $this->assertEqual($form[$this->field_name][$langcode]['#title'], $this->instance['label'], "First field's form title is {$this->instance['label']}"); + $this->assertEqual($form[$this->field_name][$langcode]['#title'], $this->instance->label, "First field's form title is {$this->instance->label}"); $this->assertEqual($form[$this->field_name_2][$langcode]['#title'], $this->instance_2['label'], "Second field's form title is {$this->instance_2['label']}"); - for ($delta = 0; $delta < $this->field['cardinality']; $delta++) { + for ($delta = 0; $delta < $this->field->cardinality; $delta++) { // field_test_widget uses 'textfield' $this->assertEqual($form[$this->field_name][$langcode][$delta]['value']['#type'], 'textfield', "First field's form delta $delta widget is textfield"); } - for ($delta = 0; $delta < $this->field_2['cardinality']; $delta++) { + for ($delta = 0; $delta < $this->field_2->cardinality; $delta++) { // field_test_widget uses 'textfield' $this->assertEqual($form[$this->field_name_2][$langcode][$delta]['value']['#type'], 'textfield', "Second field's form delta $delta widget is textfield"); } @@ -453,7 +453,7 @@ function testFieldAttachForm() { $this->assertFalse(isset($form[$this->field_name]), 'The first field does not exist in the form'); $this->assertEqual($form[$this->field_name_2][$langcode]['#title'], $this->instance_2['label'], "Second field's form title is {$this->instance_2['label']}"); - for ($delta = 0; $delta < $this->field_2['cardinality']; $delta++) { + for ($delta = 0; $delta < $this->field_2->cardinality; $delta++) { // field_test_widget uses 'textfield' $this->assertEqual($form[$this->field_name_2][$langcode][$delta]['value']['#type'], 'textfield', "Second field's form delta $delta widget is textfield"); } @@ -466,7 +466,7 @@ function testFieldAttachExtractFormValues() { $this->createFieldWithInstance('_2'); $entity_type = 'test_entity'; - $entity_init = field_test_create_entity(0, 0, $this->instance['bundle']); + $entity_init = field_test_create_entity(0, 0, $this->instance->bundle); $langcode = LANGUAGE_NOT_SPECIFIED; // Build the form for all fields. @@ -478,11 +478,11 @@ function testFieldAttachExtractFormValues() { // First field. $values = array(); $weights = array(); - for ($delta = 0; $delta < $this->field['cardinality']; $delta++) { + for ($delta = 0; $delta < $this->field->cardinality; $delta++) { $values[$delta]['value'] = mt_rand(1, 127); // Assign random weight. do { - $weight = mt_rand(0, $this->field['cardinality']); + $weight = mt_rand(0, $this->field->cardinality); } while (in_array($weight, $weights)); $weights[$delta] = $weight; $values[$delta]['_weight'] = $weight; @@ -492,11 +492,11 @@ function testFieldAttachExtractFormValues() { // Second field. $values_2 = array(); $weights_2 = array(); - for ($delta = 0; $delta < $this->field_2['cardinality']; $delta++) { + for ($delta = 0; $delta < $this->field_2->cardinality; $delta++) { $values_2[$delta]['value'] = mt_rand(1, 127); // Assign random weight. do { - $weight = mt_rand(0, $this->field_2['cardinality']); + $weight = mt_rand(0, $this->field_2->cardinality); } while (in_array($weight, $weights_2)); $weights_2[$delta] = $weight; $values_2[$delta]['_weight'] = $weight; diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php index 5ae3610..9072351 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php @@ -31,7 +31,7 @@ public static function getInfo() { function testFieldAttachSaveLoad() { // Configure the instance so that we test hook_field_load() (see // field_test_field_load() in field_test.module). - $this->instance['settings']['test_hook_field_load'] = TRUE; + $this->instance->settings['test_hook_field_load'] = TRUE; field_update_instance($this->instance); $langcode = LANGUAGE_NOT_SPECIFIED; @@ -42,9 +42,9 @@ function testFieldAttachSaveLoad() { // Preparation: create three revisions and store them in $revision array. for ($revision_id = 0; $revision_id < 3; $revision_id++) { - $revision[$revision_id] = field_test_create_entity(0, $revision_id, $this->instance['bundle']); + $revision[$revision_id] = field_test_create_entity(0, $revision_id, $this->instance->bundle); // Note: we try to insert one extra value. - $values[$revision_id] = $this->_generateTestFieldValues($this->field['cardinality'] + 1); + $values[$revision_id] = $this->_generateTestFieldValues($this->field->cardinality + 1); $current_revision = $revision_id; // If this is the first revision do an insert. if (!$revision_id) { @@ -59,11 +59,11 @@ function testFieldAttachSaveLoad() { } // Confirm current revision loads the correct data. - $entity = field_test_create_entity(0, 0, $this->instance['bundle']); + $entity = field_test_create_entity(0, 0, $this->instance->bundle); field_attach_load($entity_type, array(0 => $entity)); // Number of values per field loaded equals the field cardinality. - $this->assertEqual(count($entity->{$this->field_name}[$langcode]), $this->field['cardinality'], 'Current revision: expected number of values'); - for ($delta = 0; $delta < $this->field['cardinality']; $delta++) { + $this->assertEqual(count($entity->{$this->field_name}[$langcode]), $this->field->cardinality, 'Current revision: expected number of values'); + for ($delta = 0; $delta < $this->field->cardinality; $delta++) { // The field value loaded matches the one inserted or updated. $this->assertEqual($entity->{$this->field_name}[$langcode][$delta]['value'] , $values[$current_revision][$delta]['value'], format_string('Current revision: expected value %delta was found.', array('%delta' => $delta))); // The value added in hook_field_load() is found. @@ -72,11 +72,11 @@ function testFieldAttachSaveLoad() { // Confirm each revision loads the correct data. foreach (array_keys($revision) as $revision_id) { - $entity = field_test_create_entity(0, $revision_id, $this->instance['bundle']); + $entity = field_test_create_entity(0, $revision_id, $this->instance->bundle); field_attach_load_revision($entity_type, array(0 => $entity)); // Number of values per field loaded equals the field cardinality. - $this->assertEqual(count($entity->{$this->field_name}[$langcode]), $this->field['cardinality'], format_string('Revision %revision_id: expected number of values.', array('%revision_id' => $revision_id))); - for ($delta = 0; $delta < $this->field['cardinality']; $delta++) { + $this->assertEqual(count($entity->{$this->field_name}[$langcode]), $this->field->cardinality, format_string('Revision %revision_id: expected number of values.', array('%revision_id' => $revision_id))); + for ($delta = 0; $delta < $this->field->cardinality; $delta++) { // The field value loaded matches the one inserted or updated. $this->assertEqual($entity->{$this->field_name}[$langcode][$delta]['value'], $values[$revision_id][$delta]['value'], format_string('Revision %revision_id: expected value %delta was found.', array('%revision_id' => $revision_id, '%delta' => $delta))); // The value added in hook_field_load() is found. @@ -112,7 +112,7 @@ function testFieldAttachLoadMultiple() { $field_names[$i] = 'field_' . $i; $field = array('field_name' => $field_names[$i], 'type' => 'test_field'); $field = field_create_field($field); - $field_ids[$i] = $field['id']; + $field_ids[$i] = $field->id(); foreach ($field_bundles_map[$i] as $bundle) { $instance = array( 'field_name' => $field_names[$i], @@ -199,7 +199,7 @@ function testFieldAttachSaveLoadDifferentStorage() { $entity = clone($entity_init); $values = array(); foreach ($fields as $field) { - $values[$field['field_name']] = $this->_generateTestFieldValues($this->field['cardinality']); + $values[$field['field_name']] = $this->_generateTestFieldValues($this->field->cardinality); $entity->{$field['field_name']}[$langcode] = $values[$field['field_name']]; } field_attach_insert($entity); @@ -233,13 +233,13 @@ function testFieldStorageDetailsAlter() { ); field_create_instance($instance); - $field = field_info_field($instance['field_name']); - $instance = field_info_instance($instance['entity_type'], $instance['field_name'], $instance['bundle']); + $field = field_info_field($instance->field_name); + $instance = field_info_instance($instance->entity_type, $instance->field_name, $instance->bundle); // The storage details are indexed by a storage engine type. - $this->assertTrue(array_key_exists('drupal_variables', $field['storage']['details']), 'The storage type is Drupal variables.'); + $this->assertTrue(array_key_exists('drupal_variables', $field->storage['details']), 'The storage type is Drupal variables.'); - $details = $field['storage']['details']['drupal_variables']; + $details = $field->storage['details']['drupal_variables']; // The field_test storage details are indexed by variable name. The details // are altered, so moon and mars are correct for this test. @@ -248,7 +248,7 @@ function testFieldStorageDetailsAlter() { // Test current and revision storage details together because the columns // are the same. - foreach ((array) $field['columns'] as $column_name => $attributes) { + foreach ($field->columns as $column_name => $attributes) { $this->assertEqual($details[FIELD_LOAD_CURRENT]['moon'][$column_name], $column_name, format_string('Column name %value matches the definition in %bin.', array('%value' => $column_name, '%bin' => 'moon[FIELD_LOAD_CURRENT]'))); $this->assertEqual($details[FIELD_LOAD_REVISION]['mars'][$column_name], $column_name, format_string('Column name %value matches the definition in %bin.', array('%value' => $column_name, '%bin' => 'mars[FIELD_LOAD_REVISION]'))); } @@ -379,51 +379,51 @@ function testFieldAttachSaveMissingDataDefaultValue() { function testFieldAttachDelete() { $entity_type = 'test_entity'; $langcode = LANGUAGE_NOT_SPECIFIED; - $rev[0] = field_test_create_entity(0, 0, $this->instance['bundle']); + $rev[0] = field_test_create_entity(0, 0, $this->instance->bundle); // Create revision 0 - $values = $this->_generateTestFieldValues($this->field['cardinality']); + $values = $this->_generateTestFieldValues($this->field->cardinality); $rev[0]->{$this->field_name}[$langcode] = $values; field_attach_insert($rev[0]); // Create revision 1 - $rev[1] = field_test_create_entity(0, 1, $this->instance['bundle']); + $rev[1] = field_test_create_entity(0, 1, $this->instance->bundle); $rev[1]->{$this->field_name}[$langcode] = $values; field_attach_update($rev[1]); // Create revision 2 - $rev[2] = field_test_create_entity(0, 2, $this->instance['bundle']); + $rev[2] = field_test_create_entity(0, 2, $this->instance->bundle); $rev[2]->{$this->field_name}[$langcode] = $values; field_attach_update($rev[2]); // Confirm each revision loads foreach (array_keys($rev) as $vid) { - $read = field_test_create_entity(0, $vid, $this->instance['bundle']); + $read = field_test_create_entity(0, $vid, $this->instance->bundle); field_attach_load_revision($entity_type, array(0 => $read)); - $this->assertEqual(count($read->{$this->field_name}[$langcode]), $this->field['cardinality'], "The test entity revision $vid has {$this->field['cardinality']} values."); + $this->assertEqual(count($read->{$this->field_name}[$langcode]), $this->field->cardinality, "The test entity revision $vid has {$this->field->cardinality} values."); } // Delete revision 1, confirm the other two still load. field_attach_delete_revision($rev[1]); foreach (array(0, 2) as $vid) { - $read = field_test_create_entity(0, $vid, $this->instance['bundle']); + $read = field_test_create_entity(0, $vid, $this->instance->bundle); field_attach_load_revision($entity_type, array(0 => $read)); - $this->assertEqual(count($read->{$this->field_name}[$langcode]), $this->field['cardinality'], "The test entity revision $vid has {$this->field['cardinality']} values."); + $this->assertEqual(count($read->{$this->field_name}[$langcode]), $this->field->cardinality, "The test entity revision $vid has {$this->field->cardinality} values."); } // Confirm the current revision still loads - $read = field_test_create_entity(0, 2, $this->instance['bundle']); + $read = field_test_create_entity(0, 2, $this->instance->bundle); field_attach_load($entity_type, array(0 => $read)); - $this->assertEqual(count($read->{$this->field_name}[$langcode]), $this->field['cardinality'], "The test entity current revision has {$this->field['cardinality']} values."); + $this->assertEqual(count($read->{$this->field_name}[$langcode]), $this->field->cardinality, "The test entity current revision has {$this->field->cardinality} values."); // Delete all field data, confirm nothing loads field_attach_delete($rev[2]); foreach (array(0, 1, 2) as $vid) { - $read = field_test_create_entity(0, $vid, $this->instance['bundle']); + $read = field_test_create_entity(0, $vid, $this->instance->bundle); field_attach_load_revision($entity_type, array(0 => $read)); $this->assertIdentical($read->{$this->field_name}, array(), "The test entity revision $vid is deleted."); } - $read = field_test_create_entity(0, 2, $this->instance['bundle']); + $read = field_test_create_entity(0, 2, $this->instance->bundle); field_attach_load($entity_type, array(0 => $read)); $this->assertIdentical($read->{$this->field_name}, array(), 'The test entity current revision is deleted.'); } @@ -437,34 +437,34 @@ function testFieldAttachCreateRenameBundle() { field_test_create_bundle($new_bundle); // Add an instance to that bundle. - $this->instance['bundle'] = $new_bundle; + $this->instance->bundle = $new_bundle; field_create_instance($this->instance); // Save an entity with data in the field. - $entity = field_test_create_entity(0, 0, $this->instance['bundle']); + $entity = field_test_create_entity(0, 0, $this->instance->bundle); $langcode = LANGUAGE_NOT_SPECIFIED; - $values = $this->_generateTestFieldValues($this->field['cardinality']); + $values = $this->_generateTestFieldValues($this->field->cardinality); $entity->{$this->field_name}[$langcode] = $values; $entity_type = 'test_entity'; field_attach_insert($entity); // Verify the field data is present on load. - $entity = field_test_create_entity(0, 0, $this->instance['bundle']); + $entity = field_test_create_entity(0, 0, $this->instance->bundle); field_attach_load($entity_type, array(0 => $entity)); - $this->assertEqual(count($entity->{$this->field_name}[$langcode]), $this->field['cardinality'], "Data is retrieved for the new bundle"); + $this->assertEqual(count($entity->{$this->field_name}[$langcode]), $this->field->cardinality, "Data is retrieved for the new bundle"); // Rename the bundle. $new_bundle = 'test_bundle_' . drupal_strtolower($this->randomName()); - field_test_rename_bundle($this->instance['bundle'], $new_bundle); + field_test_rename_bundle($this->instance->bundle, $new_bundle); // Check that the instance definition has been updated. $this->instance = field_info_instance($entity_type, $this->field_name, $new_bundle); - $this->assertIdentical($this->instance['bundle'], $new_bundle, "Bundle name has been updated in the instance."); + $this->assertIdentical($this->instance->bundle, $new_bundle, "Bundle name has been updated in the instance."); // Verify the field data is present on load. $entity = field_test_create_entity(0, 0, $new_bundle); field_attach_load($entity_type, array(0 => $entity)); - $this->assertEqual(count($entity->{$this->field_name}[$langcode]), $this->field['cardinality'], "Bundle name has been updated in the field storage"); + $this->assertEqual(count($entity->{$this->field_name}[$langcode]), $this->field->cardinality, "Bundle name has been updated in the field storage"); } /** @@ -476,7 +476,7 @@ function testFieldAttachDeleteBundle() { field_test_create_bundle($new_bundle); // Add an instance to that bundle. - $this->instance['bundle'] = $new_bundle; + $this->instance->bundle = $new_bundle; field_create_instance($this->instance); // Create a second field for the test bundle @@ -486,7 +486,7 @@ function testFieldAttachDeleteBundle() { $instance = array( 'field_name' => $field_name, 'entity_type' => 'test_entity', - 'bundle' => $this->instance['bundle'], + 'bundle' => $this->instance->bundle, 'label' => $this->randomName() . '_label', 'description' => $this->randomName() . '_description', 'weight' => mt_rand(0, 127), @@ -498,30 +498,30 @@ function testFieldAttachDeleteBundle() { field_create_instance($instance); // Save an entity with data for both fields - $entity = field_test_create_entity(0, 0, $this->instance['bundle']); + $entity = field_test_create_entity(0, 0, $this->instance->bundle); $langcode = LANGUAGE_NOT_SPECIFIED; - $values = $this->_generateTestFieldValues($this->field['cardinality']); + $values = $this->_generateTestFieldValues($this->field->cardinality); $entity->{$this->field_name}[$langcode] = $values; $entity->{$field_name}[$langcode] = $this->_generateTestFieldValues(1); field_attach_insert($entity); // Verify the fields are present on load - $entity = field_test_create_entity(0, 0, $this->instance['bundle']); + $entity = field_test_create_entity(0, 0, $this->instance->bundle); field_attach_load('test_entity', array(0 => $entity)); $this->assertEqual(count($entity->{$this->field_name}[$langcode]), 4, 'First field got loaded'); $this->assertEqual(count($entity->{$field_name}[$langcode]), 1, 'Second field got loaded'); // Delete the bundle. - field_test_delete_bundle($this->instance['bundle']); + field_test_delete_bundle($this->instance->bundle); // Verify no data gets loaded - $entity = field_test_create_entity(0, 0, $this->instance['bundle']); + $entity = field_test_create_entity(0, 0, $this->instance->bundle); field_attach_load('test_entity', array(0 => $entity)); $this->assertFalse(isset($entity->{$this->field_name}[$langcode]), 'No data for first field'); $this->assertFalse(isset($entity->{$field_name}[$langcode]), 'No data for second field'); // Verify that the instances are gone - $this->assertFalse(field_read_instance('test_entity', $this->field_name, $this->instance['bundle']), "First field is deleted"); - $this->assertFalse(field_read_instance('test_entity', $field_name, $instance['bundle']), "Second field is deleted"); + $this->assertFalse(field_read_instance('test_entity', $this->field_name, $this->instance->bundle), "First field is deleted"); + $this->assertFalse(field_read_instance('test_entity', $field_name, $instance->bundle), "Second field is deleted"); } } diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldAttachTestBase.php b/core/modules/field/lib/Drupal/field/Tests/FieldAttachTestBase.php index 9d15ba2..53bf543 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldAttachTestBase.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldAttachTestBase.php @@ -38,7 +38,7 @@ function createFieldWithInstance($suffix = '') { $this->$field_name = drupal_strtolower($this->randomName() . '_field_name' . $suffix); $this->$field = array('field_name' => $this->$field_name, 'type' => 'test_field', 'cardinality' => 4); $this->$field = field_create_field($this->$field); - $this->$field_id = $this->{$field}['id']; + $this->$field_id = $this->{$field}->uuid; $this->$instance = array( 'field_name' => $this->$field_name, 'entity_type' => 'test_entity', diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldImportChangeTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldImportChangeTest.php new file mode 100644 index 0000000..67657da --- /dev/null +++ b/core/modules/field/lib/Drupal/field/Tests/FieldImportChangeTest.php @@ -0,0 +1,69 @@ + 'Field config change tests', + 'description' => 'Update field and instances during config change method invocation.', + 'group' => 'Field API', + ); + } + + function setUp() { + parent::setUp(); + + $this->instance_manifest = 'manifest.field.instance'; + $this->instance_name = 'field.instance.node.test_import.field_test_import'; + $this->drupalCreateContentType(array('type' => 'test_import', 'name' => 'Test import')); + + $admin_user = $this->drupalCreateUser(array('access administration pages', 'access content overview', 'administer nodes', 'bypass node access')); + $this->drupalLogin($admin_user); + } + + /** + * Test importing changes. + */ + function testImportChange() { + + // Assert default test import. + $this->drupalGet('node/add/test_import'); + $this->assertRaw('Test import field'); + + // Change label. + $active = $this->container->get('config.storage'); + $staging = $this->container->get('config.storage.staging'); + $manifest = $active->read($this->instance_manifest); + $instance = $active->read($this->instance_name); + $instance['data']['label'] = 'Test update import field'; + $staging->write($this->instance_name, $instance); + $staging->write($this->instance_manifest, $manifest); + + // Import. + config_import(); + + // Assert updated label. + $this->drupalGet('node/add/test_import'); + $this->assertText('Test update import field'); + } +} diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldImportCreateTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldImportCreateTest.php new file mode 100644 index 0000000..6bda93f --- /dev/null +++ b/core/modules/field/lib/Drupal/field/Tests/FieldImportCreateTest.php @@ -0,0 +1,87 @@ + 'Field config create tests', + 'description' => 'Create field and instances during config create method invocation.', + 'group' => 'Field API', + ); + } + + function setUp() { + parent::setUp(); + + $this->field_test_import_staging = 'field.field.field_test_import_staging'; + $this->instance_test_import_staging = 'field.instance.node.test_import.field_test_import_staging'; + $this->field_manifest = 'manifest.field.field'; + $this->instance_manifest = 'manifest.field.instance'; + $this->drupalCreateContentType(array('type' => 'test_import', 'name' => 'Test import')); + + $admin_user = $this->drupalCreateUser(array('access administration pages', 'access content overview', 'administer nodes', 'bypass node access')); + $this->drupalLogin($admin_user); + } + + /** + * Test importing new fields. + */ + function testImportCreate() { + + // Assert default test import. + $this->drupalGet('node/add/test_import'); + $this->assertNoText('Test import field'); + + // Enable field_test_config module and assert the test import + // field and instance is available on the Test content type. + // This tests creating fields and instances that are provided + // by a module. + module_enable(array('field_test_config')); + $this->drupalGet('node/add/test_import'); + $this->assertText('Test import field'); + $module_path = drupal_get_path('module', 'field_test_config'); + + // Copy another field and instance to the staging directory + // on the Test content type and run config_import() to test + // importing from the staging directory. + $active = $this->container->get('config.storage'); + $staging = $this->container->get('config.storage.staging'); + $field_manifest = $active->read($this->field_manifest); + $instance_manifest = $active->read($this->instance_manifest); + + // Copy the files. + $copied = file_unmanaged_copy($module_path .'/staging/' . $this->field_test_import_staging . '.yml', 'public://config_staging/' . $this->field_test_import_staging . '.yml'); + $this->assertTrue($copied); + $copied = file_unmanaged_copy($module_path .'/staging/' . $this->instance_test_import_staging . '.yml', 'public://config_staging/' . $this->instance_test_import_staging . '.yml'); + $this->assertTrue($copied); + + // Add to manifest. + $field_manifest['field_test_import_staging'] = array('name' => $this->field_test_import_staging); + $instance_manifest['node.test_import.field_test_import_staging'] = array('name' => $this->instance_test_import_staging); + + // Write to manifest and new config. + $staging->write($this->field_manifest, $field_manifest); + $staging->write($this->instance_manifest, $instance_manifest); + + // Import. + config_import(); + + // Assert the staging field is there. + $this->drupalGet('node/add/test_import'); + $this->assertText('Import from staging'); + } +} diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldImportDeleteTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldImportDeleteTest.php new file mode 100644 index 0000000..f6d548f --- /dev/null +++ b/core/modules/field/lib/Drupal/field/Tests/FieldImportDeleteTest.php @@ -0,0 +1,100 @@ + 'Field config delete tests', + 'description' => 'Delete field and instances during config delete method invocation.', + 'group' => 'Field API', + ); + } + + function setUp() { + parent::setUp(); + + $this->body_field_name = 'field.field.body'; + $this->test_import_field_name = 'field.field.test_import'; + $this->body_instance_name = 'field.instance.node.test_import.body'; + $this->test_import_instance_name = 'field.instance.node.test_import.field_test_import'; + $this->field_manifest = 'manifest.field.field'; + $this->instance_manifest = 'manifest.field.instance'; + $this->drupalCreateContentType(array('type' => 'test_import', 'name' => 'Test import')); + + $admin_user = $this->drupalCreateUser(array('access administration pages', 'access content overview', 'administer nodes', 'bypass node access')); + $this->drupalLogin($admin_user); + } + + /** + * Test importing deletions. + */ + function testImportDelete() { + + $body_field = field_info_field('body'); + $field_test_import = field_info_field('field_test_import'); + + // Assert default test import. + $this->drupalGet('node/add/test_import'); + $this->assertRaw('Test import field'); + $this->assertRaw('Body'); + + // Delete body field and instance, the test import instance + // from the manifest. + $active = $this->container->get('config.storage'); + $staging = $this->container->get('config.storage.staging'); + $field_manifest = $active->read($this->field_manifest); + $instance_manifest = $active->read($this->instance_manifest); + unset($field_manifest['body']); + unset($instance_manifest['node.test_import.body']); + unset($instance_manifest['node.test_import.field_test_import']); + $staging->write($this->field_manifest, $field_manifest); + $staging->write($this->instance_manifest, $instance_manifest); + + // Import. + config_import(); + + // Assert the field and instance are gone from the form. + $this->drupalGet('node/add/test_import'); + $this->assertNoText('Test import field'); + $this->assertNoText('Body'); + + // Check that body and import field are in state of deleted fields. + $deleted_fields = state()->get('field.field.deleted') ?: array(); + $this->assertTrue(isset($deleted_fields[$body_field->id()])); + $this->assertTrue(isset($deleted_fields[$field_test_import->uuid])); + + // Run purge_batch(). + field_purge_batch(10); + + // Check that the deleted fields are removed from state. + $deleted_fields = state()->get('field.field.deleted') ?: array(); + $this->assertTrue(empty($deleted_fields), 'Fields are deleted'); + + // Check all config files are gone. + $active = $this->container->get('config.storage'); + $this->assertIdentical($active->listAll($this->body_field_name), array()); + $this->assertIdentical($active->listAll($this->test_import_field_name), array()); + $this->assertIdentical($active->listAll($this->body_instance_name), array()); + $this->assertIdentical($active->listAll($this->test_import_instance_name), array()); + } +} diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php index 3115851..effa382 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php @@ -65,22 +65,22 @@ function testFieldInfo() { 'field_name' => drupal_strtolower($this->randomName()), 'type' => 'test_field', ); - field_create_field($field); + $field = field_create_field($field); $fields = field_info_fields(); $this->assertEqual(count($fields), count($core_fields) + 1, 'One new field exists'); - $this->assertEqual($fields[$field['field_name']]['field_name'], $field['field_name'], 'info fields contains field name'); - $this->assertEqual($fields[$field['field_name']]['type'], $field['type'], 'info fields contains field type'); - $this->assertEqual($fields[$field['field_name']]['module'], 'field_test', 'info fields contains field module'); + $this->assertEqual($fields[$field->field_name]->field_name, $field->field_name, 'info fields contains field name'); + $this->assertEqual($fields[$field->field_name]->type, $field->type, 'info fields contains field type'); + $this->assertEqual($fields[$field->field_name]->module, 'field_test', 'info fields contains field module'); $settings = array('test_field_setting' => 'dummy test string'); foreach ($settings as $key => $val) { - $this->assertEqual($fields[$field['field_name']]['settings'][$key], $val, format_string('Field setting %key has correct default value %value', array('%key' => $key, '%value' => $val))); + $this->assertEqual($fields[$field->field_name]->settings[$key], $val, format_string('Field setting %key has correct default value %value', array('%key' => $key, '%value' => $val))); } - $this->assertEqual($fields[$field['field_name']]['cardinality'], 1, 'info fields contains cardinality 1'); - $this->assertEqual($fields[$field['field_name']]['active'], 1, 'info fields contains active 1'); + $this->assertEqual($fields[$field->field_name]->cardinality, 1, 'info fields contains cardinality 1'); + $this->assertEqual($fields[$field->field_name]->active, 1, 'info fields contains active 1'); // Create an instance, verify that it shows up $instance = array( - 'field_name' => $field['field_name'], + 'field_name' => $field->field_name, 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', 'label' => $this->randomName(), @@ -94,18 +94,18 @@ function testFieldInfo() { field_create_instance($instance); $info = entity_get_info('test_entity'); - $instances = field_info_instances('test_entity', $instance['bundle']); + $instances = field_info_instances('test_entity', $instance->bundle); $this->assertEqual(count($instances), 1, format_string('One instance shows up in info when attached to a bundle on a @label.', array( '@label' => $info['label'] ))); - $this->assertTrue($instance < $instances[$instance['field_name']], 'Instance appears in info correctly'); + $this->assertTrue($instance < $instances[$instance->field_name], 'Instance appears in info correctly'); // Test a valid entity type but an invalid bundle. $instances = field_info_instances('test_entity', 'invalid_bundle'); $this->assertIdentical($instances, array(), "field_info_instances('test_entity', 'invalid_bundle') returns an empty array."); // Test invalid entity type and bundle. - $instances = field_info_instances('invalid_entity', $instance['bundle']); + $instances = field_info_instances('invalid_entity', $instance->bundle); $this->assertIdentical($instances, array(), "field_info_instances('invalid_entity', 'test_bundle') returns an empty array."); // Test invalid entity type, no bundle provided. @@ -143,13 +143,9 @@ function testFieldPrepare() { // Simulate a stored field definition missing a field setting (e.g. a // third-party module adding a new field setting has been enabled, and // existing fields do not know the setting yet). - $data = db_query('SELECT data FROM {field_config} WHERE field_name = :field_name', array(':field_name' => $field_definition['field_name']))->fetchField(); - $data = unserialize($data); - $data['settings'] = array(); - db_update('field_config') - ->fields(array('data' => serialize($data))) - ->condition('field_name', $field_definition['field_name']) - ->execute(); + $field = entity_load('field_entity', $field_definition['field_name']); + $field->settings = array(); + field_update_field($field); field_cache_clear(); @@ -158,7 +154,7 @@ function testFieldPrepare() { // Check that all expected settings are in place. $field_type = field_info_field_types($field_definition['type']); - $this->assertIdentical($field['settings'], $field_type['settings'], 'All expected default field settings are present.'); + $this->assertEqual($field->settings, $field_type['settings'], 'All expected default field settings are present.'); } /** @@ -180,25 +176,19 @@ function testInstancePrepare() { // Simulate a stored instance definition missing various settings (e.g. a // third-party module adding instance or widget settings has been enabled, // but existing instances do not know the new settings). - $data = db_query('SELECT data FROM {field_config_instance} WHERE field_name = :field_name AND bundle = :bundle', array(':field_name' => $instance_definition['field_name'], ':bundle' => $instance_definition['bundle']))->fetchField(); - $data = unserialize($data); - $data['settings'] = array(); - $data['widget']['settings'] = 'unavailable_widget'; - $data['widget']['settings'] = array(); - db_update('field_config_instance') - ->fields(array('data' => serialize($data))) - ->condition('field_name', $instance_definition['field_name']) - ->condition('bundle', $instance_definition['bundle']) - ->execute(); - - field_cache_clear(); + $entity_instance = entity_load('field_instance', $instance_definition['entity_type'] . '.' . $instance_definition['bundle'] . '.' . $instance_definition['field_name']); + $instance = $entity_instance->data; + $instance->settings = array(); + $instance->widget_settings['settings'] = 'unavailable_widget'; + $instance->widget_settings['settings'] = array(); + field_update_instance($instance); // Read the instance back. $instance = field_info_instance($instance_definition['entity_type'], $instance_definition['field_name'], $instance_definition['bundle']); // Check that all expected instance settings are in place. $field_type = field_info_field_types($field_definition['type']); - $this->assertIdentical($instance['settings'], $field_type['instance_settings'] , 'All expected instance settings are present.'); + $this->assertEqual($instance->settings, $field_type['instance_settings'] , 'All expected instance settings are present.'); // Check that the default widget is used and expected settings are in place. $widget = $instance->getWidget(); diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php index 90fcc05..cf1ae2e 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php @@ -8,6 +8,7 @@ namespace Drupal\field\Tests; use Drupal\field\FieldException; +use Drupal\field\Plugin\Core\Entity\FieldInstance; class FieldInstanceCrudTest extends FieldTestBase { @@ -35,9 +36,9 @@ function setUp() { 'field_name' => drupal_strtolower($this->randomName()), 'type' => 'test_field', ); - field_create_field($this->field); + $this->field = field_create_field($this->field); $this->instance_definition = array( - 'field_name' => $this->field['field_name'], + 'field_name' => $this->field->field_name, 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', ); @@ -55,26 +56,24 @@ function setUp() { function testCreateFieldInstance() { field_create_instance($this->instance_definition); - // Read the raw record from the {field_config_instance} table. - $result = db_query('SELECT * FROM {field_config_instance} WHERE field_name = :field_name AND bundle = :bundle', array(':field_name' => $this->instance_definition['field_name'], ':bundle' => $this->instance_definition['bundle'])); - $record = $result->fetchAssoc(); - $record['data'] = unserialize($record['data']); + // Read the configuration. + $load_instance = entity_load('field_instance', $this->instance_definition['entity_type'] . '.' . $this->instance_definition['bundle'] . '.' . $this->instance_definition['field_name']); - $field_type = field_info_field_types($this->field['type']); + $field_type = field_info_field_types($this->field->type); $widget_type = field_info_widget_types($field_type['default_widget']); // Check that the ID key is filled in. - $this->assertIdentical($record['id'], $this->instance_definition['id'], 'The instance id is filled in'); + //$this->assertIdentical($record['id'], $this->instance_definition['id'], 'The instance id is filled in'); // Check that default values are set. - $this->assertIdentical($record['data']['required'], FALSE, 'Required defaults to false.'); - $this->assertIdentical($record['data']['label'], $this->instance_definition['field_name'], 'Label defaults to field name.'); - $this->assertIdentical($record['data']['description'], '', 'Description defaults to empty string.'); - $this->assertIdentical($record['data']['widget']['type'], $field_type['default_widget'], 'Default widget has been written.'); + $this->assertEqual($record->required, FALSE, 'Required defaults to false.'); + $this->assertIdentical($record->label, $this->instance_definition['field_name'], 'Label defaults to field name.'); + $this->assertIdentical($record->description, '', 'Description defaults to empty string.'); + $this->assertIdentical($record->widget_settings['type'], $field_type['default_widget'], 'Default widget has been written.'); // Check that default settings are set. - $this->assertIdentical($record['data']['settings'], $field_type['instance_settings'] , 'Default instance settings have been written.'); - $this->assertIdentical($record['data']['widget']['settings'], $widget_type['settings'] , 'Default widget settings have been written.'); + $this->assertEqual($record->settings, $field_type['instance_settings'] , 'Default instance settings have been written.'); + $this->assertIdentical($record->widget_settings['settings'], $widget_type['settings'] , 'Default widget settings have been written.'); // Guarantee that the field/bundle combination is unique. try { @@ -107,8 +106,8 @@ function testCreateFieldInstance() { // by the field. try { $instance = $this->instance_definition; - $instance['field_name'] = $field_restricted['field_name']; - $instance['entity_type'] = 'test_cacheable_entity'; + $instance->field_name = $field_restricted['field_name']; + $instance->entity_type = 'test_cacheable_entity'; field_create_instance($instance); $this->pass(t('Can create an instance on an entity type allowed by the field.')); } @@ -120,7 +119,7 @@ function testCreateFieldInstance() { // forbidden by the field. try { $instance = $this->instance_definition; - $instance['field_name'] = $field_restricted['field_name']; + $instance->field_name = $field_restricted['field_name']; field_create_instance($instance); $this->fail(t('Cannot create an instance on an entity type forbidden by the field.')); } @@ -139,7 +138,13 @@ function testReadFieldInstance() { // Read the instance back. $instance = field_read_instance('test_entity', $this->instance_definition['field_name'], $this->instance_definition['bundle']); - $this->assertTrue($this->instance_definition == $instance, 'The field was properly read.'); + //$this->assertTrue($this->instance_definition['id'] == $instance->id, 'The field was properly read.'); + $this->assertTrue($this->instance_definition['label'] == $instance->label, 'The field was properly read.'); + $this->assertTrue($this->instance_definition['field_id'] == $instance->field_id, 'The field was properly read.'); + $this->assertTrue($this->instance_definition['entity_type'] == $instance->entity_type, 'The field was properly read.'); + $this->assertTrue($this->instance_definition['bundle'] == $instance->bundle, 'The field was properly read.'); + $this->assertTrue($this->instance_definition['settings'] == $instance->settings, 'The field was properly read.'); + $this->assertTrue($this->instance_definition['widget'] == $instance->widget_settings, 'The field was properly read.'); } /** @@ -150,28 +155,28 @@ function testUpdateFieldInstance() { // Check that basic changes are saved. $instance = field_read_instance('test_entity', $this->instance_definition['field_name'], $this->instance_definition['bundle']); - $instance['required'] = !$instance['required']; - $instance['label'] = $this->randomName(); - $instance['description'] = $this->randomName(); - $instance['settings']['test_instance_setting'] = $this->randomName(); - $instance['widget']['settings']['test_widget_setting'] =$this->randomName(); - $instance['widget']['weight']++; + $instance->required = !$instance->required; + $instance->label = $this->randomName(); + $instance->description = $this->randomName(); + $instance->settings['test_instance_setting'] = $this->randomName(); + $instance->widget_settings['settings']['test_widget_setting'] =$this->randomName(); + $instance->widget_settings['weight']++; field_update_instance($instance); $instance_new = field_read_instance('test_entity', $this->instance_definition['field_name'], $this->instance_definition['bundle']); - $this->assertEqual($instance['required'], $instance_new['required'], '"required" change is saved'); - $this->assertEqual($instance['label'], $instance_new['label'], '"label" change is saved'); - $this->assertEqual($instance['description'], $instance_new['description'], '"description" change is saved'); - $this->assertEqual($instance['widget']['settings']['test_widget_setting'], $instance_new['widget']['settings']['test_widget_setting'], 'Widget setting change is saved'); - $this->assertEqual($instance['widget']['weight'], $instance_new['widget']['weight'], 'Widget weight change is saved'); + $this->assertEqual($instance->required, $instance_new['required'], '"required" change is saved'); + $this->assertEqual($instance->label, $instance_new['label'], '"label" change is saved'); + $this->assertEqual($instance->description, $instance_new['description'], '"description" change is saved'); + $this->assertEqual($instance->widget_settings['settings']['test_widget_setting'], $instance_new['widget']['settings']['test_widget_setting'], 'Widget setting change is saved'); + $this->assertEqual($instance->widget_settings['weight'], $instance_new['widget']['weight'], 'Widget weight change is saved'); // Check that changing the widget type updates the default settings. $instance = field_read_instance('test_entity', $this->instance_definition['field_name'], $this->instance_definition['bundle']); - $instance['widget']['type'] = 'test_field_widget_multiple'; + $instance->widget_settings['type'] = 'test_field_widget_multiple'; field_update_instance($instance); $instance_new = field_read_instance('test_entity', $this->instance_definition['field_name'], $this->instance_definition['bundle']); - $this->assertEqual($instance['widget']['type'], $instance_new['widget']['type'] , 'Widget type change is saved.'); + $this->assertEqual($instance->widget_settings['type'], $instance_new['widget']['type'] , 'Widget type change is saved.'); $settings = field_info_widget_settings($instance_new['widget']['type']); $this->assertIdentical($settings, array_intersect_key($instance_new['widget']['settings'], $settings) , 'Widget type change updates default settings.'); @@ -213,7 +218,9 @@ function testDeleteFieldInstance() { // Make sure the field is deleted when its last instance is deleted. field_delete_instance($another_instance); - $field = field_read_field($another_instance['field_name'], array('include_deleted' => TRUE)); - $this->assertTrue(!empty($field['deleted']), 'A deleted field is marked for deletion after all its instances have been marked for deletion.'); + $deleted_fields = state()->get('field.field.deleted'); + $this->assertTrue(isset($deleted_fields[$another_instance->field_id]), 'A deleted field is marked for deletion.'); + $field = field_read_field(array('field_name' => $another_instance->field_name)); + $this->assertFalse($field, 'The field marked to be deleted is not found anymore in the configuration.'); } } diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldItemUnitTestBase.php b/core/modules/field/lib/Drupal/field/Tests/FieldItemUnitTestBase.php index e201fb5..dd9b6a8 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldItemUnitTestBase.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldItemUnitTestBase.php @@ -24,8 +24,6 @@ class FieldItemUnitTestBase extends DrupalUnitTestBase { public function setUp() { parent::setUp(); $this->installSchema('system', 'sequences'); - $this->installSchema('field', 'field_config'); - $this->installSchema('field', 'field_config_instance'); $this->installSchema('entity_test', 'entity_test'); } diff --git a/core/modules/field/lib/Drupal/field/Tests/FormTest.php b/core/modules/field/lib/Drupal/field/Tests/FormTest.php index 2f5dbee..b65dc9d 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FormTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FormTest.php @@ -56,8 +56,8 @@ function setUp() { function testFieldFormSingle() { $this->field = $this->field_single; $this->field_name = $this->field['field_name']; - $this->instance['field_name'] = $this->field_name; - field_create_field($this->field); + $this->instance->field_name = $this->field_name; + $this->field = field_create_field($this->field); field_create_instance($this->instance); $langcode = LANGUAGE_NOT_SPECIFIED; @@ -77,7 +77,7 @@ function testFieldFormSingle() { // Submit with invalid value (field-level validation). $edit = array("{$this->field_name}[$langcode][0][value]" => -1); $this->drupalPost(NULL, $edit, t('Save')); - $this->assertRaw(t('%name does not accept the value -1.', array('%name' => $this->instance['label'])), 'Field validation fails with invalid input.'); + $this->assertRaw(t('%name does not accept the value -1.', array('%name' => $this->instance->label)), 'Field validation fails with invalid input.'); // TODO : check that the correct field is flagged for error. // Create an entity @@ -120,10 +120,10 @@ function testFieldFormSingle() { function testFieldFormDefaultValue() { $this->field = $this->field_single; $this->field_name = $this->field['field_name']; - $this->instance['field_name'] = $this->field_name; + $this->instance->field_name = $this->field_name; $default = rand(1, 127); $this->instance['default_value'] = array(array('value' => $default)); - field_create_field($this->field); + $this->field = field_create_field($this->field); field_create_instance($this->instance); $langcode = LANGUAGE_NOT_SPECIFIED; @@ -145,16 +145,16 @@ function testFieldFormDefaultValue() { function testFieldFormSingleRequired() { $this->field = $this->field_single; $this->field_name = $this->field['field_name']; - $this->instance['field_name'] = $this->field_name; - $this->instance['required'] = TRUE; - field_create_field($this->field); + $this->instance->field_name = $this->field_name; + $this->instance->required = TRUE; + $this->field = field_create_field($this->field); field_create_instance($this->instance); $langcode = LANGUAGE_NOT_SPECIFIED; // Submit with missing required value. $edit = array(); $this->drupalPost('test-entity/add/test_bundle', $edit, t('Save')); - $this->assertRaw(t('!name field is required.', array('!name' => $this->instance['label'])), 'Required field with no value fails validation'); + $this->assertRaw(t('!name field is required.', array('!name' => $this->instance->label)), 'Required field with no value fails validation'); // Create an entity $value = mt_rand(1, 127); @@ -170,13 +170,13 @@ function testFieldFormSingleRequired() { $value = ''; $edit = array("{$this->field_name}[$langcode][0][value]" => $value); $this->drupalPost('test-entity/manage/' . $id . '/edit', $edit, t('Save')); - $this->assertRaw(t('!name field is required.', array('!name' => $this->instance['label'])), 'Required field with no value fails validation'); + $this->assertRaw(t('!name field is required.', array('!name' => $this->instance->label)), 'Required field with no value fails validation'); } // function testFieldFormMultiple() { // $this->field = $this->field_multiple; -// $this->field_name = $this->field['field_name']; -// $this->instance['field_name'] = $this->field_name; +// $this->field_name = $this->field->field_name; +// $this->instance->field_name = $this->field_name; // field_create_field($this->field); // field_create_instance($this->instance); // } @@ -184,8 +184,8 @@ function testFieldFormSingleRequired() { function testFieldFormUnlimited() { $this->field = $this->field_unlimited; $this->field_name = $this->field['field_name']; - $this->instance['field_name'] = $this->field_name; - field_create_field($this->field); + $this->instance->field_name = $this->field_name; + $this->field = field_create_field($this->field); field_create_instance($this->instance); $langcode = LANGUAGE_NOT_SPECIFIED; @@ -264,8 +264,8 @@ function testFieldFormMultivalueWithRequiredRadio() { // Create a multivalue test field. $this->field = $this->field_unlimited; $this->field_name = $this->field['field_name']; - $this->instance['field_name'] = $this->field_name; - field_create_field($this->field); + $this->instance->field_name = $this->field_name; + $this->field = field_create_field($this->field); field_create_instance($this->instance); $langcode = LANGUAGE_NOT_SPECIFIED; @@ -306,8 +306,8 @@ function testFieldFormMultivalueWithRequiredRadio() { function testFieldFormJSAddMore() { $this->field = $this->field_unlimited; $this->field_name = $this->field['field_name']; - $this->instance['field_name'] = $this->field_name; - field_create_field($this->field); + $this->instance->field_name = $this->field_name; + $this->field = field_create_field($this->field); field_create_instance($this->instance); $langcode = LANGUAGE_NOT_SPECIFIED; @@ -365,9 +365,9 @@ function testFieldFormMultipleWidget() { // widget. $this->field = $this->field_multiple; $this->field_name = $this->field['field_name']; - $this->instance['field_name'] = $this->field_name; - $this->instance['widget']['type'] = 'test_field_widget_multiple'; - field_create_field($this->field); + $this->instance->field_name = $this->field_name; + $this->instance->widget_settings['type'] = 'test_field_widget_multiple'; + $this->field = field_create_field($this->field); field_create_instance($this->instance); $langcode = LANGUAGE_NOT_SPECIFIED; @@ -405,8 +405,8 @@ function testFieldFormAccess() { $field = $this->field_single; $field_name = $field['field_name']; $instance = $this->instance; - $instance['field_name'] = $field_name; - field_create_field($field); + $instance->field_name = $field_name; + $this->field = field_create_field($field); field_create_instance($instance); // Create a field with no edit access - see field_test_field_access(). @@ -429,7 +429,7 @@ function testFieldFormAccess() { // Test that the form structure includes full information for each delta // apart from #access. $entity_type = 'test_entity'; - $entity = field_test_create_entity(0, 0, $this->instance['bundle']); + $entity = field_test_create_entity(0, 0, $this->instance->bundle); $form = array(); $form_state = form_state_defaults(); @@ -476,11 +476,11 @@ function testNestedFieldForm() { // Add two instances on the 'test_bundle' field_create_field($this->field_single); field_create_field($this->field_unlimited); - $this->instance['field_name'] = 'field_single'; - $this->instance['label'] = 'Single field'; + $this->instance->field_name = 'field_single'; + $this->instance->label = 'Single field'; field_create_instance($this->instance); - $this->instance['field_name'] = 'field_unlimited'; - $this->instance['label'] = 'Unlimited field'; + $this->instance->field_name = 'field_unlimited'; + $this->instance->label = 'Unlimited field'; field_create_instance($this->instance); // Create two entities. diff --git a/core/modules/field/lib/Drupal/field/Tests/TranslationTest.php b/core/modules/field/lib/Drupal/field/Tests/TranslationTest.php index f2ada97..0c4a8d1 100644 --- a/core/modules/field/lib/Drupal/field/Tests/TranslationTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/TranslationTest.php @@ -47,6 +47,7 @@ function setUp() { ); field_create_field($field); $this->field = field_read_field($this->field_name); + $this->field_definition = $field; $instance = array( 'field_name' => $this->field_name, @@ -71,8 +72,8 @@ function setUp() { function testFieldAvailableLanguages() { // Test 'translatable' fieldable info. field_test_entity_info_translatable('test_entity', FALSE); - $field = $this->field; - $field['field_name'] .= '_untranslatable'; + $field = clone($this->field); + $field->field_name .= '_untranslatable'; // Enable field translations for the entity. field_test_entity_info_translatable('test_entity', TRUE); @@ -90,7 +91,7 @@ function testFieldAvailableLanguages() { $this->assertFalse(in_array('en', $available_langcodes), format_string('%language was made unavailable.', array('%language' => 'en'))); // Test field_available_languages() behavior for untranslatable fields. - $this->field['translatable'] = FALSE; + $this->field->translatable = FALSE; field_update_field($this->field); $available_langcodes = field_available_languages($this->entity_type, $this->field); $this->assertTrue(count($available_langcodes) == 1 && $available_langcodes[0] === LANGUAGE_NOT_SPECIFIED, 'For untranslatable fields only LANGUAGE_NOT_SPECIFIED is available.'); @@ -104,7 +105,7 @@ function testFieldInvoke() { field_test_entity_info_translatable('test_entity', TRUE); $entity_type = 'test_entity'; - $entity = field_test_create_entity(0, 0, $this->instance['bundle']); + $entity = field_test_create_entity(0, 0, $this->instance->bundle); // Populate some extra languages to check if _field_invoke() correctly uses // the result of field_available_languages(). @@ -117,7 +118,7 @@ function testFieldInvoke() { // For each given language provide some random values. foreach ($langcodes as $langcode) { - for ($delta = 0; $delta < $this->field['cardinality']; $delta++) { + for ($delta = 0; $delta < $this->field->cardinality; $delta++) { $values[$langcode][$delta]['value'] = mt_rand(1, 127); } } @@ -149,7 +150,7 @@ function testFieldInvokeMultiple() { $available_langcodes = field_available_languages($this->entity_type, $this->field); for ($id = 1; $id <= $entity_count; ++$id) { - $entity = field_test_create_entity($id, $id, $this->instance['bundle']); + $entity = field_test_create_entity($id, $id, $this->instance->bundle); $langcodes = $available_langcodes; // Populate some extra languages to check whether _field_invoke() @@ -167,7 +168,7 @@ function testFieldInvokeMultiple() { // per-entity language suggestions work even when available field values // are different for each language. if ($i !== $id) { - for ($delta = 0; $delta < $this->field['cardinality']; $delta++) { + for ($delta = 0; $delta < $this->field->cardinality; $delta++) { $values[$id][$langcode][$delta]['value'] = mt_rand(1, 127); } } @@ -220,12 +221,12 @@ function testTranslatableFieldSaveLoad() { field_test_entity_info_translatable('test_entity', TRUE); $eid = $evid = 1; $entity_type = 'test_entity'; - $entity = field_test_create_entity($eid, $evid, $this->instance['bundle']); + $entity = field_test_create_entity($eid, $evid, $this->instance->bundle); $field_translations = array(); $available_langcodes = field_available_languages($entity_type, $this->field); $this->assertTrue(count($available_langcodes) > 1, 'Field is translatable.'); foreach ($available_langcodes as $langcode) { - $field_translations[$langcode] = $this->_generateTestFieldValues($this->field['cardinality']); + $field_translations[$langcode] = $this->_generateTestFieldValues($this->field->cardinality); } // Save and reload the field translations. @@ -245,10 +246,10 @@ function testTranslatableFieldSaveLoad() { // Test default values. $field_name_default = drupal_strtolower($this->randomName() . '_field_name'); - $field = $this->field; + $field = $this->field_definition; $field['field_name'] = $field_name_default; $instance = $this->instance; - $instance['field_name'] = $field_name_default; + $instance->field_name = $field_name_default; $default = rand(1, 127); $instance['default_value'] = array(array('value' => $default)); field_create_field($field); @@ -259,9 +260,9 @@ function testTranslatableFieldSaveLoad() { $eid++; $evid++; - $values = array('eid' => $eid, 'evid' => $evid, 'fttype' => $instance['bundle'], 'langcode' => $translation_langcodes[0]); + $values = array('eid' => $eid, 'evid' => $evid, 'fttype' => $instance->bundle, 'langcode' => $translation_langcodes[0]); foreach ($translation_langcodes as $langcode) { - $values[$this->field_name][$langcode] = $this->_generateTestFieldValues($this->field['cardinality']); + $values[$this->field_name][$langcode] = $this->_generateTestFieldValues($this->field->cardinality); } $entity = entity_create($entity_type, $values); @@ -277,9 +278,9 @@ function testTranslatableFieldSaveLoad() { foreach (array(NULL, array()) as $empty_items) { $eid++; $evid++; - $values = array('eid' => $eid, 'evid' => $evid, 'fttype' => $instance['bundle'], 'langcode' => $translation_langcodes[0]); + $values = array('eid' => $eid, 'evid' => $evid, 'fttype' => $instance->bundle, 'langcode' => $translation_langcodes[0]); foreach ($translation_langcodes as $langcode) { - $values[$this->field_name][$langcode] = $this->_generateTestFieldValues($this->field['cardinality']); + $values[$this->field_name][$langcode] = $this->_generateTestFieldValues($this->field->cardinality); $values[$field_name_default][$langcode] = $empty_items; } $entity = entity_create($entity_type, $values); @@ -314,8 +315,8 @@ function testFieldDisplayLanguage() { ); field_create_instance($instance); - $entity = field_test_create_entity(1, 1, $this->instance['bundle']); - $instances = field_info_instances($entity_type, $this->instance['bundle']); + $entity = field_test_create_entity(1, 1, $this->instance->bundle); + $instances = field_info_instances($entity_type, $this->instance->bundle); $enabled_langcodes = field_content_languages(); $langcodes = array(); @@ -326,7 +327,7 @@ function testFieldDisplayLanguage() { // Generate field translations for languages different from the first // enabled. foreach ($instances as $instance) { - $field_name = $instance['field_name']; + $field_name = $instance->field_name; $field = field_info_field($field_name); do { // Index 0 is reserved for the requested language, this way we ensure @@ -335,7 +336,7 @@ function testFieldDisplayLanguage() { } while (isset($langcodes[$langcode])); $langcodes[$langcode] = TRUE; - $entity->{$field_name}[$langcode] = $this->_generateTestFieldValues($field['cardinality']); + $entity->{$field_name}[$langcode] = $this->_generateTestFieldValues($field->cardinality); // If the langcode is one of the locked languages, then that one // will also be used for display. Otherwise, the default one should be // used, which is LANGUAGE_NOT_SPECIFIED. @@ -353,7 +354,7 @@ function testFieldDisplayLanguage() { $requested_langcode = $enabled_langcodes[0]; $display_langcodes = field_language($entity, NULL, $requested_langcode); foreach ($instances as $instance) { - $field_name = $instance['field_name']; + $field_name = $instance->field_name; $this->assertTrue($display_langcodes[$field_name] == $locked_languages[$field_name], format_string('The display language for field %field_name is %language.', array('%field_name' => $field_name, '%language' => $locked_languages[$field_name]))); } @@ -362,7 +363,7 @@ function testFieldDisplayLanguage() { drupal_static_reset('field_language'); $display_langcodes = field_language($entity, NULL, $requested_langcode); foreach ($instances as $instance) { - $field_name = $instance['field_name']; + $field_name = $instance->field_name; $langcode = $display_langcodes[$field_name]; // As the requested language was not assinged to any field, if the // returned language is defined for the current field, core fallback rules @@ -393,10 +394,10 @@ function testFieldFormTranslationRevisions() { // Prepare the field translations. field_test_entity_info_translatable($this->entity_type, TRUE); $eid = 1; - $entity = field_test_create_entity($eid, $eid, $this->instance['bundle']); + $entity = field_test_create_entity($eid, $eid, $this->instance->bundle); $available_langcodes = array_flip(field_available_languages($this->entity_type, $this->field)); unset($available_langcodes[LANGUAGE_NOT_SPECIFIED]); - $field_name = $this->field['field_name']; + $field_name = $this->field->field_name; // Store the field translations. $entity->enforceIsNew(); @@ -420,7 +421,7 @@ function testFieldFormTranslationRevisions() { * by the passed arguments were correctly stored. */ private function checkTranslationRevisions($eid, $evid, $available_langcodes) { - $field_name = $this->field['field_name']; + $field_name = $this->field->field_name; $entity = field_test_entity_test_load($eid, $evid); foreach ($available_langcodes as $langcode => $value) { $passed = isset($entity->{$field_name}[$langcode]) && $entity->{$field_name}[$langcode][0]['value'] == $value + 1; diff --git a/core/modules/field/lib/Drupal/field/Tests/Views/FieldTestBase.php b/core/modules/field/lib/Drupal/field/Tests/Views/FieldTestBase.php index c0f500b..5e3bf52 100644 --- a/core/modules/field/lib/Drupal/field/Tests/Views/FieldTestBase.php +++ b/core/modules/field/lib/Drupal/field/Tests/Views/FieldTestBase.php @@ -66,7 +66,7 @@ function setUpFields($amount = 3) { function setUpInstances($bundle = 'page') { foreach ($this->fields as $key => $field) { $instance = array( - 'field_name' => $field['field_name'], + 'field_name' => $field->field_name, 'entity_type' => 'node', 'bundle' => 'page', ); diff --git a/core/modules/field/lib/Drupal/field/Tests/Views/HandlerFieldFieldTest.php b/core/modules/field/lib/Drupal/field/Tests/Views/HandlerFieldFieldTest.php index 27504eb..afd2e26 100644 --- a/core/modules/field/lib/Drupal/field/Tests/Views/HandlerFieldFieldTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/Views/HandlerFieldFieldTest.php @@ -59,13 +59,13 @@ protected function setUp() { for ($key = 0; $key < 3; $key++) { $field = $this->fields[$key]; - $edit[$field['field_name']][LANGUAGE_NOT_SPECIFIED][0]['value'] = $this->randomName(8); + $edit[$field->field_name][LANGUAGE_NOT_SPECIFIED][0]['value'] = $this->randomName(8); } for ($j = 0; $j < 5; $j++) { - $edit[$this->fields[3]['field_name']][LANGUAGE_NOT_SPECIFIED][$j]['value'] = $this->randomName(8); + $edit[$this->fields[3]->field_name][LANGUAGE_NOT_SPECIFIED][$j]['value'] = $this->randomName(8); } // Set this field to be empty. - $edit[$this->fields[4]['field_name']] = array(LANGUAGE_NOT_SPECIFIED => array(0 => array('value' => NULL))); + $edit[$this->fields[4]->field_name] = array(LANGUAGE_NOT_SPECIFIED => array(0 => array('value' => NULL))); $this->nodes[$i] = $this->drupalCreateNode($edit); } @@ -80,9 +80,9 @@ protected function setUp() { protected function prepareView(ViewExecutable $view) { $view->initDisplay(); foreach ($this->fields as $key => $field) { - $view->display_handler->options['fields'][$field['field_name']]['id'] = $field['field_name']; - $view->display_handler->options['fields'][$field['field_name']]['table'] = 'field_data_' . $field['field_name']; - $view->display_handler->options['fields'][$field['field_name']]['field'] = $field['field_name']; + $view->display_handler->options['fields'][$field->field_name]['id'] = $field->field_name; + $view->display_handler->options['fields'][$field->field_name]['table'] = 'field_data_' . $field->field_name; + $view->display_handler->options['fields'][$field->field_name]['field'] = $field->field_name; } } @@ -101,8 +101,8 @@ public function _testSimpleFieldRender() { for ($i = 0; $i < 3; $i++) { for ($key = 0; $key < 2; $key++) { $field = $this->fields[$key]; - $rendered_field = $view->style_plugin->get_field($i, $field['field_name']); - $expected_field = $this->nodes[$i]->{$field['field_name']}[LANGUAGE_NOT_SPECIFIED][0]['value']; + $rendered_field = $view->style_plugin->get_field($i, $field->field_name); + $expected_field = $this->nodes[$i]->{$field->field_name}[LANGUAGE_NOT_SPECIFIED][0]['value']; $this->assertEqual($rendered_field, $expected_field); } } @@ -114,8 +114,8 @@ public function _testSimpleFieldRender() { public function _testFormatterSimpleFieldRender() { $view = views_get_view('test_view_fieldapi'); $this->prepareView($view); - $view->displayHandlers->get('default')->options['fields'][$this->fields[0]['field_name']]['type'] = 'text_trimmed'; - $view->displayHandlers->get('default')->options['fields'][$this->fields[0]['field_name']]['settings'] = array( + $view->displayHandlers->get('default')->options['fields'][$this->fields[0]->field_name]['type'] = 'text_trimmed'; + $view->displayHandlers->get('default')->options['fields'][$this->fields[0]->field_name]['settings'] = array( 'trim_length' => 3, ); $this->executeView($view); @@ -123,14 +123,14 @@ public function _testFormatterSimpleFieldRender() { // Take sure that the formatter works as expected. // @TODO: actually there should be a specific formatter. for ($i = 0; $i < 2; $i++) { - $rendered_field = $view->style_plugin->get_field($i, $this->fields[0]['field_name']); + $rendered_field = $view->style_plugin->get_field($i, $this->fields[0]->field_name); $this->assertEqual(strlen($rendered_field), 3); } } public function _testMultipleFieldRender() { $view = views_get_view('test_view_fieldapi'); - $field_name = $this->fields[3]['field_name']; + $field_name = $this->fields[3]->field_name; // Test delta limit. $this->prepareView($view); @@ -150,7 +150,7 @@ public function _testMultipleFieldRender() { } // Test that an empty field is rendered without error. - $rendered_field = $view->style_plugin->get_field(4, $this->fields[4]['field_name']); + $rendered_field = $view->style_plugin->get_field(4, $this->fields[4]->field_name); $view->destroy(); diff --git a/core/modules/field/tests/modules/field_test/field_test.field.inc b/core/modules/field/tests/modules/field_test/field_test.field.inc index 7166f4b..7b637f2 100644 --- a/core/modules/field/tests/modules/field_test/field_test.field.inc +++ b/core/modules/field/tests/modules/field_test/field_test.field.inc @@ -62,7 +62,7 @@ function field_test_field_widget_info_alter(&$info) { * Implements hook_field_update_forbid(). */ function field_test_field_update_forbid($field, $prior_field, $has_data) { - if ($field['type'] == 'test_field' && $field['settings']['unchangeable'] != $prior_field['settings']['unchangeable']) { + if ($field->type == 'test_field' && $field->settings['unchangeable'] != $prior_field->settings['unchangeable']) { throw new FieldException("field_test 'unchangeable' setting cannot be changed'"); } } @@ -124,9 +124,9 @@ function field_test_field_validate(EntityInterface $entity = NULL, $field, $inst foreach ($items as $delta => $item) { if ($item['value'] == -1) { - $errors[$field['field_name']][$langcode][$delta][] = array( + $errors[$field->field_name][$langcode][$delta][] = array( 'error' => 'field_test_invalid', - 'message' => t('%name does not accept the value -1.', array('%name' => $instance['label'])), + 'message' => t('%name does not accept the value -1.', array('%name' => $instance->label)), ); } } @@ -143,7 +143,7 @@ function field_test_field_is_empty($item, $field) { * Implements hook_field_settings_form(). */ function field_test_field_settings_form($field, $instance, $has_data) { - $settings = $field['settings']; + $settings = $field->settings; $form['test_field_setting'] = array( '#type' => 'textfield', @@ -160,7 +160,7 @@ function field_test_field_settings_form($field, $instance, $has_data) { * Implements hook_field_instance_settings_form(). */ function field_test_field_instance_settings_form($field, $instance) { - $settings = $instance['settings']; + $settings = $instance->settings; $form['test_instance_setting'] = array( '#type' => 'textfield', @@ -196,13 +196,13 @@ function field_test_default_value(EntityInterface $entity, $field, $instance) { * Implements hook_field_access(). */ function field_test_field_access($op, $field, $entity_type, $entity, $account) { - if ($field['field_name'] == "field_no_{$op}_access") { + if ($field->field_name == "field_no_{$op}_access") { return FALSE; } // Only grant view access to test_view_field fields when the user has // 'view test_view_field content' permission. - if ($field['field_name'] == 'test_view_field' && $op == 'view' && !user_access('view test_view_field content')) { + if ($field->field_name == 'test_view_field' && $op == 'view' && !user_access('view test_view_field content')) { return FALSE; } diff --git a/core/modules/field/tests/modules/field_test/field_test.install b/core/modules/field/tests/modules/field_test/field_test.install index 5355e9b..96f606a 100644 --- a/core/modules/field/tests/modules/field_test/field_test.install +++ b/core/modules/field/tests/modules/field_test/field_test.install @@ -114,7 +114,7 @@ function field_test_schema() { * Implements hook_field_schema(). */ function field_test_field_schema($field) { - if ($field['type'] == 'test_field') { + if ($field->type == 'test_field') { return array( 'columns' => array( 'value' => array( 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 03094dd..3ed0ce5 100644 --- a/core/modules/field/tests/modules/field_test/field_test.module +++ b/core/modules/field/tests/modules/field_test/field_test.module @@ -80,7 +80,7 @@ function field_test_menu() { * This simulates a field operation callback to be invoked by _field_invoke(). */ function field_test_field_test_op(EntityInterface $entity, $field, $instance, $langcode, &$items) { - return array($langcode => hash('sha256', serialize(array($entity, $field['field_name'], $langcode, $items)))); + return array($langcode => hash('sha256', serialize(array($entity, $field->field_name, $langcode, $items)))); } /** @@ -217,7 +217,7 @@ function field_test_field_attach_view_alter(&$output, $context) { */ function field_test_field_widget_properties_alter(&$widget, $context) { // Make the alter_test_text field 42 characters for nodes and comments. - if (in_array($context['entity_type'], array('node', 'comment')) && ($context['field']['field_name'] == 'alter_test_text')) { + if (in_array($context['entity_type'], array('node', 'comment')) && ($context['field']->field_name == 'alter_test_text')) { $widget['settings']['size'] = 42; } } @@ -227,7 +227,7 @@ function field_test_field_widget_properties_alter(&$widget, $context) { */ function field_test_field_widget_properties_user_alter(&$widget, $context) { // Always use buttons for the alter_test_options field on user forms. - if ($context['field']['field_name'] == 'alter_test_options') { + if ($context['field']->field_name == 'alter_test_options') { $widget['type'] = 'options_buttons'; } } @@ -236,7 +236,7 @@ function field_test_field_widget_properties_user_alter(&$widget, $context) { * Implements hook_field_widget_form_alter(). */ function field_test_field_widget_form_alter(&$element, &$form_state, $context) { - switch ($context['field']['field_name']) { + switch ($context['field']->field_name) { case 'alter_test_text': drupal_set_message('Field size: ' . $context['instance']->getWidget()->getSetting('size')); break; diff --git a/core/modules/field/tests/modules/field_test/field_test.storage.inc b/core/modules/field/tests/modules/field_test/field_test.storage.inc index a53b941..f1b2420 100644 --- a/core/modules/field/tests/modules/field_test/field_test.storage.inc +++ b/core/modules/field/tests/modules/field_test/field_test.storage.inc @@ -31,7 +31,7 @@ function field_test_field_storage_details($field) { // Add field columns. $columns = array(); - foreach ((array) $field['columns'] as $column_name => $attributes) { + foreach ((array) $field->columns as $column_name => $attributes) { $columns[$column_name] = $column_name; } return array( @@ -50,9 +50,9 @@ function field_test_field_storage_details($field) { function field_test_field_storage_details_alter(&$details, $field) { // For testing, storage details are changed only because of the field name. - if ($field['field_name'] == 'field_test_change_my_details') { + if ($field->field_name == 'field_test_change_my_details') { $columns = array(); - foreach ((array) $field['columns'] as $column_name => $attributes) { + foreach ((array) $field->columns as $column_name => $attributes) { $columns[$column_name] = $column_name; } $details['drupal_variables'] = array( @@ -88,8 +88,8 @@ function field_test_field_storage_load($entity_type, $entities, $age, $fields, $ foreach ($fields as $field_id => $ids) { $field = field_info_field_by_id($field_id); - $field_name = $field['field_name']; - $field_data = $data[$field['id']]; + $field_name = $field->field_name; + $field_data = $data[$field->id()]; $sub_table = $load_current ? 'current' : 'revisions'; $delta_count = array(); foreach ($field_data[$sub_table] as $row) { @@ -99,9 +99,9 @@ function field_test_field_storage_load($entity_type, $entities, $age, $fields, $ if (!isset($delta_count[$row->entity_id][$row->langcode])) { $delta_count[$row->entity_id][$row->langcode] = 0; } - if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->langcode] < $field['cardinality']) { + if ($field->cardinality == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->langcode] < $field->cardinality) { $item = array(); - foreach ($field['columns'] as $column => $attributes) { + foreach ($field->columns as $column => $attributes) { $item[$column] = $row->{$column}; } $entities[$row->entity_id]->{$field_name}[$row->langcode][] = $item; @@ -126,7 +126,7 @@ function field_test_field_storage_write(EntityInterface $entity, $op, $fields) { foreach ($fields as $field_id) { $field = field_info_field_by_id($field_id); - $field_name = $field['field_name']; + $field_name = $field->field_name; $field_data = &$data[$field_id]; $all_langcodes = field_available_languages($entity->entityType(), $field); @@ -167,7 +167,7 @@ function field_test_field_storage_write(EntityInterface $entity, $op, $fields) { 'deleted' => FALSE, 'langcode' => $langcode, ); - foreach ($field['columns'] as $column => $attributes) { + foreach ($field->columns as $column => $attributes) { $row->{$column} = isset($item[$column]) ? $item[$column] : NULL; } @@ -176,7 +176,7 @@ function field_test_field_storage_write(EntityInterface $entity, $op, $fields) { $field_data['revisions'][] = $row; } - if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && ++$delta_count == $field['cardinality']) { + if ($field->cardinality != FIELD_CARDINALITY_UNLIMITED && ++$delta_count == $field->cardinality) { break; } } @@ -193,8 +193,8 @@ function field_test_field_storage_delete(EntityInterface $entity, $fields) { // Note: reusing field_test_storage_purge(), like field_sql_storage.module // does, is highly inefficient in our case... foreach (field_info_instances($entity->entityType(), $entity->bundle()) as $instance) { - if (isset($fields[$instance['field_id']])) { - $field = field_info_field_by_id($instance['field_id']); + if (isset($fields[$instance->field_id])) { + $field = field_info_field_by_id($instance->field_id); field_test_field_storage_purge($entity, $field, $instance); } } @@ -206,7 +206,7 @@ function field_test_field_storage_delete(EntityInterface $entity, $fields) { function field_test_field_storage_purge(EntityInterface $entity, $field, $instance) { $data = _field_test_storage_data(); - $field_data = &$data[$field['id']]; + $field_data = &$data[$field->id()]; foreach (array('current', 'revisions') as $sub_table) { foreach ($field_data[$sub_table] as $key => $row) { if ($row->type == $entity->entityType() && $row->entity_id == $entity->id()) { @@ -247,9 +247,9 @@ function field_test_field_storage_query($field_id, $conditions, $count, &$cursor $load_current = $age == FIELD_LOAD_CURRENT; $field = field_info_field_by_id($field_id); - $field_columns = array_keys($field['columns']); + $field_columns = array_keys($field->columns); - $field_data = $data[$field['id']]; + $field_data = $data[$field->id()]; $sub_table = $load_current ? 'current' : 'revisions'; // We need to sort records by entity type and entity id. usort($field_data[$sub_table], '_field_test_field_storage_query_sort_helper'); @@ -267,7 +267,7 @@ function field_test_field_storage_query($field_id, $conditions, $count, &$cursor break; } - if ($row->field_id == $field['id']) { + if ($row->field_id == $field->id()) { $match = TRUE; $condition_deleted = FALSE; // Add conditions. @@ -366,13 +366,13 @@ function _field_test_field_storage_query_sort_helper($a, $b) { * Implements hook_field_storage_create_field(). */ function field_test_field_storage_create_field($field) { - if ($field['storage']['type'] == 'field_test_storage_failure') { + if ($field->storage['type'] == 'field_test_storage_failure') { throw new Exception('field_test_storage_failure engine always fails to create fields'); } $data = _field_test_storage_data(); - $data[$field['id']] = array( + $data[$field->id()] = array( 'current' => array(), 'revisions' => array(), ); @@ -386,7 +386,7 @@ function field_test_field_storage_create_field($field) { function field_test_field_storage_delete_field($field) { $data = _field_test_storage_data(); - $field_data = &$data[$field['id']]; + $field_data = &$data[$field->id()]; foreach (array('current', 'revisions') as $sub_table) { foreach ($field_data[$sub_table] as &$row) { $row->deleted = TRUE; @@ -402,11 +402,11 @@ function field_test_field_storage_delete_field($field) { function field_test_field_storage_delete_instance($instance) { $data = _field_test_storage_data(); - $field = field_info_field($instance['field_name']); - $field_data = &$data[$field['id']]; + $field = field_info_field($instance->field_name); + $field_data = &$data[$field->id()]; foreach (array('current', 'revisions') as $sub_table) { foreach ($field_data[$sub_table] as &$row) { - if ($row->bundle == $instance['bundle']) { + if ($row->bundle == $instance->bundle) { $row->deleted = TRUE; } } @@ -431,9 +431,9 @@ function field_test_field_attach_rename_bundle($bundle_old, $bundle_new) { // We need to account for deleted or inactive fields and instances. $instances = field_read_instances(array('bundle' => $bundle_new), array('include_deleted' => TRUE, 'include_inactive' => TRUE)); foreach ($instances as $field_name => $instance) { - $field = field_info_field_by_id($instance['field_id']); - if ($field['storage']['type'] == 'field_test_storage') { - $field_data = &$data[$field['id']]; + $field = field_info_field_by_id($instance->field_id); + 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) { @@ -454,9 +454,9 @@ function field_test_field_attach_delete_bundle($entity_type, $bundle, $instances $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']]; + $field = field_info_field($instance->field_name); + if ($field && $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) { diff --git a/core/modules/field/tests/modules/field_test_config/config/field.field.field_test_import.yml b/core/modules/field/tests/modules/field_test_config/config/field.field.field_test_import.yml new file mode 100644 index 0000000..45afef5 --- /dev/null +++ b/core/modules/field/tests/modules/field_test_config/config/field.field.field_test_import.yml @@ -0,0 +1,49 @@ +id: field_test_import +uuid: fb38277f-1fd4-49d5-8d09-9d7037fdcce9 +field_name: field_test_import +type: text +translatable: false +entity_types: { } +cardinality: '1' +locked: false +settings: + max_length: '255' +module: text +active: 1 +deleted: 0 +storage_type: field_sql_storage +storage_module: field_sql_storage +storage_active: 1 +storage: + type: field_sql_storage + settings: { } + module: field_sql_storage + active: 1 + details: + sql: + FIELD_LOAD_CURRENT: + field_data_field_test_import: + value: field_test_import_value + format: field_test_import_format + FIELD_LOAD_REVISION: + field_revision_field_test_import: + value: field_test_import_value + format: field_test_import_format +columns: + value: + type: varchar + length: '255' + 'not null': false + format: + type: varchar + length: 255 + 'not null': false +'foreign_keys': + format: + table: filter_format + columns: + format: format +indexes: + format: + - format +langcode: und diff --git a/core/modules/field/tests/modules/field_test_config/config/field.instance.node.test_import.field_test_import.yml b/core/modules/field/tests/modules/field_test_config/config/field.instance.node.test_import.field_test_import.yml new file mode 100644 index 0000000..d2690db --- /dev/null +++ b/core/modules/field/tests/modules/field_test_config/config/field.instance.node.test_import.field_test_import.yml @@ -0,0 +1,33 @@ +id: node.test_import.field_test_import +uuid: 392b4e9d-6157-412e-9603-3d622512f498 +name: '' +label: node.test_import.field_test_import +data: + field_name: field_test_import + entity_type: node + bundle: test_import + label: 'Test import field' + widget: + weight: '-2' + type: text_textfield + module: text + active: 0 + settings: + size: '60' + field_id: fb38277f-1fd4-49d5-8d09-9d7037fdcce9 + id: 948dff + settings: + text_processing: '0' + user_register_form: false + required: 0 + description: '' + deleted: 0 + active: 1 + locked: false + type: text + module: text + storage_type: field_sql_storage + storage_active: 1 + storage_module: field_sql_storage + default_value: null +langcode: und diff --git a/core/modules/field/tests/modules/field_test_config/field_test_config.info b/core/modules/field/tests/modules/field_test_config/field_test_config.info new file mode 100644 index 0000000..49b0509 --- /dev/null +++ b/core/modules/field/tests/modules/field_test_config/field_test_config.info @@ -0,0 +1,6 @@ +name = "Field API configuration tests" +description = "Support module for the Field API configuration tests." +core = 8.x +package = Testing +version = VERSION +hidden = TRUE diff --git a/core/modules/field/tests/modules/field_test_config/field_test_config.module b/core/modules/field/tests/modules/field_test_config/field_test_config.module new file mode 100644 index 0000000..e208464 --- /dev/null +++ b/core/modules/field/tests/modules/field_test_config/field_test_config.module @@ -0,0 +1,6 @@ + TRUE, 'include_inactive' => TRUE)); - drupal_load('module', 'field_sql_storage'); - foreach ($fields as $field) { + // During a Drupal 7 to Drupal 8 upgrade it is only safe to call + // field configurations once field_update_8002() has finished. + if (drupal_get_installed_schema_version('field') >= 8002) { + // We don't call field_read_fields() as this will cause endless loops + // in entity_get_info(). + // @todo verify now that bundle info and view modes are moved out. + $fields = config_get_storage_names_with_prefix('field.field'); + foreach ($fields as $name) { + $field = config($name)->get(); if ($field['storage']['type'] == 'field_sql_storage') { - $schema += _field_sql_storage_schema($field); + $schema += _field_sql_storage_schema((object) $field); } } } + return $schema; } @@ -113,8 +119,8 @@ function field_sql_storage_update_8000(&$sandbox) { // Update schema. foreach ($fields as $field) { - $data_table = _field_sql_storage_tablename($field); - $revision_table = _field_sql_storage_revision_tablename($field); + $data_table = _field_sql_storage_tablename((object) $field); + $revision_table = _field_sql_storage_revision_tablename((object) $field); $table_info = array($data_table => $primary_key_data, $revision_table => $primary_key_revision); foreach ($table_info as $table => $primary_key) { diff --git a/core/modules/field_sql_storage/field_sql_storage.module b/core/modules/field_sql_storage/field_sql_storage.module index accca35..80a846f 100644 --- a/core/modules/field_sql_storage/field_sql_storage.module +++ b/core/modules/field_sql_storage/field_sql_storage.module @@ -44,11 +44,11 @@ function field_sql_storage_field_storage_info() { * A string containing the generated name for the database table. */ function _field_sql_storage_tablename($field) { - if ($field['deleted']) { - return "field_deleted_data_{$field['id']}"; + if ($field->deleted) { + return "field_deleted_data_" . $field->generate_table_id(); } else { - return "field_data_{$field['field_name']}"; + return "field_data_{$field->field_name}"; } } @@ -62,11 +62,11 @@ function _field_sql_storage_tablename($field) { * A string containing the generated name for the database table. */ function _field_sql_storage_revision_tablename($field) { - if ($field['deleted']) { - return "field_deleted_revision_{$field['id']}"; + if ($field->deleted) { + return "field_deleted_revision_" . $field->generate_table_id(); } else { - return "field_revision_{$field['field_name']}"; + return "field_revision_{$field->field_name}"; } } @@ -115,9 +115,9 @@ function _field_sql_storage_indexname($name, $index) { * One or more tables representing the schema for the field. */ function _field_sql_storage_schema($field) { - $deleted = $field['deleted'] ? 'deleted ' : ''; + $deleted = $field->deleted ? 'deleted ' : ''; $current = array( - 'description' => "Data storage for {$deleted}field {$field['id']} ({$field['field_name']})", + 'description' => "Data storage for {$deleted}field {$field->id} ({$field->field_name})", 'fields' => array( 'entity_type' => array( 'type' => 'varchar', @@ -178,44 +178,50 @@ function _field_sql_storage_schema($field) { ), ); - $field += array('columns' => array(), 'indexes' => array(), 'foreign keys' => array()); + $schema_properties = array('columns' => array(), 'indexes' => array(), 'foreign keys' => array()); + foreach ($schema_properties as $key => $value) { + if (!isset($field->data[$key])) { + $field->data[$key] = $value; + } + } + // Add field columns. - foreach ($field['columns'] as $column_name => $attributes) { - $real_name = _field_sql_storage_columnname($field['field_name'], $column_name); + foreach ($field->columns as $column_name => $attributes) { + $real_name = _field_sql_storage_columnname($field->field_name, $column_name); $current['fields'][$real_name] = $attributes; } // Add indexes. - foreach ($field['indexes'] as $index_name => $columns) { - $real_name = _field_sql_storage_indexname($field['field_name'], $index_name); + foreach ($field->indexes as $index_name => $columns) { + $real_name = _field_sql_storage_indexname($field->field_name, $index_name); foreach ($columns as $column_name) { // Indexes can be specified as either a column name or an array with // column name and length. Allow for either case. if (is_array($column_name)) { $current['indexes'][$real_name][] = array( - _field_sql_storage_columnname($field['field_name'], $column_name[0]), + _field_sql_storage_columnname($field->field_name, $column_name[0]), $column_name[1], ); } else { - $current['indexes'][$real_name][] = _field_sql_storage_columnname($field['field_name'], $column_name); + $current['indexes'][$real_name][] = _field_sql_storage_columnname($field->field_name, $column_name); } } } // Add foreign keys. - foreach ($field['foreign keys'] as $specifier => $specification) { - $real_name = _field_sql_storage_indexname($field['field_name'], $specifier); + foreach ($field->foreign_keys as $specifier => $specification) { + $real_name = _field_sql_storage_indexname($field->field_name, $specifier); $current['foreign keys'][$real_name]['table'] = $specification['table']; foreach ($specification['columns'] as $column => $referenced) { - $sql_storage_column = _field_sql_storage_columnname($field['field_name'], $column_name); + $sql_storage_column = _field_sql_storage_columnname($field->field_name, $column_name); $current['foreign keys'][$real_name]['columns'][$sql_storage_column] = $referenced; } } // Construct the revision table. $revision = $current; - $revision['description'] = "Revision archive storage for {$deleted}field {$field['id']} ({$field['field_name']})"; + $revision['description'] = "Revision archive storage for {$deleted}field {$field->id} ({$field->field_name})"; $revision['primary key'] = array('entity_type', 'entity_id', 'revision_id', 'deleted', 'delta', 'langcode'); $revision['fields']['revision_id']['not null'] = TRUE; $revision['fields']['revision_id']['description'] = 'The entity revision id this data is attached to'; @@ -244,7 +250,7 @@ function field_sql_storage_field_storage_create_field($field) { * data. */ function field_sql_storage_field_update_forbid($field, $prior_field, $has_data) { - if ($has_data && $field['columns'] != $prior_field['columns']) { + if ($has_data && $field->columns != $prior_field->columns) { throw new FieldUpdateForbiddenException("field_sql_storage cannot change the schema for an existing field with data."); } } @@ -294,30 +300,30 @@ function field_sql_storage_field_storage_update_field($field, $prior_field, $has // priors that exist unchanged. $table = _field_sql_storage_tablename($prior_field); $revision_table = _field_sql_storage_revision_tablename($prior_field); - foreach ($prior_field['indexes'] as $name => $columns) { - if (!isset($field['indexes'][$name]) || $columns != $field['indexes'][$name]) { - $real_name = _field_sql_storage_indexname($field['field_name'], $name); + foreach ($prior_field->indexes as $name => $columns) { + if (!isset($field->indexes[$name]) || $columns != $field->indexes[$name]) { + $real_name = _field_sql_storage_indexname($field->field_name, $name); db_drop_index($table, $real_name); db_drop_index($revision_table, $real_name); } } $table = _field_sql_storage_tablename($field); $revision_table = _field_sql_storage_revision_tablename($field); - foreach ($field['indexes'] as $name => $columns) { - if (!isset($prior_field['indexes'][$name]) || $columns != $prior_field['indexes'][$name]) { - $real_name = _field_sql_storage_indexname($field['field_name'], $name); + foreach ($field->indexes as $name => $columns) { + if (!isset($prior_field->indexes[$name]) || $columns != $prior_field->indexes[$name]) { + $real_name = _field_sql_storage_indexname($field->field_name, $name); $real_columns = array(); foreach ($columns as $column_name) { // Indexes can be specified as either a column name or an array with // column name and length. Allow for either case. if (is_array($column_name)) { $real_columns[] = array( - _field_sql_storage_columnname($field['field_name'], $column_name[0]), + _field_sql_storage_columnname($field->field_name, $column_name[0]), $column_name[1], ); } else { - $real_columns[] = _field_sql_storage_columnname($field['field_name'], $column_name); + $real_columns[] = _field_sql_storage_columnname($field->field_name, $column_name); } } db_add_index($table, $real_name, $real_columns); @@ -333,7 +339,7 @@ function field_sql_storage_field_storage_update_field($field, $prior_field, $has */ function field_sql_storage_field_storage_delete_field($field) { // Mark all data associated with the field for deletion. - $field['deleted'] = 0; + $field->deleted = 0; $table = _field_sql_storage_tablename($field); $revision_table = _field_sql_storage_revision_tablename($field); db_update($table) @@ -341,7 +347,7 @@ function field_sql_storage_field_storage_delete_field($field) { ->execute(); // Move the table to a unique name while the table contents are being deleted. - $field['deleted'] = 1; + $field->deleted = 1; $new_table = _field_sql_storage_tablename($field); $revision_new_table = _field_sql_storage_revision_tablename($field); db_rename_table($table, $new_table); @@ -357,7 +363,7 @@ function field_sql_storage_field_storage_load($entity_type, $entities, $age, $fi foreach ($fields as $field_id => $ids) { $field = field_info_field_by_id($field_id); - $field_name = $field['field_name']; + $field_name = $field->field_name; $table = $load_current ? _field_sql_storage_tablename($field) : _field_sql_storage_revision_tablename($field); $query = db_select($table, 't') @@ -379,11 +385,11 @@ function field_sql_storage_field_storage_load($entity_type, $entities, $age, $fi $delta_count[$row->entity_id][$row->langcode] = 0; } - if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->langcode] < $field['cardinality']) { + if ($field->cardinality == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->langcode] < $field->cardinality) { $item = array(); // For each column declared by the field, populate the item // from the prefixed database column. - foreach ($field['columns'] as $column => $attributes) { + foreach ($field->columns as $column => $attributes) { $column_name = _field_sql_storage_columnname($field_name, $column); $item[$column] = $row->$column_name; } @@ -410,7 +416,7 @@ function field_sql_storage_field_storage_write(EntityInterface $entity, $op, $fi foreach ($fields as $field_id) { $field = field_info_field_by_id($field_id); - $field_name = $field['field_name']; + $field_name = $field->field_name; $table_name = _field_sql_storage_tablename($field); $revision_name = _field_sql_storage_revision_tablename($field); @@ -444,7 +450,7 @@ function field_sql_storage_field_storage_write(EntityInterface $entity, $op, $fi // Prepare the multi-insert query. $do_insert = FALSE; $columns = array('entity_type', 'entity_id', 'revision_id', 'bundle', 'delta', 'langcode'); - foreach ($field['columns'] as $column => $attributes) { + foreach ($field->columns as $column => $attributes) { $columns[] = _field_sql_storage_columnname($field_name, $column); } $query = db_insert($table_name)->fields($columns); @@ -464,7 +470,7 @@ function field_sql_storage_field_storage_write(EntityInterface $entity, $op, $fi 'delta' => $delta, 'langcode' => $langcode, ); - foreach ($field['columns'] as $column => $attributes) { + foreach ($field->columns as $column => $attributes) { $record[_field_sql_storage_columnname($field_name, $column)] = isset($item[$column]) ? $item[$column] : NULL; } $query->values($record); @@ -472,7 +478,7 @@ function field_sql_storage_field_storage_write(EntityInterface $entity, $op, $fi $revision_query->values($record); } - if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && ++$delta_count == $field['cardinality']) { + if ($field->cardinality != FIELD_CARDINALITY_UNLIMITED && ++$delta_count == $field->cardinality) { break; } } @@ -497,8 +503,8 @@ function field_sql_storage_field_storage_write(EntityInterface $entity, $op, $fi */ function field_sql_storage_field_storage_delete(EntityInterface $entity, $fields) { foreach (field_info_instances($entity->entityType(), $entity->bundle()) as $instance) { - if (isset($fields[$instance['field_id']])) { - $field = field_info_field_by_id($instance['field_id']); + if (isset($fields[$instance->field_id])) { + $field = field_info_field_by_id($instance->field_id); field_sql_storage_field_storage_purge($entity, $field, $instance); } } @@ -549,18 +555,18 @@ function field_sql_storage_field_storage_delete_revision(EntityInterface $entity * This function simply marks for deletion all data associated with the field. */ function field_sql_storage_field_storage_delete_instance($instance) { - $field = field_info_field($instance['field_name']); + $field = field_info_field($instance->field_name); $table_name = _field_sql_storage_tablename($field); $revision_name = _field_sql_storage_revision_tablename($field); db_update($table_name) ->fields(array('deleted' => 1)) - ->condition('entity_type', $instance['entity_type']) - ->condition('bundle', $instance['bundle']) + ->condition('entity_type', $instance->entity_type) + ->condition('bundle', $instance->bundle) ->execute(); db_update($revision_name) ->fields(array('deleted' => 1)) - ->condition('entity_type', $instance['entity_type']) - ->condition('bundle', $instance['bundle']) + ->condition('entity_type', $instance->entity_type) + ->condition('bundle', $instance->bundle) ->execute(); } @@ -571,8 +577,8 @@ function field_sql_storage_field_attach_rename_bundle($entity_type, $bundle_old, // 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) { - $field = field_info_field_by_id($instance['field_id']); - if ($field['storage']['type'] == 'field_sql_storage') { + $field = field_info_field_by_id($instance->field_id); + if ($field->storage['type'] == 'field_sql_storage') { $table_name = _field_sql_storage_tablename($field); $revision_name = _field_sql_storage_revision_tablename($field); db_update($table_name) @@ -607,10 +613,10 @@ function field_sql_storage_field_storage_purge_field($field) { */ function field_sql_storage_field_storage_details($field) { $details = array(); - if (!empty($field['columns'])) { + if (!empty($field->columns)) { // Add field columns. - foreach ($field['columns'] as $column_name => $attributes) { - $real_name = _field_sql_storage_columnname($field['field_name'], $column_name); + foreach ($field->columns as $column_name => $attributes) { + $real_name = _field_sql_storage_columnname($field->field_name, $column_name); $columns[$column_name] = $real_name; } return array( diff --git a/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Entity/Tables.php b/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Entity/Tables.php index 09c8fda..2760cb8 100644 --- a/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Entity/Tables.php +++ b/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Entity/Tables.php @@ -110,7 +110,7 @@ function addField($field, $type, $langcode) { if ($key < $count) { $next = $specifiers[$key + 1]; // Is this a field column? - if (isset($field['columns'][$next]) || in_array($next, field_reserved_columns())) { + if (isset($field->columns[$next]) || in_array($next, field_reserved_columns())) { // Use it. $column = $next; // Do not process it again. @@ -128,7 +128,7 @@ function addField($field, $type, $langcode) { // Get the field definitions form a mocked entity. $entity = entity_create($entity_type, array()); - $propertyDefinitions = $entity->{$field['field_name']}->getPropertyDefinitions(); + $propertyDefinitions = $entity->{$field->field_name}->getPropertyDefinitions(); // If the column is not yet known, ie. the // $node->field_image->entity case then use the id source as the @@ -148,7 +148,7 @@ function addField($field, $type, $langcode) { $column = 'value'; } $table = $this->ensureFieldTable($index_prefix, $field, $type, $langcode, $base_table, $entity_id_field, $field_id_field); - $sql_column = _field_sql_storage_columnname($field['field_name'], $column); + $sql_column = _field_sql_storage_columnname($field->field_name, $column); } // This is an entity property (non-configurable field). else { @@ -231,10 +231,10 @@ protected function ensureEntityTable($index_prefix, $property, $type, $langcode, * @throws \Drupal\Core\Entity\Query\QueryException */ protected function ensureFieldTable($index_prefix, &$field, $type, $langcode, $base_table, $entity_id_field, $field_id_field) { - $field_name = $field['field_name']; + $field_name = $field->field_name; if (!isset($this->fieldTables[$index_prefix . $field_name])) { $table = $this->sqlQuery->getMetaData('age') == FIELD_LOAD_CURRENT ? _field_sql_storage_tablename($field) : _field_sql_storage_revision_tablename($field); - if ($field['cardinality'] != 1) { + if ($field->cardinality != 1) { $this->sqlQuery->addMetaData('simple_query', FALSE); } $entity_type = $this->sqlQuery->getMetaData('entity_type'); diff --git a/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Tests/FieldSqlStorageTest.php b/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Tests/FieldSqlStorageTest.php index e79757e..c3f1cfb 100644 --- a/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Tests/FieldSqlStorageTest.php +++ b/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Tests/FieldSqlStorageTest.php @@ -68,7 +68,7 @@ function testFieldAttachLoad() { for ($evid = 0; $evid < 4; ++$evid) { $values[$evid] = array(); // Note: we insert one extra value ('<=' instead of '<'). - for ($delta = 0; $delta <= $this->field['cardinality']; $delta++) { + for ($delta = 0; $delta <= $this->field->cardinality; $delta++) { $value = mt_rand(1, 127); $values[$evid][] = $value; $query->values(array($entity_type, $eid, $evid, $delta, $langcode, $value)); @@ -84,10 +84,10 @@ function testFieldAttachLoad() { $query->execute(); // Load the "most current revision" - $entity = field_test_create_entity($eid, 0, $this->instance['bundle']); + $entity = field_test_create_entity($eid, 0, $this->instance->bundle); field_attach_load($entity_type, array($eid => $entity)); foreach ($values[0] as $delta => $value) { - if ($delta < $this->field['cardinality']) { + if ($delta < $this->field->cardinality) { $this->assertEqual($entity->{$this->field_name}[$langcode][$delta]['value'], $value, "Value $delta is loaded correctly for current revision"); } else { @@ -97,10 +97,10 @@ function testFieldAttachLoad() { // Load every revision for ($evid = 0; $evid < 4; ++$evid) { - $entity = field_test_create_entity($eid, $evid, $this->instance['bundle']); + $entity = field_test_create_entity($eid, $evid, $this->instance->bundle); field_attach_load_revision($entity_type, array($eid => $entity)); foreach ($values[$evid] as $delta => $value) { - if ($delta < $this->field['cardinality']) { + if ($delta < $this->field->cardinality) { $this->assertEqual($entity->{$this->field_name}[$langcode][$delta]['value'], $value, "Value $delta for revision $evid is loaded correctly"); } else { @@ -113,7 +113,7 @@ function testFieldAttachLoad() { // loaded. $eid = $evid = 1; $unavailable_langcode = 'xx'; - $entity = field_test_create_entity($eid, $evid, $this->instance['bundle']); + $entity = field_test_create_entity($eid, $evid, $this->instance->bundle); $values = array($entity_type, $eid, $evid, 0, $unavailable_langcode, mt_rand(1, 127)); db_insert($this->table)->fields($columns)->values($values)->execute(); db_insert($this->revision_table)->fields($columns)->values($values)->execute(); @@ -127,14 +127,14 @@ function testFieldAttachLoad() { */ function testFieldAttachInsertAndUpdate() { $entity_type = 'test_entity'; - $entity = field_test_create_entity(0, 0, $this->instance['bundle']); + $entity = field_test_create_entity(0, 0, $this->instance->bundle); $langcode = LANGUAGE_NOT_SPECIFIED; // Test insert. $values = array(); // Note: we try to insert one extra value ('<=' instead of '<'). // TODO : test empty values filtering and "compression" (store consecutive deltas). - for ($delta = 0; $delta <= $this->field['cardinality']; $delta++) { + for ($delta = 0; $delta <= $this->field->cardinality; $delta++) { $values[$delta]['value'] = mt_rand(1, 127); } $entity->{$this->field_name}[$langcode] = $rev_values[0] = $values; @@ -142,7 +142,7 @@ function testFieldAttachInsertAndUpdate() { $rows = db_select($this->table, 't')->fields('t')->execute()->fetchAllAssoc('delta', PDO::FETCH_ASSOC); foreach ($values as $delta => $value) { - if ($delta < $this->field['cardinality']) { + if ($delta < $this->field->cardinality) { $this->assertEqual($rows[$delta][$this->field_name . '_value'], $value['value'], t("Value $delta is inserted correctly")); } else { @@ -151,17 +151,17 @@ function testFieldAttachInsertAndUpdate() { } // Test update. - $entity = field_test_create_entity(0, 1, $this->instance['bundle']); + $entity = field_test_create_entity(0, 1, $this->instance->bundle); $values = array(); // Note: we try to update one extra value ('<=' instead of '<'). - for ($delta = 0; $delta <= $this->field['cardinality']; $delta++) { + for ($delta = 0; $delta <= $this->field->cardinality; $delta++) { $values[$delta]['value'] = mt_rand(1, 127); } $entity->{$this->field_name}[$langcode] = $rev_values[1] = $values; field_attach_update($entity); $rows = db_select($this->table, 't')->fields('t')->execute()->fetchAllAssoc('delta', PDO::FETCH_ASSOC); foreach ($values as $delta => $value) { - if ($delta < $this->field['cardinality']) { + if ($delta < $this->field->cardinality) { $this->assertEqual($rows[$delta][$this->field_name . '_value'], $value['value'], t("Value $delta is updated correctly")); } else { @@ -191,7 +191,7 @@ function testFieldAttachInsertAndUpdate() { field_attach_update($entity); $rows = db_select($this->table, 't')->fields('t')->execute()->fetchAllAssoc('delta', PDO::FETCH_ASSOC); foreach ($values as $delta => $value) { - if ($delta < $this->field['cardinality']) { + if ($delta < $this->field->cardinality) { $this->assertEqual($rows[$delta][$this->field_name . '_value'], $value['value'], t("Update with no field_name entry leaves value $delta untouched")); } } @@ -208,7 +208,7 @@ function testFieldAttachInsertAndUpdate() { */ function testFieldAttachSaveMissingData() { $entity_type = 'test_entity'; - $entity = field_test_create_entity(0, 0, $this->instance['bundle']); + $entity = field_test_create_entity(0, 0, $this->instance->bundle); $langcode = LANGUAGE_NOT_SPECIFIED; // Insert: Field is missing @@ -259,7 +259,7 @@ function testFieldAttachSaveMissingData() { $unavailable_langcode = 'xx'; db_insert($this->table) ->fields(array('entity_type', 'bundle', 'deleted', 'entity_id', 'revision_id', 'delta', 'langcode')) - ->values(array($entity_type, $this->instance['bundle'], 0, 0, 0, 0, $unavailable_langcode)) + ->values(array($entity_type, $this->instance->bundle, 0, 0, 0, 0, $unavailable_langcode)) ->execute(); $count = db_select($this->table) ->countQuery() @@ -307,12 +307,12 @@ function testUpdateFieldSchemaWithData() { $field = field_create_field($field); $instance = array('field_name' => 'decimal52', 'entity_type' => 'test_entity', 'bundle' => 'test_bundle'); $instance = field_create_instance($instance); - $entity = field_test_create_entity(0, 0, $instance['bundle']); + $entity = field_test_create_entity(0, 0, $instance->bundle); $entity->decimal52[LANGUAGE_NOT_SPECIFIED][0]['value'] = '1.235'; $entity->save(); // Attempt to update the field in a way that would work without data. - $field['settings']['scale'] = 3; + $field->settings['scale'] = 3; try { field_update_field($field); $this->fail(t('Cannot update field schema with data.')); @@ -332,7 +332,7 @@ function testFieldUpdateFailure() { // Attempt to update the field in a way that would break the storage. $prior_field = $field; - $field['settings']['max_length'] = -1; + $field->settings['max_length'] = -1; try { field_update_field($field); $this->fail(t('Update succeeded.')); @@ -367,7 +367,7 @@ function testFieldUpdateIndexesWithData() { } // Add data so the table cannot be dropped. - $entity = field_test_create_entity(1, 1, $instance['bundle']); + $entity = field_test_create_entity(1, 1, $instance->bundle); $entity->{$field_name}[LANGUAGE_NOT_SPECIFIED][0]['value'] = 'field data'; $entity->save(); @@ -387,7 +387,7 @@ function testFieldUpdateIndexesWithData() { } // Verify that the tables were not dropped. - $entity = field_test_create_entity(1, 1, $instance['bundle']); + $entity = field_test_create_entity(1, 1, $instance->bundle); field_attach_load('test_entity', array(1 => $entity)); $this->assertEqual($entity->{$field_name}[LANGUAGE_NOT_SPECIFIED][0]['value'], 'field data', t("Index changes performed without dropping the tables")); } @@ -400,21 +400,21 @@ function testFieldStorageDetails() { $revision = _field_sql_storage_revision_tablename($this->field); // Retrieve the field and instance with field_info so the storage details are attached. - $field = field_info_field($this->field['field_name']); - $instance = field_info_instance($this->instance['entity_type'], $this->instance['field_name'], $this->instance['bundle']); + $field = field_info_field($this->field->field_name); + $instance = field_info_instance($this->instance->entity_type, $this->instance->field_name, $this->instance->bundle); // The storage details are indexed by a storage engine type. - $this->assertTrue(array_key_exists('sql', $field['storage']['details']), 'The storage type is SQL.'); + $this->assertTrue(array_key_exists('sql', $field->storage['details']), 'The storage type is SQL.'); // The SQL details are indexed by table name. - $details = $field['storage']['details']['sql']; + $details = $field->storage['details']['sql']; $this->assertTrue(array_key_exists($current, $details[FIELD_LOAD_CURRENT]), 'Table name is available in the instance array.'); $this->assertTrue(array_key_exists($revision, $details[FIELD_LOAD_REVISION]), 'Revision table name is available in the instance array.'); // Test current and revision storage details together because the columns // are the same. - foreach ((array) $this->field['columns'] as $column_name => $attributes) { - $storage_column_name = _field_sql_storage_columnname($this->field['field_name'], $column_name); + foreach ($this->field->columns as $column_name => $attributes) { + $storage_column_name = _field_sql_storage_columnname($this->field->field_name, $column_name); $this->assertEqual($details[FIELD_LOAD_CURRENT][$current][$column_name], $storage_column_name, t('Column name %value matches the definition in %bin.', array('%value' => $column_name, '%bin' => $current))); $this->assertEqual($details[FIELD_LOAD_REVISION][$revision][$column_name], $storage_column_name, t('Column name %value matches the definition in %bin.', array('%value' => $column_name, '%bin' => $revision))); } @@ -431,13 +431,13 @@ function testFieldSqlStorageForeignKeys() { // Retrieve the field and instance with field_info and verify the foreign // keys are in place. $field = field_info_field($field_name); - $this->assertEqual($field['foreign keys']['format']['table'], 'filter_format', 'Foreign key table name preserved through CRUD'); - $this->assertEqual($field['foreign keys']['format']['columns']['format'], 'format', 'Foreign key column name preserved through CRUD'); + $this->assertEqual($field->foreign_keys['format']['table'], 'filter_format', 'Foreign key table name preserved through CRUD'); + $this->assertEqual($field->foreign_keys['format']['columns']['format'], 'format', 'Foreign key column name preserved through CRUD'); // Now grab the SQL schema and verify that too. $schema = drupal_get_schema(_field_sql_storage_tablename($field)); $this->assertEqual(count($schema['foreign keys']), 1, t("There is 1 foreign key in the schema")); $foreign_key = reset($schema['foreign keys']); - $filter_column = _field_sql_storage_columnname($field['field_name'], 'format'); + $filter_column = _field_sql_storage_columnname($field->field_name, 'format'); $this->assertEqual($foreign_key['table'], 'filter_format', 'Foreign key table name preserved in the schema'); $this->assertEqual($foreign_key['columns'][$filter_column], 'format', 'Foreign key column name preserved in the schema'); } diff --git a/core/modules/field_ui/field_ui.admin.inc b/core/modules/field_ui/field_ui.admin.inc index 4056e4a..189af72 100644 --- a/core/modules/field_ui/field_ui.admin.inc +++ b/core/modules/field_ui/field_ui.admin.inc @@ -5,7 +5,7 @@ * Administrative interface for custom field type creation. */ -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; use Drupal\field_ui\FieldOverview; use Drupal\field_ui\DisplayOverview; @@ -34,10 +34,10 @@ function field_ui_fields_list() { // Initialize the row if we encounter the field for the first time. if (!isset($rows[$field_name])) { - $rows[$field_name]['class'] = $field['locked'] ? array('menu-disabled') : array(''); - $rows[$field_name]['data'][0] = $field['locked'] ? t('@field_name (Locked)', array('@field_name' => $field_name)) : $field_name; - $module_name = $field_types[$field['type']]['module']; - $rows[$field_name]['data'][1] = $field_types[$field['type']]['label'] . ' ' . t('(module: !module)', array('!module' => $modules[$module_name]->info['name'])); + $rows[$field_name]['class'] = $field->locked ? array('menu-disabled') : array(''); + $rows[$field_name]['data'][0] = $field->locked ? t('@field_name (Locked)', array('@field_name' => $field_name)) : $field_name; + $module_name = $field_types[$field->type]['module']; + $rows[$field_name]['data'][1] = $field_types[$field->type]['label'] . ' ' . t('(module: !module)', array('!module' => $modules[$module_name]->info['name'])); } // Add the current instance. @@ -71,10 +71,10 @@ function field_ui_inactive_message($entity_type, $bundle) { foreach ($inactive_instances as $field_name => $instance) { $list[] = t('%field (@field_name) field requires the %widget_type widget provided by %widget_module module', array( - '%field' => $instance['label'], - '@field_name' => $instance['field_name'], - '%widget_type' => isset($widget_types[$instance['widget']['type']]) ? $widget_types[$instance['widget']['type']]['label'] : $instance['widget']['type'], - '%widget_module' => $instance['widget']['module'], + '%field' => $instance->label, + '@field_name' => $instance->field_name, + '%widget_type' => isset($widget_types[$instance->widget_settings['type']]) ? $widget_types[$instance->widget_settings['type']]['label'] : $instance->widget_settings['type'], + '%widget_module' => $instance->widget_settings['module'], )); } drupal_set_message(t('Inactive fields are not shown unless their providing modules are enabled. The following fields are not enabled: !list', array('!list' => theme('item_list', array('items' => $list)))), 'error'); @@ -465,23 +465,23 @@ function field_ui_existing_field_options($entity_type, $bundle) { // No need to look in the current bundle. if (!($existing_bundle == $bundle && $existing_entity_type == $entity_type)) { foreach ($instances as $instance) { - $field = field_info_field($instance['field_name']); + $field = field_info_field($instance->field_name); // Don't show // - locked fields, // - fields already in the current bundle, // - fields that cannot be added to the entity type, // - fields that should not be added via user interface. - if (empty($field['locked']) - && !field_info_instance($entity_type, $field['field_name'], $bundle) - && (empty($field['entity_types']) || in_array($entity_type, $field['entity_types'])) - && empty($field_types[$field['type']]['no_ui'])) { - $info[$instance['field_name']] = array( - 'type' => $field['type'], - 'type_label' => $field_types[$field['type']]['label'], - 'field' => $field['field_name'], - 'label' => $instance['label'], - 'widget_type' => $instance['widget']['type'], + if (empty($field->locked) + && !field_info_instance($entity_type, $field->field_name, $bundle) + && (empty($field->entity_types) || in_array($entity_type, $field->entity_types)) + && empty($field_types[$field->type]['no_ui'])) { + $info[$instance->field_name] = array( + 'type' => $field->type, + 'type_label' => $field_types[$field->type]['label'], + 'field' => $field->field_name, + 'label' => $instance->label, + 'widget_type' => $instance->widget_settings['type'], ); } } @@ -499,16 +499,16 @@ function field_ui_existing_field_options($entity_type, $bundle) { * @ingroups forms */ function field_ui_field_settings_form($form, &$form_state, $instance) { - $bundle = $instance['bundle']; - $entity_type = $instance['entity_type']; - $field = field_info_field($instance['field_name']); + $bundle = $instance->bundle; + $entity_type = $instance->entity_type; + $field = field_info_field($instance->field_name); $form['#field'] = $field; $form['#entity_type'] = $entity_type; $form['#bundle'] = $bundle; - drupal_set_title($instance['label']); + drupal_set_title($instance->label); - $description = '

' . t('These settings apply to the %field field everywhere it is used. These settings impact the way that data is stored in the database and cannot be changed once data has been created.', array('%field' => $instance['label'])) . '

'; + $description = '

' . t('These settings apply to the %field field everywhere it is used. These settings impact the way that data is stored in the database and cannot be changed once data has been created.', array('%field' => $instance->label)) . '

'; // Create a form structure for the field values. $form['field'] = array( @@ -524,7 +524,7 @@ function field_ui_field_settings_form($form, &$form_state, $instance) { } // Build the configurable field values. - $cardinality = $field['cardinality']; + $cardinality = $field->cardinality; $form['field']['container'] = array( // We can't use the container element because it doesn't support the title // or description properties. @@ -559,10 +559,10 @@ function field_ui_field_settings_form($form, &$form_state, $instance) { } // Build the non-configurable field values. - $form['field']['field_name'] = array('#type' => 'value', '#value' => $field['field_name']); - $form['field']['type'] = array('#type' => 'value', '#value' => $field['type']); - $form['field']['module'] = array('#type' => 'value', '#value' => $field['module']); - $form['field']['active'] = array('#type' => 'value', '#value' => $field['active']); + $form['field']['field_name'] = array('#type' => 'value', '#value' => $field->field_name); + $form['field']['type'] = array('#type' => 'value', '#value' => $field->type); + $form['field']['module'] = array('#type' => 'value', '#value' => $field->module); + $form['field']['active'] = array('#type' => 'value', '#value' => $field->active); // Add settings provided by the field module. The field module is // responsible for not returning settings that cannot be changed if @@ -570,7 +570,7 @@ function field_ui_field_settings_form($form, &$form_state, $instance) { $form['field']['settings'] = array( '#weight' => 10, ); - $additions = module_invoke($field['module'], 'field_settings_form', $field, $instance, $has_data); + $additions = module_invoke($field->module, 'field_settings_form', $field, $instance, $has_data); if (is_array($additions)) { $form['field']['settings'] += $additions; } @@ -616,18 +616,20 @@ function field_ui_field_settings_form_submit($form, &$form_state) { $entity_type = $form['#entity_type']; $bundle = $form['#bundle']; - $instance = field_info_instance($entity_type, $field['field_name'], $bundle); + $instance = field_info_instance($entity_type, $field->field_name, $bundle); // Update the field. - $field = array_merge($field, $field_values); + foreach ($field_values as $key => $value) { + $field->{$key} = $value; + } try { field_update_field($field); - drupal_set_message(t('Updated field %label field settings.', array('%label' => $instance['label']))); + drupal_set_message(t('Updated field %label field settings.', array('%label' => $instance->label))); $form_state['redirect'] = field_ui_next_destination($entity_type, $bundle); } catch (Exception $e) { - drupal_set_message(t('Attempt to update field %label failed: %message.', array('%label' => $instance['label'], '%message' => $e->getMessage())), 'error'); + drupal_set_message(t('Attempt to update field %label failed: %message.', array('%label' => $instance->label, '%message' => $e->getMessage())), 'error'); } } @@ -639,11 +641,11 @@ function field_ui_field_settings_form_submit($form, &$form_state) { * @ingroup forms */ function field_ui_widget_type_form($form, &$form_state, FieldInstance $instance) { - drupal_set_title($instance['label']); + drupal_set_title($instance->label); - $bundle = $instance['bundle']; - $entity_type = $instance['entity_type']; - $field_name = $instance['field_name']; + $bundle = $instance->bundle; + $entity_type = $instance->entity_type; + $field_name = $instance->field_name; $field = field_info_field($field_name); $bundles = entity_get_bundles(); @@ -659,7 +661,7 @@ function field_ui_widget_type_form($form, &$form_state, FieldInstance $instance) '#type' => 'select', '#title' => t('Widget type'), '#required' => TRUE, - '#options' => field_ui_widget_type_options($field['type']), + '#options' => field_ui_widget_type_options($field->type), '#default_value' => $instance->getWidget()->getPluginId(), '#description' => t('The type of form element you would like to present to the user when creating this field in the %type type.', array('%type' => $bundle_label)), ); @@ -689,15 +691,15 @@ function field_ui_widget_type_form_submit($form, &$form_state) { $widget_type = field_info_widget_types($form_values['widget_type']); $widget_module = $widget_type['module']; - $instance['widget']['type'] = $form_values['widget_type']; - $instance['widget']['module'] = $widget_module; + $instance->widget_settings['type'] = $form_values['widget_type']; + $instance->widget_settings['module'] = $widget_module; try { field_update_instance($instance); - drupal_set_message(t('Changed the widget for field %label.', array('%label' => $instance['label']))); + drupal_set_message(t('Changed the widget for field %label.', array('%label' => $instance->label))); } catch (Exception $e) { - drupal_set_message(t('There was a problem changing the widget for field %label.', array('%label' => $instance['label'])), 'error'); + drupal_set_message(t('There was a problem changing the widget for field %label.', array('%label' => $instance->label)), 'error'); } $form_state['redirect'] = field_ui_next_destination($entity_type, $bundle); @@ -711,25 +713,25 @@ function field_ui_widget_type_form_submit($form, &$form_state) { * @ingroup forms */ function field_ui_field_delete_form($form, &$form_state, $instance) { - $bundle = $instance['bundle']; - $entity_type = $instance['entity_type']; - $field = field_info_field($instance['field_name']); + $bundle = $instance->bundle; + $entity_type = $instance->entity_type; + $field = field_info_field($instance->field_name); $admin_path = field_ui_bundle_admin_path($entity_type, $bundle); $form['entity_type'] = array('#type' => 'value', '#value' => $entity_type); $form['bundle'] = array('#type' => 'value', '#value' => $bundle); - $form['field_name'] = array('#type' => 'value', '#value' => $field['field_name']); + $form['field_name'] = array('#type' => 'value', '#value' => $field->field_name); $output = confirm_form($form, - t('Are you sure you want to delete the field %field?', array('%field' => $instance['label'])), + t('Are you sure you want to delete the field %field?', array('%field' => $instance->label)), $admin_path . '/fields', t('If you have any content left in this field, it will be lost. This action cannot be undone.'), t('Delete'), t('Cancel'), 'confirm' ); - if ($field['locked']) { + if ($field->locked) { unset($output['actions']['submit']); $output['description']['#markup'] = t('This field is locked and cannot be deleted.'); } @@ -754,12 +756,12 @@ function field_ui_field_delete_form_submit($form, &$form_state) { $bundles = entity_get_bundles(); $bundle_label = $bundles[$entity_type][$bundle]['label']; - if (!empty($bundle) && $field && !$field['locked'] && $form_values['confirm']) { + if (!empty($bundle) && $field && !$field->locked && $form_values['confirm']) { field_delete_instance($instance); - drupal_set_message(t('The field %field has been deleted from the %type content type.', array('%field' => $instance['label'], '%type' => $bundle_label))); + drupal_set_message(t('The field %field has been deleted from the %type content type.', array('%field' => $instance->label, '%type' => $bundle_label))); } else { - drupal_set_message(t('There was a problem removing the %field from the %type content type.', array('%field' => $instance['label'], '%type' => $bundle_label)), 'error'); + drupal_set_message(t('There was a problem removing the %field from the %type content type.', array('%field' => $instance->label, '%type' => $bundle_label)), 'error'); } $admin_path = field_ui_bundle_admin_path($entity_type, $bundle); @@ -784,31 +786,31 @@ function field_ui_field_delete_form_submit($form, &$form_state) { * @ingroup forms */ function field_ui_field_edit_form($form, &$form_state, $instance) { - $bundle = $instance['bundle']; - $entity_type = $instance['entity_type']; - $field = field_info_field($instance['field_name']); + $bundle = $instance->bundle; + $entity_type = $instance->entity_type; + $field = field_info_field($instance->field_name); $bundles = entity_get_bundles(); drupal_set_title(t('%instance settings for %bundle', array( - '%instance' => $instance['label'], + '%instance' => $instance->label, '%bundle' => $bundles[$entity_type][$bundle]['label'], )), PASS_THROUGH); $form['#field'] = $field; $form['#instance'] = $instance; // Create an arbitrary entity object (used by the 'default value' widget). - $ids = (object) array('entity_type' => $instance['entity_type'], 'bundle' => $instance['bundle'], 'entity_id' => NULL); + $ids = (object) array('entity_type' => $instance->entity_type, 'bundle' => $instance->bundle, 'entity_id' => NULL); $form['#entity'] = _field_create_entity_from_ids($ids); $form['#entity']->field_ui_default_value = TRUE; - if (!empty($field['locked'])) { + if (!empty($field->locked)) { $form['locked'] = array( - '#markup' => t('The field %field is locked and cannot be edited.', array('%field' => $instance['label'])), + '#markup' => t('The field %field is locked and cannot be edited.', array('%field' => $instance->label)), ); return $form; } - $widget_type = field_info_widget_types($instance['widget']['type']); + $widget_type = field_info_widget_types($instance->widget_settings['type']); // Create a form structure for the instance values. $form['instance'] = array( @@ -818,7 +820,7 @@ function field_ui_field_edit_form($form, &$form_state, $instance) { // Build the non-configurable instance values. $form['instance']['field_name'] = array( '#type' => 'value', - '#value' => $instance['field_name'], + '#value' => $instance->field_name, ); $form['instance']['entity_type'] = array( '#type' => 'value', @@ -828,16 +830,16 @@ function field_ui_field_edit_form($form, &$form_state, $instance) { '#type' => 'value', '#value' => $bundle, ); - $form['instance']['widget']['weight'] = array( + $form['instance']['widget_settings']['weight'] = array( '#type' => 'value', - '#value' => !empty($instance['widget']['weight']) ? $instance['widget']['weight'] : 0, + '#value' => !empty($instance->widget_settings['weight']) ? $instance->widget_settings['weight'] : 0, ); // Build the configurable instance values. $form['instance']['label'] = array( '#type' => 'textfield', '#title' => t('Label'), - '#default_value' => !empty($instance['label']) ? $instance['label'] : $field['field_name'], + '#default_value' => !empty($instance->label) ? $instance->label : $field->field_name, '#required' => TRUE, '#weight' => -20, ); @@ -845,7 +847,7 @@ function field_ui_field_edit_form($form, &$form_state, $instance) { $form['instance']['description'] = array( '#type' => 'textarea', '#title' => t('Help text'), - '#default_value' => !empty($instance['description']) ? $instance['description'] : '', + '#default_value' => !empty($instance->description) ? $instance->description : '', '#rows' => 5, '#description' => t('Instructions to present to the user below this field on the editing form.
Allowed HTML tags: @tags', array('@tags' => _field_filter_xss_display_allowed_tags())) . '
' . t('This field supports tokens.'), '#weight' => -10, @@ -854,26 +856,26 @@ function field_ui_field_edit_form($form, &$form_state, $instance) { $form['instance']['required'] = array( '#type' => 'checkbox', '#title' => t('Required field'), - '#default_value' => !empty($instance['required']), + '#default_value' => !empty($instance->required), '#weight' => -5, ); // Build the widget component of the instance. - $form['instance']['widget']['type'] = array( + $form['instance']['widget_settings']['type'] = array( '#type' => 'value', - '#value' => $instance['widget']['type'], + '#value' => $instance->widget_settings['type'], ); - $form['instance']['widget']['module'] = array( + $form['instance']['widget_settings']['module'] = array( '#type' => 'value', '#value' => $widget_type['module'], ); - $form['instance']['widget']['active'] = array( + $form['instance']['widget_settings']['active'] = array( '#type' => 'value', - '#value' => !empty($field['instance']['widget']['active']) ? 1 : 0, + '#value' => !empty($instance->widget_settings['active']) ? 1 : 0, // straight bug ? ); // Add additional field instance settings from the field module. - $additions = module_invoke($field['module'], 'field_instance_settings_form', $field, $instance, $form_state); + $additions = module_invoke($field->module, 'field_instance_settings_form', $field, $instance, $form_state); if (is_array($additions)) { $form['instance']['settings'] = $additions; $form['instance']['settings']['#weight'] = 10; @@ -881,11 +883,11 @@ function field_ui_field_edit_form($form, &$form_state, $instance) { // Add widget settings for the widget type. $additions = $instance->getWidget()->settingsForm($form, $form_state); - $form['instance']['widget']['settings'] = $additions ? $additions : array('#type' => 'value', '#value' => array()); - $form['instance']['widget']['#weight'] = 20; + $form['instance']['widget_settings']['settings'] = $additions ? $additions : array('#type' => 'value', '#value' => array()); + $form['instance']['widget_settings']['#weight'] = 20; // Add handling for default value if not provided by any other module. - if (field_behaviors_widget('default_value', $instance) == FIELD_BEHAVIOR_DEFAULT && empty($instance['default_value_function'])) { + if (field_behaviors_widget('default_value', $instance) == FIELD_BEHAVIOR_DEFAULT && empty($instance->default_value_function)) { $form['instance']['default_value_widget'] = field_ui_default_value_widget($field, $instance, $form, $form_state); } @@ -912,14 +914,14 @@ function field_ui_field_edit_form_delete_submit($form, &$form_state) { unset($_GET['destination']); } $instance = $form['#instance']; - $form_state['redirect'] = array('admin/structure/types/manage/' . $instance['bundle'] . '/fields/' . $instance['field_name'] . '/delete', array('query' => $destination)); + $form_state['redirect'] = array('admin/structure/types/manage/' . $instance->bundle . '/fields/' . $instance->field_name . '/delete', array('query' => $destination)); } /** * Builds the default value widget for a given field instance. */ function field_ui_default_value_widget($field, $instance, &$form, &$form_state) { - $field_name = $field['field_name']; + $field_name = $field->field_name; $entity = $form['#entity']; $element = array( @@ -935,12 +937,12 @@ function field_ui_default_value_widget($field, $instance, &$form, &$form_state) // Adjust the instance definition used for the form element. We want a // non-required input and no description. - $instance['required'] = FALSE; - $instance['description'] = ''; + $instance->required = FALSE; + $instance->description = ''; // Insert the widget. Since we do not use the "official" instance definition, // the whole flow cannot use field_invoke_method(). - $items = (array) $instance['default_value']; + $items = (array) $instance->default_value; $element += $instance->getWidget()->form($entity, LANGUAGE_NOT_SPECIFIED, $items, $element, $form_state); return $element; @@ -955,7 +957,7 @@ function field_ui_field_edit_form_validate($form, &$form_state) { // Take the incoming values as the $instance definition, so that the 'default // value' gets validated using the instance settings being submitted. $instance = $form['#instance']; - $field_name = $instance['field_name']; + $field_name = $instance->field_name; $entity = $form['#entity']; if (isset($form['instance']['default_value_widget'])) { @@ -971,7 +973,7 @@ function field_ui_field_edit_form_validate($form, &$form_state) { // Validate the value. $errors = array(); - $function = $field['module'] . '_field_validate'; + $function = $field->module . '_field_validate'; if (function_exists($function)) { $function(NULL, $field, $instance, LANGUAGE_NOT_SPECIFIED, $items, $errors); } @@ -1006,18 +1008,18 @@ function field_ui_field_edit_form_submit($form, &$form_state) { $items = array(); $instance->getWidget()->extractFormValues($entity, LANGUAGE_NOT_SPECIFIED, $items, $element, $form_state); - $instance['default_value'] = $items ? $items : NULL; + $instance->default_value = $items ? $items : NULL; } // Merge incoming values into the instance. foreach ($form_state['values']['instance'] as $key => $value) { - $instance[$key] = $value; + $instance->$key = $value; } field_update_instance($instance); - drupal_set_message(t('Saved %label configuration.', array('%label' => $instance['label']))); + drupal_set_message(t('Saved %label configuration.', array('%label' => $instance->label))); - $form_state['redirect'] = field_ui_next_destination($instance['entity_type'], $instance['bundle']); + $form_state['redirect'] = field_ui_next_destination($instance->entity_type, $instance->bundle); } /** diff --git a/core/modules/field_ui/field_ui.api.php b/core/modules/field_ui/field_ui.api.php index eb72e04..35a5f51 100644 --- a/core/modules/field_ui/field_ui.api.php +++ b/core/modules/field_ui/field_ui.api.php @@ -36,7 +36,7 @@ * The form definition for the field settings. */ function hook_field_settings_form($field, $instance, $has_data) { - $settings = $field['settings']; + $settings = $field->settings; $form['max_length'] = array( '#type' => 'number', '#title' => t('Maximum length'), @@ -65,7 +65,7 @@ function hook_field_settings_form($field, $instance, $has_data) { * The form definition for the field instance settings. */ function hook_field_instance_settings_form($field, $instance, $form_state) { - $settings = $instance['settings']; + $settings = $instance->settings; $form['text_processing'] = array( '#type' => 'radios', @@ -76,7 +76,7 @@ function hook_field_instance_settings_form($field, $instance, $form_state) { t('Filtered text (user selects text format)'), ), ); - if ($field['type'] == 'text_with_summary') { + if ($field->type == 'text_with_summary') { $form['display_summary'] = array( '#type' => 'select', '#title' => t('Display summary'), diff --git a/core/modules/field_ui/field_ui.module b/core/modules/field_ui/field_ui.module index b9d1a1c..64c255d 100644 --- a/core/modules/field_ui/field_ui.module +++ b/core/modules/field_ui/field_ui.module @@ -270,7 +270,7 @@ function field_ui_instance_load($field_name, $entity_type, $bundle_name, $bundle * @see field_ui_menu() */ function field_ui_instance_title($instance) { - return $instance['label']; + return $instance->label; } /** @@ -358,8 +358,8 @@ function field_ui_inactive_instances($entity_type, $bundle_name = NULL) { // $active list collected earlier. $all_instances = field_read_instances($params, array('include_inactive' => TRUE)); foreach ($all_instances as $instance) { - if (!isset($active[$instance['bundle']][$instance['field_name']])) { - $inactive[$instance['bundle']][$instance['field_name']] = $instance; + if (!isset($active[$instance->bundle][$instance->field_name])) { + $inactive[$instance->bundle][$instance->field_name] = $instance; } } diff --git a/core/modules/field_ui/lib/Drupal/field_ui/DisplayOverview.php b/core/modules/field_ui/lib/Drupal/field_ui/DisplayOverview.php index 4314fb4..f9b4e02 100644 --- a/core/modules/field_ui/lib/Drupal/field_ui/DisplayOverview.php +++ b/core/modules/field_ui/lib/Drupal/field_ui/DisplayOverview.php @@ -110,14 +110,14 @@ public function form(array $form, array &$form_state) { '#region_callback' => 'field_ui_display_overview_row_region', '#js_settings' => array( 'rowHandler' => 'field', - 'defaultFormatter' => $field_types[$field['type']]['default_formatter'], + 'defaultFormatter' => $field_types[$field->type]['default_formatter'], ), 'human_name' => array( - '#markup' => check_plain($instance['label']), + '#markup' => check_plain($instance->label), ), 'weight' => array( '#type' => 'textfield', - '#title' => t('Weight for @title', array('@title' => $instance['label'])), + '#title' => t('Weight for @title', array('@title' => $instance->label)), '#title_display' => 'invisible', '#default_value' => $display_options ? $display_options['weight'] : '0', '#size' => 3, @@ -126,7 +126,7 @@ public function form(array $form, array &$form_state) { 'parent_wrapper' => array( 'parent' => array( '#type' => 'select', - '#title' => t('Label display for @title', array('@title' => $instance['label'])), + '#title' => t('Label display for @title', array('@title' => $instance->label)), '#title_display' => 'invisible', '#options' => $table['#parent_options'], '#empty_value' => '', @@ -141,19 +141,19 @@ public function form(array $form, array &$form_state) { ), 'label' => array( '#type' => 'select', - '#title' => t('Label display for @title', array('@title' => $instance['label'])), + '#title' => t('Label display for @title', array('@title' => $instance->label)), '#title_display' => 'invisible', '#options' => $field_label_options, '#default_value' => $display_options ? $display_options['label'] : 'above', ), ); - $formatter_options = field_ui_formatter_options($field['type']); + $formatter_options = field_ui_formatter_options($field->type); $formatter_options['hidden'] = '<' . t('Hidden') . '>'; $table[$name]['format'] = array( 'type' => array( '#type' => 'select', - '#title' => t('Formatter for @title', array('@title' => $instance['label'])), + '#title' => t('Formatter for @title', array('@title' => $instance->label)), '#title_display' => 'invisible', '#options' => $formatter_options, '#default_value' => $display_options ? $display_options['type'] : 'hidden', diff --git a/core/modules/field_ui/lib/Drupal/field_ui/FieldOverview.php b/core/modules/field_ui/lib/Drupal/field_ui/FieldOverview.php index a977df9..a9cf2fa 100644 --- a/core/modules/field_ui/lib/Drupal/field_ui/FieldOverview.php +++ b/core/modules/field_ui/lib/Drupal/field_ui/FieldOverview.php @@ -96,27 +96,27 @@ public function form(array $form, array &$form_state) { // Fields. foreach ($instances as $name => $instance) { - $field = field_info_field($instance['field_name']); - $admin_field_path = $this->adminPath . '/fields/' . $instance['field_name']; + $field = field_info_field($instance->field_name); + $admin_field_path = $this->adminPath . '/fields/' . $instance->field_name; $table[$name] = array( '#attributes' => array('class' => array('draggable', 'tabledrag-leaf')), '#row_type' => 'field', '#region_callback' => 'field_ui_field_overview_row_region', 'label' => array( - '#markup' => check_plain($instance['label']), + '#markup' => check_plain($instance->label), ), 'weight' => array( '#type' => 'textfield', - '#title' => t('Weight for @title', array('@title' => $instance['label'])), + '#title' => t('Weight for @title', array('@title' => $instance->label)), '#title_display' => 'invisible', - '#default_value' => $instance['widget']['weight'], + '#default_value' => $instance->widget_settings['weight'], '#size' => 3, '#attributes' => array('class' => array('field-weight')), ), 'parent_wrapper' => array( 'parent' => array( '#type' => 'select', - '#title' => t('Parent for @title', array('@title' => $instance['label'])), + '#title' => t('Parent for @title', array('@title' => $instance->label)), '#title_display' => 'invisible', '#options' => $table['#parent_options'], '#empty_value' => '', @@ -130,17 +130,17 @@ public function form(array $form, array &$form_state) { ), ), 'field_name' => array( - '#markup' => $instance['field_name'], + '#markup' => $instance->field_name, ), 'type' => array( '#type' => 'link', - '#title' => t($field_types[$field['type']]['label']), + '#title' => t($field_types[$field->type]['label']), '#href' => $admin_field_path . '/field-settings', '#options' => array('attributes' => array('title' => t('Edit field settings.'))), ), 'widget_type' => array( '#type' => 'link', - '#title' => t($widget_types[$instance['widget']['type']]['label']), + '#title' => t($widget_types[$instance->widget_settings['type']]['label']), '#href' => $admin_field_path . '/widget-type', '#options' => array('attributes' => array('title' => t('Change widget type.'))), ), @@ -162,7 +162,7 @@ public function form(array $form, array &$form_state) { '#links' => $links, ); - if (!empty($instance['locked'])) { + if (!empty($instance->locked)) { $table[$name]['operations'] = array('#value' => t('Locked')); $table[$name]['#attributes']['class'][] = 'menu-disabled'; } @@ -522,7 +522,7 @@ protected function validateAddExisting(array $form, array &$form_state) { } // Wrong widget type. elseif ($field['field_name'] && ($existing_field = field_info_field($field['field_name']))) { - $widget_types = field_ui_widget_type_options($existing_field['type']); + $widget_types = field_ui_widget_type_options($existing_field->type); if (!isset($widget_types[$field['widget_type']])) { form_set_error('fields][_add_existing_field][widget_type', t('Re-use existing field: invalid widget.')); } @@ -548,7 +548,7 @@ public function submit(array $form, array &$form_state) { foreach ($form_values as $key => $values) { if (in_array($key, $form['#fields'])) { $instance = field_read_instance($this->entity_type, $key, $this->bundle); - $instance['widget']['weight'] = $values['weight']; + $instance->widget_settings['weight'] = $values['weight']; field_update_instance($instance); } elseif (in_array($key, $form['#extra'])) { @@ -583,26 +583,26 @@ public function submit(array $form, array &$form_state) { // Create the field and instance. try { - field_create_field($field); + $field = field_create_field($field); field_create_instance($instance); // Make sure the field is displayed in the 'default' view mode (using // default formatter and settings). It stays hidden for other view // modes until it is explicitly configured. entity_get_display($this->entity_type, $this->bundle, 'default') - ->setComponent($field['field_name']) + ->setComponent($field->field_name) ->save(); // Always show the field settings step, as the cardinality needs to be // configured for new fields. - $destinations[] = $this->adminPath. '/fields/' . $field['field_name'] . '/field-settings'; - $destinations[] = $this->adminPath . '/fields/' . $field['field_name']; + $destinations[] = $this->adminPath. '/fields/' . $field->field_name . '/field-settings'; + $destinations[] = $this->adminPath . '/fields/' . $field->field_name; // Store new field information for any additional submit handlers. - $form_state['fields_added']['_add_new_field'] = $field['field_name']; + $form_state['fields_added']['_add_new_field'] = $field->field_name; } catch (Exception $e) { - drupal_set_message(t('There was a problem creating field %label: !message', array('%label' => $instance['label'], '!message' => $e->getMessage())), 'error'); + drupal_set_message(t('There was a problem creating field %label: !message', array('%label' => $instance->label, '!message' => $e->getMessage())), 'error'); } } @@ -610,12 +610,12 @@ public function submit(array $form, array &$form_state) { if (!empty($form_values['_add_existing_field']['field_name'])) { $values = $form_values['_add_existing_field']; $field = field_info_field($values['field_name']); - if (!empty($field['locked'])) { + if (!empty($field->locked)) { drupal_set_message(t('The field %label cannot be added because it is locked.', array('%label' => $values['label'])), 'error'); } else { $instance = array( - 'field_name' => $field['field_name'], + 'field_name' => $field->field_name, 'entity_type' => $this->entity_type, 'bundle' => $this->bundle, 'label' => $values['label'], @@ -632,15 +632,15 @@ public function submit(array $form, array &$form_state) { // default formatter and settings). It stays hidden for other view // modes until it is explicitly configured. entity_get_display($this->entity_type, $this->bundle, 'default') - ->setComponent($field['field_name']) + ->setComponent($field->field_name) ->save(); - $destinations[] = $this->adminPath . '/fields/' . $instance['field_name']; + $destinations[] = $this->adminPath . '/fields/' . $instance->field_name; // Store new field information for any additional submit handlers. - $form_state['fields_added']['_add_existing_field'] = $instance['field_name']; + $form_state['fields_added']['_add_existing_field'] = $instance->field_name; } catch (Exception $e) { - drupal_set_message(t('There was a problem creating field instance %label: @message.', array('%label' => $instance['label'], '@message' => $e->getMessage())), 'error'); + drupal_set_message(t('There was a problem creating field instance %label: @message.', array('%label' => $instance->label, '@message' => $e->getMessage())), 'error'); } } } diff --git a/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php b/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php index cbc132f..82b827b 100644 --- a/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php +++ b/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php @@ -229,12 +229,12 @@ function assertFieldSettings($bundle, $field_name, $string = 'dummy test string' field_info_cache_clear(); // Assert field settings. $field = field_info_field($field_name); - $this->assertTrue($field['settings']['test_field_setting'] == $string, 'Field settings were found.'); + $this->assertTrue($field->settings['test_field_setting'] == $string, 'Field settings were found.'); // Assert instance and widget settings. $instance = field_info_instance($entity_type, $field_name, $bundle); - $this->assertTrue($instance['settings']['test_instance_setting'] == $string, 'Field instance settings were found.'); - $this->assertTrue($instance['widget']['settings']['test_widget_setting'] == $string, 'Field widget settings were found.'); + $this->assertTrue($instance->settings['test_instance_setting'] == $string, 'Field instance settings were found.'); + $this->assertTrue($instance->widget_settings['settings']['test_widget_setting'] == $string, 'Field widget settings were found.'); } /** @@ -287,7 +287,7 @@ function testDefaultValue() { $this->assertEqual($instance['default_value'], NULL, 'The default value was correctly saved.'); // Change the widget to TestFieldWidgetNoDefault. - $instance['widget']['type'] = 'test_field_widget_no_default'; + $instance->widget_settings['type'] = 'test_field_widget_no_default'; field_update_instance($instance); $this->drupalGet($admin_path); @@ -366,7 +366,7 @@ function testHiddenFields() { // Check that the newly added instance appears on the 'Manage Fields' // screen. $this->drupalGet($bundle_path); - $this->assertFieldByXPath('//table[@id="field-overview"]//td[1]', $instance['label'], 'Field was created and appears in the overview page.'); + $this->assertFieldByXPath('//table[@id="field-overview"]//td[1]', $instance->label, 'Field was created and appears in the overview page.'); // Check that the instance does not appear in the 're-use existing field' row // on other bundles. @@ -417,7 +417,7 @@ function testWidgetChange() { // Check that the field_tags field currently uses the 'options_select' // widget. $instance = field_info_instance('node', 'field_tags', 'article'); - $this->assertEqual($instance['widget']['type'], 'options_select'); + $this->assertEqual($instance->widget_settings['type'], 'options_select'); // Check that the "Manage fields" page shows the correct widget type. $this->drupalGet($url_fields); @@ -442,7 +442,7 @@ function testWidgetChange() { // Check that the field uses the newly set widget. field_cache_clear(); $instance = field_info_instance('node', 'field_tags', 'article'); - $this->assertEqual($instance['widget']['type'], 'options_buttons'); + $this->assertEqual($instance->widget_settings['type'], 'options_buttons'); // Go to the 'Widget type' form and check that the correct widget is // selected. diff --git a/core/modules/file/file.field.inc b/core/modules/file/file.field.inc index e166b82..fe7a932 100644 --- a/core/modules/file/file.field.inc +++ b/core/modules/file/file.field.inc @@ -37,8 +37,8 @@ function file_field_info() { * Implements hook_field_settings_form(). */ function file_field_settings_form($field, $instance, $has_data) { - $defaults = field_info_field_settings($field['type']); - $settings = array_merge($defaults, $field['settings']); + $defaults = field_info_field_settings($field->type); + $settings = array_merge($defaults, $field->settings); $form['#attached']['library'][] = array('file', 'drupal.file'); @@ -80,7 +80,7 @@ function file_field_settings_form($field, $instance, $has_data) { * Implements hook_field_instance_settings_form(). */ function file_field_instance_settings_form($field, $instance) { - $settings = $instance['settings']; + $settings = $instance->settings; $form['file_directory'] = array( '#type' => 'textfield', @@ -250,8 +250,8 @@ function file_field_update(EntityInterface $entity, $field, $instance, $langcode // Compare the original field values with the ones that are being saved. $original = $entity->original; $original_fids = array(); - if (!empty($original->{$field['field_name']}[$langcode])) { - foreach ($original->{$field['field_name']}[$langcode] as $original_item) { + if (!empty($original->{$field->field_name}[$langcode])) { + foreach ($original->{$field->field_name}[$langcode] as $original_item) { $original_fids[] = $original_item['fid']; if (isset($original_item['fid']) && !in_array($original_item['fid'], $current_fids)) { // Decrement the file usage count by 1. @@ -307,7 +307,7 @@ function file_field_is_empty($item, $field) { * Boolean TRUE if the file will be displayed, FALSE if the file is hidden. */ function file_field_displayed($item, $field) { - if (!empty($field['settings']['display_field'])) { + if (!empty($field->settings['display_field'])) { return (bool) $item['display']; } return TRUE; @@ -326,8 +326,8 @@ function file_field_displayed($item, $field) { function file_field_widget_upload_validators($field, $instance) { // Cap the upload size according to the PHP limit. $max_filesize = parse_size(file_upload_max_size()); - if (!empty($instance['settings']['max_filesize']) && parse_size($instance['settings']['max_filesize']) < $max_filesize) { - $max_filesize = parse_size($instance['settings']['max_filesize']); + if (!empty($instance->settings['max_filesize']) && parse_size($instance->settings['max_filesize']) < $max_filesize) { + $max_filesize = parse_size($instance->settings['max_filesize']); } $validators = array(); @@ -336,8 +336,8 @@ function file_field_widget_upload_validators($field, $instance) { $validators['file_validate_size'] = array($max_filesize); // Add the extension check if necessary. - if (!empty($instance['settings']['file_extensions'])) { - $validators['file_validate_extensions'] = array($instance['settings']['file_extensions']); + if (!empty($instance->settings['file_extensions'])) { + $validators['file_validate_extensions'] = array($instance->settings['file_extensions']); } return $validators; @@ -359,12 +359,12 @@ function file_field_widget_upload_validators($field, $instance) { * @see token_replace() */ function file_field_widget_uri($field, $instance, $data = array()) { - $destination = trim($instance['settings']['file_directory'], '/'); + $destination = trim($instance->settings['file_directory'], '/'); // Replace tokens. $destination = token_replace($destination, $data); - return $field['settings']['uri_scheme'] . '://' . $destination; + return $field->settings['uri_scheme'] . '://' . $destination; } /** @@ -378,7 +378,7 @@ function file_field_widget_value($element, $input = FALSE, $form_state) { // If the display field is present make sure its unchecked value is saved. $field = field_widget_field($element, $form_state); if (empty($input['display'])) { - $input['display'] = $field['settings']['display_field'] ? 0 : 1; + $input['display'] = $field->settings['display_field'] ? 0 : 1; } } @@ -408,16 +408,16 @@ function file_field_widget_process($element, &$form_state, $form) { $field = field_widget_field($element, $form_state); $instance = field_widget_instance($element, $form_state); - $settings = $instance['widget']['settings']; + $settings = $instance->widget_settings['settings']; $element['#theme'] = 'file_widget'; // Add the display field if enabled. - if (!empty($field['settings']['display_field']) && $item['fid']) { + if (!empty($field->settings['display_field']) && $item['fid']) { $element['display'] = array( '#type' => empty($item['fid']) ? 'hidden' : 'checkbox', '#title' => t('Include file in display'), - '#value' => isset($item['display']) ? $item['display'] : $field['settings']['display_default'], + '#value' => isset($item['display']) ? $item['display'] : $field->settings['display_default'], '#attributes' => array('class' => array('file-display')), ); } @@ -429,7 +429,7 @@ function file_field_widget_process($element, &$form_state, $form) { } // Add the description field if enabled. - if (!empty($instance['settings']['description_field']) && $item['fid']) { + if (!empty($instance->settings['description_field']) && $item['fid']) { $config = config('file.settings'); $element['description'] = array( '#type' => $config->get('description.type'), @@ -442,7 +442,7 @@ function file_field_widget_process($element, &$form_state, $form) { // Adjust the Ajax settings so that on upload and remove of any individual // file, the entire group of file fields is updated together. - if ($field['cardinality'] != 1) { + if ($field->cardinality != 1) { $parents = array_slice($element['#array_parents'], 0, -1); $new_path = 'file/ajax/' . implode('/', $parents) . '/' . $form['form_build_id']['#value']; $field_element = NestedArray::getValue($form, $parents); @@ -767,7 +767,7 @@ function theme_file_upload_help($variables) { * fid, FALSE if it doesn't. */ function file_field_find_file_reference_column($field) { - foreach ($field['foreign keys'] as $data) { + foreach ($field->foreign_keys as $data) { if ($data['table'] == 'file_managed') { foreach ($data['columns'] as $field_column => $column) { if ($column == 'fid') { diff --git a/core/modules/file/file.install b/core/modules/file/file.install index 27a2438..472f7e5 100644 --- a/core/modules/file/file.install +++ b/core/modules/file/file.install @@ -125,8 +125,8 @@ function file_schema() { ), 'id' => array( 'description' => 'The primary key of the object using the file.', - 'type' => 'int', - 'unsigned' => TRUE, + 'type' => 'varchar', + 'length' => 64, 'not null' => TRUE, 'default' => 0, ), @@ -234,6 +234,21 @@ function file_requirements($phase) { } /** + * Implements hook_update_dependencies(). + */ +function file_update_dependencies() { + // The update hook to convert the id column in the column table needs to be + // the first one because user update hooks will be called during system + // update hooks due to other dependencies and the user picture conversion + // might need to save a default image and the field it's stored in, does + // not have an integer id anymore. + $dependencies['system'][8001] = array( + 'file' => 8001, + ); + return $dependencies; +} + +/** * Converts default_file_main variable to config. * * @ingroup config_upgrade @@ -245,3 +260,17 @@ function file_update_8000() { 'file_icon_directory'=>'icon.directory', )); } + +/** + * Convert the id column in file_usage to store UUID's. + */ +function file_update_8001() { + $spec = array( + 'description' => 'The primary key of the object using the file.', + 'type' => 'varchar', + 'length' => 64, + 'not null' => TRUE, + 'default' => '', + ); + db_change_field('file_usage', 'id', 'id', $spec); +} diff --git a/core/modules/file/file.module b/core/modules/file/file.module index 913f75a..593ff00 100644 --- a/core/modules/file/file.module +++ b/core/modules/file/file.module @@ -1539,12 +1539,12 @@ function file_get_file_references(File $file, $field = NULL, $age = FIELD_LOAD_R $current_field = field_info_field($field_name); // If this is the first time this field type is seen, check // whether it references files. - if (!isset($field_columns[$current_field['type']])) { - $field_columns[$current_field['type']] = file_field_find_file_reference_column($current_field); + if (!isset($field_columns[$current_field->type])) { + $field_columns[$current_field->type] = file_field_find_file_reference_column($current_field); } // If the field type does reference files then record it. - if ($field_columns[$current_field['type']]) { - $file_fields[$entity_type][$bundle][$field_name] = $field_columns[$current_field['type']]; + if ($field_columns[$current_field->type]) { + $file_fields[$entity_type][$bundle][$field_name] = $field_columns[$current_field->type]; } } } @@ -1575,7 +1575,7 @@ function file_get_file_references(File $file, $field = NULL, $age = FIELD_LOAD_R if ($field || $field_type) { foreach ($return as $field_name => $data) { $current_field = field_info_field($field_name); - if (($field_type && $current_field['type'] != $field_type) || ($field && $field['id'] != $current_field['id'])) { + if (($field_type && $current_field->type != $field_type) || ($field && $field->id != $current_field['id'])) { unset($return[$field_name]); } } diff --git a/core/modules/file/file.views.inc b/core/modules/file/file.views.inc index 6b0fb59..a40b68b 100644 --- a/core/modules/file/file.views.inc +++ b/core/modules/file/file.views.inc @@ -455,12 +455,12 @@ function file_field_views_data($field) { $data = field_views_field_default_views_data($field); foreach ($data as $table_name => $table_data) { // Add the relationship only on the fid field. - $data[$table_name][$field['field_name'] . '_fid']['relationship'] = array( + $data[$table_name][$field->field_name . '_fid']['relationship'] = array( 'id' => 'standard', 'base' => 'file_managed', 'entity type' => 'file', 'base field' => 'fid', - 'label' => t('file from !field_name', array('!field_name' => $field['field_name'])), + 'label' => t('file from !field_name', array('!field_name' => $field->field_name)), ); } @@ -473,11 +473,11 @@ function file_field_views_data($field) { * Views integration to provide reverse relationships on file fields. */ function file_field_views_data_views_data_alter(&$data, $field) { - foreach ($field['bundles'] as $entity_type => $bundles) { + foreach ($field->bundles as $entity_type => $bundles) { $entity_info = entity_get_info($entity_type); - $pseudo_field_name = 'reverse_' . $field['field_name'] . '_' . $entity_type; + $pseudo_field_name = 'reverse_' . $field->field_name . '_' . $entity_type; - list($label, $all_labels) = field_views_field_label($field['field_name']); + list($label, $all_labels) = field_views_field_label($field->field_name); $entity = $entity_info['label']; if ($entity == t('Node')) { $entity = t('Content'); @@ -487,12 +487,12 @@ function file_field_views_data_views_data_alter(&$data, $field) { 'title' => t('@entity using @field', array('@entity' => $entity, '@field' => $label)), 'help' => t('Relate each @entity with a @field set to the file.', array('@entity' => $entity, '@field' => $label)), 'id' => 'entity_reverse', - 'field_name' => $field['field_name'], + 'field_name' => $field->field_name, 'field table' => _field_sql_storage_tablename($field), - 'field field' => $field['field_name'] . '_fid', + 'field field' => $field->field_name . '_fid', 'base' => $entity_info['base_table'], 'base field' => $entity_info['entity_keys']['id'], - 'label' => t('!field_name', array('!field_name' => $field['field_name'])), + 'label' => t('!field_name', array('!field_name' => $field->field_name)), 'join_extra' => array( 0 => array( 'field' => 'entity_type', diff --git a/core/modules/file/lib/Drupal/file/Plugin/field/widget/FileWidget.php b/core/modules/file/lib/Drupal/file/Plugin/field/widget/FileWidget.php index 98a7a96..95216b8 100644 --- a/core/modules/file/lib/Drupal/file/Plugin/field/widget/FileWidget.php +++ b/core/modules/file/lib/Drupal/file/Plugin/field/widget/FileWidget.php @@ -57,7 +57,7 @@ public function settingsForm(array $form, array &$form_state) { protected function formMultipleElements(EntityInterface $entity, array $items, $langcode, array &$form, array &$form_state) { $field = $this->field; $instance = $this->instance; - $field_name = $field['field_name']; + $field_name = $field->field_name; $parents = $form['#parents']; @@ -70,23 +70,23 @@ protected function formMultipleElements(EntityInterface $entity, array $items, $ } // Determine the number of widgets to display. - switch ($field['cardinality']) { + switch ($field->cardinality) { case FIELD_CARDINALITY_UNLIMITED: $max = count($items); $is_multiple = TRUE; break; default: - $max = $field['cardinality'] - 1; - $is_multiple = ($field['cardinality'] > 1); + $max = $field->cardinality - 1; + $is_multiple = ($field->cardinality > 1); break; } $id_prefix = implode('-', array_merge($parents, array($field_name))); $wrapper_id = drupal_html_id($id_prefix . '-add-more-wrapper'); - $title = check_plain($instance['label']); - $description = field_filter_xss($instance['description']); + $title = check_plain($instance->label); + $description = field_filter_xss($instance->description); $elements = array(); @@ -120,8 +120,8 @@ protected function formMultipleElements(EntityInterface $entity, array $items, $ } } - $empty_single_allowed = ($this->field['cardinality'] == 1 && $delta == 0); - $empty_multiple_allowed = ($this->field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta < $this->field['cardinality']) && empty($form_state['programmed']); + $empty_single_allowed = ($this->field->cardinality == 1 && $delta == 0); + $empty_multiple_allowed = ($this->field->cardinality == FIELD_CARDINALITY_UNLIMITED || $delta < $this->field->cardinality) && empty($form_state['programmed']); // Add one more empty row for new uploads except when this is a programmed // multiple form as it is not necessary. @@ -149,7 +149,7 @@ protected function formMultipleElements(EntityInterface $entity, array $items, $ $elements['#description'] = $description; $elements['#field_name'] = $element['#field_name']; $elements['#language'] = $element['#language']; - $elements['#display_field'] = !empty($this->field['settings']['display_field']); + $elements['#display_field'] = !empty($this->field->settings['display_field']); // Add some properties that will eventually be added to the file upload // field. These are added here so that they may be referenced easily @@ -167,7 +167,7 @@ protected function formMultipleElements(EntityInterface $entity, array $items, $ public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) { $defaults = array( 'fid' => 0, - 'display' => !empty($this->field['settings']['display_default']), + 'display' => !empty($this->field->settings['display_default']), 'description' => '', ); diff --git a/core/modules/file/lib/Drupal/file/Tests/FileFieldTestBase.php b/core/modules/file/lib/Drupal/file/Tests/FileFieldTestBase.php index 52df6f2..d2e1310 100644 --- a/core/modules/file/lib/Drupal/file/Tests/FileFieldTestBase.php +++ b/core/modules/file/lib/Drupal/file/Tests/FileFieldTestBase.php @@ -106,8 +106,8 @@ function attachFileField($name, $entity_type, $bundle, $instance_settings = arra 'settings' => array(), ), ); - $instance['settings'] = array_merge($instance['settings'], $instance_settings); - $instance['widget']['settings'] = array_merge($instance['widget']['settings'], $widget_settings); + $instance->settings = array_merge($instance->settings, $instance_settings); + $instance->widget_settings['settings'] = array_merge($instance->widget_settings['settings'], $widget_settings); field_create_instance($instance); } @@ -116,8 +116,8 @@ function attachFileField($name, $entity_type, $bundle, $instance_settings = arra */ function updateFileField($name, $type_name, $instance_settings = array(), $widget_settings = array()) { $instance = field_info_instance('node', $name, $type_name); - $instance['settings'] = array_merge($instance['settings'], $instance_settings); - $instance['widget']['settings'] = array_merge($instance['widget']['settings'], $widget_settings); + $instance->settings = array_merge($instance->settings, $instance_settings); + $instance->widget_settings['settings'] = array_merge($instance->widget_settings['settings'], $widget_settings); field_update_instance($instance); } diff --git a/core/modules/file/lib/Drupal/file/Tests/FileFieldValidateTest.php b/core/modules/file/lib/Drupal/file/Tests/FileFieldValidateTest.php index 7a40a1c..986e2de 100644 --- a/core/modules/file/lib/Drupal/file/Tests/FileFieldValidateTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/FileFieldValidateTest.php @@ -38,7 +38,7 @@ function testRequired() { $langcode = LANGUAGE_NOT_SPECIFIED; $edit = array("title" => $this->randomName()); $this->drupalPost('node/add/' . $type_name, $edit, t('Save and publish')); - $this->assertRaw(t('!title field is required.', array('!title' => $instance['label'])), t('Node save failed when required file field was empty.')); + $this->assertRaw(t('!title field is required.', array('!title' => $instance->label)), t('Node save failed when required file field was empty.')); // Create a new node with the uploaded file. $nid = $this->uploadNodeFile($test_file, $field_name, $type_name); @@ -57,7 +57,7 @@ function testRequired() { // Try to post a new node without uploading a file in the multivalue field. $edit = array('title' => $this->randomName()); $this->drupalPost('node/add/' . $type_name, $edit, t('Save and publish')); - $this->assertRaw(t('!title field is required.', array('!title' => $instance['label'])), t('Node save failed when required multiple value file field was empty.')); + $this->assertRaw(t('!title field is required.', array('!title' => $instance->label)), t('Node save failed when required multiple value file field was empty.')); // Create a new node with the uploaded file into the multivalue field. $nid = $this->uploadNodeFile($test_file, $field_name, $type_name); diff --git a/core/modules/file/tests/file_module_test.module b/core/modules/file/tests/file_module_test.module index b962e2a..56ce681 100644 --- a/core/modules/file/tests/file_module_test.module +++ b/core/modules/file/tests/file_module_test.module @@ -76,7 +76,7 @@ function file_module_test_form_submit($form, &$form_state) { * Implements hook_file_download_access(). */ function file_module_test_file_download_access($field, EntityInterface $entity, File $file) { - $instance = field_info_instance($entity->entityType(), $field['field_name'], $entity->bundle()); + $instance = field_info_instance($entity->entityType(), $field->field_name, $entity->bundle()); // Allow the file to be downloaded only if the given arguments are correct. // If any are wrong, $instance will be NULL. if (empty($instance)) { diff --git a/core/modules/forum/forum.install b/core/modules/forum/forum.install index b1e63de..3173cfb 100644 --- a/core/modules/forum/forum.install +++ b/core/modules/forum/forum.install @@ -22,10 +22,6 @@ function forum_install() { * Implements hook_enable(). */ function forum_enable() { - // If we enable forum at the same time as taxonomy we need to call - // field_associate_fields() as otherwise the field won't be enabled until - // hook modules_enabled is called which takes place after hook_enable events. - field_associate_fields('taxonomy'); // Create the forum vocabulary if it does not exist. // @todo Change Forum module so forum.settings can contain the vocabulary's @@ -55,7 +51,7 @@ function forum_enable() { } // Create the 'taxonomy_forums' field if it doesn't already exist. - if (!field_info_field('taxonomy_forums')) { + if (!field_read_field('taxonomy_forums', array('include_inactive' => TRUE))) { $field = array( 'field_name' => 'taxonomy_forums', 'type' => 'taxonomy_term_reference', diff --git a/core/modules/forum/forum.module b/core/modules/forum/forum.module index 872eb95..94ab671 100644 --- a/core/modules/forum/forum.module +++ b/core/modules/forum/forum.module @@ -179,7 +179,7 @@ function forum_menu_local_tasks_alter(&$data, $router_item, $root_path) { $links = array(); // Loop through all bundles for forum taxonomy vocabulary field. $field = field_info_field('taxonomy_forums'); - foreach ($field['bundles']['node'] as $type) { + foreach ($field->bundles['node'] as $type) { if (node_access('create', $type)) { $links[$type] = array( '#theme' => 'menu_local_action', diff --git a/core/modules/image/image.field.inc b/core/modules/image/image.field.inc index e83d542..93108a8 100644 --- a/core/modules/image/image.field.inc +++ b/core/modules/image/image.field.inc @@ -42,8 +42,8 @@ function image_field_info() { * Implements hook_field_settings_form(). */ function image_field_settings_form($field, $instance) { - $defaults = field_info_field_settings($field['type']); - $settings = array_merge($defaults, $field['settings']); + $defaults = field_info_field_settings($field->type); + $settings = array_merge($defaults, $field->settings); $scheme_options = array(); foreach (file_get_stream_wrappers(STREAM_WRAPPERS_WRITE_VISIBLE) as $scheme => $stream_wrapper) { @@ -64,7 +64,7 @@ function image_field_settings_form($field, $instance) { '#title' => t('Default image'), '#type' => 'managed_file', '#description' => t('If no image is uploaded, this image will be shown on display.'), - '#default_value' => $field['settings']['default_image'], + '#default_value' => $field->settings['default_image'], '#upload_location' => $settings['uri_scheme'] . '://default_images/', ); @@ -75,7 +75,7 @@ function image_field_settings_form($field, $instance) { * Implements hook_field_instance_settings_form(). */ function image_field_instance_settings_form($field, $instance) { - $settings = $instance['settings']; + $settings = $instance->settings; // Use the file field instance settings form as a basis. $form = file_field_instance_settings_form($field, $instance); @@ -183,7 +183,7 @@ function image_field_instance_settings_form($field, $instance) { '#type' => 'managed_file', '#description' => t("If no image is uploaded, this image will be shown on display and will override the field's default image."), '#default_value' => $settings['default_image'], - '#upload_location' => $field['settings']['uri_scheme'] . '://default_images/', + '#upload_location' => $field->settings['uri_scheme'] . '://default_images/', ); return $form; @@ -218,12 +218,12 @@ function image_field_prepare_view($entity_type, $entities, $field, $instances, $ if (empty($items[$id])) { $fid = 0; // Use the default for the instance if one is available. - if (!empty($instances[$id]['settings']['default_image'])) { - $fid = $instances[$id]['settings']['default_image']; + if (!empty($instances[$id]->settings['default_image'])) { + $fid = $instances[$id]->settings['default_image']; } // Otherwise, use the default for the field. - elseif (!empty($field['settings']['default_image'])) { - $fid = $field['settings']['default_image']; + elseif (!empty($field->settings['default_image'])) { + $fid = $field->settings['default_image']; } // Add the default image if one is found. @@ -346,7 +346,7 @@ function image_field_widget_process($element, &$form_state, $form) { // Get field settings. $instance = field_widget_instance($element, $form_state); - $settings = $instance['settings']; + $settings = $instance->settings; // Add the additional alt and title fields. $element['alt'] = array( diff --git a/core/modules/image/image.module b/core/modules/image/image.module index 956eafd..cb7e9d2 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -352,24 +352,24 @@ function image_image_style_update(ImageStyle $style) { $instances = field_read_instances(); // Loop through all fields searching for image fields. foreach ($instances as $instance) { - if ($instance['widget']['module'] == 'image') { - $view_modes = entity_get_view_modes($instance['entity_type']); + if ($instance->widget_settings['module'] == 'image') { + $view_modes = entity_get_view_modes($instance->entity_type); $view_modes = array('default') + array_keys($view_modes); foreach ($view_modes as $view_mode) { - $display = entity_get_display($instance['entity_type'], $instance['bundle'], $view_mode); - $display_options = $display->getComponent($instance['field_name']); + $display = entity_get_display($instance->entity_type, $instance->bundle, $view_mode); + $display_options = $display->getComponent($instance->field_name); // Check if the formatter involves an image style. if ($display_options && $display_options['type'] == 'image' && $display_options['settings']['image_style'] == $style->getOriginalID()) { // Update display information for any instance using the image // style that was just deleted. $display_options['settings']['image_style'] = $style->id(); - $display->setComponent($instance['field_name'], $display_options) + $display->setComponent($instance->field_name, $display_options) ->save(); } } - if ($instance['widget']['settings']['preview_image_style'] == $style->getOriginalID()) { - $instance['widget']['settings']['preview_image_style'] = $style->id(); + if ($instance->widget_settings['settings']['preview_image_style'] == $style->getOriginalID()) { + $instance->widget_settings['settings']['preview_image_style'] = $style->id(); field_update_instance($instance); } } @@ -397,12 +397,12 @@ function image_image_style_delete(ImageStyle $style) { * Implements hook_field_delete_field(). */ function image_field_delete_field($field) { - if ($field['type'] != 'image') { + if ($field->type != 'image') { return; } // The value of a managed_file element can be an array if #extended == TRUE. - $fid = (is_array($field['settings']['default_image']) ? $field['settings']['default_image']['fid'] : $field['settings']['default_image']); + $fid = (is_array($field->settings['default_image']) ? $field->settings['default_image']['fid'] : $field->settings['default_image']); if ($fid && ($file = file_load($fid))) { file_usage()->delete($file, 'image', 'default_image', $field['id']); } @@ -412,13 +412,13 @@ function image_field_delete_field($field) { * Implements hook_field_update_field(). */ function image_field_update_field($field, $prior_field, $has_data) { - if ($field['type'] != 'image') { + if ($field->type != 'image') { return; } // The value of a managed_file element can be an array if #extended == TRUE. - $fid_new = (is_array($field['settings']['default_image']) ? $field['settings']['default_image']['fid'] : $field['settings']['default_image']); - $fid_old = (is_array($prior_field['settings']['default_image']) ? $prior_field['settings']['default_image']['fid'] : $prior_field['settings']['default_image']); + $fid_new = (is_array($field->settings['default_image']) ? $field->settings['default_image']['fid'] : $field->settings['default_image']); + $fid_old = (is_array($prior_field->settings['default_image']) ? $prior_field->settings['default_image']['fid'] : $prior_field->settings['default_image']); $file_new = $fid_new ? file_load($fid_new) : FALSE; @@ -428,18 +428,18 @@ function image_field_update_field($field, $prior_field, $has_data) { if ($file_new) { $file_new->status = FILE_STATUS_PERMANENT; $file_new->save(); - file_usage()->add($file_new, 'image', 'default_image', $field['id']); + file_usage()->add($file_new, 'image', 'default_image', $field->id()); } // Is there an old file? if ($fid_old && ($file_old = file_load($fid_old))) { - file_usage()->delete($file_old, 'image', 'default_image', $field['id']); + file_usage()->delete($file_old, 'image', 'default_image', $field->id()); } } // If the upload destination changed, then move the file. - if ($file_new && (file_uri_scheme($file_new->uri) != $field['settings']['uri_scheme'])) { - $directory = $field['settings']['uri_scheme'] . '://default_images/'; + if ($file_new && (file_uri_scheme($file_new->uri) != $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); } @@ -450,14 +450,14 @@ function image_field_update_field($field, $prior_field, $has_data) { */ function image_field_delete_instance($instance) { // Only act on image fields. - $field = field_read_field($instance['field_name']); - if ($field['type'] != 'image') { + $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']; + $fid = $instance->settings['default_image']; if (is_array($fid)) { $fid = $fid['fid']; } @@ -473,18 +473,18 @@ function image_field_delete_instance($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') { + $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']; + $fid_new = $instance->settings['default_image']; if (is_array($fid_new)) { $fid_new = $fid_new['fid']; } - $fid_old = $prior_instance['settings']['default_image']; + $fid_old = $prior_instance->settings['default_image']; if (is_array($fid_old)) { $fid_old = $fid_old['fid']; } @@ -505,8 +505,8 @@ function image_field_update_instance($instance, $prior_instance) { } // If the upload destination changed, then move the file. - if ($file_new && (file_uri_scheme($file_new->uri) != $field['settings']['uri_scheme'])) { - $directory = $field['settings']['uri_scheme'] . '://default_images/'; + if ($file_new && (file_uri_scheme($file_new->uri) != $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); } diff --git a/core/modules/image/image.views.inc b/core/modules/image/image.views.inc index 77c4ea3..2402a9c 100644 --- a/core/modules/image/image.views.inc +++ b/core/modules/image/image.views.inc @@ -19,11 +19,11 @@ function image_field_views_data($field) { $data = field_views_field_default_views_data($field); foreach ($data as $table_name => $table_data) { // Add the relationship only on the fid field. - $data[$table_name][$field['field_name'] . '_fid']['relationship'] = array( + $data[$table_name][$field->field_name . '_fid']['relationship'] = array( 'id' => 'standard', 'base' => 'file_managed', 'base field' => 'fid', - 'label' => t('image from !field_name', array('!field_name' => $field['field_name'])), + 'label' => t('image from !field_name', array('!field_name' => $field->field_name)), ); } @@ -36,11 +36,11 @@ function image_field_views_data($field) { * Views integration to provide reverse relationships on image fields. */ function image_field_views_data_views_data_alter(&$data, $field) { - foreach ($field['bundles'] as $entity_type => $bundles) { + foreach ($field->bundles as $entity_type => $bundles) { $entity_info = entity_get_info($entity_type); - $pseudo_field_name = 'reverse_' . $field['field_name'] . '_' . $entity_type; + $pseudo_field_name = 'reverse_' . $field->field_name . '_' . $entity_type; - list($label, $all_labels) = field_views_field_label($field['field_name']); + list($label, $all_labels) = field_views_field_label($field->field_name); $entity = $entity_info['label']; if ($entity == t('Node')) { $entity = t('Content'); @@ -50,12 +50,12 @@ function image_field_views_data_views_data_alter(&$data, $field) { 'title' => t('@entity using @field', array('@entity' => $entity, '@field' => $label)), 'help' => t('Relate each @entity with a @field set to the image.', array('@entity' => $entity, '@field' => $label)), 'id' => 'entity_reverse', - 'field_name' => $field['field_name'], + 'field_name' => $field->field_name, 'field table' => _field_sql_storage_tablename($field), - 'field field' => $field['field_name'] . '_fid', + 'field field' => $field->field_name . '_fid', 'base' => $entity_info['base_table'], 'base field' => $entity_info['entity_keys']['id'], - 'label' => t('!field_name', array('!field_name' => $field['field_name'])), + 'label' => t('!field_name', array('!field_name' => $field->field_name)), 'join_extra' => array( 0 => array( 'field' => 'entity_type', diff --git a/core/modules/image/lib/Drupal/image/Plugin/field/widget/ImageWidget.php b/core/modules/image/lib/Drupal/image/Plugin/field/widget/ImageWidget.php index 35cb3fa..3b0cedc 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/field/widget/ImageWidget.php +++ b/core/modules/image/lib/Drupal/image/Plugin/field/widget/ImageWidget.php @@ -59,10 +59,10 @@ public function settingsForm(array $form, array &$form_state) { protected function formMultipleElements(EntityInterface $entity, array $items, $langcode, array &$form, array &$form_state) { $elements = parent::formMultipleElements($entity, $items, $langcode, $form, $form_state); - if ($this->field['cardinality'] == 1) { + if ($this->field->cardinality == 1) { // If there's only one field, return it as delta 0. if (empty($elements[0]['#default_value']['fid'])) { - $elements[0]['#description'] = theme('file_upload_help', array('description' => $this->instance['description'], 'upload_validators' => $elements[0]['#upload_validators'])); + $elements[0]['#description'] = theme('file_upload_help', array('description' => $this->instance->description, 'upload_validators' => $elements[0]['#upload_validators'])); } } else { @@ -78,7 +78,7 @@ protected function formMultipleElements(EntityInterface $entity, array $items, $ public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) { $element = parent::formElement($items, $delta, $element, $langcode, $form, $form_state); - $settings = $this->instance['settings']; + $settings = $this->instance->settings; // Add upload resolution validation. if ($settings['max_resolution'] || $settings['min_resolution']) { diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageFieldDefaultImagesTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageFieldDefaultImagesTest.php index 01fa5f4..e42acac 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageFieldDefaultImagesTest.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageFieldDefaultImagesTest.php @@ -57,20 +57,20 @@ function testDefaultImages() { // Add another instance with another default image to the page content type. $instance2 = array( - 'field_name' => $field['field_name'], + 'field_name' => $field->field_name, 'entity_type' => 'node', 'bundle' => 'page', - 'label' => $instance['label'], - 'required' => $instance['required'], + 'label' => $instance->label, + 'required' => $instance->required, 'settings' => array( 'default_image' => $default_images['instance2']->fid, ), - 'widget' => $instance['widget'], + 'widget' => $instance->widget_settings, ); field_create_instance($instance2); $instance2 = field_info_instance('node', $field_name, 'page'); entity_get_display('node', 'page', 'default') - ->setComponent($field['field_name']) + ->setComponent($field->field_name) ->save(); // Confirm the defaults are present on the article field settings form. @@ -140,7 +140,7 @@ function testDefaultImages() { ); // Upload a new default for the field. - $field['settings']['default_image'] = $default_images['field_new']->fid; + $field->settings['default_image'] = $default_images['field_new']->fid; field_update_field($field); // Confirm that the new default is used on the article field settings form. @@ -175,7 +175,7 @@ function testDefaultImages() { ); // Upload a new default for the article's field instance. - $instance['settings']['default_image'] = $default_images['instance_new']->fid; + $instance->settings['default_image'] = $default_images['instance_new']->fid; field_update_instance($instance); // Confirm the new field instance default is used on the article field @@ -214,7 +214,7 @@ function testDefaultImages() { ); // Remove the instance default from articles. - $instance['settings']['default_image'] = NULL; + $instance->settings['default_image'] = NULL; field_update_instance($instance); // Confirm the article field instance default has been removed. diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageFieldDisplayTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageFieldDisplayTest.php index 7c42a5b..1df0930 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageFieldDisplayTest.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageFieldDisplayTest.php @@ -146,8 +146,8 @@ function testImageFieldSettings() { $widget_settings = array( 'preview_image_style' => 'medium', ); - $field = $this->createImageField($field_name, 'article', array(), $instance_settings, $widget_settings); - $field['deleted'] = 0; + $this->createImageField($field_name, 'article', array(), $instance_settings, $widget_settings); + $field = field_info_field($field_name); $table = _field_sql_storage_tablename($field); $schema = drupal_get_schema($table, TRUE); $instance = field_info_instance('node', $field_name, 'article'); @@ -233,7 +233,7 @@ function testImageFieldDefaultImage() { // Clear field info cache so the new default image is detected. field_info_cache_clear(); $field = field_info_field($field_name); - $image = file_load($field['settings']['default_image']); + $image = file_load($field->settings['default_image']); $this->assertTrue($image->status == FILE_STATUS_PERMANENT, 'The default image status is permanent.'); $default_output = theme('image', array('uri' => $image->uri)); $this->drupalGet('node/' . $node->nid); @@ -261,7 +261,7 @@ function testImageFieldDefaultImage() { // Clear field info cache so the new default image is detected. field_info_cache_clear(); $field = field_info_field($field_name); - $this->assertFalse($field['settings']['default_image'], 'Default image removed from field.'); + $this->assertFalse($field->settings['default_image'], 'Default image removed from field.'); // Create an image field that uses the private:// scheme and test that the // default image works as expected. $private_field_name = strtolower($this->randomName()); @@ -275,7 +275,7 @@ function testImageFieldDefaultImage() { field_info_cache_clear(); $private_field = field_info_field($private_field_name); - $image = file_load($private_field['settings']['default_image']); + $image = file_load($private_field->settings['default_image']); $this->assertEqual('private', file_uri_scheme($image->uri), 'Default image uses private:// scheme.'); $this->assertTrue($image->status == FILE_STATUS_PERMANENT, 'The default image status is permanent.'); // Create a new node with no image attached and ensure that default private diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageFieldTestBase.php b/core/modules/image/lib/Drupal/image/Tests/ImageFieldTestBase.php index 5a4e20e..eb9f0c8 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageFieldTestBase.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageFieldTestBase.php @@ -90,8 +90,8 @@ function createImageField($name, $type_name, $field_settings = array(), $instanc 'settings' => array(), ), ); - $instance['settings'] = array_merge($instance['settings'], $instance_settings); - $instance['widget']['settings'] = array_merge($instance['widget']['settings'], $widget_settings); + $instance->settings = array_merge($instance->settings, $instance_settings); + $instance->widget_settings['settings'] = array_merge($instance->widget_settings['settings'], $widget_settings); $field_instance = field_create_instance($instance); diff --git a/core/modules/link/lib/Drupal/link/Tests/LinkFieldTest.php b/core/modules/link/lib/Drupal/link/Tests/LinkFieldTest.php index a608873..786bd20 100644 --- a/core/modules/link/lib/Drupal/link/Tests/LinkFieldTest.php +++ b/core/modules/link/lib/Drupal/link/Tests/LinkFieldTest.php @@ -48,9 +48,9 @@ function testURLValidation() { 'field_name' => drupal_strtolower($this->randomName()), 'type' => 'link', ); - field_create_field($this->field); + $this->field = field_create_field($this->field); $this->instance = array( - 'field_name' => $this->field['field_name'], + 'field_name' => $this->field->field_name, 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', 'settings' => array( @@ -65,7 +65,7 @@ function testURLValidation() { ); field_create_instance($this->instance); entity_get_display('test_entity', 'test_bundle', 'full') - ->setComponent($this->field['field_name'], array( + ->setComponent($this->field->field_name, array( 'type' => 'link', )) ->save(); @@ -74,13 +74,13 @@ function testURLValidation() { // Display creation form. $this->drupalGet('test-entity/add/test_bundle'); - $this->assertFieldByName("{$this->field['field_name']}[$langcode][0][url]", '', 'Link URL field is displayed'); + $this->assertFieldByName("{$this->field->field_name}[$langcode][0][url]", '', 'Link URL field is displayed'); $this->assertRaw('placeholder="http://example.com"'); // Verify that a valid URL can be submitted. $value = 'http://www.example.com/'; $edit = array( - "{$this->field['field_name']}[$langcode][0][url]" => $value, + "{$this->field->field_name}[$langcode][0][url]" => $value, ); $this->drupalPost(NULL, $edit, t('Save')); preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match); @@ -100,7 +100,7 @@ function testURLValidation() { $this->drupalGet('test-entity/add/test_bundle'); foreach ($wrong_entries as $invalid_value) { $edit = array( - "{$this->field['field_name']}[$langcode][0][url]" => $invalid_value, + "{$this->field->field_name}[$langcode][0][url]" => $invalid_value, ); $this->drupalPost(NULL, $edit, t('Save')); $this->assertText(t('The URL @url is not valid.', array('@url' => $invalid_value))); @@ -116,9 +116,9 @@ function testLinkTitle() { 'field_name' => drupal_strtolower($this->randomName()), 'type' => 'link', ); - field_create_field($this->field); + $this->field = field_create_field($this->field); $this->instance = array( - 'field_name' => $this->field['field_name'], + 'field_name' => $this->field->field_name, 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', 'settings' => array( @@ -134,7 +134,7 @@ function testLinkTitle() { ); field_create_instance($this->instance); entity_get_display('test_entity', 'test_bundle', 'full') - ->setComponent($this->field['field_name'], array( + ->setComponent($this->field->field_name, array( 'type' => 'link', 'label' => 'hidden', )) @@ -145,33 +145,33 @@ function testLinkTitle() { // Verify that the title field works according to the field setting. foreach (array(DRUPAL_DISABLED, DRUPAL_REQUIRED, DRUPAL_OPTIONAL) as $title_setting) { // Update the title field setting. - $this->instance['settings']['title'] = $title_setting; + $this->instance->settings['title'] = $title_setting; field_update_instance($this->instance); // Display creation form. $this->drupalGet('test-entity/add/test_bundle'); - $this->assertFieldByName("{$this->field['field_name']}[$langcode][0][url]", '', 'URL field found.'); + $this->assertFieldByName("{$this->field->field_name}[$langcode][0][url]", '', 'URL field found.'); $this->assertRaw('placeholder="http://example.com"'); if ($title_setting === DRUPAL_DISABLED) { - $this->assertNoFieldByName("{$this->field['field_name']}[$langcode][0][title]", '', 'Title field not found.'); + $this->assertNoFieldByName("{$this->field->field_name}[$langcode][0][title]", '', 'Title field not found.'); $this->assertNoRaw('placeholder="Enter a title for this link"'); } else { $this->assertRaw('placeholder="Enter a title for this link"'); - $this->assertFieldByName("{$this->field['field_name']}[$langcode][0][title]", '', 'Title field found.'); + $this->assertFieldByName("{$this->field->field_name}[$langcode][0][title]", '', 'Title field found.'); if ($title_setting === DRUPAL_REQUIRED) { // Verify that the title is required, if the URL is non-empty. $edit = array( - "{$this->field['field_name']}[$langcode][0][url]" => 'http://www.example.com', + "{$this->field->field_name}[$langcode][0][url]" => 'http://www.example.com', ); $this->drupalPost(NULL, $edit, t('Save')); $this->assertText(t('!name field is required.', array('!name' => t('Title')))); // Verify that the title is not required, if the URL is empty. $edit = array( - "{$this->field['field_name']}[$langcode][0][url]" => '', + "{$this->field->field_name}[$langcode][0][url]" => '', ); $this->drupalPost(NULL, $edit, t('Save')); $this->assertNoText(t('!name field is required.', array('!name' => t('Title')))); @@ -179,8 +179,8 @@ function testLinkTitle() { // Verify that a URL and title meets requirements. $this->drupalGet('test-entity/add/test_bundle'); $edit = array( - "{$this->field['field_name']}[$langcode][0][url]" => 'http://www.example.com', - "{$this->field['field_name']}[$langcode][0][title]" => 'Example', + "{$this->field->field_name}[$langcode][0][url]" => 'http://www.example.com', + "{$this->field->field_name}[$langcode][0][title]" => 'Example', ); $this->drupalPost(NULL, $edit, t('Save')); $this->assertNoText(t('!name field is required.', array('!name' => t('Title')))); @@ -191,8 +191,8 @@ function testLinkTitle() { // Verify that a link without title is rendered using the URL as link text. $value = 'http://www.example.com/'; $edit = array( - "{$this->field['field_name']}[$langcode][0][url]" => $value, - "{$this->field['field_name']}[$langcode][0][title]" => '', + "{$this->field->field_name}[$langcode][0][url]" => $value, + "{$this->field->field_name}[$langcode][0][title]" => '', ); $this->drupalPost(NULL, $edit, t('Save')); preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match); @@ -206,7 +206,7 @@ function testLinkTitle() { // Verify that a link with title is rendered using the title as link text. $title = $this->randomName(); $edit = array( - "{$this->field['field_name']}[$langcode][0][title]" => $title, + "{$this->field->field_name}[$langcode][0][title]" => $title, ); $this->drupalPost("test-entity/manage/$id/edit", $edit, t('Save')); $this->assertRaw(t('test_entity @id has been updated.', array('@id' => $id))); @@ -226,9 +226,9 @@ function testLinkFormatter() { 'type' => 'link', 'cardinality' => 2, ); - field_create_field($this->field); + $this->field = field_create_field($this->field); $this->instance = array( - 'field_name' => $this->field['field_name'], + 'field_name' => $this->field->field_name, 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', 'settings' => array( @@ -244,7 +244,7 @@ function testLinkFormatter() { ); field_create_instance($this->instance); entity_get_display('test_entity', 'test_bundle', 'full') - ->setComponent($this->field['field_name'], $display_options) + ->setComponent($this->field->field_name, $display_options) ->save(); $langcode = LANGUAGE_NOT_SPECIFIED; @@ -261,11 +261,11 @@ function testLinkFormatter() { // Intentionally contains an ampersand that needs sanitization on output. $title2 = 'A very long & strange example title that could break the nice layout of the site'; $edit = array( - "{$this->field['field_name']}[$langcode][0][url]" => $url1, + "{$this->field->field_name}[$langcode][0][url]" => $url1, // Note that $title1 is not submitted. - "{$this->field['field_name']}[$langcode][0][title]" => '', - "{$this->field['field_name']}[$langcode][1][url]" => $url2, - "{$this->field['field_name']}[$langcode][1][title]" => $title2, + "{$this->field->field_name}[$langcode][0][title]" => '', + "{$this->field->field_name}[$langcode][1][url]" => $url2, + "{$this->field->field_name}[$langcode][1][title]" => $title2, ); $this->drupalPost(NULL, $edit, t('Save')); preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match); @@ -298,7 +298,7 @@ function testLinkFormatter() { $display_options['settings'] = $new_value; } entity_get_display('test_entity', 'test_bundle', 'full') - ->setComponent($this->field['field_name'], $display_options) + ->setComponent($this->field->field_name, $display_options) ->save(); $this->renderTestEntity($id); @@ -362,9 +362,9 @@ function testLinkSeparateFormatter() { 'type' => 'link', 'cardinality' => 2, ); - field_create_field($this->field); + $this->field = field_create_field($this->field); $this->instance = array( - 'field_name' => $this->field['field_name'], + 'field_name' => $this->field->field_name, 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', 'settings' => array( @@ -380,7 +380,7 @@ function testLinkSeparateFormatter() { ); field_create_instance($this->instance); entity_get_display('test_entity', 'test_bundle', 'full') - ->setComponent($this->field['field_name'], $display_options) + ->setComponent($this->field->field_name, $display_options) ->save(); $langcode = LANGUAGE_NOT_SPECIFIED; @@ -396,9 +396,9 @@ function testLinkSeparateFormatter() { // Intentionally contains an ampersand that needs sanitization on output. $title2 = 'A very long & strange example title that could break the nice layout of the site'; $edit = array( - "{$this->field['field_name']}[$langcode][0][url]" => $url1, - "{$this->field['field_name']}[$langcode][1][url]" => $url2, - "{$this->field['field_name']}[$langcode][1][title]" => $title2, + "{$this->field->field_name}[$langcode][0][url]" => $url1, + "{$this->field->field_name}[$langcode][1][url]" => $url2, + "{$this->field->field_name}[$langcode][1][title]" => $title2, ); $this->drupalPost(NULL, $edit, t('Save')); preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match); @@ -416,7 +416,7 @@ function testLinkSeparateFormatter() { // Update the field formatter settings. $display_options['settings'] = array($setting => $new_value); entity_get_display('test_entity', 'test_bundle', 'full') - ->setComponent($this->field['field_name'], $display_options) + ->setComponent($this->field->field_name, $display_options) ->save(); $this->renderTestEntity($id); diff --git a/core/modules/link/link.module b/core/modules/link/link.module index f8ee409..f9a3b5e 100644 --- a/core/modules/link/link.module +++ b/core/modules/link/link.module @@ -43,7 +43,7 @@ function link_field_instance_settings_form($field, $instance) { $form['title'] = array( '#type' => 'radios', '#title' => t('Allow link title'), - '#default_value' => isset($instance['settings']['title']) ? $instance['settings']['title'] : DRUPAL_OPTIONAL, + '#default_value' => isset($instance->settings['title']) ? $instance->settings['title'] : DRUPAL_OPTIONAL, '#options' => array( DRUPAL_DISABLED => t('Disabled'), DRUPAL_OPTIONAL => t('Optional'), @@ -111,7 +111,7 @@ function link_field_widget_info() { * Implements hook_field_widget_settings_form(). */ function link_field_widget_settings_form($field, $instance) { - $widget = $instance['widget']; + $widget = $instance->widget_settings; $settings = $widget['settings']; $form['placeholder_url'] = array( @@ -139,8 +139,8 @@ function link_field_widget_settings_form($field, $instance) { * Implements hook_field_widget_form(). */ function link_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { - $settings = $instance['settings']; - $widget_settings = $instance['widget']['settings']; + $settings = $instance->settings; + $widget_settings = $instance->widget_settings['settings']; $element['url'] = array( '#type' => 'url', diff --git a/core/modules/node/lib/Drupal/node/Plugin/views/wizard/Node.php b/core/modules/node/lib/Drupal/node/Plugin/views/wizard/Node.php index a7f4f88..a14d52c 100644 --- a/core/modules/node/lib/Drupal/node/Plugin/views/wizard/Node.php +++ b/core/modules/node/lib/Drupal/node/Plugin/views/wizard/Node.php @@ -280,8 +280,8 @@ protected function build_filters(&$form, &$form_state) { foreach (field_info_instances($this->entity_type, $bundle) as $instance) { // We define "tag-like" taxonomy fields as ones that use the // "Autocomplete term widget (tagging)" widget. - if ($instance['widget']['type'] == 'taxonomy_autocomplete') { - $tag_fields[] = $instance['field_name']; + if ($instance->widget_settings['type'] == 'taxonomy_autocomplete') { + $tag_fields[] = $instance->field_name; } } } diff --git a/core/modules/node/lib/Drupal/node/Tests/NodeFieldMultilingualTestCase.php b/core/modules/node/lib/Drupal/node/Tests/NodeFieldMultilingualTestCase.php index 6ea519a..05a8c4d 100644 --- a/core/modules/node/lib/Drupal/node/Tests/NodeFieldMultilingualTestCase.php +++ b/core/modules/node/lib/Drupal/node/Tests/NodeFieldMultilingualTestCase.php @@ -60,7 +60,7 @@ function setUp() { // Make node body translatable. $field = field_info_field('body'); - $field['translatable'] = TRUE; + $field->translatable = TRUE; field_update_field($field); } diff --git a/core/modules/node/lib/Drupal/node/Tests/NodeTypeTest.php b/core/modules/node/lib/Drupal/node/Tests/NodeTypeTest.php index 64fa105..da32cd1 100644 --- a/core/modules/node/lib/Drupal/node/Tests/NodeTypeTest.php +++ b/core/modules/node/lib/Drupal/node/Tests/NodeTypeTest.php @@ -84,7 +84,7 @@ function testNodeTypeEditing() { $this->drupalLogin($web_user); $instance = field_info_instance('node', 'body', 'page'); - $this->assertEqual($instance['label'], 'Body', 'Body field was found.'); + $this->assertEqual($instance->label, 'Body', 'Body field was found.'); // Verify that title and body fields are displayed. $this->drupalGet('node/add/page'); diff --git a/core/modules/node/node.module b/core/modules/node/node.module index f2206a0..addec07 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -583,20 +583,20 @@ function node_add_body_field($type, $label = 'Body') { 'entity_type' => 'node', 'bundle' => $type->type, 'label' => $label, - 'widget' => array('type' => 'text_textarea_with_summary'), + 'widget_settings' => array('type' => 'text_textarea_with_summary'), 'settings' => array('display_summary' => TRUE), ); $instance = field_create_instance($instance); // Assign display settings for the 'default' and 'teaser' view modes. entity_get_display('node', $type->type, 'default') - ->setComponent($field['field_name'], array( + ->setComponent($field->field_name, array( 'label' => 'hidden', 'type' => 'text_default', )) ->save(); entity_get_display('node', $type->type, 'teaser') - ->setComponent($field['field_name'], array( + ->setComponent($field->field_name, array( 'label' => 'hidden', 'type' => 'text_summary_or_trimmed', )) diff --git a/core/modules/node/node.tokens.inc b/core/modules/node/node.tokens.inc index c882450..60aece7 100644 --- a/core/modules/node/node.tokens.inc +++ b/core/modules/node/node.tokens.inc @@ -161,7 +161,7 @@ function node_tokens($type, $tokens, array $data = array(), array $options = arr $length = $settings['trim_length']; } - $output = text_summary($output, $instance['settings']['text_processing'] ? $items[0]['format'] : NULL, $length); + $output = text_summary($output, $instance->settings['text_processing'] ? $items[0]['format'] : NULL, $length); } } $replacements[$original] = $output; diff --git a/core/modules/number/lib/Drupal/number/Plugin/field/formatter/DefaultNumberFormatter.php b/core/modules/number/lib/Drupal/number/Plugin/field/formatter/DefaultNumberFormatter.php index 529ce03..3374dd6 100644 --- a/core/modules/number/lib/Drupal/number/Plugin/field/formatter/DefaultNumberFormatter.php +++ b/core/modules/number/lib/Drupal/number/Plugin/field/formatter/DefaultNumberFormatter.php @@ -72,8 +72,8 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) { // Account for prefix and suffix. if ($this->getSetting('prefix_suffix')) { - $prefixes = isset($instance['settings']['prefix']) ? array_map('field_filter_xss', explode('|', $instance['settings']['prefix'])) : array(''); - $suffixes = isset($instance['settings']['suffix']) ? array_map('field_filter_xss', explode('|', $instance['settings']['suffix'])) : array(''); + $prefixes = isset($instance->settings['prefix']) ? array_map('field_filter_xss', explode('|', $instance->settings['prefix'])) : array(''); + $suffixes = isset($instance->settings['suffix']) ? array_map('field_filter_xss', explode('|', $instance->settings['suffix'])) : array(''); $prefix = (count($prefixes) > 1) ? format_plural($item['value'], $prefixes[0], $prefixes[1]) : $prefixes[0]; $suffix = (count($suffixes) > 1) ? format_plural($item['value'], $suffixes[0], $suffixes[1]) : $suffixes[0]; $output = $prefix . $output . $suffix; diff --git a/core/modules/number/lib/Drupal/number/Plugin/field/widget/NumberWidget.php b/core/modules/number/lib/Drupal/number/Plugin/field/widget/NumberWidget.php index ec9eaf6..bd31f3f 100644 --- a/core/modules/number/lib/Drupal/number/Plugin/field/widget/NumberWidget.php +++ b/core/modules/number/lib/Drupal/number/Plugin/field/widget/NumberWidget.php @@ -59,9 +59,9 @@ public function formElement(array $items, $delta, array $element, $langcode, arr ); // Set the step for floating point and decimal numbers. - switch ($field['type']) { + switch ($field->type) { case 'number_decimal': - $element['#step'] = pow(0.1, $field['settings']['scale']); + $element['#step'] = pow(0.1, $field->settings['scale']); break; case 'number_float': @@ -70,20 +70,20 @@ public function formElement(array $items, $delta, array $element, $langcode, arr } // Set minimum and maximum. - if (is_numeric($instance['settings']['min'])) { - $element['#min'] = $instance['settings']['min']; + if (is_numeric($instance->settings['min'])) { + $element['#min'] = $instance->settings['min']; } - if (is_numeric($instance['settings']['max'])) { - $element['#max'] = $instance['settings']['max']; + if (is_numeric($instance->settings['max'])) { + $element['#max'] = $instance->settings['max']; } // Add prefix and suffix. - if (!empty($instance['settings']['prefix'])) { - $prefixes = explode('|', $instance['settings']['prefix']); + if (!empty($instance->settings['prefix'])) { + $prefixes = explode('|', $instance->settings['prefix']); $element['#field_prefix'] = field_filter_xss(array_pop($prefixes)); } - if (!empty($instance['settings']['suffix'])) { - $suffixes = explode('|', $instance['settings']['suffix']); + if (!empty($instance->settings['suffix'])) { + $suffixes = explode('|', $instance->settings['suffix']); $element['#field_suffix'] = field_filter_xss(array_pop($suffixes)); } diff --git a/core/modules/number/lib/Drupal/number/Tests/NumberFieldTest.php b/core/modules/number/lib/Drupal/number/Tests/NumberFieldTest.php index 925828c..db9ae72 100644 --- a/core/modules/number/lib/Drupal/number/Tests/NumberFieldTest.php +++ b/core/modules/number/lib/Drupal/number/Tests/NumberFieldTest.php @@ -52,9 +52,9 @@ function testNumberDecimalField() { 'precision' => 8, 'scale' => 4, 'decimal_separator' => '.', ) ); - field_create_field($this->field); + $this->field = field_create_field($this->field); $this->instance = array( - 'field_name' => $this->field['field_name'], + 'field_name' => $this->field->field_name, 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', 'widget' => array( @@ -71,19 +71,19 @@ function testNumberDecimalField() { ); field_create_instance($this->instance); entity_get_display('test_entity', 'test_bundle', 'default') - ->setComponent($this->field['field_name']) + ->setComponent($this->field->field_name) ->save(); // Display creation form. $this->drupalGet('test-entity/add/test_bundle'); $langcode = LANGUAGE_NOT_SPECIFIED; - $this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value]", '', 'Widget is displayed'); + $this->assertFieldByName("{$this->field->field_name}[$langcode][0][value]", '', 'Widget is displayed'); $this->assertRaw('placeholder="0.00"'); // Submit a signed decimal value within the allowed precision and scale. $value = '-1234.5678'; $edit = array( - "{$this->field['field_name']}[$langcode][0][value]" => $value, + "{$this->field->field_name}[$langcode][0][value]" => $value, ); $this->drupalPost(NULL, $edit, t('Save')); preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match); @@ -103,10 +103,10 @@ function testNumberDecimalField() { foreach ($wrong_entries as $wrong_entry) { $this->drupalGet('test-entity/add/test_bundle'); $edit = array( - "{$this->field['field_name']}[$langcode][0][value]" => $wrong_entry, + "{$this->field->field_name}[$langcode][0][value]" => $wrong_entry, ); $this->drupalPost(NULL, $edit, t('Save')); - $this->assertRaw(t('%name must be a number.', array('%name' => $this->field['field_name'])), 'Correctly failed to save decimal value with more than one decimal point.'); + $this->assertRaw(t('%name must be a number.', array('%name' => $this->field->field_name)), 'Correctly failed to save decimal value with more than one decimal point.'); } // Try to create entries with minus sign not in the first position. @@ -121,10 +121,10 @@ function testNumberDecimalField() { foreach ($wrong_entries as $wrong_entry) { $this->drupalGet('test-entity/add/test_bundle'); $edit = array( - "{$this->field['field_name']}[$langcode][0][value]" => $wrong_entry, + "{$this->field->field_name}[$langcode][0][value]" => $wrong_entry, ); $this->drupalPost(NULL, $edit, t('Save')); - $this->assertRaw(t('%name must be a number.', array('%name' => $this->field['field_name'])), 'Correctly failed to save decimal value with minus sign in the wrong position.'); + $this->assertRaw(t('%name must be a number.', array('%name' => $this->field->field_name)), 'Correctly failed to save decimal value with minus sign in the wrong position.'); } } diff --git a/core/modules/number/number.install b/core/modules/number/number.install index 3c4ae7f..4520c95 100644 --- a/core/modules/number/number.install +++ b/core/modules/number/number.install @@ -9,7 +9,7 @@ * Implements hook_field_schema(). */ function number_field_schema($field) { - switch ($field['type']) { + switch ($field->type) { case 'number_integer' : $columns = array( 'value' => array( @@ -32,8 +32,8 @@ function number_field_schema($field) { $columns = array( 'value' => array( 'type' => 'numeric', - 'precision' => $field['settings']['precision'], - 'scale' => $field['settings']['scale'], + 'precision' => $field->settings['precision'], + 'scale' => $field->settings['scale'], 'not null' => FALSE ), ); diff --git a/core/modules/number/number.module b/core/modules/number/number.module index 4615519..73762cb 100644 --- a/core/modules/number/number.module +++ b/core/modules/number/number.module @@ -57,7 +57,7 @@ function number_field_info() { * Implements hook_field_settings_form(). */ function number_field_settings_form($field, $instance, $has_data) { - $settings = $field['settings']; + $settings = $field->settings; $form = array(); if ($field['type'] == 'number_decimal') { @@ -86,7 +86,7 @@ function number_field_settings_form($field, $instance, $has_data) { * Implements hook_field_instance_settings_form(). */ function number_field_instance_settings_form($field, $instance) { - $settings = $instance['settings']; + $settings = $instance->settings; $form['min'] = array( '#type' => 'textfield', @@ -130,16 +130,16 @@ function number_field_instance_settings_form($field, $instance) { function number_field_validate(EntityInterface $entity = NULL, $field, $instance, $langcode, $items, &$errors) { foreach ($items as $delta => $item) { if ($item['value'] != '') { - if (is_numeric($instance['settings']['min']) && $item['value'] < $instance['settings']['min']) { + if (is_numeric($instance->settings['min']) && $item['value'] < $instance->settings['min']) { $errors[$field['field_name']][$langcode][$delta][] = array( 'error' => 'number_min', - 'message' => t('%name: the value may be no less than %min.', array('%name' => $instance['label'], '%min' => $instance['settings']['min'])), + 'message' => t('%name: the value may be no less than %min.', array('%name' => $instance->label, '%min' => $instance->settings['min'])), ); } - if (is_numeric($instance['settings']['max']) && $item['value'] > $instance['settings']['max']) { + if (is_numeric($instance->settings['max']) && $item['value'] > $instance->settings['max']) { $errors[$field['field_name']][$langcode][$delta][] = array( 'error' => 'number_max', - 'message' => t('%name: the value may be no greater than %max.', array('%name' => $instance['label'], '%max' => $instance['settings']['max'])), + 'message' => t('%name: the value may be no greater than %max.', array('%name' => $instance->label, '%max' => $instance->settings['max'])), ); } } @@ -150,12 +150,12 @@ function number_field_validate(EntityInterface $entity = NULL, $field, $instance * Implements hook_field_presave(). */ function number_field_presave(EntityInterface $entity, $field, $instance, $langcode, &$items) { - if ($field['type'] == 'number_decimal') { + if ($field->type == 'number_decimal') { // Let PHP round the value to ensure consistent behavior across storage // backends. foreach ($items as $delta => $item) { if (isset($item['value'])) { - $items[$delta]['value'] = round($item['value'], $field['settings']['scale']); + $items[$delta]['value'] = round($item['value'], $field->settings['scale']); } } } diff --git a/core/modules/options/lib/Drupal/options/Tests/OptionsFieldTest.php b/core/modules/options/lib/Drupal/options/Tests/OptionsFieldTest.php index 1027eb1..6d6a660 100644 --- a/core/modules/options/lib/Drupal/options/Tests/OptionsFieldTest.php +++ b/core/modules/options/lib/Drupal/options/Tests/OptionsFieldTest.php @@ -73,7 +73,7 @@ function testUpdateAllowedValues() { $entity = field_test_create_entity(); $entity->{$this->field_name}[$langcode][0] = array('value' => 1); field_test_entity_save($entity); - $this->field['settings']['allowed_values'] = array(2 => 'Two'); + $this->field->settings['allowed_values'] = array(2 => 'Two'); try { field_update_field($this->field); $this->fail(t('Cannot update a list field to not include keys with existing data.')); @@ -86,7 +86,7 @@ function testUpdateAllowedValues() { field_test_entity_save($entity); // Removed options do not appear. - $this->field['settings']['allowed_values'] = array(2 => 'Two'); + $this->field->settings['allowed_values'] = array(2 => 'Two'); field_update_field($this->field); $entity = field_test_create_entity(); $form = entity_get_form($entity); @@ -95,7 +95,7 @@ function testUpdateAllowedValues() { $this->assertTrue(empty($form[$this->field_name][$langcode][3]), 'Option 3 does not exist'); // Completely new options appear. - $this->field['settings']['allowed_values'] = array(10 => 'Update', 20 => 'Twenty'); + $this->field->settings['allowed_values'] = array(10 => 'Update', 20 => 'Twenty'); field_update_field($this->field); $form = entity_get_form($entity); $this->assertTrue(empty($form[$this->field_name][$langcode][1]), 'Option 1 does not exist'); @@ -106,8 +106,8 @@ function testUpdateAllowedValues() { // Options are reset when a new field with the same name is created. field_delete_field($this->field_name); - unset($this->field['id']); - $this->field['settings']['allowed_values'] = array(1 => 'One', 2 => 'Two', 3 => 'Three'); + $this->field->enforceIsNew(); + $this->field->settings['allowed_values'] = array(1 => 'One', 2 => 'Two', 3 => 'Three'); $this->field = field_create_field($this->field); $this->instance = array( 'field_name' => $this->field_name, diff --git a/core/modules/options/lib/Drupal/options/Tests/OptionsFieldUITest.php b/core/modules/options/lib/Drupal/options/Tests/OptionsFieldUITest.php index 3409185..dfd0fd2 100644 --- a/core/modules/options/lib/Drupal/options/Tests/OptionsFieldUITest.php +++ b/core/modules/options/lib/Drupal/options/Tests/OptionsFieldUITest.php @@ -220,9 +220,9 @@ function testOptionsAllowedValuesBoolean() { $this->assertFieldByName('on', $on, t("The 'On' value is stored correctly.")); $this->assertFieldByName('off', $off, t("The 'Off' value is stored correctly.")); $field = field_info_field($this->field_name); - $this->assertEqual($field['settings']['allowed_values'], $allowed_values, 'The allowed value is correct'); - $this->assertFalse(isset($field['settings']['on']), 'The on value is not saved into settings'); - $this->assertFalse(isset($field['settings']['off']), 'The off value is not saved into settings'); + $this->assertEqual($field->settings['allowed_values'], $allowed_values, 'The allowed value is correct'); + $this->assertFalse(isset($field->settings['on']), 'The on value is not saved into settings'); + $this->assertFalse(isset($field->settings['off']), 'The off value is not saved into settings'); } /** @@ -269,7 +269,7 @@ protected function createOptionsField($type) { * element. * @param $result * Either an expected resulting array in - * $field['settings']['allowed_values'], or an expected error message. + * $field->settings['allowed_values'], or an expected error message. * @param $message * Message to display. */ @@ -283,7 +283,7 @@ function assertAllowedValuesInput($input_string, $result, $message) { else { field_info_cache_clear(); $field = field_info_field($this->field_name); - $this->assertIdentical($field['settings']['allowed_values'], $result, $message); + $this->assertIdentical($field->settings['allowed_values'], $result, $message); } } } diff --git a/core/modules/options/lib/Drupal/options/Tests/OptionsWidgetsTest.php b/core/modules/options/lib/Drupal/options/Tests/OptionsWidgetsTest.php index 023f012..264230e 100644 --- a/core/modules/options/lib/Drupal/options/Tests/OptionsWidgetsTest.php +++ b/core/modules/options/lib/Drupal/options/Tests/OptionsWidgetsTest.php @@ -79,7 +79,7 @@ function setUp() { function testRadioButtons() { // Create an instance of the 'single value' field. $instance = array( - 'field_name' => $this->card_1['field_name'], + 'field_name' => $this->card_1->field_name, 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', 'widget' => array( @@ -119,9 +119,9 @@ function testRadioButtons() { $this->assertFieldValues($entity_init, 'card_1', $langcode, array()); // Check that required radios with one option is auto-selected. - $this->card_1['settings']['allowed_values'] = array(99 => 'Only allowed value'); + $this->card_1->settings['allowed_values'] = array(99 => 'Only allowed value'); field_update_field($this->card_1); - $instance['required'] = TRUE; + $instance->required = TRUE; field_update_instance($instance); $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit'); $this->assertFieldChecked("edit-card-1-$langcode-99"); @@ -133,7 +133,7 @@ function testRadioButtons() { function testCheckBoxes() { // Create an instance of the 'multiple values' field. $instance = array( - 'field_name' => $this->card_2['field_name'], + 'field_name' => $this->card_2->field_name, 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', 'widget' => array( @@ -206,9 +206,9 @@ function testCheckBoxes() { $this->assertFieldValues($entity_init, 'card_2', $langcode, array()); // Required checkbox with one option is auto-selected. - $this->card_2['settings']['allowed_values'] = array(99 => 'Only allowed value'); + $this->card_2->settings['allowed_values'] = array(99 => 'Only allowed value'); field_update_field($this->card_2); - $instance['required'] = TRUE; + $instance->required = TRUE; field_update_instance($instance); $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit'); $this->assertFieldChecked("edit-card-2-$langcode-99"); @@ -220,7 +220,7 @@ function testCheckBoxes() { function testSelectListSingle() { // Create an instance of the 'single value' field. $instance = array( - 'field_name' => $this->card_1['field_name'], + 'field_name' => $this->card_1->field_name, 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', 'required' => TRUE, @@ -252,7 +252,7 @@ function testSelectListSingle() { // Submit form: select invalid 'none' option. $edit = array("card_1[$langcode]" => '_none'); $this->drupalPost(NULL, $edit, t('Save')); - $this->assertRaw(t('!title field is required.', array('!title' => $instance['field_name'])), 'Cannot save a required field when selecting "none" from the select list.'); + $this->assertRaw(t('!title field is required.', array('!title' => $instance->field_name)), 'Cannot save a required field when selecting "none" from the select list.'); // Submit form: select first option. $edit = array("card_1[$langcode]" => 0); @@ -268,7 +268,7 @@ function testSelectListSingle() { $this->assertNoOptionSelected("edit-card-1-$langcode", 2); // Make the field non required. - $instance['required'] = FALSE; + $instance->required = FALSE; field_update_instance($instance); // Display form. @@ -282,8 +282,8 @@ function testSelectListSingle() { // Test optgroups. - $this->card_1['settings']['allowed_values'] = array(); - $this->card_1['settings']['allowed_values_function'] = 'options_test_allowed_values_callback'; + $this->card_1->settings['allowed_values'] = array(); + $this->card_1->settings['allowed_values_function'] = 'options_test_allowed_values_callback'; field_update_field($this->card_1); // Display form: with no field data, nothing is selected @@ -317,7 +317,7 @@ function testSelectListSingle() { function testSelectListMultiple() { // Create an instance of the 'multiple values' field. $instance = array( - 'field_name' => $this->card_2['field_name'], + 'field_name' => $this->card_2->field_name, 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', 'widget' => array( @@ -386,7 +386,7 @@ function testSelectListMultiple() { $this->assertFieldValues($entity_init, 'card_2', $langcode, array()); // A required select list does not have an empty key. - $instance['required'] = TRUE; + $instance->required = TRUE; field_update_instance($instance); $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit'); $this->assertFalse($this->xpath('//select[@id=:id]//option[@value=""]', array(':id' => 'edit-card-2-' . $langcode)), 'A required select list does not have an empty key.'); @@ -397,10 +397,10 @@ function testSelectListMultiple() { // Test optgroups. // Use a callback function defining optgroups. - $this->card_2['settings']['allowed_values'] = array(); - $this->card_2['settings']['allowed_values_function'] = 'options_test_allowed_values_callback'; + $this->card_2->settings['allowed_values'] = array(); + $this->card_2->settings['allowed_values_function'] = 'options_test_allowed_values_callback'; field_update_field($this->card_2); - $instance['required'] = FALSE; + $instance->required = FALSE; field_update_instance($instance); // Display form: with no field data, nothing is selected. @@ -434,7 +434,7 @@ function testSelectListMultiple() { function testOnOffCheckbox() { // Create an instance of the 'boolean' field. $instance = array( - 'field_name' => $this->bool['field_name'], + 'field_name' => $this->bool->field_name, 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', 'widget' => array( @@ -482,10 +482,10 @@ function testOnOffCheckbox() { // Create a test field instance. $fieldUpdate = $this->bool; - $fieldUpdate['settings']['allowed_values'] = array(0 => 0, 1 => 'MyOnValue'); + $fieldUpdate->settings['allowed_values'] = array(0 => 0, 1 => 'MyOnValue'); field_update_field($fieldUpdate); $instance = array( - 'field_name' => $this->bool['field_name'], + 'field_name' => $this->bool->field_name, 'entity_type' => 'node', 'bundle' => 'page', 'widget' => array( @@ -505,7 +505,7 @@ function testOnOffCheckbox() { ); $this->assertFieldByXPath( - '*//label[@for="edit-' . $this->bool['field_name'] . '-und" and text()="MyOnValue"]', + '*//label[@for="edit-' . $this->bool->field_name . '-und" and text()="MyOnValue"]', TRUE, t('Default case shows "On value"') ); @@ -527,7 +527,7 @@ function testOnOffCheckbox() { t('Display settings checkbox checked') ); $this->assertFieldByXPath( - '*//label[@for="edit-' . $this->bool['field_name'] . '-und" and text()="' . $this->bool['field_name'] . '"]', + '*//label[@for="edit-' . $this->bool->field_name . '-und" and text()="' . $this->bool->field_name . '"]', TRUE, t('Display label changes label of the checkbox') ); diff --git a/core/modules/options/options.api.php b/core/modules/options/options.api.php index 2a303d5..6bdda32 100644 --- a/core/modules/options/options.api.php +++ b/core/modules/options/options.api.php @@ -58,7 +58,7 @@ function hook_options_list($field, $instance, $entity) { // In actual implementations, the array of options will most probably depend // on properties of the field. Example from taxonomy.module: $options = array(); - foreach ($field['settings']['allowed_values'] as $tree) { + foreach ($field->settings['allowed_values'] as $tree) { $terms = taxonomy_get_tree($tree['vid'], $tree['parent'], NULL, TRUE); if ($terms) { foreach ($terms as $term) { diff --git a/core/modules/options/options.install b/core/modules/options/options.install index 6c2c8af..ae08db2 100644 --- a/core/modules/options/options.install +++ b/core/modules/options/options.install @@ -9,7 +9,7 @@ * Implements hook_field_schema(). */ function options_field_schema($field) { - switch ($field['type']) { + switch ($field->type) { case 'list_text': $columns = array( 'value' => array( diff --git a/core/modules/options/options.module b/core/modules/options/options.module index f1639ca..db31d48 100644 --- a/core/modules/options/options.module +++ b/core/modules/options/options.module @@ -73,9 +73,9 @@ function options_field_info() { * Implements hook_field_settings_form(). */ function options_field_settings_form($field, $instance, $has_data) { - $settings = $field['settings']; + $settings = $field->settings; - switch ($field['type']) { + switch ($field->type) { case 'list_integer': case 'list_float': case 'list_text': @@ -87,12 +87,12 @@ function options_field_settings_form($field, $instance, $has_data) { '#element_validate' => array('options_field_settings_form_validate_allowed_values'), '#field_has_data' => $has_data, '#field' => $field, - '#field_type' => $field['type'], + '#field_type' => $field->type, '#access' => empty($settings['allowed_values_function']), ); $description = '

' . t('The possible values this field can contain. Enter one value per line, in the format key|label.'); - if ($field['type'] == 'list_integer' || $field['type'] == 'list_float') { + if ($field->type == 'list_integer' || $field->type == 'list_float') { $description .= '
' . t('The key is the stored value, and must be numeric. The label will be used in displayed values and edit forms.'); $description .= '
' . t('The label is optional: if a line contains a single number, it will be used as key and label.'); $description .= '
' . t('Lists of labels are also accepted (one label per line), only if the field does not hold any values yet. Numeric keys will be automatically generated from the positions in the list.'); @@ -147,10 +147,10 @@ function options_field_settings_form($field, $instance, $has_data) { } // Alter the description for allowed values depending on the widget type. - if ($instance['widget']['type'] == 'options_onoff') { + if ($instance->widget_settings['type'] == 'options_onoff') { $form['allowed_values']['#description'] .= '

' . t("For a 'single on/off checkbox' widget, define the 'off' value first, then the 'on' value in the Allowed values section. Note that the checkbox will be labeled with the label of the 'on' value.") . '

'; } - elseif ($instance['widget']['type'] == 'options_buttons') { + elseif ($instance->widget_settings['type'] == 'options_buttons') { $form['allowed_values']['#description'] .= '

' . t("The 'checkboxes/radio buttons' widget will display checkboxes if the Number of values option is greater than 1 for this field, otherwise radios will be displayed.") . '

'; } $form['allowed_values']['#description'] .= '

' . t('Allowed HTML tags in labels: @tags', array('@tags' => _field_filter_xss_display_allowed_tags())) . '

'; @@ -175,10 +175,10 @@ function options_field_settings_form($field, $instance, $has_data) { function options_field_settings_form_validate_allowed_values($element, &$form_state) { $field = $element['#field']; $has_data = $element['#field_has_data']; - $field_type = $field['type']; + $field_type = $field->type; $generate_keys = ($field_type == 'list_integer' || $field_type == 'list_float') && !$has_data; - $values = options_extract_allowed_values($element['#value'], $field['type'], $generate_keys); + $values = options_extract_allowed_values($element['#value'], $field->type, $generate_keys); if (!is_array($values)) { form_error($element, t('Allowed values list: invalid input.')); @@ -202,7 +202,7 @@ function options_field_settings_form_validate_allowed_values($element, &$form_st // Prevent removing values currently in use. if ($has_data) { - $lost_keys = array_diff(array_keys($field['settings']['allowed_values']), array_keys($values)); + $lost_keys = array_diff(array_keys($field->settings['allowed_values']), array_keys($values)); if (_options_values_in_use($field, $lost_keys)) { form_error($element, t('Allowed values list: some values are being removed while currently in use.')); } @@ -248,8 +248,8 @@ function options_field_update_field($field, $prior_field, $has_data) { function options_allowed_values($field, $instance = NULL, EntityInterface $entity = NULL) { $allowed_values = &drupal_static(__FUNCTION__, array()); - if (!isset($allowed_values[$field['id']])) { - $function = $field['settings']['allowed_values_function']; + if (!isset($allowed_values[$field->id()])) { + $function = $field->settings['allowed_values_function']; // If $cacheable is FALSE, then the allowed values are not statically // cached. See options_test_dynamic_values_callback() for an example of // generating dynamic and uncached values. @@ -258,18 +258,18 @@ function options_allowed_values($field, $instance = NULL, EntityInterface $entit $values = $function($field, $instance, $entity, $cacheable); } else { - $values = $field['settings']['allowed_values']; + $values = $field->settings['allowed_values']; } if ($cacheable) { - $allowed_values[$field['id']] = $values; + $allowed_values[$field->id()] = $values; } else { return $values; } } - return $allowed_values[$field['id']]; + return $allowed_values[$field->id()]; } /** @@ -370,11 +370,11 @@ function options_allowed_values_string($values) { * Implements hook_field_update_forbid(). */ function options_field_update_forbid($field, $prior_field, $has_data) { - if ($field['module'] == 'options' && $has_data) { + if ($field->module == 'options' && $has_data) { // Forbid any update that removes allowed values with actual data. - $lost_keys = array_diff(array_keys($prior_field['settings']['allowed_values']), array_keys($field['settings']['allowed_values'])); + $lost_keys = array_diff(array_keys($prior_field->settings['allowed_values']), array_keys($field->settings['allowed_values'])); if (_options_values_in_use($field, $lost_keys)) { - throw new FieldUpdateForbiddenException(t('A list field (@field_name) with existing data cannot have its keys changed.', array('@field_name' => $field['field_name']))); + throw new FieldUpdateForbiddenException(t('A list field (@field_name) with existing data cannot have its keys changed.', array('@field_name' => $field->field_name))); } } } @@ -384,11 +384,11 @@ function options_field_update_forbid($field, $prior_field, $has_data) { */ function _options_values_in_use($field, $values) { if ($values) { - $field = field_info_field_by_id($field['id']); + $field = field_info_field_by_id($field->id()); $factory = drupal_container()->get('entity.query'); - foreach ($field['bundles'] as $entity_type => $bundle) { + foreach ($field->bundles as $entity_type => $bundle) { $result = $factory->get($entity_type) - ->condition($field['field_name'] . '.value', $values) + ->condition($field->field_name . '.value', $values) ->count() ->accessCheck(FALSE) ->range(0, 1) @@ -413,9 +413,9 @@ function options_field_validate(EntityInterface $entity = NULL, $field, $instanc foreach ($items as $delta => $item) { if (!empty($item['value'])) { if (!empty($allowed_values) && !isset($allowed_values[$item['value']])) { - $errors[$field['field_name']][$langcode][$delta][] = array( + $errors[$field->field_name][$langcode][$delta][] = array( 'error' => 'list_illegal_value', - 'message' => t('%name: illegal value.', array('%name' => $instance['label'])), + 'message' => t('%name: illegal value.', array('%name' => $instance->label)), ); } } @@ -473,10 +473,13 @@ function options_field_widget_info() { function options_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { // Abstract over the actual field columns, to allow different field types to // reuse those widgets. - $value_key = key($field['columns']); - $type = str_replace('options_', '', $instance['widget']['type']); - $multiple = $field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED; + // Reset internal pointer since we're dealing with objects now. + reset($field->columns); + $value_key = key($field->columns); + + $type = str_replace('options_', '', $instance->widget_settings['type']); + $multiple = $field->cardinality > 1 || $field->cardinality == FIELD_CARDINALITY_UNLIMITED; $required = $element['#required']; $has_value = isset($items[0][$value_key]); $properties = _options_properties($type, $multiple, $required, $has_value); @@ -536,8 +539,8 @@ function options_field_widget_form(&$form, &$form_state, $field, $instance, $lan // Override the title from the incoming $element. $element['#title'] = isset($options[$on_value]) ? $options[$on_value] : ''; - if ($instance['widget']['settings']['display_label']) { - $element['#title'] = $instance['label']; + if ($instance->widget_settings['settings']['display_label']) { + $element['#title'] = $instance->label; } break; } @@ -556,11 +559,11 @@ function options_field_widget_form(&$form, &$form_state, $field, $instance, $lan */ function options_field_widget_settings_form($field, $instance) { $form = array(); - if ($instance['widget']['type'] == 'options_onoff') { + if ($instance->widget_settings['type'] == 'options_onoff') { $form['display_label'] = array( '#type' => 'checkbox', '#title' => t('Use field label instead of the "On value" as label'), - '#default_value' => $instance['widget']['settings']['display_label'], + '#default_value' => $instance->widget_settings['settings']['display_label'], '#weight' => -1, ); } @@ -656,7 +659,7 @@ function _options_properties($type, $multiple, $required, $has_value) { */ function _options_get_options($field, $instance, $properties, EntityInterface $entity) { // Get the list of options. - $options = (array) module_invoke($field['module'], 'options_list', $field, $instance, $entity); + $options = (array) module_invoke($field->module, 'options_list', $field, $instance, $entity); // Sanitize the options. _options_prepare_options($options, $properties); @@ -847,7 +850,7 @@ function theme_options_none($variables) { $option = $variables['option']; $output = ''; - switch ($instance['widget']['type']) { + switch ($instance->widget_settings['type']) { case 'options_buttons': $output = t('N/A'); break; diff --git a/core/modules/search/lib/Drupal/search/Tests/SearchMultilingualEntityTest.php b/core/modules/search/lib/Drupal/search/Tests/SearchMultilingualEntityTest.php index e534971..1ef20ff 100644 --- a/core/modules/search/lib/Drupal/search/Tests/SearchMultilingualEntityTest.php +++ b/core/modules/search/lib/Drupal/search/Tests/SearchMultilingualEntityTest.php @@ -49,7 +49,7 @@ function setUp() { // Make the body field translatable. // The parent class has already created the article and page content types. $field = field_info_field('body'); - $field['translatable'] = TRUE; + $field->translatable = TRUE; field_update_field($field); // Create a few page nodes with multilingual body values. diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php index e3ad6fe..07c8da0 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php @@ -568,7 +568,7 @@ public function testComputedProperties() { protected function assertComputedProperties($entity_type) { // Make the test text field processed. $instance = field_info_instance($entity_type, 'field_test_text', $entity_type); - $instance['settings']['text_processing'] = 1; + $instance->settings['text_processing'] = 1; field_update_instance($instance); $entity = $this->createTestEntity($entity_type); diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityQueryTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityQueryTest.php index 1882e6f..093af8a 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityQueryTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityQueryTest.php @@ -75,7 +75,7 @@ function setUp() { field_test_create_bundle($bundle); foreach ($fields as $field) { $instance = array( - 'field_name' => $field['field_name'], + 'field_name' => $field->field_name, 'entity_type' => 'test_entity', 'bundle' => $bundle, ); diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTranslationFormTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTranslationFormTest.php index bb3ac2c..a176f5b 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTranslationFormTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTranslationFormTest.php @@ -106,10 +106,10 @@ function testEntityFormLanguage() { // Make body translatable. $field = field_info_field('body'); - $field['translatable'] = TRUE; + $field->translatable = TRUE; field_update_field($field); $field = field_info_field('body'); - $this->assertTrue($field['translatable'], 'Field body is translatable.'); + $this->assertTrue($field->translatable, 'Field body is translatable.'); // Create a body translation and check the form language. $langcode2 = $this->langcodes[1]; diff --git a/core/modules/system/lib/Drupal/system/Tests/Upgrade/FieldUpgradePathTest.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/FieldUpgradePathTest.php index c0de21b..89cac76 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Upgrade/FieldUpgradePathTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/FieldUpgradePathTest.php @@ -87,4 +87,16 @@ public function testEntityDisplayUpgrade() { $this->assertEqual($displays['teaser']['content']['language'], $expected['teaser']); } + /** + * Tests upgrade of fields and instances to config. + */ + function testFieldUpgradeToConfig() { + $this->assertTrue($this->performUpgrade(), t('The upgrade was completed successfully.')); + + // Assert the body field and instance on the article are converted. + $body_field = field_info_field('body'); + $this->assertNotNull($body_field, 'The body field has been found.'); + $body_field_instance = field_info_instance('node', 'body', 'article'); + $this->assertNotNull($body_field, 'The body field instance on the article content type has been found.'); + } } diff --git a/core/modules/system/lib/Drupal/system/Tests/Upgrade/UserPictureUpgradePathTest.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/UserPictureUpgradePathTest.php index 04070c9..a61db81 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Upgrade/UserPictureUpgradePathTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/UserPictureUpgradePathTest.php @@ -40,8 +40,8 @@ public function testUserPictureUpgrade() { // Retrieve the field instance and check for migrated settings. $instance = field_info_instance('user', 'user_picture', 'user'); - $file = entity_load('file', $instance['settings']['default_image']); - $this->assertIdentical($instance['settings']['default_image'], $file->id(), 'Default user picture has been migrated.'); + $file = entity_load('file', $instance->settings['default_image']); + $this->assertIdentical($instance->settings['default_image'], $file->id(), 'Default user picture has been migrated.'); $this->assertEqual($file->uri, 'public://user_pictures_dir/druplicon.png', 'File id matches the uri expected.'); $this->assertEqual($file->filename, 'druplicon.png'); $this->assertEqual($file->langcode, LANGUAGE_NOT_SPECIFIED); @@ -51,12 +51,12 @@ public function testUserPictureUpgrade() { // Check file usage for the default image. $usage = file_usage()->listUsage($file); $field = field_info_field('user_picture'); - $this->assertEqual(1, $usage['image']['default_image'][$field['id']]); + $this->assertTrue(isset($usage['image']['default_image'][$field->id()])); - $this->assertEqual($instance['settings']['max_resolution'], '800x800', 'User picture maximum resolution has been migrated.'); - $this->assertEqual($instance['settings']['max_filesize'], '700 KB', 'User picture maximum filesize has been migrated.'); - $this->assertEqual($instance['description'], 'These are user picture guidelines.', 'User picture guidelines are now the user picture field description.'); - $this->assertEqual($instance['settings']['file_directory'], 'user_pictures_dir', 'User picture directory path has been migrated.'); + $this->assertEqual($instance->settings['max_resolution'], '800x800', 'User picture maximum resolution has been migrated.'); + $this->assertEqual($instance->settings['max_filesize'], '700 KB', 'User picture maximum filesize has been migrated.'); + $this->assertEqual($instance->description, 'These are user picture guidelines.', 'User picture guidelines are now the user picture field description.'); + $this->assertEqual($instance->settings['file_directory'], 'user_pictures_dir', 'User picture directory path has been migrated.'); $display_options = entity_get_display('user', 'user', 'default')->getComponent('user_picture'); $this->assertEqual($display_options['settings']['image_style'], 'thumbnail', 'User picture image style setting has been migrated.'); diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/entity_reference/selection/TermSelection.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/entity_reference/selection/TermSelection.php index c99c6c5..1290af2 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/entity_reference/selection/TermSelection.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/entity_reference/selection/TermSelection.php @@ -43,7 +43,7 @@ public static function settingsForm(&$field, &$instance) { $form['auto_create'] = array( '#type' => 'checkbox', '#title' => t("Create referenced entities if they don't already exist"), - '#default_value' => $instance['settings']['handler_settings']['auto_create'], + '#default_value' => $instance->settings['handler_settings']['auto_create'], ); return $form; @@ -61,7 +61,7 @@ public function getReferencableEntities($match = NULL, $match_operator = 'CONTAI $options = array(); $bundles = entity_get_bundles('taxonomy_term'); - $bundle_names = !empty($this->instance['settings']['handler_settings']['target_bundles']) ? $this->instance['settings']['handler_settings']['target_bundles'] : array_keys($bundles); + $bundle_names = !empty($this->instance->settings['handler_settings']['target_bundles']) ? $this->instance->settings['handler_settings']['target_bundles'] : array_keys($bundles); foreach ($bundle_names as $bundle) { if ($vocabulary = entity_load('taxonomy_vocabulary', $bundle)) { diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php index 0d5dfd4..ea2019d 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php @@ -57,7 +57,7 @@ public function formElement(array $items, $delta, array $element, $langcode, arr $element += array( '#type' => 'textfield', '#default_value' => taxonomy_implode_tags($tags), - '#autocomplete_path' => $this->getSetting('autocomplete_path') . '/' . $field['field_name'], + '#autocomplete_path' => $this->getSetting('autocomplete_path') . '/' . $field->field_name, '#size' => $this->getSetting('size'), '#placeholder' => $this->getSetting('placeholder'), '#maxlength' => 1024, @@ -77,7 +77,7 @@ public function massageFormValues(array $values, array $form, array &$form_state $field = $this->field; // Collect candidate vocabularies. - foreach ($field['settings']['allowed_values'] as $tree) { + foreach ($field->settings['allowed_values'] as $tree) { if ($vocabulary = entity_load('taxonomy_vocabulary', $tree['vocabulary'])) { $vocabularies[$vocabulary->id()] = $vocabulary; } diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/RssTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/RssTest.php index 3d28ec0..8d7cbaf 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/RssTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/RssTest.php @@ -92,7 +92,7 @@ function testTaxonomyRss() { $edit = array(); $langcode = LANGUAGE_NOT_SPECIFIED; $edit["title"] = $this->randomName(); - $edit[$this->instance['field_name'] . '[' . $langcode . '][]'] = $term1->tid; + $edit[$this->instance->field_name . '[' . $langcode . '][]'] = $term1->tid; $this->drupalPost('node/add/article', $edit, t('Save')); // Check that the term is displayed when the RSS feed is viewed. diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldMultipleVocabularyTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldMultipleVocabularyTest.php index 8c5571c..aab02ff 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldMultipleVocabularyTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldMultipleVocabularyTest.php @@ -123,7 +123,7 @@ function testTaxonomyTermFieldMultipleVocabularies() { // Verify that field and instance settings are correct. $field_info = field_info_field($this->field_name); - $this->assertEqual(count($field_info['settings']['allowed_values']), 1, 'Only one vocabulary is allowed for the field.'); + $this->assertEqual(count($field_info->settings['allowed_values']), 1, 'Only one vocabulary is allowed for the field.'); // The widget should still be displayed. $this->drupalGet('test-entity/add/test_bundle'); diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php index d5de5fa..13a6534 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php @@ -53,7 +53,7 @@ function setUp() { ), ) ); - field_create_field($this->field); + $this->field = field_create_field($this->field); $this->instance = array( 'field_name' => $this->field_name, 'entity_type' => 'test_entity', @@ -141,7 +141,7 @@ function testTaxonomyTermFieldWidgets() { function testTaxonomyTermFieldChangeMachineName() { // Add several entries in the 'allowed_values' setting, to make sure that // they all get updated. - $this->field['settings']['allowed_values'] = array( + $this->field->settings['allowed_values'] = array( array( 'vocabulary' => $this->vocabulary->id(), 'parent' => '0', @@ -163,7 +163,7 @@ function testTaxonomyTermFieldChangeMachineName() { // Check that the field instance is still attached to the vocabulary. $field = field_info_field($this->field_name); - $allowed_values = $field['settings']['allowed_values']; + $allowed_values = $field->settings['allowed_values']; $this->assertEqual($allowed_values[0]['vocabulary'], $new_name, 'Index 0: Machine name was updated correctly.'); $this->assertEqual($allowed_values[1]['vocabulary'], $new_name, 'Index 1: Machine name was updated correctly.'); $this->assertEqual($allowed_values[2]['vocabulary'], 'foo', 'Index 2: Machine name was left untouched.'); diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTest.php index 93cc8d4..5e12831 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTest.php @@ -51,7 +51,7 @@ function setUp() { ); field_create_instance($this->instance); entity_get_display('node', 'article', 'default') - ->setComponent($this->instance['field_name'], array( + ->setComponent($this->instance->field_name, array( 'type' => 'taxonomy_term_reference_link', )) ->save(); @@ -109,7 +109,7 @@ function testTaxonomyNode() { $langcode = LANGUAGE_NOT_SPECIFIED; $edit["title"] = $this->randomName(); $edit["body[$langcode][0][value]"] = $this->randomName(); - $edit[$this->instance['field_name'] . '[' . $langcode . '][]'] = $term1->tid; + $edit[$this->instance->field_name . '[' . $langcode . '][]'] = $term1->tid; $this->drupalPost('node/add/article', $edit, t('Save')); // Check that the term is displayed when the node is viewed. @@ -123,7 +123,7 @@ function testTaxonomyNode() { $this->assertText($term1->name, 'Term is displayed after saving the node with no changes.'); // Edit the node with a different term. - $edit[$this->instance['field_name'] . '[' . $langcode . '][]'] = $term2->tid; + $edit[$this->instance->field_name . '[' . $langcode . '][]'] = $term2->tid; $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save')); $this->drupalGet('node/' . $node->nid); @@ -142,7 +142,7 @@ function testTaxonomyNode() { function testNodeTermCreationAndDeletion() { // Enable tags in the vocabulary. $instance = $this->instance; - $instance['widget'] = array( + $instance->widget_settings = array( 'type' => 'taxonomy_autocomplete', 'settings' => array( 'placeholder' => 'Start typing here.', @@ -162,7 +162,7 @@ function testNodeTermCreationAndDeletion() { $edit["body[$langcode][0][value]"] = $this->randomName(); // Insert the terms in a comma separated list. Vocabulary 1 is a // free-tagging field created by the default profile. - $edit[$instance['field_name'] . "[$langcode]"] = drupal_implode_tags($terms); + $edit[$instance->field_name . "[$langcode]"] = drupal_implode_tags($terms); // Verify the placeholder is there. $this->drupalGet('node/add/article'); @@ -506,7 +506,7 @@ function testTaxonomyGetTermByName() { function testReSavingTags() { // Enable tags in the vocabulary. $instance = $this->instance; - $instance['widget'] = array('type' => 'taxonomy_autocomplete'); + $instance->widget_settings = array('type' => 'taxonomy_autocomplete'); field_update_instance($instance); // Create a term and a node using it. @@ -515,7 +515,7 @@ function testReSavingTags() { $edit = array(); $edit["title"] = $this->randomName(8); $edit["body[$langcode][0][value]"] = $this->randomName(16); - $edit[$this->instance['field_name'] . '[' . $langcode . ']'] = $term->label(); + $edit[$this->instance->field_name . '[' . $langcode . ']'] = $term->label(); $this->drupalPost('node/add/article', $edit, t('Save')); // Check that the term is displayed when editing and saving the node with no diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TokenReplaceTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TokenReplaceTest.php index ea73152..69dcb5f 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TokenReplaceTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TokenReplaceTest.php @@ -77,7 +77,7 @@ function testTaxonomyTokenReplacement() { // Create node with term2. $edit = array(); $node = $this->drupalCreateNode(array('type' => 'article')); - $edit[$this->instance['field_name'] . '[' . $this->langcode . '][]'] = $term2->tid; + $edit[$this->instance->field_name . '[' . $this->langcode . '][]'] = $term2->tid; $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save')); // Generate and test sanitized tokens for term1. diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/Views/TaxonomyTestBase.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/Views/TaxonomyTestBase.php index ec91070..34d9ff8 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/Views/TaxonomyTestBase.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/Views/TaxonomyTestBase.php @@ -108,13 +108,13 @@ protected function mockStandardInstall() { field_create_instance($instance); entity_get_display('node', 'article', 'default') - ->setComponent($instance['field_name'], array( + ->setComponent($instance->field_name, array( 'type' => 'taxonomy_term_reference_link', 'weight' => 10, )) ->save(); entity_get_display('node', 'article', 'teaser') - ->setComponent($instance['field_name'], array( + ->setComponent($instance->field_name, array( 'type' => 'taxonomy_term_reference_link', 'weight' => 10, )) diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/VocabularyUnitTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/VocabularyUnitTest.php index d73302b..e969c15 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/VocabularyUnitTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/VocabularyUnitTest.php @@ -176,7 +176,7 @@ function testUninstallReinstall() { // removed when the module is uninstalled. $this->field_name = drupal_strtolower($this->randomName() . '_field_name'); $this->field = array('field_name' => $this->field_name, 'type' => 'text', 'cardinality' => 4); - $this->field = field_create_field($this->field); + field_create_field($this->field); $this->instance = array( 'field_name' => $this->field_name, 'entity_type' => 'taxonomy_term', @@ -196,7 +196,6 @@ function testUninstallReinstall() { // an instance of this field on the same bundle name should be successful. $this->vocabulary->enforceIsNew(); taxonomy_vocabulary_save($this->vocabulary); - unset($this->field['id']); field_create_field($this->field); field_create_instance($this->instance); } diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/VocabularyStorageController.php b/core/modules/taxonomy/lib/Drupal/taxonomy/VocabularyStorageController.php index b14fc2f..8cb9a58 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/VocabularyStorageController.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/VocabularyStorageController.php @@ -28,8 +28,8 @@ protected function postSave(EntityInterface $entity, $update) { $fields = field_read_fields(); foreach ($fields as $field_name => $field) { $update_field = FALSE; - if ($field['type'] == 'taxonomy_term_reference') { - foreach ($field['settings']['allowed_values'] as $key => &$value) { + if ($field->type == 'taxonomy_term_reference') { + foreach ($field->settings['allowed_values'] as $key => &$value) { if ($value['vocabulary'] == $entity->getOriginalID()) { $value['vocabulary'] = $entity->id(); $update_field = TRUE; @@ -74,14 +74,14 @@ protected function postDelete($entities) { $modified_field = FALSE; // Term reference fields may reference terms from more than one // vocabulary. - foreach ($taxonomy_field['settings']['allowed_values'] as $key => $allowed_value) { + foreach ($taxonomy_field->settings['allowed_values'] as $key => $allowed_value) { if (isset($vocabularies[$allowed_value['vocabulary']])) { - unset($taxonomy_field['settings']['allowed_values'][$key]); + unset($taxonomy_field->settings['allowed_values'][$key]); $modified_field = TRUE; } } if ($modified_field) { - if (empty($taxonomy_field['settings']['allowed_values'])) { + if (empty($taxonomy_field->settings['allowed_values'])) { field_delete_field($field_name); } else { diff --git a/core/modules/taxonomy/taxonomy.module b/core/modules/taxonomy/taxonomy.module index 4b1a2b1..a0aaa91 100644 --- a/core/modules/taxonomy/taxonomy.module +++ b/core/modules/taxonomy/taxonomy.module @@ -1004,7 +1004,7 @@ function taxonomy_field_widget_info_alter(&$info) { * Implements hook_options_list(). */ function taxonomy_options_list($field, $instance, $entity) { - $function = !empty($field['settings']['options_list_callback']) ? $field['settings']['options_list_callback'] : 'taxonomy_allowed_values'; + $function = !empty($field->settings['options_list_callback']) ? $field->settings['options_list_callback'] : 'taxonomy_allowed_values'; return $function($field, $instance, $entity); } @@ -1036,7 +1036,7 @@ function taxonomy_field_validate(EntityInterface $entity = NULL, $field, $instan $validate = TRUE; if (!empty($item['tid']) && $item['tid'] != 'autocreate') { $validate = FALSE; - foreach ($field['settings']['allowed_values'] as $settings) { + foreach ($field->settings['allowed_values'] as $settings) { // If no parent is specified, check if the term is in the vocabulary. if (isset($settings['vocabulary']) && empty($settings['parent'])) { if ($settings['vocabulary'] == $terms[$item['tid']]->bundle()) { @@ -1058,9 +1058,9 @@ function taxonomy_field_validate(EntityInterface $entity = NULL, $field, $instan } } if (!$validate) { - $errors[$field['field_name']][$langcode][$delta][] = array( + $errors[$field->field_name][$langcode][$delta][] = array( 'error' => 'taxonomy_term_reference_illegal_value', - 'message' => t('%name: illegal value.', array('%name' => $instance['label'])), + 'message' => t('%name: illegal value.', array('%name' => $instance->label)), ); } } @@ -1171,7 +1171,7 @@ function taxonomy_field_formatter_view(EntityInterface $entity, $field, $instanc */ function taxonomy_allowed_values($field, $instance, EntityInterface $entity) { $options = array(); - foreach ($field['settings']['allowed_values'] as $tree) { + foreach ($field->settings['allowed_values'] as $tree) { if ($vocabulary = taxonomy_vocabulary_load($tree['vocabulary'])) { if ($terms = taxonomy_get_tree($vocabulary->id(), $tree['parent'], NULL, TRUE)) { foreach ($terms as $term) { @@ -1273,7 +1273,7 @@ function taxonomy_field_settings_form($field, $instance, $has_data) { '#tree' => TRUE, ); - foreach ($field['settings']['allowed_values'] as $delta => $tree) { + foreach ($field->settings['allowed_values'] as $delta => $tree) { $form['allowed_values'][$delta]['vocabulary'] = array( '#type' => 'select', '#title' => t('Vocabulary'), @@ -1410,9 +1410,9 @@ function taxonomy_build_node_index($node) { // Collect a unique list of all the term IDs from all node fields. $tid_all = array(); foreach (field_info_instances('node', $node->type) as $instance) { - $field_name = $instance['field_name']; + $field_name = $instance->field_name; $field = field_info_field($field_name); - if ($field['module'] == 'taxonomy' && $field['storage']['type'] == 'field_sql_storage') { + if ($field->module == 'taxonomy' && $field->storage['type'] == 'field_sql_storage') { // If a field value is not set in the node object when node_save() is // called, the old value from $node->original is used. if (isset($node->{$field_name})) { diff --git a/core/modules/taxonomy/taxonomy.pages.inc b/core/modules/taxonomy/taxonomy.pages.inc index fbd29f1..01df0ef 100644 --- a/core/modules/taxonomy/taxonomy.pages.inc +++ b/core/modules/taxonomy/taxonomy.pages.inc @@ -109,7 +109,7 @@ function taxonomy_autocomplete($field_name) { $tags_typed = drupal_container()->get('request')->query->get('q'); // Make sure the field exists and is a taxonomy field. - if (!($field = field_info_field($field_name)) || $field['type'] !== 'taxonomy_term_reference') { + if (!($field = field_info_field($field_name)) || $field->type !== 'taxonomy_term_reference') { // Error string. The JavaScript handler will realize this is not JSON and // will display it as debugging information. print t('Taxonomy field @field_name not found.', array('@field_name' => $field_name)); @@ -125,7 +125,7 @@ function taxonomy_autocomplete($field_name) { // Part of the criteria for the query come from the field's own settings. $vids = array(); - foreach ($field['settings']['allowed_values'] as $tree) { + foreach ($field->settings['allowed_values'] as $tree) { $vids[] = $tree['vocabulary']; } diff --git a/core/modules/taxonomy/taxonomy.views.inc b/core/modules/taxonomy/taxonomy.views.inc index 0644f82..8bbe7ee 100644 --- a/core/modules/taxonomy/taxonomy.views.inc +++ b/core/modules/taxonomy/taxonomy.views.inc @@ -341,16 +341,16 @@ function taxonomy_field_views_data($field) { foreach ($table_data as $field_name => $field_data) { if (isset($field_data['filter']) && $field_name != 'delta') { $data[$table_name][$field_name]['filter']['id'] = 'taxonomy_index_tid'; - $data[$table_name][$field_name]['filter']['vocabulary'] = $field['settings']['allowed_values'][0]['vocabulary']; + $data[$table_name][$field_name]['filter']['vocabulary'] = $field->settings['allowed_values'][0]['vocabulary']; } } // Add the relationship only on the tid field. - $data[$table_name][$field['field_name'] . '_tid']['relationship'] = array( + $data[$table_name][$field->field_name . '_tid']['relationship'] = array( 'id' => 'standard', 'base' => 'taxonomy_term_data', 'base field' => 'tid', - 'label' => t('term from !field_name', array('!field_name' => $field['field_name'])), + 'label' => t('term from !field_name', array('!field_name' => $field->field_name)), ); } @@ -364,11 +364,11 @@ function taxonomy_field_views_data($field) { * Views integration to provide reverse relationships on term references. */ function taxonomy_field_views_data_views_data_alter(&$data, $field) { - foreach ($field['bundles'] as $entity_type => $bundles) { + foreach ($field->bundles as $entity_type => $bundles) { $entity_info = entity_get_info($entity_type); - $pseudo_field_name = 'reverse_' . $field['field_name'] . '_' . $entity_type; + $pseudo_field_name = 'reverse_' . $field->field_name . '_' . $entity_type; - list($label, $all_labels) = field_views_field_label($field['field_name']); + list($label, $all_labels) = field_views_field_label($field->field_name); $entity = $entity_info['label']; if ($entity == t('Node')) { $entity = t('Content'); @@ -378,12 +378,12 @@ function taxonomy_field_views_data_views_data_alter(&$data, $field) { 'title' => t('@entity using @field', array('@entity' => $entity, '@field' => $label)), 'help' => t('Relate each @entity with a @field set to the term.', array('@entity' => $entity, '@field' => $label)), 'id' => 'entity_reverse', - 'field_name' => $field['field_name'], + 'field_name' => $field->field_name, 'field table' => _field_sql_storage_tablename($field), - 'field field' => $field['field_name'] . '_tid', + 'field field' => $field->field_name . '_tid', 'base' => $entity_info['base_table'], 'base field' => $entity_info['entity_keys']['id'], - 'label' => t('!field_name', array('!field_name' => $field['field_name'])), + 'label' => t('!field_name', array('!field_name' => $field->field_name)), 'join_extra' => array( 0 => array( 'field' => 'entity_type', diff --git a/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextTrimmedFormatter.php b/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextTrimmedFormatter.php index 37709b5..16433fd 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextTrimmedFormatter.php +++ b/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextTrimmedFormatter.php @@ -74,7 +74,7 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) { } else { $output = _text_sanitize($this->instance, $langcode, $item, 'value'); - $output = text_summary($output, $this->instance['settings']['text_processing'] ? $item['format'] : NULL, $this->getSetting('trim_length')); + $output = text_summary($output, $this->instance->settings['text_processing'] ? $item['format'] : NULL, $this->getSetting('trim_length')); } $elements[$delta] = array('#markup' => $output); } diff --git a/core/modules/text/lib/Drupal/text/Plugin/field/widget/TextareaWidget.php b/core/modules/text/lib/Drupal/text/Plugin/field/widget/TextareaWidget.php index 1ab4184..43d8042 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/field/widget/TextareaWidget.php +++ b/core/modules/text/lib/Drupal/text/Plugin/field/widget/TextareaWidget.php @@ -61,7 +61,7 @@ public function formElement(array $items, $delta, array $element, $langcode, arr '#attributes' => array('class' => array('text-full')), ); - if ($this->instance['settings']['text_processing']) { + if ($this->instance->settings['text_processing']) { $element = $main_widget; $element['#type'] = 'text_format'; $element['#format'] = isset($items[$delta]['format']) ? $items[$delta]['format'] : NULL; diff --git a/core/modules/text/lib/Drupal/text/Plugin/field/widget/TextareaWithSummaryWidget.php b/core/modules/text/lib/Drupal/text/Plugin/field/widget/TextareaWithSummaryWidget.php index 6982902..9a72dff 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/field/widget/TextareaWithSummaryWidget.php +++ b/core/modules/text/lib/Drupal/text/Plugin/field/widget/TextareaWithSummaryWidget.php @@ -35,7 +35,7 @@ class TextareaWithSummaryWidget extends TextareaWidget { function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) { $element = parent::formElement($items, $delta, $element, $langcode, $form, $form_state); - $display_summary = !empty($items[$delta]['summary']) || $this->instance['settings']['display_summary']; + $display_summary = !empty($items[$delta]['summary']) || $this->instance->settings['display_summary']; $element['summary'] = array( '#type' => $display_summary ? 'textarea' : 'value', '#default_value' => isset($items[$delta]['summary']) ? $items[$delta]['summary'] : NULL, diff --git a/core/modules/text/lib/Drupal/text/Plugin/field/widget/TextfieldWidget.php b/core/modules/text/lib/Drupal/text/Plugin/field/widget/TextfieldWidget.php index 00396ae..19a12bd 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/field/widget/TextfieldWidget.php +++ b/core/modules/text/lib/Drupal/text/Plugin/field/widget/TextfieldWidget.php @@ -58,11 +58,11 @@ public function formElement(array $items, $delta, array $element, $langcode, arr '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL, '#size' => $this->getSetting('size'), '#placeholder' => $this->getSetting('placeholder'), - '#maxlength' => $this->field['settings']['max_length'], + '#maxlength' => $this->field->settings['max_length'], '#attributes' => array('class' => array('text-full')), ); - if ($this->instance['settings']['text_processing']) { + if ($this->instance->settings['text_processing']) { $element = $main_widget; $element['#type'] = 'text_format'; $element['#format'] = isset($items[$delta]['format']) ? $items[$delta]['format'] : NULL; diff --git a/core/modules/text/lib/Drupal/text/Tests/TextFieldTest.php b/core/modules/text/lib/Drupal/text/Tests/TextFieldTest.php index f8c585b..d6e6eeb 100644 --- a/core/modules/text/lib/Drupal/text/Tests/TextFieldTest.php +++ b/core/modules/text/lib/Drupal/text/Tests/TextFieldTest.php @@ -57,9 +57,9 @@ function testTextFieldValidation() { 'max_length' => $max_length, ) ); - field_create_field($this->field); + $this->field = field_create_field($this->field); $this->instance = array( - 'field_name' => $this->field['field_name'], + 'field_name' => $this->field->field_name, 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', 'widget' => array( @@ -68,14 +68,14 @@ function testTextFieldValidation() { ); field_create_instance($this->instance); entity_get_display('test_entity', 'test_bundle', 'default') - ->setComponent($this->field['field_name']) + ->setComponent($this->field->field_name) ->save(); // Test valid and invalid values with field_attach_validate(). $entity = field_test_create_entity(); $langcode = LANGUAGE_NOT_SPECIFIED; for ($i = 0; $i <= $max_length + 2; $i++) { - $entity->{$this->field['field_name']}[$langcode][0]['value'] = str_repeat('x', $i); + $entity->{$this->field->field_name}[$langcode][0]['value'] = str_repeat('x', $i); try { field_attach_validate($entity); $this->assertTrue($i <= $max_length, "Length $i does not cause validation error when max_length is $max_length"); @@ -102,7 +102,7 @@ function _testTextfieldWidgets($field_type, $widget_type) { $entity_type = 'test_entity'; $this->field_name = drupal_strtolower($this->randomName()); $this->field = array('field_name' => $this->field_name, 'type' => $field_type); - field_create_field($this->field); + $this->field = field_create_field($this->field); $this->instance = array( 'field_name' => $this->field_name, 'entity_type' => 'test_entity', @@ -165,7 +165,7 @@ function _testTextfieldWidgetsFormatted($field_type, $widget_type) { $entity_type = 'test_entity'; $this->field_name = drupal_strtolower($this->randomName()); $this->field = array('field_name' => $this->field_name, 'type' => $field_type); - field_create_field($this->field); + $this->field = field_create_field($this->field); $this->instance = array( 'field_name' => $this->field_name, 'entity_type' => 'test_entity', diff --git a/core/modules/text/lib/Drupal/text/TextProcessed.php b/core/modules/text/lib/Drupal/text/TextProcessed.php index c73af6f..d1ffd05 100644 --- a/core/modules/text/lib/Drupal/text/TextProcessed.php +++ b/core/modules/text/lib/Drupal/text/TextProcessed.php @@ -69,7 +69,7 @@ public function getValue($langcode = NULL) { $entity = $field->getParent(); $instance = field_info_instance($entity->entityType(), $field->getName(), $entity->bundle()); - if (!empty($instance['settings']['text_processing']) && $this->format->getValue()) { + if (!empty($instance->settings['text_processing']) && $this->format->getValue()) { return check_markup($this->text->getValue(), $this->format->getValue(), $entity->language()->langcode); } else { diff --git a/core/modules/text/text.install b/core/modules/text/text.install index 72d879f..d87d7f7 100644 --- a/core/modules/text/text.install +++ b/core/modules/text/text.install @@ -9,12 +9,12 @@ * Implements hook_field_schema(). */ function text_field_schema($field) { - switch ($field['type']) { + switch ($field->type) { case 'text': $columns = array( 'value' => array( 'type' => 'varchar', - 'length' => $field['settings']['max_length'], + 'length' => $field->settings['max_length'], 'not null' => FALSE, ), ); diff --git a/core/modules/text/text.module b/core/modules/text/text.module index 07c962e..9adeb73 100644 --- a/core/modules/text/text.module +++ b/core/modules/text/text.module @@ -85,11 +85,11 @@ function text_field_info() { * Implements hook_field_settings_form(). */ function text_field_settings_form($field, $instance, $has_data) { - $settings = $field['settings']; + $settings = $field->settings; $form = array(); - if ($field['type'] == 'text') { + if ($field->type == 'text') { $form['max_length'] = array( '#type' => 'number', '#title' => t('Maximum length'), @@ -110,7 +110,7 @@ function text_field_settings_form($field, $instance, $has_data) { * Implements hook_field_instance_settings_form(). */ function text_field_instance_settings_form($field, $instance) { - $settings = $instance['settings']; + $settings = $instance->settings; $form['text_processing'] = array( '#type' => 'radios', @@ -121,7 +121,7 @@ function text_field_instance_settings_form($field, $instance) { t('Filtered text (user selects text format)'), ), ); - if ($field['type'] == 'text_with_summary') { + if ($field->type == 'text_with_summary') { $form['display_summary'] = array( '#type' => 'checkbox', '#title' => t('Summary input'), @@ -146,17 +146,17 @@ function text_field_validate(EntityInterface $entity = NULL, $field, $instance, // length can be exceeded very easily. foreach (array('value', 'summary') as $column) { if (!empty($item[$column])) { - if (!empty($field['settings']['max_length']) && drupal_strlen($item[$column]) > $field['settings']['max_length']) { + if (!empty($field->settings['max_length']) && drupal_strlen($item[$column]) > $field->settings['max_length']) { switch ($column) { case 'value': - $message = t('%name: the text may not be longer than %max characters.', array('%name' => $instance['label'], '%max' => $field['settings']['max_length'])); + $message = t('%name: the text may not be longer than %max characters.', array('%name' => $instance->label, '%max' => $field->settings['max_length'])); break; case 'summary': - $message = t('%name: the summary may not be longer than %max characters.', array('%name' => $instance['label'], '%max' => $field['settings']['max_length'])); + $message = t('%name: the summary may not be longer than %max characters.', array('%name' => $instance->label, '%max' => $field->settings['max_length'])); break; } - $errors[$field['field_name']][$langcode][$delta][] = array( + $errors[$field->field_name][$langcode][$delta][] = array( 'error' => "text_{$column}_length", 'message' => $message, ); @@ -180,7 +180,7 @@ function text_field_load($entity_type, $entities, $field, $instances, $langcode, // by formatters if needed. if (empty($instances[$id]['settings']['text_processing']) || filter_format_allowcache($item['format'])) { $items[$id][$delta]['safe_value'] = isset($item['value']) ? _text_sanitize($instances[$id], $langcode, $item, 'value') : ''; - if ($field['type'] == 'text_with_summary') { + if ($field->type == 'text_with_summary') { $items[$id][$delta]['safe_summary'] = isset($item['summary']) ? _text_sanitize($instances[$id], $langcode, $item, 'summary') : ''; } } @@ -222,7 +222,7 @@ function _text_sanitize($instance, $langcode, $item, $column) { if (isset($item["safe_$column"])) { return $item["safe_$column"]; } - return $instance['settings']['text_processing'] ? check_markup($item[$column], $item['format'], $langcode) : check_plain($item[$column]); + return $instance->settings['text_processing'] ? check_markup($item[$column], $item['format'], $langcode) : check_plain($item[$column]); } /** @@ -352,7 +352,7 @@ function text_summary($text, $format = NULL, $size = NULL) { function text_field_prepare_translation(EntityInterface $entity, $field, $instance, $langcode, &$items, EntityInterface $source_entity, $source_langcode) { // If the translating user is not permitted to use the assigned text format, // we must not expose the source values. - $field_name = $field['field_name']; + $field_name = $field->field_name; if (!empty($source_entity->{$field_name}[$source_langcode])) { $formats = filter_formats(); foreach ($source_entity->{$field_name}[$source_langcode] as $delta => $item) { diff --git a/core/modules/translation_entity/lib/Drupal/translation_entity/EntityTranslationController.php b/core/modules/translation_entity/lib/Drupal/translation_entity/EntityTranslationController.php index 1dcbc35..f9ea06c 100644 --- a/core/modules/translation_entity/lib/Drupal/translation_entity/EntityTranslationController.php +++ b/core/modules/translation_entity/lib/Drupal/translation_entity/EntityTranslationController.php @@ -49,9 +49,9 @@ public function removeTranslation(EntityInterface $entity, $langcode) { // @todo Handle properties. // Remove field translations. foreach (field_info_instances($entity->entityType(), $entity->bundle()) as $instance) { - $field_name = $instance['field_name']; + $field_name = $instance->field_name; $field = field_info_field($field_name); - if ($field['translatable']) { + if ($field->translatable) { $entity->{$field_name}[$langcode] = array(); } } diff --git a/core/modules/translation_entity/lib/Drupal/translation_entity/Tests/EntityTranslationSettingsTest.php b/core/modules/translation_entity/lib/Drupal/translation_entity/Tests/EntityTranslationSettingsTest.php index 41600ac..9a95330 100644 --- a/core/modules/translation_entity/lib/Drupal/translation_entity/Tests/EntityTranslationSettingsTest.php +++ b/core/modules/translation_entity/lib/Drupal/translation_entity/Tests/EntityTranslationSettingsTest.php @@ -88,7 +88,7 @@ function testSettingsUI() { ); $this->assertSettings('comment', 'comment_node_article', TRUE, $edit); $field = field_info_field('comment_body'); - $this->assertTrue($field['translatable'], 'Comment body is translatable.'); + $this->assertTrue($field->translatable, 'Comment body is translatable.'); // Test that language settings are correctly stored. $language_configuration = language_get_default_configuration('comment', 'comment_node_article'); diff --git a/core/modules/translation_entity/translation_entity.admin.inc b/core/modules/translation_entity/translation_entity.admin.inc index aca0dd0..cfc3e4d 100644 --- a/core/modules/translation_entity/translation_entity.admin.inc +++ b/core/modules/translation_entity/translation_entity.admin.inc @@ -44,9 +44,9 @@ function _translation_entity_form_language_content_settings_form_alter(array &$f foreach (field_info_instances($entity_type, $bundle) as $field_name => $instance) { $field = field_info_field($field_name); $form['settings'][$entity_type][$bundle]['fields'][$field_name] = array( - '#label' => $instance['label'], + '#label' => $instance->label, '#type' => 'checkbox', - '#default_value' => $field['translatable'], + '#default_value' => $field->translatable, ); } } @@ -150,7 +150,7 @@ function _translation_entity_update_field_translatability($settings) { $operations = array(); foreach ($fields as $field_name => $translatable) { $field = field_info_field($field_name); - if ($field['translatable'] != $translatable) { + if ($field->translatable != $translatable) { // If a field is untranslatable, it can have no data except under // LANGUAGE_NOT_SPECIFIED. Thus we need a field to be translatable before // we convert data to the entity language. Conversely we need to switch @@ -187,7 +187,7 @@ function translation_entity_translatable_form(array $form, array &$form_state, $ $t_args = array('%name' => $field_name); $warning = t('By submitting this form these changes will apply to the %name field everywhere it is used.', $t_args); - if ($field['translatable']) { + if ($field->translatable) { $title = t('Are you sure you want to disable translation for the %name field?', $t_args); $warning .= "
" . t("All the existing translations of this field will be deleted.
This action cannot be undone."); } @@ -202,7 +202,7 @@ function translation_entity_translatable_form(array $form, array &$form_state, $ // submits from toggling translatability. $form['translatable'] = array( '#type' => 'hidden', - '#default_value' => $field['translatable'], + '#default_value' => $field->translatable, ); return confirm_form($form, $title, '', $warning); @@ -226,7 +226,7 @@ function translation_entity_translatable_form_submit(array $form, array $form_st $field_name = $form_state['field']['field_name']; $field = field_info_field($field_name); - if ($field['translatable'] !== $translatable) { + if ($field->translatable !== $translatable) { // Field translatability has changed since form creation, abort. $t_args = array('%field_name'); $msg = $translatable ? @@ -273,8 +273,8 @@ function translation_entity_translatable_form_submit(array $form, array $form_st */ function translation_entity_translatable_switch($translatable, $field_name) { $field = field_info_field($field_name); - if ($field['translatable'] !== $translatable) { - $field['translatable'] = $translatable; + if ($field->translatable !== $translatable) { + $field->translatable = $translatable; field_update_field($field); } } @@ -290,7 +290,7 @@ function translation_entity_translatable_switch($translatable, $field_name) { */ function translation_entity_translatable_batch($translatable, $field_name, &$context) { $field = field_info_field($field_name); - $column = isset($field['columns']['value']) ? 'value' : key($field['columns']); + $column = isset($field->columns['value']) ? 'value' : key($field->columns); $query_field = "$field_name.$column"; // Determine the entity types to act on. diff --git a/core/modules/translation_entity/translation_entity.module b/core/modules/translation_entity/translation_entity.module index 0167180..c159528 100644 --- a/core/modules/translation_entity/translation_entity.module +++ b/core/modules/translation_entity/translation_entity.module @@ -606,9 +606,9 @@ function translation_entity_form_alter(array &$form, array &$form_state) { } else { foreach (field_info_instances($entity->entityType(), $entity->bundle()) as $instance) { - $field_name = $instance['field_name']; + $field_name = $instance->field_name; $field = field_info_field($field_name); - $form[$field_name]['#multilingual'] = !empty($field['translatable']); + $form[$field_name]['#multilingual'] = !empty($field->translatable); } } } @@ -735,8 +735,8 @@ function translation_entity_field_extra_fields() { */ function translation_entity_form_field_ui_field_settings_form_alter(array &$form, array &$form_state, $form_id) { $field = $form['#field']; - $field_name = $field['field_name']; - $translatable = $field['translatable']; + $field_name = $field->field_name; + $translatable = $field->translatable; $label = t('Field translation'); if (field_has_data($field)) { diff --git a/core/modules/translation_entity/translation_entity.pages.inc b/core/modules/translation_entity/translation_entity.pages.inc index 11e85e6..83e13ce 100644 --- a/core/modules/translation_entity/translation_entity.pages.inc +++ b/core/modules/translation_entity/translation_entity.pages.inc @@ -39,9 +39,9 @@ function translation_entity_overview(EntityInterface $entity) { // Determine whether the current entity is translatable. $translatable = FALSE; foreach (field_info_instances($entity->entityType(), $entity->bundle()) as $instance) { - $field_name = $instance['field_name']; + $field_name = $instance->field_name; $field = field_info_field($field_name); - if ($field['translatable']) { + if ($field->translatable) { $translatable = TRUE; break; } @@ -255,7 +255,7 @@ function translation_entity_prepare_translation(EntityInterface $entity, Languag else { foreach ($instances as $field_name => $instance) { $field = field_info_field($field_name); - if (!empty($field['translatable'])) { + if (!empty($field->translatable)) { $value = $entity->get($field_name); $value[$target->langcode] = isset($value[$source->langcode]) ? $value[$source->langcode] : array(); $entity->set($field_name, $value); diff --git a/core/modules/user/lib/Drupal/user/Plugin/entity_reference/selection/UserSelection.php b/core/modules/user/lib/Drupal/user/Plugin/entity_reference/selection/UserSelection.php index 865b564..e52193e 100644 --- a/core/modules/user/lib/Drupal/user/Plugin/entity_reference/selection/UserSelection.php +++ b/core/modules/user/lib/Drupal/user/Plugin/entity_reference/selection/UserSelection.php @@ -34,7 +34,7 @@ public static function settingsForm(&$field, &$instance) { $form = parent::settingsForm($field, $instance); // Merge in default values. - $instance['settings']['handler_settings'] += array( + $instance->settings['handler_settings'] += array( 'filter' => array( 'type' => '_none', ), @@ -50,7 +50,7 @@ public static function settingsForm(&$field, &$instance) { ), '#ajax' => TRUE, '#limit_validation_errors' => array(), - '#default_value' => $instance['settings']['handler_settings']['filter']['type'], + '#default_value' => $instance->settings['handler_settings']['filter']['type'], ); $form['filter']['settings'] = array( @@ -59,9 +59,9 @@ public static function settingsForm(&$field, &$instance) { '#process' => array('_entity_reference_form_process_merge_parent'), ); - if ($instance['settings']['handler_settings']['filter']['type'] == 'role') { + if ($instance->settings['handler_settings']['filter']['type'] == 'role') { // Merge in default values. - $instance['settings']['handler_settings']['filter'] += array( + $instance->settings['handler_settings']['filter'] += array( 'role' => NULL, ); @@ -70,7 +70,7 @@ public static function settingsForm(&$field, &$instance) { '#title' => t('Restrict to the selected roles'), '#required' => TRUE, '#options' => array_diff_key(user_role_names(TRUE), drupal_map_assoc(array(DRUPAL_AUTHENTICATED_RID))), - '#default_value' => $instance['settings']['handler_settings']['filter']['role'], + '#default_value' => $instance->settings['handler_settings']['filter']['role'], ); } @@ -133,8 +133,8 @@ public function entityQueryAlter(SelectInterface $query) { } // Add the filter by role option. - if (!empty($this->instance['settings']['handler_settings']['filter'])) { - $filter_settings = $this->instance['settings']['handler_settings']['filter']; + if (!empty($this->instance->settings['handler_settings']['filter'])) { + $filter_settings = $this->instance->settings['handler_settings']['filter']; if ($filter_settings['type'] == 'role') { $tables = $query->getTables(); $base_table = $tables['base_table']['alias']; diff --git a/core/modules/user/lib/Drupal/user/RegisterFormController.php b/core/modules/user/lib/Drupal/user/RegisterFormController.php index 2a84412..b1109c2 100644 --- a/core/modules/user/lib/Drupal/user/RegisterFormController.php +++ b/core/modules/user/lib/Drupal/user/RegisterFormController.php @@ -45,7 +45,7 @@ public function form(array $form, array &$form_state, EntityInterface $account) // setting is not on. field_attach_form($account, $form, $form_state); foreach (field_info_instances('user', 'user') as $field_name => $instance) { - if (empty($instance['settings']['user_register_form'])) { + if (empty($instance->settings['user_register_form'])) { $form[$field_name]['#access'] = FALSE; } } diff --git a/core/modules/user/lib/Drupal/user/Tests/UserRegistrationTest.php b/core/modules/user/lib/Drupal/user/Tests/UserRegistrationTest.php index 38ca376..53ee027 100644 --- a/core/modules/user/lib/Drupal/user/Tests/UserRegistrationTest.php +++ b/core/modules/user/lib/Drupal/user/Tests/UserRegistrationTest.php @@ -199,7 +199,7 @@ function testRegistrationWithUserFields() { 'field_name' => 'test_user_field', 'cardinality' => 1, ); - field_create_field($field); + $field = field_create_field($field); $instance = array( 'field_name' => 'test_user_field', 'entity_type' => 'user', @@ -212,13 +212,13 @@ function testRegistrationWithUserFields() { // Check that the field does not appear on the registration form. $this->drupalGet('user/register'); - $this->assertNoText($instance['label'], 'The field does not appear on user registration form'); + $this->assertNoText($instance->label, 'The field does not appear on user registration form'); // Have the field appear on the registration form. - $instance['settings']['user_register_form'] = TRUE; + $instance->settings['user_register_form'] = TRUE; field_update_instance($instance); $this->drupalGet('user/register'); - $this->assertText($instance['label'], 'The field appears on user registration form'); + $this->assertText($instance->label, 'The field appears on user registration form'); // Check that validation errors are correctly reported. $edit = array(); @@ -227,11 +227,11 @@ function testRegistrationWithUserFields() { // Missing input in required field. $edit['test_user_field[und][0][value]'] = ''; $this->drupalPost(NULL, $edit, t('Create new account')); - $this->assertRaw(t('@name field is required.', array('@name' => $instance['label'])), 'Field validation error was correctly reported.'); + $this->assertRaw(t('@name field is required.', array('@name' => $instance->label)), 'Field validation error was correctly reported.'); // Invalid input. $edit['test_user_field[und][0][value]'] = '-1'; $this->drupalPost(NULL, $edit, t('Create new account')); - $this->assertRaw(t('%name does not accept the value -1.', array('%name' => $instance['label'])), 'Field validation error was correctly reported.'); + $this->assertRaw(t('%name does not accept the value -1.', array('%name' => $instance->label)), 'Field validation error was correctly reported.'); // Submit with valid data. $value = rand(1, 255); @@ -243,7 +243,7 @@ function testRegistrationWithUserFields() { $this->assertEqual($new_user->test_user_field[LANGUAGE_NOT_SPECIFIED][0]['value'], $value, 'The field value was correclty saved.'); // Check that the 'add more' button works. - $field['cardinality'] = FIELD_CARDINALITY_UNLIMITED; + $field->cardinality = FIELD_CARDINALITY_UNLIMITED; field_update_field($field); foreach (array('js', 'nojs') as $js) { $this->drupalGet('user/register'); diff --git a/core/modules/user/user.install b/core/modules/user/user.install index 9a0b663..afee54b 100644 --- a/core/modules/user/user.install +++ b/core/modules/user/user.install @@ -315,10 +315,6 @@ function user_install_picture_field() { 'uri_scheme' => 'public', 'default_image' => FALSE, ), - 'storage' => array( - 'type' => 'field_sql_storage', - 'settings' => array(), - ), ); $field = field_create_field($field); @@ -706,7 +702,7 @@ function user_update_8011() { 'settings' => array(), ), ); - _update_7000_field_create_field($field); + $field = _update_7000_field_create_field($field); $instance = array( 'field_name' => 'user_picture', diff --git a/core/modules/user/user.module b/core/modules/user/user.module index 6a9fb78..b4f5c2f 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -2537,7 +2537,7 @@ function user_block_user_action(&$entity, $context = array()) { function user_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) { $instance = $form['#instance']; - if ($instance['entity_type'] == 'user') { + if ($instance->entity_type == 'user') { $form['instance']['settings']['user_register_form'] = array( '#type' => 'checkbox', '#title' => t('Display on user registration form.'), @@ -2545,7 +2545,7 @@ function user_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id // Field instances created in D7 beta releases before the setting was // introduced might be set as 'required' and 'not shown on user_register // form'. We make sure the checkbox comes as 'checked' for those. - '#default_value' => $instance['settings']['user_register_form'] || $instance['required'], + '#default_value' => $instance->settings['user_register_form'] || $instance->required, // Display just below the 'required' checkbox. '#weight' => $form['instance']['required']['#weight'] + .1, // Disabled when the 'required' checkbox is checked. @@ -2572,7 +2572,7 @@ function user_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id function user_form_field_ui_field_edit_form_submit($form, &$form_state) { $instance = $form_state['values']['instance']; - if (!empty($instance['required'])) { + if (!empty($instance->required)) { form_set_value($form['instance']['settings']['user_register_form'], 1, $form_state); } } diff --git a/core/modules/views/lib/Drupal/views/Plugin/entity_reference/selection/ViewsSelection.php b/core/modules/views/lib/Drupal/views/Plugin/entity_reference/selection/ViewsSelection.php index 5a6b770..d3d8af0 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/entity_reference/selection/ViewsSelection.php +++ b/core/modules/views/lib/Drupal/views/Plugin/entity_reference/selection/ViewsSelection.php @@ -46,11 +46,11 @@ public function __construct($field, $instance = NULL, EntityInterface $entity = * Implements \Drupal\entity_reference\Plugin\Type\Selection\SelectionInterface::settingsForm(). */ public static function settingsForm(&$field, &$instance) { - $view_settings = empty($instance['settings']['handler_settings']['view']) ? array() : $instance['settings']['handler_settings']['view']; + $view_settings = empty($instance->settings['handler_settings']['view']) ? array() : $instance->settings['handler_settings']['view']; $displays = views_get_applicable_views('entity_reference_display'); // Filter views that list the entity type we want, and group the separate // displays by view. - $entity_info = entity_get_info($field['settings']['target_type']); + $entity_info = entity_get_info($field->settings['target_type']); $options = array(); foreach ($displays as $data) { list($view, $display_id) = $data; @@ -117,13 +117,13 @@ public static function settingsForm(&$field, &$instance) { * Return TRUE if the view was initialized, FALSE otherwise. */ protected function initializeView($match = NULL, $match_operator = 'CONTAINS', $limit = 0, $ids = NULL) { - $view_name = $this->instance['settings']['handler_settings']['view']['view_name']; - $display_name = $this->instance['settings']['handler_settings']['view']['display_name']; + $view_name = $this->instance->settings['handler_settings']['view']['view_name']; + $display_name = $this->instance->settings['handler_settings']['view']['display_name']; // Check that the view is valid and the display still exists. $this->view = views_get_view($view_name); if (!$this->view || !$this->view->access($display_name)) { - drupal_set_message(t('The reference view %view_name used in the %field_name field cannot be found.', array('%view_name' => $view_name, '%field_name' => $this->instance['label'])), 'warning'); + drupal_set_message(t('The reference view %view_name used in the %field_name field cannot be found.', array('%view_name' => $view_name, '%field_name' => $this->instance->label)), 'warning'); return FALSE; } $this->view->setDisplay($display_name); @@ -143,8 +143,8 @@ protected function initializeView($match = NULL, $match_operator = 'CONTAINS', $ * Implements \Drupal\entity_reference\Plugin\Type\Selection\SelectionInterface::getReferencableEntities(). */ public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) { - $display_name = $this->instance['settings']['handler_settings']['view']['display_name']; - $arguments = $this->instance['settings']['handler_settings']['view']['arguments']; + $display_name = $this->instance->settings['handler_settings']['view']['display_name']; + $arguments = $this->instance->settings['handler_settings']['view']['arguments']; $result = array(); if ($this->initializeView($match, $match_operator, $limit)) { // Get the results. @@ -173,8 +173,8 @@ public function countReferencableEntities($match = NULL, $match_operator = 'CONT * Implements \Drupal\entity_reference\Plugin\Type\Selection\SelectionInterface::validateReferencableEntities(). */ public function validateReferencableEntities(array $ids) { - $display_name = $this->instance['settings']['handler_settings']['view']['display_name']; - $arguments = $this->instance['settings']['handler_settings']['view']['arguments']; + $display_name = $this->instance->settings['handler_settings']['view']['display_name']; + $arguments = $this->instance->settings['handler_settings']['view']['arguments']; $result = array(); if ($this->initializeView(NULL, 'CONTAINS', 0, $ids)) { // Get the results. diff --git a/core/modules/views/lib/Drupal/views/Tests/Wizard/TaggedWithTest.php b/core/modules/views/lib/Drupal/views/Tests/Wizard/TaggedWithTest.php index 6416193..a2c2817 100644 --- a/core/modules/views/lib/Drupal/views/Tests/Wizard/TaggedWithTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/Wizard/TaggedWithTest.php @@ -66,7 +66,7 @@ function setUp() { ), ), ); - field_create_field($this->tag_field); + $this->tag_field = field_create_field($this->tag_field); // Create an instance of the tag field on one of the content types, and // configure it to display an autocomplete widget. @@ -103,7 +103,7 @@ function testTaggedWith() { $node_add_path = 'node/add/' . $this->node_type_with_tags->type; // Create three nodes, with different tags. - $tag_field = $this->tag_field['field_name'] . '[' . LANGUAGE_NOT_SPECIFIED . ']'; + $tag_field = $this->tag_field->field_name . '[' . LANGUAGE_NOT_SPECIFIED . ']'; $edit = array(); $edit['title'] = $node_tag1_title = $this->randomName(); $edit[$tag_field] = 'tag1'; @@ -183,7 +183,7 @@ function testTaggedWithByNodeType() { // If we add an instance of the tagging field to the second node type, the // "tagged with" form element should not appear for it too. $instance = $this->tag_instance; - $instance['bundle'] = $this->node_type_without_tags->type; + $instance->bundle = $this->node_type_without_tags->type; field_create_instance($instance); $view['show[type]'] = $this->node_type_with_tags->type; $this->drupalPost('admin/structure/views/add', $view, t('Update "of type" choice')); diff --git a/core/modules/views/views_ui/admin.inc b/core/modules/views/views_ui/admin.inc index 4014831..ec4b749 100644 --- a/core/modules/views/views_ui/admin.inc +++ b/core/modules/views/views_ui/admin.inc @@ -255,8 +255,8 @@ function views_ui_taxonomy_autocomplete_validate($element, &$form_state) { // vocabulary IDs. $field = field_info_field($element['#field_name']); $vocabularies = array(); - if (!empty($field['settings']['allowed_values'])) { - foreach ($field['settings']['allowed_values'] as $tree) { + if (!empty($field->settings['allowed_values'])) { + foreach ($field->settings['allowed_values'] as $tree) { if ($vocabulary = entity_load('taxonomy_vocabulary', $tree['vocabulary'])) { $vocabularies[$vocabulary->id()] = $tree['vocabulary']; } diff --git a/core/profiles/standard/standard.install b/core/profiles/standard/standard.install index 8802bbc..d78cbc0 100644 --- a/core/profiles/standard/standard.install +++ b/core/profiles/standard/standard.install @@ -125,7 +125,7 @@ function standard_install() { 'label' => 'Tags', 'bundle' => 'article', 'description' => $vocabulary->help, - 'widget' => array( + 'widget_settings' => array( 'type' => 'taxonomy_autocomplete', 'weight' => -4, ), @@ -134,13 +134,13 @@ function standard_install() { // Assign display settings for the 'default' and 'teaser' view modes. entity_get_display('node', 'article', 'default') - ->setComponent($instance['field_name'], array( + ->setComponent($instance->field_name, array( 'type' => 'taxonomy_term_reference_link', 'weight' => 10, )) ->save(); entity_get_display('node', 'article', 'teaser') - ->setComponent($instance['field_name'], array( + ->setComponent($instance->field_name, array( 'type' => 'taxonomy_term_reference_link', 'weight' => 10, )) @@ -155,11 +155,11 @@ function standard_install() { 'type' => 'image', 'cardinality' => 1, 'locked' => FALSE, - 'indexes' => array('fid' => array('fid')), 'settings' => array( 'uri_scheme' => 'public', 'default_image' => FALSE, ), + 'indexes' => array('fid' => array('fid')), 'storage' => array( 'type' => 'field_sql_storage', 'settings' => array(), @@ -188,7 +188,7 @@ function standard_install() { 'title_field' => '', ), - 'widget' => array( + 'widget_settings' => array( 'type' => 'image_image', 'settings' => array( 'progress_indicator' => 'throbber', @@ -201,7 +201,7 @@ function standard_install() { // Assign display settings for the 'default' and 'teaser' view modes. entity_get_display('node', 'article', 'default') - ->setComponent($instance['field_name'], array( + ->setComponent($instance->field_name, array( 'label' => 'hidden', 'type' => 'image', 'settings' => array('image_style' => 'large', 'image_link' => ''), @@ -209,7 +209,7 @@ function standard_install() { )) ->save(); entity_get_display('node', 'article', 'teaser') - ->setComponent($instance['field_name'], array( + ->setComponent($instance->field_name, array( 'label' => 'hidden', 'type' => 'image', 'settings' => array('image_style' => 'medium', 'image_link' => 'content'), diff --git a/core/scripts/generate-d7-content.sh b/core/scripts/generate-d7-content.sh index a19d329..85e767f 100644 --- a/core/scripts/generate-d7-content.sh +++ b/core/scripts/generate-d7-content.sh @@ -98,7 +98,7 @@ foreach ($node_types as $bundle) { $instance = array( 'label' => $vocabulary->name, - 'field_name' => $field['field_name'], + 'field_name' => $field->field_name, 'bundle' => $bundle, 'entity_type' => 'node', 'settings' => array(), @@ -117,7 +117,7 @@ ), ); if ($vocabulary->tags) { - $instance['widget'] = array( + $instance->widget_settings = array( 'type' => 'taxonomy_autocomplete', 'module' => 'taxonomy', 'settings' => array( @@ -127,7 +127,7 @@ ); } else { - $instance['widget'] = array( + $instance->widget_settings = array( 'type' => 'options_select', 'settings' => array(), );