diff --git a/core/includes/entity.inc b/core/includes/entity.inc index 708706e..b8dfbd6 100644 --- a/core/includes/entity.inc +++ b/core/includes/entity.inc @@ -718,6 +718,66 @@ function entity_get_render_display(EntityInterface $entity, $view_mode) { } /** + * Returns the entity_form_display object associated to a bundle and form mode. + * + * Use this function when assigning suggested widget options for a component + * in a given form mode. Note that they will only be actually used at render + * time if the form mode itself is configured to use dedicated widget settings + * for the bundle; if not, the 'default' form is used instead. + * + * The function reads the entity_form_display object from the current + * configuration, or returns a ready-to-use empty one if configuration entry + * exists yet for this bundle and form mode. This streamlines manipulation of + * EntityFormDisplay objects by always returning a consistent object that + * reflects the current state of the configuration. + * + * Example usage: + * - Set the 'body' field to be displayed and the 'field_image' field to be + * hidden on article nodes in the 'default' form mode. + * @code + * entity_get_form_display('node', 'article', 'default') + * ->setComponent('body', array( + * 'type' => 'text_textarea_with_summary', + * 'weight' => 1, + * )) + * ->removeComponent('field_image') + * ->save(); + * @endcode + * + * @param string $entity_type + * The entity type. + * @param string $bundle + * The bundle. + * @param string $form_mode + * (optional) The form mode. Defaults to 'default'. + * + * @todo Make the $form_mode parameter non-optional by introducing the form + * modes concept. + * + * @return \Drupal\entity\Plugin\Core\Entity\EntityFormDisplay + * The EntityFormDisplay object associated to the form mode. + */ +function entity_get_form_display($entity_type, $bundle, $form_mode = 'default') { + // Try loading the entity from configuration. + $entity_form_display = entity_load('entity_form_display', $entity_type . '.' . $bundle . '.' . $form_mode); + + // If not found, create a fresh entity object. We do not preemptively create + // new EntityFormDisplay configuration entries for each existing entity type + // and bundle whenever a new form mode becomes available. Instead, + // configuration entries are only created when a EntityFormDisplay object is + // explicitly configured and saved. + if (!$entity_form_display) { + $entity_form_display = entity_create('entity_form_display', array( + 'targetEntityType' => $entity_type, + 'bundle' => $bundle, + 'formMode' => $form_mode, + )); + } + + return $entity_form_display; +} + +/** * Returns the entity query object for this entity type. * * @param $entity_type diff --git a/core/modules/block/block.install b/core/modules/block/block.install index b1fc925..1eee21e 100644 --- a/core/modules/block/block.install +++ b/core/modules/block/block.install @@ -270,11 +270,19 @@ function block_update_8008() { 'entity_type' => 'custom_block', 'bundle' => 'basic', 'label' => 'Block body', - 'widget' => array('type' => 'text_textarea_with_summary'), 'settings' => array('display_summary' => FALSE), ); _update_7000_field_create_instance($body_field, $instance); + module_load_install('entity'); + // Assign form settings for the 'default' form mode. + $form_display = _update_8000_entity_get_form_display('custom_block', 'basic', 'default'); + $form_display->set('content.user_picture', array( + 'type' => 'text_textarea_with_summary', + )) + ->save(); + update_config_manifest_add('entity.form_display', array($form_display->get('id'))); + // Initialize state for future calls. $sandbox['last'] = 0; $sandbox['count'] = 0; diff --git a/core/modules/block/custom_block/custom_block.module b/core/modules/block/custom_block/custom_block.module index a3ce18c..2f69788 100644 --- a/core/modules/block/custom_block/custom_block.module +++ b/core/modules/block/custom_block/custom_block.module @@ -237,11 +237,17 @@ function custom_block_add_body_field($block_type_id, $label = 'Block body') { 'entity_type' => 'custom_block', 'bundle' => $block_type_id, 'label' => $label, - 'widget' => array('type' => 'text_textarea_with_summary'), 'settings' => array('display_summary' => FALSE), ); $instance = field_create_instance($instance); + // Assign widget settings for the 'default' form mode. + entity_get_form_display('custom_block', $block_type_id, 'default') + ->setComponent('block_body', array( + 'type' => 'text_textarea_with_summary', + )) + ->save(); + // Assign display settings for 'default' view mode. entity_get_display('custom_block', $block_type_id, 'default') ->setComponent('block_body', array( diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index dd87b29..604111b 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -386,6 +386,15 @@ function _comment_body_field_create($info) { 'required' => TRUE, ); field_create_instance($instance); + + // Assign widget settings for the 'default' form mode. + entity_get_form_display('comment', 'comment_node_' . $info->type, 'default') + ->setComponent('comment_body', array( + 'type' => 'text_textarea', + )) + ->save(); + + // Assign display settings for the 'default' view mode. entity_get_display('comment', 'comment_node_' . $info->type, 'default') ->setComponent('comment_body', array( 'label' => 'hidden', diff --git a/core/modules/datetime/datetime.module b/core/modules/datetime/datetime.module index 79905f8..b545c36 100644 --- a/core/modules/datetime/datetime.module +++ b/core/modules/datetime/datetime.module @@ -134,9 +134,7 @@ function datetime_field_settings_form($field, $instance, $has_data) { * Implements hook_field_instance_settings_form(). */ function datetime_field_instance_settings_form($field, $instance) { - $widget = $instance['widget']; $settings = $instance['settings']; - $widget_settings = $instance['widget']['settings']; $form['default_value'] = array( '#type' => 'select', diff --git a/core/modules/datetime/lib/Drupal/datetime/Tests/DateTimeFieldTest.php b/core/modules/datetime/lib/Drupal/datetime/Tests/DateTimeFieldTest.php index 232d019..081c191 100644 --- a/core/modules/datetime/lib/Drupal/datetime/Tests/DateTimeFieldTest.php +++ b/core/modules/datetime/lib/Drupal/datetime/Tests/DateTimeFieldTest.php @@ -58,15 +58,19 @@ function setUp() { 'field_name' => $this->field['field_name'], 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', - 'widget' => array( - 'type' => 'datetime_default', - ), 'settings' => array( 'default_value' => 'blank', ), ); field_create_instance($this->instance); + $this->form_display_options = array( + 'type' => 'datetime_default', + ); + entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default') + ->setComponent($this->field['field_name'], $this->form_display_options) + ->save(); + $this->display_options = array( 'type' => 'datetime_default', 'label' => 'hidden', @@ -219,17 +223,17 @@ function testDatelistWidget() { field_update_field($this->field); // Change the widget to a datelist widget. - $increment = 1; - $date_order = 'YMD'; - $time_type = '12'; - - $this->instance['widget']['type'] = 'datetime_datelist'; - $this->instance['widget']['settings'] = array( - 'increment' => $increment, - 'date_order' => $date_order, - 'time_type' => $time_type, + $this->form_display_options = array( + 'type' => 'datetime_datelist', + 'settings' => array( + 'increment' => 1, + 'date_order' => 'YMD', + 'time_type' => '12', + ), ); - field_update_instance($this->instance); + entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default') + ->setComponent($this->instance['field_name'], $this->form_display_options) + ->save(); field_cache_clear(); // Display creation form. diff --git a/core/modules/edit/lib/Drupal/edit/Tests/EditTestBase.php b/core/modules/edit/lib/Drupal/edit/Tests/EditTestBase.php index 6ac8b3b..0a3704c 100644 --- a/core/modules/edit/lib/Drupal/edit/Tests/EditTestBase.php +++ b/core/modules/edit/lib/Drupal/edit/Tests/EditTestBase.php @@ -74,13 +74,16 @@ function createFieldWithInstance($field_name, $type, $cardinality, $label, $inst 'description' => $label, 'weight' => mt_rand(0, 127), 'settings' => $instance_settings, - 'widget' => array( + ); + field_create_instance($this->$instance); + + entity_get_form_display('entity_test', 'entity_test', 'default') + ->setComponent($field_name, array( 'type' => $widget_type, 'label' => $label, 'settings' => $widget_settings, - ), - ); - field_create_instance($this->$instance); + )) + ->save(); entity_get_display('entity_test', 'entity_test', 'default') ->setComponent($field_name, array( diff --git a/core/modules/email/lib/Drupal/email/Tests/EmailFieldTest.php b/core/modules/email/lib/Drupal/email/Tests/EmailFieldTest.php index c719c97..dbb2bed 100644 --- a/core/modules/email/lib/Drupal/email/Tests/EmailFieldTest.php +++ b/core/modules/email/lib/Drupal/email/Tests/EmailFieldTest.php @@ -54,14 +54,18 @@ function testEmailField() { 'field_name' => $this->field['field_name'], 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', - 'widget' => array( + ); + field_create_instance($this->instance); + + // Create a form display for the default form mode. + entity_get_form_display('test_entity', 'test_bundle', 'default') + ->setComponent($this->field['field_name'], array( 'type' => 'email_default', 'settings' => array( 'placeholder' => 'example@example.com', ), - ), - ); - field_create_instance($this->instance); + )) + ->save(); // Create a display for the full view mode. entity_get_display('test_entity', 'test_bundle', 'full') ->setComponent($this->field['field_name'], array( diff --git a/core/modules/email/lib/Drupal/email/Tests/EmailItemTest.php b/core/modules/email/lib/Drupal/email/Tests/EmailItemTest.php index def99f1..1555b8d 100644 --- a/core/modules/email/lib/Drupal/email/Tests/EmailItemTest.php +++ b/core/modules/email/lib/Drupal/email/Tests/EmailItemTest.php @@ -44,11 +44,15 @@ public function setUp() { 'entity_type' => 'entity_test', 'field_name' => 'field_email', 'bundle' => 'entity_test', - 'widget' => array( - 'type' => 'email_default', - ), ); field_create_instance($this->instance); + + // Create a form display for the default form mode. + entity_get_form_display('entity_test', 'entity_test', 'default') + ->setComponent('field_email', array( + 'type' => 'email_default', + )) + ->save(); } /** diff --git a/core/modules/entity/entity.install b/core/modules/entity/entity.install index 2e1efd7..4e95f12 100644 --- a/core/modules/entity/entity.install +++ b/core/modules/entity/entity.install @@ -45,3 +45,42 @@ function _update_8000_entity_get_display($entity_type, $bundle, $view_mode) { } return $config; } + +/** + * Returns the raw configuration object for an EntityFormDisplay entity. + * + * The function returns the existing configuration entry if it exists, or + * creates a fresh structure. + * + * @param string $entity_type + * The entity type. + * @param string $bundle + * The bundle name. + * @param string $form_mode + * The form mode. + * + * @return \Drupal\Core\Config\Config + * The configuration object. + */ +function _update_8000_entity_get_form_display($entity_type, $bundle, $form_mode) { + $id = $entity_type . '.' . $bundle . '.' . $form_mode; + $config = config("entity.form_display.$id"); + if ($config->get()) { + return $config; + } + + // Initialize a fresh structure. + $uuid = new Uuid(); + $properties = array( + 'id' => $id, + 'uuid' => $uuid->generate(), + 'targetEntityType' => $entity_type, + 'bundle' => $bundle, + 'formMode' => $form_mode, + 'content' => array(), + ); + foreach ($properties as $key => $value) { + $config->set($key, $value); + } + return $config; +} 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 d88967f..f68ca6a 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 @@ -302,7 +302,7 @@ public function getHighestWeight() { } // Let other modules feedback about their own additions. - $weights = array_merge($weights, module_invoke_all('field_info_max_weight', $this->targetEntityType, $this->bundle, $this->viewMode)); + $weights = array_merge($weights, module_invoke_all('field_info_max_weight', $this->targetEntityType, $this->bundle, 'display', $this->viewMode)); return $weights ? max($weights) : NULL; } diff --git a/core/modules/entity/lib/Drupal/entity/Plugin/Core/Entity/EntityFormDisplay.php b/core/modules/entity/lib/Drupal/entity/Plugin/Core/Entity/EntityFormDisplay.php new file mode 100644 index 0000000..ba86256 --- /dev/null +++ b/core/modules/entity/lib/Drupal/entity/Plugin/Core/Entity/EntityFormDisplay.php @@ -0,0 +1,287 @@ +originalFormMode = $this->formMode; + } + + /** + * Overrides \Drupal\Core\Entity\Entity::id(). + */ + public function id() { + return $this->targetEntityType . '.' . $this->bundle . '.' . $this->formMode; + } + + /** + * Overrides \Drupal\config\ConfigEntityBase::save(). + */ + public function save() { + // Build an ID if none is set. + if (empty($this->id)) { + $this->id = $this->id(); + } + return parent::save(); + } + + /** + * Overrides \Drupal\config\ConfigEntityBase::getExportProperties(); + */ + public function getExportProperties() { + $names = array( + 'id', + 'uuid', + 'targetEntityType', + 'bundle', + 'formMode', + 'content', + ); + $properties = array(); + foreach ($names as $name) { + $properties[$name] = $this->get($name); + } + return $properties; + } + + /** + * Creates a duplicate of the EntityFormDisplay object on a different form mode. + * + * The new object necessarily has the same $targetEntityType and $bundle + * properties than the original one. + * + * @param $form_mode + * The form mode for the new object. + * + * @return \Drupal\entity\Plugin\Core\Entity\EntityFormDisplay + * The new object. + */ + public function createCopy($form_mode) { + $entity = $this->createDuplicate(); + $entity->formMode = $entity->originalFormMode = $form_mode; + return $entity; + } + + /** + * Gets the widget options for all components. + * + * @return array + * The array of widget options, keyed by component name. + */ + public function getComponents() { + return $this->content; + } + + /** + * Gets the widget options set for a component. + * + * @param string $name + * The name of the component. + * + * @return array + * The widget options for the component. + */ + public function getComponent($name) { + if (isset($this->content[$name])) { + return $this->content[$name]; + } + } + + /** + * Sets the widget options for a component. + * + * @param string $name + * The name of the component. + * @param array $options + * The widget options. + * + * @return \Drupal\entity\Plugin\Core\Entity\EntityFormDisplay + * The EntityFormDisplay object. + */ + public function setComponent($name, array $options = array()) { + // If no weight specified, make sure the field sinks at the bottom. + if (!isset($options['weight'])) { + $max = $this->getHighestWeight(); + $options['weight'] = isset($max) ? $max + 1 : 0; + } + + if ($instance = field_info_instance($this->targetEntityType, $name, $this->bundle)) { + $field = field_info_field($instance['field_name']); + $options = drupal_container()->get('plugin.manager.field.widget')->prepareConfiguration($field['type'], $options); + + // Clear the persisted component, if any. + unset($this->widgets[$name]); + } + + $this->content[$name] = $options; + + return $this; + } + + /** + * Sets a component to be hidden. + * + * @param string $name + * The name of the component. + * + * @return \Drupal\entity\Plugin\Core\Entity\EntityFormDisplay + * The EntityFormDisplay object. + */ + public function removeComponent($name) { + // @todo This method can't do anything until http://drupal.org/node/1465774 + // is in. + return $this; + } + + /** + * Returns the highest weight of the components in the form. + * + * @return int|null + * The highest weight of the components in the form, or NULL if the form is + * empty. + */ + public function getHighestWeight() { + $weights = array(); + + // Collect weights for the components in the form. + foreach ($this->content as $options) { + if (isset($options['weight'])) { + $weights[] = $options['weight']; + } + } + + // Let other modules feedback about their own additions. + $weights = array_merge($weights, module_invoke_all('field_info_max_weight', $this->targetEntityType, $this->bundle, 'form', $this->formMode)); + + return $weights ? max($weights) : NULL; + } + + /** + * Returns the Widget plugin for a field. + * + * @param string $field_name + * The field name. + * + * @return \Drupal\field\Plugin\Type\Widget\WidgetInterface|null + * A Widget plugin or NULL if the field does not exist. + */ + public function getWidget($field_name) { + if (isset($this->widgets[$field_name])) { + return $this->widgets[$field_name]; + } + + // Instantiate the widget object from the stored display properties. + if ($configuration = $this->getComponent($field_name)) { + $instance = field_info_instance($this->targetEntityType, $field_name, $this->bundle); + $widget = drupal_container()->get('plugin.manager.field.widget')->getInstance(array( + 'instance' => $instance, + 'form_mode' => $this->originalFormMode, + // No need to prepare, defaults have been merged in setComponent(). + 'prepare' => FALSE, + 'configuration' => $configuration + )); + } + else { + $widget = NULL; + } + + // Persist the widget object. + $this->widgets[$field_name] = $widget; + return $widget; + } +} diff --git a/core/modules/entity_reference/entity_reference.module b/core/modules/entity_reference/entity_reference.module index 3566121..0f1f162 100644 --- a/core/modules/entity_reference/entity_reference.module +++ b/core/modules/entity_reference/entity_reference.module @@ -593,7 +593,8 @@ function entity_reference_autocomplete_callback_get_matches($field, $instance, $ if (isset($string)) { // Get an array of matching entities. - $match_operator = !empty($instance['widget']['settings']['match_operator']) ? $instance['widget']['settings']['match_operator'] : 'CONTAINS'; + $widget = entity_get_form_display($instance['entity_type'], $instance['bundle'])->getComponent($instance['field_name']); + $match_operator = !empty($widget['settings']['match_operator']) ? $widget['settings']['match_operator'] : 'CONTAINS'; $entity_labels = $handler->getReferencableEntities($string, $match_operator, 10); // Loop through the entities and convert them into autocomplete output. diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutoCreateTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutoCreateTest.php index d7b2d84..bcf3763 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutoCreateTest.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutoCreateTest.php @@ -66,6 +66,12 @@ function setUp() { ); field_create_instance($instance); + + entity_get_form_display('node', $referencing->type, 'default') + ->setComponent('test_field', array( + 'type' => 'entity_reference_autocomplete', + )) + ->save(); } /** 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 70961b0..8f22ce6 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 @@ -44,9 +44,6 @@ function setUp() { 'field_name' => $this->field_name, 'bundle' => 'article', 'entity_type' => 'node', - 'widget' => array( - 'type' => 'options_select', - ), 'settings' => array( 'handler' => 'default', 'handler_settings' => array( diff --git a/core/modules/field/field.api.php b/core/modules/field/field.api.php index 1cf252d..e21a4db 100644 --- a/core/modules/field/field.api.php +++ b/core/modules/field/field.api.php @@ -777,38 +777,6 @@ function hook_field_widget_WIDGET_TYPE_form_alter(&$element, &$form_state, $cont } /** - * Alters the widget properties of a field instance before it gets displayed. - * - * Note that instead of hook_field_widget_properties_alter(), which is called - * for all fields on all entity types, - * hook_field_widget_properties_ENTITY_TYPE_alter() may be used to alter widget - * properties for fields on a specific entity type only. - * - * This hook is called once per field per added or edit entity. If the result - * of the hook involves reading from the database, it is highly recommended to - * statically cache the information. - * - * @param array $widget_properties - * The instance's widget properties. - * @param array $context - * An associative array containing: - * - entity_type: The entity type, e.g., 'node' or 'user'. - * - bundle: The bundle, e.g., 'page' or 'article'. - * - field: The field that the widget belongs to. - * - instance: The instance of the field. - * - * @see hook_field_widget_properties_ENTITY_TYPE_alter() - */ -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') { - $time = date('H'); - $widget_properties['type'] = $time < 12 ? 'widget_am' : 'widget_pm'; - } -} - -/** * @} End of "defgroup field_widget". */ @@ -1873,21 +1841,24 @@ function hook_field_storage_pre_update(\Drupal\Core\Entity\EntityInterface $enti * Field API takes care of fields and 'extra_fields'. This hook is intended for * third-party modules adding other entity components (e.g. field_group). * - * @param $entity_type + * @param string $entity_type * The type of entity; e.g. 'node' or 'user'. - * @param $bundle + * @param string $bundle * The bundle name. - * @param $context - * The context for which the maximum weight is requested. Either 'form', or - * the name of a view mode. - * @return + * @param string $context + * The context for which the maximum weight is requested. Either 'form' or + * 'display'. + * @param string $context_mode + * The view or form mode name. + * + * @return int * The maximum weight of the entity's components, or NULL if no components * were found. */ -function hook_field_info_max_weight($entity_type, $bundle, $context) { +function hook_field_info_max_weight($entity_type, $bundle, $context, $context_mode) { $weights = array(); - foreach (my_module_entity_additions($entity_type, $bundle, $context) as $addition) { + foreach (my_module_entity_additions($entity_type, $bundle, $context, $context_mode) as $addition) { $weights[] = $addition['weight']; } diff --git a/core/modules/field/field.attach.inc b/core/modules/field/field.attach.inc index 69102f4..f2ea5d6 100644 --- a/core/modules/field/field.attach.inc +++ b/core/modules/field/field.attach.inc @@ -8,6 +8,7 @@ use Drupal\field\FieldValidationException; use Drupal\Core\Entity\EntityInterface; use Drupal\entity\Plugin\Core\Entity\EntityDisplay; +use Drupal\entity\Plugin\Core\Entity\EntityFormDisplay; /** * @defgroup field_storage Field Storage API @@ -704,12 +705,15 @@ function _field_invoke_get_instances($entity_type, $bundle, $options) { * * Used to invoke methods on an instance's widget. * + * @param \Drupal\entity\Plugin\Core\Entity\EntityFormDisplay $entity_form_display + * An EntityFormDisplay object. + * * @return callable $target_function * A 'target function' for field_invoke_method(). */ -function _field_invoke_widget_target() { - return function ($instance) { - return $instance->getWidget(); +function _field_invoke_widget_target($entity_form_display) { + return function ($instance) use ($entity_form_display) { + return $entity_form_display->getWidget($instance['field_name']); }; } @@ -824,9 +828,12 @@ function field_attach_form(EntityInterface $entity, &$form, &$form_state, $langc // Set #parents to 'top-level' by default. $form += array('#parents' => array()); + // Get the entity_form_display object for this bundle. + $entity_form_display = entity_get_form_display($entity->entityType(), $entity->bundle()); + // If no language is provided use the default site language. $options['langcode'] = field_valid_language($langcode); - $form += (array) field_invoke_method('form', _field_invoke_widget_target(), $entity, $form, $form_state, $options); + $form += (array) field_invoke_method('form', _field_invoke_widget_target($entity_form_display), $entity, $form, $form_state, $options); // Add custom weight handling. $form['#pre_render'][] = '_field_extra_fields_pre_render'; @@ -1091,7 +1098,8 @@ function field_attach_form_validate(EntityInterface $entity, $form, &$form_state field_form_set_state($form['#parents'], $field_name, $langcode, $form_state, $field_state); } } - field_invoke_method('flagErrors', _field_invoke_widget_target(), $entity, $form, $form_state, $options); + $entity_form_display = entity_get_form_display($entity->entityType(), $entity->bundle()); + field_invoke_method('flagErrors', _field_invoke_widget_target($entity_form_display), $entity, $form, $form_state, $options); } } @@ -1115,7 +1123,8 @@ function field_attach_form_validate(EntityInterface $entity, $form, &$form_state */ function field_attach_extract_form_values(EntityInterface $entity, $form, &$form_state, array $options = array()) { // Extract field values from submitted values. - field_invoke_method('extractFormValues', _field_invoke_widget_target(), $entity, $form, $form_state, $options); + $entity_form_display = entity_get_form_display($entity->entityType(), $entity->bundle()); + field_invoke_method('extractFormValues', _field_invoke_widget_target($entity_form_display), $entity, $form, $form_state, $options); // Let other modules act on submitting the entity. // Avoid module_invoke_all() to let $form_state be taken by reference. diff --git a/core/modules/field/field.crud.inc b/core/modules/field/field.crud.inc index f6ee8a2..0ae76b1 100644 --- a/core/modules/field/field.crud.inc +++ b/core/modules/field/field.crud.inc @@ -576,7 +576,6 @@ function field_update_instance($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. @@ -589,7 +588,6 @@ function _field_write_instance(&$instance, $update = FALSE) { // Set defaults. $instance += array( 'settings' => array(), - 'widget' => array(), 'required' => FALSE, 'label' => $instance['field_name'], 'description' => '', @@ -599,22 +597,6 @@ function _field_write_instance(&$instance, $update = FALSE) { // Set default instance settings. $instance['settings'] += field_info_instance_settings($field['type']); - // Set default widget and settings. - $instance['widget'] += 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; - } - // 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. diff --git a/core/modules/field/field.info.inc b/core/modules/field/field.info.inc index b422b47..6aea80f 100644 --- a/core/modules/field/field.info.inc +++ b/core/modules/field/field.info.inc @@ -59,6 +59,8 @@ function field_info_cache_clear() { // functions are moved to the entity API. entity_info_cache_clear(); + entity_get_controller('entity_form_display')->resetCache(); + _field_info_collate_types_reset(); _field_info_field_cache()->flush(); } @@ -166,7 +168,11 @@ 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']); + // @todo Cleanup this crap. + $info = array(); + if ($test = entity_get_form_display($instance['entity_type'], $instance['bundle'])->getWidget($instance['field_name'])) { + $info = $test->getDefinition(); + } return isset($info[$op]) ? $info[$op] : FIELD_BEHAVIOR_DEFAULT; } @@ -497,40 +503,6 @@ function field_info_extra_fields($entity_type, $bundle, $context) { } /** - * Returns the maximum weight of all the components in a form entity. - * - * This includes fields, 'extra_fields', and other components added by - * third-party modules (e.g. field_group). - * - * @param $entity_type - * The type of entity; e.g. 'node' or 'user'. - * @param $bundle - * The bundle name. - * - * @return - * The maximum weight of the entity's components, or NULL if no components - * were found. - */ -function field_info_max_weight($entity_type, $bundle) { - $weights = array(); - - // Collect weights for fields. - foreach (field_info_instances($entity_type, $bundle) as $instance) { - $weights[] = $instance['widget']['weight']; - } - // Collect weights for extra fields. - foreach (field_info_extra_fields($entity_type, $bundle, 'form') as $extra) { - $weights[] = $extra['weight']; - } - - // Let other modules feedback about their own additions. - $weights = array_merge($weights, module_invoke_all('field_info_max_weight', $entity_type, $bundle, 'form')); - $max_weight = $weights ? max($weights) : NULL; - - return $max_weight; -} - -/** * Returns a field type's default settings. * * @param $type diff --git a/core/modules/field/field.install b/core/modules/field/field.install index 695fd89..bbe1877 100644 --- a/core/modules/field/field.install +++ b/core/modules/field/field.install @@ -394,12 +394,12 @@ function field_update_8001() { } /** - * Migrate all instance display settings to configuration. + * Migrate all instance widget and display settings to configuration. * * @ingroup config_upgrade */ function field_update_8002() { - $displays = array(); + $form_displays = $displays = array(); module_load_install('entity'); $query = db_select('field_config_instance', 'fc')->fields('fc'); @@ -410,10 +410,23 @@ function field_update_8002() { // Skip field instances that were created directly with the new API earlier // in the upgrade path. - if (!isset($data['display'])) { + if (!isset($data['widget']) && !isset($data['display'])) { continue; } + // Migrate 'widget' settings. + $widget_options = $data['widget']; + // Determine name and create initial entry in the $form_displays array. + $form_display_id = $record->entity_type . '.' . $record->bundle . '.default'; + if (!isset($form_displays[$form_display_id])) { + $form_displays[$form_display_id] = _update_8000_entity_get_display($record->entity_type, $record->bundle, 'default'); + } + + // We do not need the 'module' key anymore. + unset($widget_options['module']); + $form_displays[$form_display_id]->set("content.$record->field_name", $widget_options); + + // Migrate 'display' settings. foreach ($data['display'] as $view_mode => $display_options) { // Determine name and create initial entry in the $displays array if it // does not exist yet. @@ -430,8 +443,9 @@ function field_update_8002() { } } - // Remove the 'display' key and save the record back into the table. - unset($data['display']); + // Remove the 'widget' and 'display' keys and save the record back into the + // table. + unset($data['display'], $data['widget']); db_update('field_config_instance') ->condition('id', $record->id) ->fields(array( @@ -448,6 +462,19 @@ function field_update_8002() { if (preg_match('/field_bundle_settings_(.*)__(.*)/', $variable_name, $matches)) { $entity_type = $matches[1]; $bundle = $matches[2]; + + if (isset($variable_value['extra_fields']['form'])) { + foreach ($variable_value['extra_fields']['form'] as $field_name => $field_settings) { + // Determine name and create initial entry in the $form_displays + // array if it does not exist yet. + $form_display_id = $record->entity_type . '.' . $record->bundle . '.default'; + if (!isset($form_displays[$form_display_id])) { + $form_displays[$form_display_id] = _update_8000_entity_get_display($record->entity_type, $record->bundle, 'default'); + } + $form_displays[$form_display_id]->set("content.$field_name", $variable_value['extra_fields']['form']); + } + } + if (isset($variable_value['extra_fields']['display'])) { foreach ($variable_value['extra_fields']['display'] as $field_name => $field_settings) { foreach ($field_settings as $view_mode => $display_options) { @@ -476,6 +503,12 @@ function field_update_8002() { } } + // Save the form displays to configuration. + foreach ($form_displays as $config) { + $config->save(); + } + update_config_manifest_add('entity.form_display', array_keys($form_displays)); + // Save the displays to configuration. foreach ($displays as $config) { $config->save(); diff --git a/core/modules/field/field.module b/core/modules/field/field.module index 3435e73..176be31 100644 --- a/core/modules/field/field.module +++ b/core/modules/field/field.module @@ -705,10 +705,6 @@ function field_bundle_settings($entity_type, $bundle, $settings = NULL) { $settings = variable_get('field_bundle_settings_' . $entity_type . '__' . $bundle, array()); $settings += array( 'view_modes' => array(), - 'extra_fields' => array(), - ); - $settings['extra_fields'] += array( - 'form' => array(), ); return $settings; diff --git a/core/modules/field/lib/Drupal/field/FieldInfo.php b/core/modules/field/lib/Drupal/field/FieldInfo.php index 7ff6850..f58d2a9 100644 --- a/core/modules/field/lib/Drupal/field/FieldInfo.php +++ b/core/modules/field/lib/Drupal/field/FieldInfo.php @@ -513,9 +513,10 @@ public function prepareInstance($instance, $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; - } + // @todo Update this code. +// if (field_behaviors_widget('default value', $instance) == FIELD_BEHAVIOR_DEFAULT && !isset($instance['default_value'])) { +// $instance['default_value'] = NULL; +// } return $instance; } diff --git a/core/modules/field/lib/Drupal/field/FieldInstance.php b/core/modules/field/lib/Drupal/field/FieldInstance.php index ba6ea44..8d40439 100644 --- a/core/modules/field/lib/Drupal/field/FieldInstance.php +++ b/core/modules/field/lib/Drupal/field/FieldInstance.php @@ -37,37 +37,6 @@ public function __construct(array $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) { @@ -90,12 +59,6 @@ public function offsetSet($offset, $value) { 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); - } } /** @@ -103,12 +66,6 @@ public function offsetSet($offset, $value) { */ 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); - } } /** 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 b60552e..6f328c8 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 @@ -292,6 +292,7 @@ protected function formSingleElement(EntityInterface $entity, array $items, $del 'form' => $form, 'field' => $field, 'instance' => $instance, + 'entity_form_display' => entity_get_form_display($instance['entity_type'], $instance['bundle']), 'langcode' => $langcode, 'items' => $items, 'delta' => $delta, 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 7014acc..7de723e 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 @@ -46,30 +46,80 @@ public function __construct($namespaces) { } /** - * Overrides Drupal\Component\Plugin\PluginManagerBase::getInstance(). + * Overrides PluginManagerBase::getInstance(). + * + * @param array $options + * An array with the following key/value pairs: + * - instance: (FieldInstance) The field instance. + * - form_mode: (string) The form mode. + * - prepare: (bool, optional) Whether default values should get merged in + * the 'configuration' array. Defaults to TRUE. + * - configuration: (array) the configuration for the formatter. The + * following key value pairs are allowed, and are all optional if + * 'prepare' is TRUE: + * - type: (string) The formatter to use. Defaults to the + * 'default_formatter' for the field type, specified in + * hook_field_info(). The default formatter will also be used if the + * requested formatter is not available. + * - settings: (array) Settings specific to the formatter. Each setting + * defaults to the default value specified in the formatter definition. + * + * @return \Drupal\field\Plugin\Type\Widget\WidgetInterface + * A Widget object. */ public function getInstance(array $options) { + $configuration = $options['configuration']; $instance = $options['instance']; - $type = $options['type']; - - $definition = $this->getDefinition($type); $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); + } + + $plugin_id = $configuration['type']; + // 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. + $definition = $this->getDefinition($configuration['type']); 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']); - $type = $field_type_definition['default_widget']; + $plugin_id = $field_type_definition['default_widget']; } - $configuration = array( + $configuration += array( 'instance' => $instance, - 'settings' => $options['settings'], - 'weight' => $options['weight'], + 'form_mode' => $options['form_mode'], ); - return $this->createInstance($type, $configuration); + return $this->createInstance($plugin_id, $configuration); } + /** + * Merges default values for widget configuration. + * + * @param string $field_type + * The field type. + * @param array $configuration + * An array of widget configuration. + * + * @return array + * The display properties with defaults added. + */ + public function prepareConfiguration($field_type, array $configuration) { + // Fill in defaults for missing properties. + $configuration += array( + 'settings' => array(), + ); + // If no widget is specified, use the default widget. + if (!isset($configuration['type'])) { + $field_type = field_info_field_types($field_type); + $configuration['type'] = $field_type['default_widget']; + } + // Fill in default settings values for the formatter. + $configuration['settings'] += field_info_widget_settings($configuration['type']); + + return $configuration; + } } 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 a03cb5e..f99023a 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 @@ -47,13 +47,7 @@ public function formElement(array $items, $delta, array $element, $langcode, arr $function = $definition['module'] . '_field_widget_form'; if (function_exists($function)) { - // hook_field_widget_form() implementations read widget properties directly - // 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(); - return $function($form, $form_state, $this->field, $instance, $langcode, $items, $delta, $element); } return array(); 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 c73260f..500a156 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 @@ -435,20 +435,8 @@ function fakeFieldInstance($formatter, $formatter_settings) { 'entity_type' => 'views_fake', 'bundle' => 'views_fake', - // Use the default field settings for settings and widget. + // Use the default field settings. 'settings' => field_info_instance_settings($field['type']), - 'widget' => array( - 'type' => $field_type['default_widget'], - 'settings' => array(), - ), - - // Build a dummy display mode. - 'display' => array( - '_custom' => array( - 'type' => $formatter, - 'settings' => $formatter_settings, - ), - ), // Set the other fields to their default values. // @see _field_write_instance(). diff --git a/core/modules/field/lib/Drupal/field/Tests/BulkDeleteTest.php b/core/modules/field/lib/Drupal/field/Tests/BulkDeleteTest.php index 0635f38..f1679e6 100644 --- a/core/modules/field/lib/Drupal/field/Tests/BulkDeleteTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/BulkDeleteTest.php @@ -115,9 +115,6 @@ function setUp() { 'field_name' => $field['field_name'], 'entity_type' => $this->entity_type, 'bundle' => $bundle, - 'widget' => array( - 'type' => 'test_field_widget', - ) ); $this->instances[] = field_create_instance($instance); } diff --git a/core/modules/field/lib/Drupal/field/Tests/CrudTest.php b/core/modules/field/lib/Drupal/field/Tests/CrudTest.php index f7c3a9c..da8dae1 100644 --- a/core/modules/field/lib/Drupal/field/Tests/CrudTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/CrudTest.php @@ -287,9 +287,6 @@ function testDeleteField() { 'field_name' => $this->field['field_name'], 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', - 'widget' => array( - 'type' => 'test_field_widget', - ), ); field_create_instance($this->instance_definition); $this->another_instance_definition = $this->instance_definition; diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldAccessTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldAccessTest.php index 86f1f34..b63d48d 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldAccessTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldAccessTest.php @@ -46,9 +46,6 @@ function setUp() { 'field_name' => $this->field['field_name'], 'entity_type' => 'node', 'bundle' => $this->content_type, - 'widget' => array( - 'type' => 'text_textfield', - ), ); field_create_instance($this->instance); diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php index 6c7df44..e2f352e 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php @@ -495,11 +495,7 @@ function testFieldAttachDeleteBundle() { 'label' => $this->randomName() . '_label', 'description' => $this->randomName() . '_description', 'weight' => mt_rand(0, 127), - // test_field has no instance settings - 'widget' => array( - 'type' => 'test_field_widget', - 'settings' => array( - 'size' => mt_rand(0, 255)))); + ); field_create_instance($instance); // Save an entity with data for both fields diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php index ea6f26e..8b94e8a 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php @@ -79,11 +79,7 @@ function testFieldInfo() { 'label' => $this->randomName(), 'description' => $this->randomName(), 'weight' => mt_rand(0, 127), - // test_field has no instance settings - 'widget' => array( - 'type' => 'test_field_widget', - 'settings' => array( - 'test_setting' => 999))); + ); field_create_instance($instance); $info = entity_get_info('test_entity'); @@ -170,14 +166,16 @@ function testInstancePrepare() { ); field_create_instance($instance_definition); + $entity_form_display = entity_get_form_display('test_entity', 'test_bundle') + ->setComponent($field_definition['field_name']) + ->save(); + // 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']) @@ -194,7 +192,7 @@ function testInstancePrepare() { $this->assertIdentical($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(); + $widget = entity_get_form_display('test_entity', 'test_bundle')->getWidget($instance_definition['field_name']); $this->assertIdentical($widget->getPluginId(), $field_type['default_widget'], 'Unavailable widget replaced with default widget.'); $widget_type = $widget->getDefinition(); $this->assertIdentical($widget->getSettings(), $widget_type['settings'] , 'All expected widget settings are present.'); diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php index ee30cf1..d88a89b 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php @@ -63,11 +63,9 @@ function testCreateFieldInstance() { $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.'); // 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.'); // Guarantee that the field/bundle combination is unique. try { @@ -147,26 +145,12 @@ function testUpdateFieldInstance() { $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']++; 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'); - - // 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'; - 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.'); - $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.'); // TODO: test failures. } diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldUnitTestBase.php b/core/modules/field/lib/Drupal/field/Tests/FieldUnitTestBase.php index 94b068d..6062e3d 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldUnitTestBase.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldUnitTestBase.php @@ -71,15 +71,18 @@ function createFieldWithInstance($suffix = '') { 'settings' => array( 'test_instance_setting' => $this->randomName(), ), - 'widget' => array( + ); + field_create_instance($this->$instance); + + entity_get_form_display('test_entity', 'test_bundle') + ->setComponent($this->$field_name, array( 'type' => 'test_field_widget', 'label' => 'Test Field', 'settings' => array( 'test_widget_setting' => $this->randomName(), ) - ) - ); - field_create_instance($this->$instance); + )) + ->save(); } /** diff --git a/core/modules/field/lib/Drupal/field/Tests/FormTest.php b/core/modules/field/lib/Drupal/field/Tests/FormTest.php index fcda223..79d3c65 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FormTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FormTest.php @@ -43,13 +43,6 @@ function setUp() { 'settings' => array( 'test_instance_setting' => $this->randomName(), ), - 'widget' => array( - 'type' => 'test_field_widget', - 'label' => 'Test Field', - 'settings' => array( - 'test_widget_setting' => $this->randomName(), - ) - ) ); } @@ -59,6 +52,9 @@ function testFieldFormSingle() { $this->instance['field_name'] = $this->field_name; field_create_field($this->field); field_create_instance($this->instance); + entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default') + ->setComponent($this->field_name) + ->save(); $langcode = LANGUAGE_NOT_SPECIFIED; // Display creation form. @@ -125,6 +121,9 @@ function testFieldFormDefaultValue() { $this->instance['default_value'] = array(array('value' => $default)); field_create_field($this->field); field_create_instance($this->instance); + entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default') + ->setComponent($this->field_name) + ->save(); $langcode = LANGUAGE_NOT_SPECIFIED; // Display creation form. @@ -149,6 +148,9 @@ function testFieldFormSingleRequired() { $this->instance['required'] = TRUE; field_create_field($this->field); field_create_instance($this->instance); + entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default') + ->setComponent($this->field_name) + ->save(); $langcode = LANGUAGE_NOT_SPECIFIED; // Submit with missing required value. @@ -187,6 +189,9 @@ function testFieldFormUnlimited() { $this->instance['field_name'] = $this->field_name; field_create_field($this->field); field_create_instance($this->instance); + entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default') + ->setComponent($this->field_name) + ->save(); $langcode = LANGUAGE_NOT_SPECIFIED; // Display creation form -> 1 widget. @@ -267,6 +272,9 @@ function testFieldFormMultivalueWithRequiredRadio() { $this->instance['field_name'] = $this->field_name; field_create_field($this->field); field_create_instance($this->instance); + entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default') + ->setComponent($this->field_name) + ->save(); $langcode = LANGUAGE_NOT_SPECIFIED; // Add a required radio field. @@ -282,11 +290,13 @@ function testFieldFormMultivalueWithRequiredRadio() { 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', 'required' => TRUE, - 'widget' => array( - 'type' => 'options_buttons', - ), ); field_create_instance($instance); + entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default') + ->setComponent($instance['field_name'], array( + 'type' => 'options_buttons', + )) + ->save(); // Display creation form. $this->drupalGet('test-entity/add/test_bundle'); @@ -309,6 +319,9 @@ function testFieldFormJSAddMore() { $this->instance['field_name'] = $this->field_name; field_create_field($this->field); field_create_instance($this->instance); + entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default') + ->setComponent($this->field_name) + ->save(); $langcode = LANGUAGE_NOT_SPECIFIED; // Display creation form -> 1 widget. @@ -366,9 +379,13 @@ function testFieldFormMultipleWidget() { $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); field_create_instance($this->instance); + entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default') + ->setComponent($this->field_name, array( + 'type' => 'test_field_widget_multiple', + )) + ->save(); $langcode = LANGUAGE_NOT_SPECIFIED; // Display creation form. @@ -408,6 +425,9 @@ function testFieldFormAccess() { $instance['field_name'] = $field_name; field_create_field($field); field_create_instance($instance); + entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default') + ->setComponent($field_name) + ->save(); // Create a field with no edit access - see field_test_field_access(). $field_no_access = array( @@ -423,6 +443,9 @@ function testFieldFormAccess() { ); field_create_field($field_no_access); field_create_instance($instance_no_access); + entity_get_form_display($instance_no_access['entity_type'], $instance_no_access['bundle'], 'default') + ->setComponent($field_name_no_access) + ->save(); $langcode = LANGUAGE_NOT_SPECIFIED; @@ -479,9 +502,15 @@ function testNestedFieldForm() { $this->instance['field_name'] = 'field_single'; $this->instance['label'] = 'Single field'; field_create_instance($this->instance); + entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default') + ->setComponent($this->instance['field_name']) + ->save(); $this->instance['field_name'] = 'field_unlimited'; $this->instance['label'] = 'Unlimited field'; field_create_instance($this->instance); + entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default') + ->setComponent($this->instance['field_name']) + ->save(); // Create two entities. $entity_1 = field_test_create_entity(1, 1); diff --git a/core/modules/field/lib/Drupal/field/Tests/ShapeItemTest.php b/core/modules/field/lib/Drupal/field/Tests/ShapeItemTest.php index 2628256..4e92d7e 100644 --- a/core/modules/field/lib/Drupal/field/Tests/ShapeItemTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/ShapeItemTest.php @@ -43,9 +43,6 @@ public function setUp() { 'entity_type' => 'entity_test', 'field_name' => 'field_shape', 'bundle' => 'entity_test', - 'widget' => array( - 'type' => 'test_field_widget', - ), ); field_create_instance($this->instance); } diff --git a/core/modules/field/lib/Drupal/field/Tests/TestItemTest.php b/core/modules/field/lib/Drupal/field/Tests/TestItemTest.php index 198be2a..c8ff8fa 100644 --- a/core/modules/field/lib/Drupal/field/Tests/TestItemTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/TestItemTest.php @@ -43,9 +43,6 @@ public function setUp() { 'entity_type' => 'entity_test', 'field_name' => 'field_test', 'bundle' => 'entity_test', - 'widget' => array( - 'type' => 'test_field_widget', - ), ); field_create_instance($this->instance); } diff --git a/core/modules/field/lib/Drupal/field/Tests/TranslationWebTest.php b/core/modules/field/lib/Drupal/field/Tests/TranslationWebTest.php index 40447f1..a32cc81 100644 --- a/core/modules/field/lib/Drupal/field/Tests/TranslationWebTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/TranslationWebTest.php @@ -53,6 +53,10 @@ function setUp() { field_create_instance($instance); $this->instance = field_read_instance('test_entity', $this->field_name, 'test_bundle'); + entity_get_form_display($this->entity_type, 'test_bundle', 'default') + ->setComponent($this->field_name) + ->save(); + for ($i = 0; $i < 3; ++$i) { $language = new Language(array( 'langcode' => 'l' . $i, 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 4ecd1a9..0cbbcb1 100644 --- a/core/modules/field/tests/modules/field_test/field_test.module +++ b/core/modules/field/tests/modules/field_test/field_test.module @@ -196,36 +196,18 @@ function field_test_field_attach_view_alter(&$output, $context) { } /** - * Implements hook_field_widget_properties_alter(). - */ -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')) { - $widget['settings']['size'] = 42; - } -} - -/** - * Implements hook_field_widget_properties_ENTITY_TYPE_alter(). - */ -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') { - $widget['type'] = 'options_buttons'; - } -} - -/** * Implements hook_field_widget_form_alter(). */ function field_test_field_widget_form_alter(&$element, &$form_state, $context) { + $instance = $context['instance']; + $entity_form_display = entity_get_form_display($instance['entity_type'], $instance['bundle']); switch ($context['field']['field_name']) { case 'alter_test_text': - drupal_set_message('Field size: ' . $context['instance']->getWidget()->getSetting('size')); + drupal_set_message('Field size: ' . $entity_form_display->getWidget($context['field']['field_name'])->getSetting('size')); break; case 'alter_test_options': - drupal_set_message('Widget type: ' . $context['instance']->getWidget()->getPluginId()); + drupal_set_message('Widget type: ' . $entity_form_display->getWidget($context['field']['field_name'])->getPluginId()); break; } // Set a message if this is for the form displayed to set default value for diff --git a/core/modules/field_ui/field_ui.admin.inc b/core/modules/field_ui/field_ui.admin.inc index 566959b..601c38c 100644 --- a/core/modules/field_ui/field_ui.admin.inc +++ b/core/modules/field_ui/field_ui.admin.inc @@ -70,11 +70,14 @@ function field_ui_inactive_message($entity_type, $bundle) { $widget_types = field_info_widget_types(); foreach ($inactive_instances as $field_name => $instance) { + $entity_form_display = entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default'); + $widget = $entity_form_display->getComponent($instance['field_name']); + $widget_definition = $entity_form_display->getWidget($instance['field_name'])->getDefinition(); $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'], + '%widget_type' => isset($widget_types[$widget['type']]) ? $widget_types[$widget['type']]['label'] : $widget['type'], + '%widget_module' => $widget_definition['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'); @@ -462,6 +465,7 @@ function field_ui_existing_field_options($entity_type, $bundle) { if (!($existing_bundle == $bundle && $existing_entity_type == $entity_type)) { foreach ($instances as $instance) { $field = field_info_field($instance['field_name']); + $widget = entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default')->getComponent($instance['field_name']); // Don't show // - locked fields, // - fields already in the current bundle, @@ -477,7 +481,7 @@ function field_ui_existing_field_options($entity_type, $bundle) { 'type_label' => $field_types[$field['type']]['label'], 'field' => $field['field_name'], 'label' => $instance['label'], - 'widget_type' => $instance['widget']['type'], + 'widget_type' => $widget['type'], ); } } @@ -641,6 +645,7 @@ function field_ui_widget_type_form($form, &$form_state, FieldInstance $instance) $entity_type = $instance['entity_type']; $field_name = $instance['field_name']; + $entity_form_display = entity_get_form_display($entity_type, $bundle); $field = field_info_field($field_name); $bundles = entity_get_bundles(); $bundle_label = $bundles[$entity_type][$bundle]['label']; @@ -649,6 +654,7 @@ function field_ui_widget_type_form($form, &$form_state, FieldInstance $instance) '#bundle' => $bundle, '#entity_type' => $entity_type, '#field_name' => $field_name, + '#instance' => $instance, ); $form['widget_type'] = array( @@ -656,7 +662,7 @@ function field_ui_widget_type_form($form, &$form_state, FieldInstance $instance) '#title' => t('Widget type'), '#required' => TRUE, '#options' => field_ui_widget_type_options($field['type']), - '#default_value' => $instance->getWidget()->getPluginId(), + '#default_value' => $entity_form_display->getWidget($field_name)->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)), ); @@ -677,19 +683,16 @@ function field_ui_widget_type_form_submit($form, &$form_state) { $bundle = $form['#bundle']; $entity_type = $form['#entity_type']; $field_name = $form['#field_name']; + $instance = $form['#instance']; - // Retrieve the stored instance settings to merge with the incoming values. - $instance = field_read_instance($entity_type, $field_name, $bundle); - - // Set the right module information. - $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; + // Update the stored instance with the incoming values. + $entity_form_display = entity_get_form_display($entity_type, $bundle) + ->setComponent($field_name, array( + 'type' => $form_values['widget_type'], + )); try { - field_update_instance($instance); + $entity_form_display->save(); drupal_set_message(t('Changed the widget for field %label.', array('%label' => $instance['label']))); } catch (Exception $e) { @@ -792,6 +795,7 @@ function field_ui_field_edit_form($form, &$form_state, $instance) { $form['#field'] = $field; $form['#instance'] = $instance; + $form['#entity_form_display'] = $entity_form_display = entity_get_form_display($entity_type, $bundle); // 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); $form['#entity'] = _field_create_entity_from_ids($ids); @@ -804,7 +808,8 @@ function field_ui_field_edit_form($form, &$form_state, $instance) { return $form; } - $widget_type = field_info_widget_types($instance['widget']['type']); + $widget_configuration = $entity_form_display->getComponent($instance['field_name']); + $widget = $entity_form_display->getWidget($instance['field_name']); // Create a form structure for the instance values. $form['instance'] = array( @@ -824,10 +829,6 @@ function field_ui_field_edit_form($form, &$form_state, $instance) { '#type' => 'value', '#value' => $bundle, ); - $form['instance']['widget']['weight'] = array( - '#type' => 'value', - '#value' => !empty($instance['widget']['weight']) ? $instance['widget']['weight'] : 0, - ); // Build the configurable instance values. $form['instance']['label'] = array( @@ -854,20 +855,6 @@ function field_ui_field_edit_form($form, &$form_state, $instance) { '#weight' => -5, ); - // Build the widget component of the instance. - $form['instance']['widget']['type'] = array( - '#type' => 'value', - '#value' => $instance['widget']['type'], - ); - $form['instance']['widget']['module'] = array( - '#type' => 'value', - '#value' => $widget_type['module'], - ); - $form['instance']['widget']['active'] = array( - '#type' => 'value', - '#value' => !empty($field['instance']['widget']['active']) ? 1 : 0, - ); - // Add additional field instance settings from the field module. $additions = module_invoke($field['module'], 'field_instance_settings_form', $field, $instance, $form_state); if (is_array($additions)) { @@ -876,7 +863,7 @@ function field_ui_field_edit_form($form, &$form_state, $instance) { } // Add widget settings for the widget type. - $additions = $instance->getWidget()->settingsForm($form, $form_state); + $additions = $widget->settingsForm($form, $form_state); $form['instance']['widget']['settings'] = $additions ? $additions : array('#type' => 'value', '#value' => array()); $form['instance']['widget']['#weight'] = 20; @@ -915,7 +902,6 @@ function field_ui_field_edit_form_delete_submit($form, &$form_state) { * 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']; $entity = $form['#entity']; $element = array( @@ -937,7 +923,7 @@ function field_ui_default_value_widget($field, $instance, &$form, &$form_state) // 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']; - $element += $instance->getWidget()->form($entity, LANGUAGE_NOT_SPECIFIED, $items, $element, $form_state); + $element += entity_get_form_display($instance['entity_type'], $instance['bundle'])->getWidget($instance['field_name'])->form($entity, LANGUAGE_NOT_SPECIFIED, $items, $element, $form_state); return $element; } @@ -953,13 +939,14 @@ function field_ui_field_edit_form_validate($form, &$form_state) { $instance = $form['#instance']; $field_name = $instance['field_name']; $entity = $form['#entity']; + $entity_form_display = $form['#entity_form_display']; if (isset($form['instance']['default_value_widget'])) { $element = $form['instance']['default_value_widget']; // Extract the 'default value'. $items = array(); - $instance->getWidget()->extractFormValues($entity, LANGUAGE_NOT_SPECIFIED, $items, $element, $form_state); + $entity_form_display->getWidget($instance['field_name'])->extractFormValues($entity, LANGUAGE_NOT_SPECIFIED, $items, $element, $form_state); // Grab the field definition from $form_state. $field_state = field_form_get_state($element['#parents'], $field_name, LANGUAGE_NOT_SPECIFIED, $form_state); @@ -979,7 +966,7 @@ function field_ui_field_edit_form_validate($form, &$form_state) { field_form_set_state($element['#parents'], $field_name, LANGUAGE_NOT_SPECIFIED, $form_state, $field_state); // Assign reported errors to the correct form element. - $instance->getWidget()->flagErrors($entity, LANGUAGE_NOT_SPECIFIED, $items, $element, $form_state); + $entity_form_display->getWidget($instance['field_name'])->flagErrors($entity, LANGUAGE_NOT_SPECIFIED, $items, $element, $form_state); } } } @@ -991,8 +978,8 @@ function field_ui_field_edit_form_validate($form, &$form_state) { */ function field_ui_field_edit_form_submit($form, &$form_state) { $instance = $form['#instance']; - $field = $form['#field']; $entity = $form['#entity']; + $entity_form_display = $form['#entity_form_display']; // Handle the default value. if (isset($form['instance']['default_value_widget'])) { @@ -1000,11 +987,15 @@ function field_ui_field_edit_form_submit($form, &$form_state) { // Extract field values. $items = array(); - $instance->getWidget()->extractFormValues($entity, LANGUAGE_NOT_SPECIFIED, $items, $element, $form_state); + $entity_form_display->getWidget($instance['field_name'])->extractFormValues($entity, LANGUAGE_NOT_SPECIFIED, $items, $element, $form_state); $instance['default_value'] = $items ? $items : NULL; } + // Handle widget settings. + $entity_form_display->setComponent($instance['field_name'], $form_state['values']['instance']['widget'])->save(); + unset($form_state['values']['instance']['widget']); + // Merge incoming values into the instance. foreach ($form_state['values']['instance'] as $key => $value) { $instance[$key] = $value; 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 925caef..02c406d 100644 --- a/core/modules/field_ui/lib/Drupal/field_ui/FieldOverview.php +++ b/core/modules/field_ui/lib/Drupal/field_ui/FieldOverview.php @@ -63,6 +63,7 @@ public function buildForm(array $form, array &$form_state) { $field_types = field_info_field_types(); $widget_types = field_info_widget_types(); $extra_fields = field_info_extra_fields($this->entity_type, $this->bundle, 'form'); + $entity_form_display = entity_get_form_display($this->entity_type, $this->bundle); $form += array( '#entity_type' => $this->entity_type, @@ -94,6 +95,7 @@ public function buildForm(array $form, array &$form_state) { // Fields. foreach ($instances as $name => $instance) { $field = field_info_field($instance['field_name']); + $widget_configuration = entity_get_form_display($instance['entity_type'], $instance['bundle'])->getComponent($instance['field_name']); $admin_field_path = $this->adminPath . '/fields/' . $instance['field_name']; $table[$name] = array( '#attributes' => array('class' => array('draggable', 'tabledrag-leaf')), @@ -106,7 +108,7 @@ public function buildForm(array $form, array &$form_state) { '#type' => 'textfield', '#title' => t('Weight for @title', array('@title' => $instance['label'])), '#title_display' => 'invisible', - '#default_value' => $instance['widget']['weight'], + '#default_value' => $widget_configuration['weight'], '#size' => 3, '#attributes' => array('class' => array('field-weight')), ), @@ -137,7 +139,7 @@ public function buildForm(array $form, array &$form_state) { ), 'widget_type' => array( '#type' => 'link', - '#title' => t($widget_types[$instance['widget']['type']]['label']), + '#title' => t($widget_types[$widget_configuration['type']]['label']), '#href' => $admin_field_path . '/widget-type', '#options' => array('attributes' => array('title' => t('Change widget type.'))), ), @@ -212,7 +214,7 @@ public function buildForm(array $form, array &$form_state) { } // Additional row: add new field. - $max_weight = field_info_max_weight($this->entity_type, $this->bundle, 'form'); + $max_weight = $entity_form_display->getHighestWeight(); $field_type_options = field_ui_field_type_options(); $widget_type_options = field_ui_widget_type_options(NULL, TRUE); if ($field_type_options && $widget_type_options) { @@ -528,27 +530,9 @@ protected function validateAddExisting(array $form, array &$form_state) { */ public function submitForm(array &$form, array &$form_state) { $form_values = $form_state['values']['fields']; - - $bundle_settings = field_bundle_settings($this->entity_type, $this->bundle); - - // Update field weights. - 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']; - field_update_instance($instance); - } - elseif (in_array($key, $form['#extra'])) { - $bundle_settings['extra_fields']['form'][$key]['weight'] = $values['weight']; - } - } - - field_bundle_settings($this->entity_type, $this->bundle, $bundle_settings); - $destinations = array(); // Create new field. - $field = array(); if (!empty($form_values['_add_new_field']['field_name'])) { $values = $form_values['_add_new_field']; @@ -562,10 +546,6 @@ public function submitForm(array &$form, array &$form_state) { 'entity_type' => $this->entity_type, 'bundle' => $this->bundle, 'label' => $values['label'], - 'widget' => array( - 'type' => $values['widget_type'], - 'weight' => $values['weight'], - ), ); // Create the field and instance. @@ -573,6 +553,15 @@ public function submitForm(array &$form, array &$form_state) { field_create_field($field); field_create_instance($instance); + // Make sure the field is displayed in the 'default' form mode (using + // the configured widget and default settings). + entity_get_form_display($this->entity_type, $this->bundle, 'default') + ->setComponent($field['field_name'], array( + 'type' => $values['widget_type'], + 'weight' => $values['weight'], + )) + ->save(); + // 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. @@ -588,7 +577,7 @@ public function submitForm(array &$form, array &$form_state) { // Store new field information for any additional submit handlers. $form_state['fields_added']['_add_new_field'] = $field['field_name']; } - catch (Exception $e) { + catch (\Exception $e) { drupal_set_message(t('There was a problem creating field %label: !message', array('%label' => $instance['label'], '!message' => $e->getMessage())), 'error'); } } @@ -606,15 +595,20 @@ public function submitForm(array &$form, array &$form_state) { 'entity_type' => $this->entity_type, 'bundle' => $this->bundle, 'label' => $values['label'], - 'widget' => array( - 'type' => $values['widget_type'], - 'weight' => $values['weight'], - ), ); try { field_create_instance($instance); + // Make sure the field is displayed in the 'default' form mode (using + // the configured widget and default settings). + entity_get_form_display($this->entity_type, $this->bundle, 'default') + ->setComponent($field['field_name'], array( + 'type' => $values['widget_type'], + 'weight' => $values['weight'], + )) + ->save(); + // 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. @@ -626,7 +620,7 @@ public function submitForm(array &$form, array &$form_state) { // Store new field information for any additional submit handlers. $form_state['fields_added']['_add_existing_field'] = $instance['field_name']; } - catch (Exception $e) { + catch (\Exception $e) { 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/AlterTest.php b/core/modules/field_ui/lib/Drupal/field_ui/Tests/AlterTest.php deleted file mode 100644 index 016b9d0..0000000 --- a/core/modules/field_ui/lib/Drupal/field_ui/Tests/AlterTest.php +++ /dev/null @@ -1,112 +0,0 @@ - 'Widget customization', - 'description' => 'Test custom field widget hooks and callbacks on field administration pages.', - 'group' => 'Field UI', - ); - } - - function setUp() { - parent::setUp(); - - // Create Article node type. - $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page')); - $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article')); - - // Create test user. - $admin_user = $this->drupalCreateUser(array('access content', 'administer content types', 'administer node fields', 'administer users', 'administer user fields')); - $this->drupalLogin($admin_user); - } - - /** - * Tests hook_field_widget_properties_alter() on the default field widget. - * - * @see field_test_field_widget_properties_alter() - * @see field_test_field_widget_properties_user_alter() - * @see field_test_field_widget_form_alter() - */ - function testDefaultWidgetPropertiesAlter() { - // Create the alter_test_text field and an instance on article nodes. - field_create_field(array( - 'field_name' => 'alter_test_text', - 'type' => 'text', - )); - $instance = array( - 'field_name' => 'alter_test_text', - 'entity_type' => 'node', - 'bundle' => 'article', - 'widget' => array( - 'type' => 'text_textfield', - 'size' => 60, - ), - ); - field_create_instance($instance); - - // Test that field_test_field_widget_properties_alter() sets the size to - // 42 and that field_test_field_widget_form_alter() reports the correct - // size when the form is displayed. - $this->drupalGet('admin/structure/types/manage/article/fields/alter_test_text'); - $this->assertText('Field size: 42', 'Altered field size is found in hook_field_widget_form_alter().'); - // Test that hook_field_widget_form_alter() registers this is the default - // value form and sets a message. - $this->assertText('From hook_field_widget_form_alter(): Default form is true.', 'Default value form detected in hook_field_widget_form_alter().'); - - // Create the alter_test_options field. - field_create_field(array( - 'field_name' => 'alter_test_options', - 'type' => 'list_text' - )); - // Create instances on users and page nodes. - $instance = array( - 'field_name' => 'alter_test_options', - 'entity_type' => 'user', - 'bundle' => 'user', - 'widget' => array( - 'type' => 'options_select', - ) - ); - field_create_instance($instance); - $instance = array( - 'field_name' => 'alter_test_options', - 'entity_type' => 'node', - 'bundle' => 'page', - 'widget' => array( - 'type' => 'options_select', - ) - ); - field_create_instance($instance); - - // Test that field_test_field_widget_properties_user_alter() replaces - // the widget and that field_test_field_widget_form_alter() reports the - // correct widget name when the form is displayed. - $this->drupalGet('admin/config/people/accounts/fields/alter_test_options'); - $this->assertText('Widget type: options_buttons', 'Widget type is altered for users in hook_field_widget_form_alter().'); - - // Test that the widget is not altered on page nodes. - $this->drupalGet('admin/structure/types/manage/page/fields/alter_test_options'); - $this->assertText('Widget type: options_select', 'Widget type is not altered for pages in hook_field_widget_form_alter().'); - } -} 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..229e546 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 @@ -52,6 +52,10 @@ function setUp() { 'bundle' => 'article', ); field_create_instance($instance); + + entity_get_form_display('node', 'article') + ->setComponent('field_' . $vocabulary->id()) + ->save(); } /** @@ -234,7 +238,10 @@ function assertFieldSettings($bundle, $field_name, $string = 'dummy test string' // 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.'); + + // Assert widget settings. + $widget_configuration = entity_get_form_display($entity_type, $bundle)->getComponent($field_name); + $this->assertTrue($widget_configuration['settings']['test_widget_setting'] == $string, 'Field widget settings were found.'); } /** @@ -255,6 +262,10 @@ function testDefaultValue() { ); field_create_instance($instance); + entity_get_form_display('node', $this->type) + ->setComponent($field_name) + ->save(); + $langcode = LANGUAGE_NOT_SPECIFIED; $admin_path = 'admin/structure/types/manage/' . $this->type . '/fields/' . $field_name; $element_id = "edit-$field_name-$langcode-0-value"; @@ -271,6 +282,7 @@ function testDefaultValue() { $edit = array($element_name => '1'); $this->drupalPost($admin_path, $edit, t('Save settings')); $this->assertText("Saved $field_name configuration", 'The form was successfully submitted.'); + field_info_cache_clear(); $instance = field_info_instance('node', $field_name, $this->type); $this->assertEqual($instance['default_value'], array(array('value' => 1)), 'The default value was correctly saved.'); @@ -287,8 +299,11 @@ 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'; - field_update_instance($instance); + entity_get_form_display($instance['entity_type'], $instance['bundle']) + ->setComponent($field_name, array( + 'type' => 'test_field_widget_no_default', + )) + ->save(); $this->drupalGet($admin_path); $this->assertNoFieldById($element_id, '', t('No default value was possible for widget that disables default value.')); @@ -358,9 +373,11 @@ function testHiddenFields() { 'bundle' => $this->type, 'entity_type' => 'node', 'label' => t('Hidden field'), - 'widget' => array('type' => 'test_field_widget'), ); field_create_instance($instance); + entity_get_form_display('node', $this->type) + ->setComponent($field_name) + ->save(); $this->assertTrue(field_read_instance('node', $field_name, $this->type), format_string('An instance of the field %field was created programmatically.', array('%field' => $field_name))); // Check that the newly added instance appears on the 'Manage Fields' @@ -377,8 +394,10 @@ function testHiddenFields() { /** * Tests renaming a bundle. + * + * @todo Bring back this test when http://drupal.org/node/1950326 is fixed. */ - function testRenameBundle() { + function XtestRenameBundle() { $type2 = strtolower($this->randomName(8)) . '_test'; $options = array( @@ -416,8 +435,8 @@ 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'); + $entity_form_display = entity_get_form_display('node', 'article')->getComponent('field_tags'); + $this->assertEqual($entity_form_display['type'], 'options_select'); // Check that the "Manage fields" page shows the correct widget type. $this->drupalGet($url_fields); @@ -441,8 +460,8 @@ 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'); + $widget_configuration = entity_get_form_display('node', 'article')->getComponent('field_tags'); + $this->assertEqual($widget_configuration['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 e3a7d7c..2d298aa 100644 --- a/core/modules/file/file.field.inc +++ b/core/modules/file/file.field.inc @@ -409,7 +409,6 @@ 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']; $element['#theme'] = 'file_widget'; diff --git a/core/modules/file/lib/Drupal/file/Tests/FileFieldTestBase.php b/core/modules/file/lib/Drupal/file/Tests/FileFieldTestBase.php index c2d922d..20e003a 100644 --- a/core/modules/file/lib/Drupal/file/Tests/FileFieldTestBase.php +++ b/core/modules/file/lib/Drupal/file/Tests/FileFieldTestBase.php @@ -101,14 +101,16 @@ function attachFileField($name, $entity_type, $bundle, $instance_settings = arra 'bundle' => $bundle, 'required' => !empty($instance_settings['required']), 'settings' => array(), - 'widget' => array( - 'type' => 'file_generic', - 'settings' => array(), - ), ); $instance['settings'] = array_merge($instance['settings'], $instance_settings); - $instance['widget']['settings'] = array_merge($instance['widget']['settings'], $widget_settings); field_create_instance($instance); + + entity_get_form_display($entity_type, $bundle, 'default') + ->setComponent($name, array( + 'type' => 'file_generic', + 'settings' => $widget_settings, + )) + ->save(); } /** @@ -117,9 +119,14 @@ 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); field_update_instance($instance); + + entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default') + ->setComponent($instance['field_name'], array( + 'settings' => $widget_settings, + )) + ->save(); } /** diff --git a/core/modules/file/lib/Drupal/file/Tests/FileItemTest.php b/core/modules/file/lib/Drupal/file/Tests/FileItemTest.php index 1e6eef5..2ec9dfa 100644 --- a/core/modules/file/lib/Drupal/file/Tests/FileItemTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/FileItemTest.php @@ -54,9 +54,6 @@ public function setUp() { 'entity_type' => 'entity_test', 'field_name' => 'file_test', 'bundle' => 'entity_test', - 'widget' => array( - 'type' => 'options_select', - ), ); field_create_instance($instance); file_put_contents('public://example.txt', $this->randomName()); diff --git a/core/modules/forum/forum.install b/core/modules/forum/forum.install index b1e63de..8e56bab 100644 --- a/core/modules/forum/forum.install +++ b/core/modules/forum/forum.install @@ -87,12 +87,16 @@ function forum_enable() { 'label' => $vocabulary->name, 'bundle' => 'forum', 'required' => TRUE, - 'widget' => array( - 'type' => 'options_select', - ), ); field_create_instance($instance); + // Assign form display settings for the 'default' form mode. + entity_get_form_display('node', 'forum', 'default') + ->setComponent('taxonomy_forums', array( + 'type' => 'options_select', + )) + ->save(); + // Assign display settings for the 'default' and 'teaser' view modes. entity_get_display('node', 'forum', 'default') ->setComponent('taxonomy_forums', array( diff --git a/core/modules/hal/lib/Drupal/hal/Tests/NormalizerTestBase.php b/core/modules/hal/lib/Drupal/hal/Tests/NormalizerTestBase.php index 224586d..9b5a4e0 100644 --- a/core/modules/hal/lib/Drupal/hal/Tests/NormalizerTestBase.php +++ b/core/modules/hal/lib/Drupal/hal/Tests/NormalizerTestBase.php @@ -20,7 +20,7 @@ * * @var array */ - public static $modules = array('entity_test', 'entity_reference', 'field', 'field_sql_storage', 'hal', 'language', 'rest', 'serialization', 'system', 'text', 'user'); + public static $modules = array('entity_test', 'entity_reference', 'field', 'field_sql_storage', 'hal', 'language', 'rest', 'serialization', 'system', 'text', 'user', 'entity'); /** * The format being tested. diff --git a/core/modules/image/image.module b/core/modules/image/image.module index b2816de..6e4f83a 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -356,7 +356,9 @@ 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') { + $entity_form_display = entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default'); + $widget_definition = $entity_form_display->getWidget($instance['field_name'])->getDefinition(); + if ($widget_definition['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) { @@ -372,9 +374,11 @@ function image_image_style_update(ImageStyle $style) { ->save(); } } - if ($instance['widget']['settings']['preview_image_style'] == $style->getOriginalID()) { - $instance['widget']['settings']['preview_image_style'] = $style->id(); - field_update_instance($instance); + $widget_configuration = $entity_form_display->getComponent($instance['field_name']); + if ($widget_configuration['settings']['preview_image_style'] == $style->getOriginalID()) { + $widget_options['settings']['preview_image_style'] = $style->id(); + $entity_form_display->setComponent($instance['field_name'], $widget_options) + ->save(); } } } diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageFieldDefaultImagesTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageFieldDefaultImagesTest.php index 01fa5f4..6c5f4eb 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageFieldDefaultImagesTest.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageFieldDefaultImagesTest.php @@ -65,10 +65,14 @@ function testDefaultImages() { 'settings' => array( 'default_image' => $default_images['instance2']->fid, ), - 'widget' => $instance['widget'], ); field_create_instance($instance2); - $instance2 = field_info_instance('node', $field_name, 'page'); + + $widget_settings = entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default')->getComponent($instance['field_name']); + entity_get_form_display('node', 'page', 'default') + ->setComponent($field['field_name'], $widget_settings) + ->save(); + entity_get_display('node', 'page', 'default') ->setComponent($field['field_name']) ->save(); diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageFieldTestBase.php b/core/modules/image/lib/Drupal/image/Tests/ImageFieldTestBase.php index 5a4e20e..508c9bc 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageFieldTestBase.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageFieldTestBase.php @@ -85,16 +85,17 @@ function createImageField($name, $type_name, $field_settings = array(), $instanc 'required' => !empty($instance_settings['required']), 'description' => !empty($instance_settings['description']) ? $instance_settings['description'] : '', 'settings' => array(), - 'widget' => array( - 'type' => 'image_image', - 'settings' => array(), - ), ); $instance['settings'] = array_merge($instance['settings'], $instance_settings); - $instance['widget']['settings'] = array_merge($instance['widget']['settings'], $widget_settings); - $field_instance = field_create_instance($instance); + entity_get_form_display('node', $type_name, 'default') + ->setComponent($field['field_name'], array( + 'type' => 'image_image', + 'settings' => $widget_settings, + )) + ->save(); + entity_get_display('node', $type_name, 'default') ->setComponent($field['field_name']) ->save(); diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageItemTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageItemTest.php index 7f863cf..709073d 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageItemTest.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageItemTest.php @@ -53,9 +53,6 @@ public function setUp() { 'entity_type' => 'entity_test', 'field_name' => 'image_test', 'bundle' => 'entity_test', - 'widget' => array( - 'type' => 'image_image', - ), ); field_create_instance($instance); file_unmanaged_copy(DRUPAL_ROOT . '/core/misc/druplicon.png', 'public://example.jpg'); diff --git a/core/modules/link/lib/Drupal/link/Tests/LinkFieldTest.php b/core/modules/link/lib/Drupal/link/Tests/LinkFieldTest.php index a608873..d0c2148 100644 --- a/core/modules/link/lib/Drupal/link/Tests/LinkFieldTest.php +++ b/core/modules/link/lib/Drupal/link/Tests/LinkFieldTest.php @@ -56,14 +56,16 @@ function testURLValidation() { 'settings' => array( 'title' => DRUPAL_DISABLED, ), - 'widget' => array( + ); + field_create_instance($this->instance); + entity_get_form_display('test_entity', 'test_bundle', 'default') + ->setComponent($this->field['field_name'], array( 'type' => 'link_default', 'settings' => array( 'placeholder_url' => 'http://example.com', ), - ), - ); - field_create_instance($this->instance); + )) + ->save(); entity_get_display('test_entity', 'test_bundle', 'full') ->setComponent($this->field['field_name'], array( 'type' => 'link', @@ -124,15 +126,17 @@ function testLinkTitle() { 'settings' => array( 'title' => DRUPAL_OPTIONAL, ), - 'widget' => array( + ); + field_create_instance($this->instance); + entity_get_form_display('test_entity', 'test_bundle', 'default') + ->setComponent($this->field['field_name'], array( 'type' => 'link_default', 'settings' => array( 'placeholder_url' => 'http://example.com', 'placeholder_title' => 'Enter a title for this link', ), - ), - ); - field_create_instance($this->instance); + )) + ->save(); entity_get_display('test_entity', 'test_bundle', 'full') ->setComponent($this->field['field_name'], array( 'type' => 'link', @@ -234,15 +238,17 @@ function testLinkFormatter() { 'settings' => array( 'title' => DRUPAL_OPTIONAL, ), - 'widget' => array( - 'type' => 'link_default', - ), ); $display_options = array( 'type' => 'link', 'label' => 'hidden', ); field_create_instance($this->instance); + entity_get_form_display('test_entity', 'test_bundle', 'default') + ->setComponent($this->field['field_name'], array( + 'type' => 'link_default', + )) + ->save(); entity_get_display('test_entity', 'test_bundle', 'full') ->setComponent($this->field['field_name'], $display_options) ->save(); @@ -370,15 +376,17 @@ function testLinkSeparateFormatter() { 'settings' => array( 'title' => DRUPAL_OPTIONAL, ), - 'widget' => array( - 'type' => 'link_default', - ), ); $display_options = array( 'type' => 'link_separate', 'label' => 'hidden', ); field_create_instance($this->instance); + entity_get_form_display('test_entity', 'test_bundle', 'default') + ->setComponent($this->field['field_name'], array( + 'type' => 'link_default', + )) + ->save(); entity_get_display('test_entity', 'test_bundle', 'full') ->setComponent($this->field['field_name'], $display_options) ->save(); diff --git a/core/modules/link/lib/Drupal/link/Tests/LinkItemTest.php b/core/modules/link/lib/Drupal/link/Tests/LinkItemTest.php index e59e403..eb40cbc 100644 --- a/core/modules/link/lib/Drupal/link/Tests/LinkItemTest.php +++ b/core/modules/link/lib/Drupal/link/Tests/LinkItemTest.php @@ -44,9 +44,6 @@ public function setUp() { 'entity_type' => 'entity_test', 'field_name' => 'field_test', 'bundle' => 'entity_test', - 'widget' => array( - 'type' => 'link_default', - ), ); field_create_instance($this->instance); } diff --git a/core/modules/link/link.module b/core/modules/link/link.module index 57d744c..57e9687 100644 --- a/core/modules/link/link.module +++ b/core/modules/link/link.module @@ -112,7 +112,7 @@ function link_field_widget_info() { * Implements hook_field_widget_settings_form(). */ function link_field_widget_settings_form($field, $instance) { - $widget = $instance['widget']; + $widget = entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default')->getComponent($instance['field_name']); $settings = $widget['settings']; $form['placeholder_url'] = array( @@ -141,7 +141,8 @@ function link_field_widget_settings_form($field, $instance) { */ function link_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { $settings = $instance['settings']; - $widget_settings = $instance['widget']['settings']; + $widget = entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default')->getComponent($instance['field_name']); + $widget_settings = $widget['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 4f868f7..644fbcf 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 @@ -278,9 +278,10 @@ protected function build_filters(&$form, &$form_state) { $tag_fields = array(); foreach ($bundles as $bundle) { foreach (field_info_instances($this->entity_type, $bundle) as $instance) { + $widget = entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default')->getComponent($instance['field_name']); // We define "tag-like" taxonomy fields as ones that use the // "Autocomplete term widget (tagging)" widget. - if ($instance['widget']['type'] == 'taxonomy_autocomplete') { + if ($widget['type'] == 'taxonomy_autocomplete') { $tag_fields[] = $instance['field_name']; } } diff --git a/core/modules/node/lib/Drupal/node/Tests/MultiStepNodeFormBasicOptionsTest.php b/core/modules/node/lib/Drupal/node/Tests/MultiStepNodeFormBasicOptionsTest.php index 7ee1e85..465a492 100644 --- a/core/modules/node/lib/Drupal/node/Tests/MultiStepNodeFormBasicOptionsTest.php +++ b/core/modules/node/lib/Drupal/node/Tests/MultiStepNodeFormBasicOptionsTest.php @@ -54,16 +54,14 @@ function testMultiStepNodeFormBasicOptions() { 'settings' => array( 'text_processing' => TRUE, ), - 'widget' => array( - 'type' => 'text_textfield', - ), - 'display' => array( - 'full' => array( - 'type' => 'text_default', - ), - ), ); field_create_instance($this->instance); + entity_get_form_display('node', 'page', 'default') + ->setComponent($this->field_name, array( + 'type' => 'text_textfield', + )) + ->save(); + $langcode = LANGUAGE_NOT_SPECIFIED; $edit = array( diff --git a/core/modules/node/lib/Drupal/node/Tests/NodeAccessFieldTest.php b/core/modules/node/lib/Drupal/node/Tests/NodeAccessFieldTest.php index 0db0bda..0e5f884 100644 --- a/core/modules/node/lib/Drupal/node/Tests/NodeAccessFieldTest.php +++ b/core/modules/node/lib/Drupal/node/Tests/NodeAccessFieldTest.php @@ -48,6 +48,9 @@ public function setUp() { entity_get_display('node', 'page', 'default') ->setComponent($this->field_name) ->save(); + entity_get_form_display('node', 'page', 'default') + ->setComponent($this->field_name, array('type' => 'text_text')) + ->save(); } /** diff --git a/core/modules/node/node.module b/core/modules/node/node.module index 4c7392b..bc51529 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -588,11 +588,17 @@ function node_add_body_field($type, $label = 'Body') { 'entity_type' => 'node', 'bundle' => $type->type, 'label' => $label, - 'widget' => array('type' => 'text_textarea_with_summary'), 'settings' => array('display_summary' => TRUE), ); $instance = field_create_instance($instance); + // Assign widget settings for the 'default' form mode. + entity_get_form_display('node', $type->type, 'default') + ->setComponent($field['field_name'], array( + 'type' => 'text_textarea_with_summary', + )) + ->save(); + // Assign display settings for the 'default' and 'teaser' view modes. entity_get_display('node', $type->type, 'default') ->setComponent($field['field_name'], array( diff --git a/core/modules/number/lib/Drupal/number/Tests/NumberFieldTest.php b/core/modules/number/lib/Drupal/number/Tests/NumberFieldTest.php index 925828c..d0b98ac 100644 --- a/core/modules/number/lib/Drupal/number/Tests/NumberFieldTest.php +++ b/core/modules/number/lib/Drupal/number/Tests/NumberFieldTest.php @@ -57,21 +57,21 @@ function testNumberDecimalField() { 'field_name' => $this->field['field_name'], 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', - 'widget' => array( + ); + field_create_instance($this->instance); + + entity_get_form_display('test_entity', 'test_bundle', 'default') + ->setComponent($this->field['field_name'], array( 'type' => 'number', 'settings' => array( 'placeholder' => '0.00' ), - ), - 'display' => array( - 'default' => array( - 'type' => 'number_decimal', - ), - ), - ); - field_create_instance($this->instance); + )) + ->save(); entity_get_display('test_entity', 'test_bundle', 'default') - ->setComponent($this->field['field_name']) + ->setComponent($this->field['field_name'], array( + 'type' => 'number_decimal', + )) ->save(); // Display creation form. diff --git a/core/modules/number/lib/Drupal/number/Tests/NumberItemTest.php b/core/modules/number/lib/Drupal/number/Tests/NumberItemTest.php index a0fc778..fa7e0fa 100644 --- a/core/modules/number/lib/Drupal/number/Tests/NumberItemTest.php +++ b/core/modules/number/lib/Drupal/number/Tests/NumberItemTest.php @@ -45,9 +45,6 @@ public function setUp() { 'entity_type' => 'entity_test', 'field_name' => 'field_' . $type, 'bundle' => 'entity_test', - 'widget' => array( - 'type' => 'number', - ), ); field_create_instance($this->instance[$type]); } diff --git a/core/modules/options/lib/Drupal/options/Tests/OptionsDynamicValuesTest.php b/core/modules/options/lib/Drupal/options/Tests/OptionsDynamicValuesTest.php index 4065b7d..4e97dbe 100644 --- a/core/modules/options/lib/Drupal/options/Tests/OptionsDynamicValuesTest.php +++ b/core/modules/options/lib/Drupal/options/Tests/OptionsDynamicValuesTest.php @@ -40,11 +40,14 @@ function setUp() { 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', 'required' => TRUE, - 'widget' => array( - 'type' => 'options_select', - ), ); $this->instance = field_create_instance($this->instance); + entity_get_form_display('test_entity', 'test_bundle', 'default') + ->setComponent($this->field_name, array( + 'type' => 'options_select', + )) + ->save(); + $this->test = array( 'id' => mt_rand(1, 10), // Make sure this does not equal the ID so that diff --git a/core/modules/options/lib/Drupal/options/Tests/OptionsFieldTest.php b/core/modules/options/lib/Drupal/options/Tests/OptionsFieldTest.php index ca9d605..51e771b 100644 --- a/core/modules/options/lib/Drupal/options/Tests/OptionsFieldTest.php +++ b/core/modules/options/lib/Drupal/options/Tests/OptionsFieldTest.php @@ -50,11 +50,13 @@ function setUp() { 'field_name' => $this->field_name, 'entity_type' => 'entity_test', 'bundle' => 'entity_test', - 'widget' => array( - 'type' => 'options_buttons', - ), ); $this->instance = field_create_instance($this->instance); + entity_get_form_display('entity_test', 'entity_test', 'default') + ->setComponent($this->field_name, array( + 'type' => 'options_buttons', + )) + ->save(); } /** @@ -115,11 +117,14 @@ function testUpdateAllowedValues() { 'field_name' => $this->field_name, 'entity_type' => 'entity_test', 'bundle' => 'entity_test', - 'widget' => array( - 'type' => 'options_buttons', - ), ); $this->instance = field_create_instance($this->instance); + entity_get_form_display('entity_test', 'entity_test', 'default') + ->setComponent($this->field_name, array( + 'type' => 'options_buttons', + )) + ->save(); + $entity = entity_create('entity_test', array()); $form = entity_get_form($entity); $this->assertTrue(!empty($form[$this->field_name][$langcode][1]), 'Option 1 exists'); diff --git a/core/modules/options/lib/Drupal/options/Tests/OptionsFieldUITest.php b/core/modules/options/lib/Drupal/options/Tests/OptionsFieldUITest.php index 5f6fcc1..6e21e86 100644 --- a/core/modules/options/lib/Drupal/options/Tests/OptionsFieldUITest.php +++ b/core/modules/options/lib/Drupal/options/Tests/OptionsFieldUITest.php @@ -258,6 +258,15 @@ protected function createOptionsField($type) { ); field_create_instance($instance); + // Grab the default widget for the field type. + $field_type_definition = field_info_field_types($field['type']); + $plugin_id = $field_type_definition['default_widget']; + entity_get_form_display('node', $this->type, 'default') + ->setComponent($this->field_name, array( + 'type' => $plugin_id, + )) + ->save(); + $this->admin_path = 'admin/structure/types/manage/' . $this->type . '/fields/' . $this->field_name . '/field-settings'; } diff --git a/core/modules/options/lib/Drupal/options/Tests/OptionsWidgetsTest.php b/core/modules/options/lib/Drupal/options/Tests/OptionsWidgetsTest.php index 023f012..9e2a43e 100644 --- a/core/modules/options/lib/Drupal/options/Tests/OptionsWidgetsTest.php +++ b/core/modules/options/lib/Drupal/options/Tests/OptionsWidgetsTest.php @@ -82,11 +82,14 @@ function testRadioButtons() { 'field_name' => $this->card_1['field_name'], 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', - 'widget' => array( - 'type' => 'options_buttons', - ), ); $instance = field_create_instance($instance); + entity_get_form_display('test_entity', 'test_bundle', 'default') + ->setComponent($this->card_1['field_name'], array( + 'type' => 'options_buttons', + )) + ->save(); + $langcode = LANGUAGE_NOT_SPECIFIED; // Create an entity. @@ -136,11 +139,14 @@ function testCheckBoxes() { 'field_name' => $this->card_2['field_name'], 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', - 'widget' => array( - 'type' => 'options_buttons', - ), ); $instance = field_create_instance($instance); + entity_get_form_display('test_entity', 'test_bundle', 'default') + ->setComponent($this->card_2['field_name'], array( + 'type' => 'options_buttons', + )) + ->save(); + $langcode = LANGUAGE_NOT_SPECIFIED; // Create an entity. @@ -224,11 +230,14 @@ function testSelectListSingle() { 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', 'required' => TRUE, - 'widget' => array( - 'type' => 'options_select', - ), ); $instance = field_create_instance($instance); + entity_get_form_display('test_entity', 'test_bundle', 'default') + ->setComponent($this->card_1['field_name'], array( + 'type' => 'options_select', + )) + ->save(); + $langcode = LANGUAGE_NOT_SPECIFIED; // Create an entity. @@ -320,11 +329,14 @@ function testSelectListMultiple() { 'field_name' => $this->card_2['field_name'], 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', - 'widget' => array( - 'type' => 'options_select', - ), ); $instance = field_create_instance($instance); + entity_get_form_display('test_entity', 'test_bundle', 'default') + ->setComponent($this->card_2['field_name'], array( + 'type' => 'options_select', + )) + ->save(); + $langcode = LANGUAGE_NOT_SPECIFIED; // Create an entity. @@ -437,11 +449,14 @@ function testOnOffCheckbox() { 'field_name' => $this->bool['field_name'], 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', - 'widget' => array( - 'type' => 'options_onoff', - ), ); $instance = field_create_instance($instance); + entity_get_form_display('test_entity', 'test_bundle', 'default') + ->setComponent($this->bool['field_name'], array( + 'type' => 'options_onoff', + )) + ->save(); + $langcode = LANGUAGE_NOT_SPECIFIED; // Create an entity. @@ -488,13 +503,15 @@ function testOnOffCheckbox() { 'field_name' => $this->bool['field_name'], 'entity_type' => 'node', 'bundle' => 'page', - 'widget' => array( - 'type' => 'options_onoff', - 'module' => 'options', - ), ); field_create_instance($instance); + entity_get_form_display('node', 'page', 'default') + ->setComponent($this->bool['field_name'], array( + 'type' => 'options_onoff', + )) + ->save(); + // Go to the edit page and check if the default settings works as expected $fieldEditUrl = 'admin/structure/types/manage/page/fields/bool'; $this->drupalGet($fieldEditUrl); diff --git a/core/modules/options/options.module b/core/modules/options/options.module index 793b07e..96b8ec3 100644 --- a/core/modules/options/options.module +++ b/core/modules/options/options.module @@ -151,10 +151,11 @@ 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') { + $widget = entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default')->getComponent($instance['field_name']); + if ($widget['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 ($widget['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())) . '

'; @@ -479,7 +480,8 @@ function options_field_widget_form(&$form, &$form_state, $field, $instance, $lan // reuse those widgets. $value_key = key($field['columns']); - $type = str_replace('options_', '', $instance['widget']['type']); + $widget = entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default')->getComponent($instance['field_name']); + $type = str_replace('options_', '', $widget['type']); $multiple = $field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED; $required = $element['#required']; $has_value = isset($items[0][$value_key]); @@ -540,7 +542,7 @@ 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']) { + if ($widget['settings']['display_label']) { $element['#title'] = $instance['label']; } break; @@ -560,11 +562,12 @@ 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') { + $widget = entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default')->getComponent($instance['field_name']); + if ($widget['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' => $widget['settings']['display_label'], '#weight' => -1, ); } @@ -851,7 +854,8 @@ function theme_options_none($variables) { $option = $variables['option']; $output = ''; - switch ($instance['widget']['type']) { + $widget = entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default')->getComponent($instance['field_name']); + switch ($widget['type']) { case 'options_buttons': $output = t('N/A'); break; diff --git a/core/modules/serialization/lib/Drupal/serialization/Tests/EntitySerializationTest.php b/core/modules/serialization/lib/Drupal/serialization/Tests/EntitySerializationTest.php index fb7019e..637564f 100644 --- a/core/modules/serialization/lib/Drupal/serialization/Tests/EntitySerializationTest.php +++ b/core/modules/serialization/lib/Drupal/serialization/Tests/EntitySerializationTest.php @@ -72,10 +72,6 @@ protected function setUp() { 'field_name' => 'field_test_text', 'bundle' => 'entity_test_mulrev', 'label' => 'Test text-field', - 'widget' => array( - 'type' => 'text_textfield', - 'weight' => 0, - ), ); field_create_instance($instance); diff --git a/core/modules/system/lib/Drupal/system/Tests/Ajax/MultiFormTest.php b/core/modules/system/lib/Drupal/system/Tests/Ajax/MultiFormTest.php index c9e7b6d..d3ba0b7 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Ajax/MultiFormTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Ajax/MultiFormTest.php @@ -46,6 +46,9 @@ function setUp() { 'bundle' => 'page', ); field_create_instance($instance); + entity_get_form_display('node', 'page', 'default') + ->setComponent($field_name, array('type' => 'text_default')) + ->save(); // Login a user who can create 'page' nodes. $this->web_user = $this->drupalCreateUser(array('create page content')); diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUnitTestBase.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUnitTestBase.php index b747e79..e021356 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUnitTestBase.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUnitTestBase.php @@ -19,7 +19,7 @@ * * @var array */ - public static $modules = array('user', 'system', 'field', 'text', 'field_sql_storage', 'entity_test'); + public static $modules = array('user', 'system', 'field', 'text', 'field_sql_storage', 'entity', 'entity_test'); public function setUp() { parent::setUp(); diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/FieldAccessTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/FieldAccessTest.php index 09e7a7a..ea0ddc3 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/FieldAccessTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/FieldAccessTest.php @@ -19,7 +19,7 @@ class FieldAccessTest extends DrupalUnitTestBase { * * @var array */ - public static $modules = array('entity_test', 'field', 'field_sql_storage', 'system', 'text', 'user'); + public static $modules = array('entity_test', 'field', 'field_sql_storage', 'system', 'text', 'user', 'entity'); /** * Holds the currently active global user ID that initiated the test run. diff --git a/core/modules/system/lib/Drupal/system/Tests/Form/ArbitraryRebuildTest.php b/core/modules/system/lib/Drupal/system/Tests/Form/ArbitraryRebuildTest.php index 1604063..f0609a1 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Form/ArbitraryRebuildTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Form/ArbitraryRebuildTest.php @@ -46,12 +46,14 @@ function setUp() { 'field_name' => 'test_multiple', 'bundle' => 'page', 'label' => 'Test a multiple valued field', - 'widget' => array( - 'type' => 'text_textfield', - 'weight' => 0, - ), ); field_create_instance($instance); + entity_get_form_display('node', 'page', 'default') + ->setComponent('test_multiple', array( + 'type' => 'text_textfield', + 'weight' => 0, + )) + ->save(); } /** diff --git a/core/modules/system/lib/Drupal/system/Tests/Form/RebuildTest.php b/core/modules/system/lib/Drupal/system/Tests/Form/RebuildTest.php index 3c0c0c5..8e9f649 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Form/RebuildTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Form/RebuildTest.php @@ -83,6 +83,9 @@ function testPreserveFormActionAfterAJAX() { 'bundle' => 'page', ); field_create_instance($instance); + entity_get_form_display('node', 'page', 'default') + ->setComponent($field_name, array('type' => 'text_test')) + ->save(); // Log in a user who can create 'page' nodes. $this->web_user = $this->drupalCreateUser(array('create page content')); diff --git a/core/modules/system/tests/modules/entity_test/entity_test.install b/core/modules/system/tests/modules/entity_test/entity_test.install index d0da050..ba43ab5 100644 --- a/core/modules/system/tests/modules/entity_test/entity_test.install +++ b/core/modules/system/tests/modules/entity_test/entity_test.install @@ -30,12 +30,12 @@ function entity_test_install() { 'field_name' => 'field_test_text', 'bundle' => $entity_type, 'label' => 'Test text-field', - 'widget' => array( - 'type' => 'text_textfield', - 'weight' => 0, - ), ); field_create_instance($instance); + + entity_get_form_display($entity_type, $entity_type, 'default') + ->setComponent('field_test_text', array('type' => 'text_text')) + ->save(); } } diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/RssTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/RssTest.php index 4c3e3d3..a1c0e67 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/RssTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/RssTest.php @@ -53,11 +53,13 @@ function setUp() { 'field_name' => 'taxonomy_' . $this->vocabulary->id(), 'bundle' => 'article', 'entity_type' => 'node', - 'widget' => array( - 'type' => 'options_select', - ), ); field_create_instance($this->instance); + entity_get_form_display('node', 'article', 'default') + ->setComponent($field['field_name'], array( + 'type' => 'options_select', + )) + ->save(); entity_get_display('node', 'article', 'default') ->setComponent('taxonomy_' . $this->vocabulary->id(), array( 'type' => 'taxonomy_term_reference_link', diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TaxonomyTermReferenceItemTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TaxonomyTermReferenceItemTest.php index ea9bc3e..4f54df9 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TaxonomyTermReferenceItemTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TaxonomyTermReferenceItemTest.php @@ -60,9 +60,6 @@ public function setUp() { 'entity_type' => 'entity_test', 'field_name' => 'field_test_taxonomy', 'bundle' => 'entity_test', - 'widget' => array( - 'type' => 'options_select', - ), ); field_create_instance($instance); $this->term = entity_create('taxonomy_term', array( diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldMultipleVocabularyTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldMultipleVocabularyTest.php index 8c5571c..0d317c4 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldMultipleVocabularyTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldMultipleVocabularyTest.php @@ -63,11 +63,13 @@ function setUp() { 'field_name' => $this->field_name, 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', - 'widget' => array( - 'type' => 'options_select', - ), ); field_create_instance($this->instance); + entity_get_form_display('test_entity', 'test_bundle', 'default') + ->setComponent($this->field_name, array( + 'type' => 'options_select', + )) + ->save(); entity_get_display('test_entity', 'test_bundle', 'full') ->setComponent($this->field_name, array( 'type' => 'taxonomy_term_reference_link', diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php index d5de5fa..b1c2bcf 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php @@ -58,11 +58,13 @@ function setUp() { 'field_name' => $this->field_name, 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', - 'widget' => array( - 'type' => 'options_select', - ), ); field_create_instance($this->instance); + entity_get_form_display('test_entity', 'test_bundle', 'default') + ->setComponent($this->field_name, array( + 'type' => 'options_select', + )) + ->save(); entity_get_display('test_entity', 'test_bundle', 'full') ->setComponent($this->field_name, array( 'type' => 'taxonomy_term_reference_link', diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermIndexTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermIndexTest.php index f9cba0d..e55945d 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermIndexTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermIndexTest.php @@ -49,11 +49,13 @@ function setUp() { 'field_name' => $this->field_name_1, 'bundle' => 'article', 'entity_type' => 'node', - 'widget' => array( - 'type' => 'options_select', - ), ); field_create_instance($this->instance_1); + entity_get_form_display('node', 'article', 'default') + ->setComponent($this->field_name_1, array( + 'type' => 'options_select', + )) + ->save(); entity_get_display('node', 'article', 'default') ->setComponent($this->field_name_1, array( 'type' => 'taxonomy_term_reference_link', @@ -79,11 +81,13 @@ function setUp() { 'field_name' => $this->field_name_2, 'bundle' => 'article', 'entity_type' => 'node', - 'widget' => array( - 'type' => 'options_select', - ), ); field_create_instance($this->instance_2); + entity_get_form_display('node', 'article', 'default') + ->setComponent($this->field_name_2, array( + 'type' => 'options_select', + )) + ->save(); entity_get_display('node', 'article', 'default') ->setComponent($this->field_name_2, array( 'type' => 'taxonomy_term_reference_link', diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTest.php index 93cc8d4..8668ef8 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTest.php @@ -45,11 +45,13 @@ function setUp() { 'field_name' => 'taxonomy_' . $this->vocabulary->id(), 'bundle' => 'article', 'entity_type' => 'node', - 'widget' => array( - 'type' => 'options_select', - ), ); field_create_instance($this->instance); + entity_get_form_display('node', 'article', 'default') + ->setComponent('taxonomy_' . $this->vocabulary->id(), array( + 'type' => 'options_select', + )) + ->save(); entity_get_display('node', 'article', 'default') ->setComponent($this->instance['field_name'], array( 'type' => 'taxonomy_term_reference_link', @@ -142,13 +144,14 @@ function testTaxonomyNode() { function testNodeTermCreationAndDeletion() { // Enable tags in the vocabulary. $instance = $this->instance; - $instance['widget'] = array( - 'type' => 'taxonomy_autocomplete', - 'settings' => array( - 'placeholder' => 'Start typing here.', - ), - ); - field_update_instance($instance); + entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default') + ->setComponent($instance['field_name'], array( + 'type' => 'taxonomy_autocomplete', + 'settings' => array( + 'placeholder' => 'Start typing here.', + ), + )) + ->save(); $terms = array( 'term1' => $this->randomName(), 'term2' => $this->randomName(), @@ -506,8 +509,11 @@ function testTaxonomyGetTermByName() { function testReSavingTags() { // Enable tags in the vocabulary. $instance = $this->instance; - $instance['widget'] = array('type' => 'taxonomy_autocomplete'); - field_update_instance($instance); + entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default') + ->setComponent($instance['field_name'], array( + 'type' => 'taxonomy_autocomplete', + )) + ->save(); // Create a term and a node using it. $term = $this->createTerm($this->vocabulary); diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TokenReplaceTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TokenReplaceTest.php index ea73152..1390139 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TokenReplaceTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TokenReplaceTest.php @@ -46,11 +46,13 @@ function setUp() { 'field_name' => 'taxonomy_' . $this->vocabulary->id(), 'bundle' => 'article', 'entity_type' => 'node', - 'widget' => array( - 'type' => 'options_select', - ), ); field_create_instance($this->instance); + entity_get_form_display('node', 'article', 'default') + ->setComponent('taxonomy_' . $this->vocabulary->id(), array( + 'type' => 'options_select', + )) + ->save(); entity_get_display('node', 'article', 'default') ->setComponent('taxonomy_' . $this->vocabulary->id(), array( 'type' => 'taxonomy_term_reference_link', 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 5f98990..d73e479 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/Views/TaxonomyTestBase.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/Views/TaxonomyTestBase.php @@ -100,13 +100,16 @@ protected function mockStandardInstall() { 'entity_type' => 'node', 'label' => 'Tags', 'bundle' => 'article', - 'widget' => array( - 'type' => 'taxonomy_autocomplete', - 'weight' => -4, - ), ); field_create_instance($instance); + entity_get_form_display('node', 'article', 'default') + ->setComponent($instance['field_name'], array( + 'type' => 'taxonomy_autocomplete', + 'weight' => -4, + )) + ->save(); + entity_get_display('node', 'article', 'default') ->setComponent($instance['field_name'], array( 'type' => 'taxonomy_term_reference_link', diff --git a/core/modules/telephone/lib/Drupal/telephone/Tests/TelephoneFieldTest.php b/core/modules/telephone/lib/Drupal/telephone/Tests/TelephoneFieldTest.php index 4d09f45..bbac418 100644 --- a/core/modules/telephone/lib/Drupal/telephone/Tests/TelephoneFieldTest.php +++ b/core/modules/telephone/lib/Drupal/telephone/Tests/TelephoneFieldTest.php @@ -67,6 +67,12 @@ function testTelephoneField() { ); field_create_instance($instance); + entity_get_form_display('node', 'article', 'default') + ->setComponent('field_telephone', array( + 'type' => 'telephone_default', + )) + ->save(); + entity_get_display('node', 'article', 'default') ->setComponent('field_telephone', array( 'type' => 'telephone_link', diff --git a/core/modules/telephone/lib/Drupal/telephone/Tests/TelephoneItemTest.php b/core/modules/telephone/lib/Drupal/telephone/Tests/TelephoneItemTest.php index 35012b3..d809161 100644 --- a/core/modules/telephone/lib/Drupal/telephone/Tests/TelephoneItemTest.php +++ b/core/modules/telephone/lib/Drupal/telephone/Tests/TelephoneItemTest.php @@ -44,9 +44,6 @@ public function setUp() { 'entity_type' => 'entity_test', 'field_name' => 'field_test', 'bundle' => 'entity_test', - 'widget' => array( - 'type' => 'telephone_default', - ), ); field_create_instance($this->instance); } diff --git a/core/modules/text/lib/Drupal/text/Tests/Formatter/TextPlainUnitTest.php b/core/modules/text/lib/Drupal/text/Tests/Formatter/TextPlainUnitTest.php index 716ead7..f65b752 100644 --- a/core/modules/text/lib/Drupal/text/Tests/Formatter/TextPlainUnitTest.php +++ b/core/modules/text/lib/Drupal/text/Tests/Formatter/TextPlainUnitTest.php @@ -62,9 +62,6 @@ function setUp() { 'text_processing' => FALSE, ); - $this->widget_type = 'text_textarea'; - $this->widget_settings = array(); - $this->formatter_type = 'text_plain'; $this->formatter_settings = array(); @@ -81,10 +78,6 @@ function setUp() { 'field_name' => $this->field_name, 'label' => $this->randomName(), 'settings' => $this->instance_settings, - 'widget' => array( - 'type' => $this->widget_type, - 'settings' => $this->widget_settings, - ), ); $this->instance = field_create_instance($this->instance); diff --git a/core/modules/text/lib/Drupal/text/Tests/TextFieldTest.php b/core/modules/text/lib/Drupal/text/Tests/TextFieldTest.php index f8c585b..d702836 100644 --- a/core/modules/text/lib/Drupal/text/Tests/TextFieldTest.php +++ b/core/modules/text/lib/Drupal/text/Tests/TextFieldTest.php @@ -62,14 +62,16 @@ function testTextFieldValidation() { 'field_name' => $this->field['field_name'], 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', - 'widget' => array( - 'type' => 'text_textfield', - ), ); field_create_instance($this->instance); - entity_get_display('test_entity', 'test_bundle', 'default') - ->setComponent($this->field['field_name']) + entity_get_form_display('test_entity', 'test_bundle', 'default') + ->setComponent($this->field['field_name'], array( + 'type' => 'text_textfield', + )) ->save(); +// entity_get_display('test_entity', 'test_bundle', 'default') +// ->setComponent($this->field['field_name']) +// ->save(); // Test valid and invalid values with field_attach_validate(). $entity = field_test_create_entity(); @@ -111,14 +113,16 @@ function _testTextfieldWidgets($field_type, $widget_type) { 'settings' => array( 'text_processing' => TRUE, ), - 'widget' => array( + ); + field_create_instance($this->instance); + entity_get_form_display('test_entity', 'test_bundle', 'default') + ->setComponent($this->field_name, array( 'type' => $widget_type, 'settings' => array( 'placeholder' => 'A placeholder on ' . $widget_type, ), - ), - ); - field_create_instance($this->instance); + )) + ->save(); entity_get_display('test_entity', 'test_bundle', 'full') ->setComponent($this->field_name) ->save(); @@ -174,11 +178,13 @@ function _testTextfieldWidgetsFormatted($field_type, $widget_type) { 'settings' => array( 'text_processing' => TRUE, ), - 'widget' => array( - 'type' => $widget_type, - ), ); field_create_instance($this->instance); + entity_get_form_display('test_entity', 'test_bundle', 'default') + ->setComponent($this->field_name, array( + 'type' => $widget_type, + )) + ->save(); entity_get_display('test_entity', 'test_bundle', 'full') ->setComponent($this->field_name) ->save(); diff --git a/core/modules/translation_entity/lib/Drupal/translation_entity/Tests/EntityTranslationSyncImageTest.php b/core/modules/translation_entity/lib/Drupal/translation_entity/Tests/EntityTranslationSyncImageTest.php index 61f93cf..aa9c626 100644 --- a/core/modules/translation_entity/lib/Drupal/translation_entity/Tests/EntityTranslationSyncImageTest.php +++ b/core/modules/translation_entity/lib/Drupal/translation_entity/Tests/EntityTranslationSyncImageTest.php @@ -69,10 +69,6 @@ protected function setupTestFields() { 'field_name' => $this->fieldName, 'bundle' => $this->entityType, 'label' => 'Test translatable image field', - 'widget' => array( - 'type' => 'image_image', - 'weight' => 0, - ), 'settings' => array( 'translation_sync' => array( 'file' => FALSE, diff --git a/core/modules/translation_entity/lib/Drupal/translation_entity/Tests/EntityTranslationTestBase.php b/core/modules/translation_entity/lib/Drupal/translation_entity/Tests/EntityTranslationTestBase.php index 13e1f59..870321f 100644 --- a/core/modules/translation_entity/lib/Drupal/translation_entity/Tests/EntityTranslationTestBase.php +++ b/core/modules/translation_entity/lib/Drupal/translation_entity/Tests/EntityTranslationTestBase.php @@ -170,12 +170,14 @@ protected function setupTestFields() { 'field_name' => $this->fieldName, 'bundle' => $this->bundle, 'label' => 'Test translatable text-field', - 'widget' => array( - 'type' => 'text_textfield', - 'weight' => 0, - ), ); field_create_instance($instance); + entity_get_form_display($this->entityType, $this->bundle, 'default') + ->setComponent($this->fieldName, array( + 'type' => 'text_textfield', + 'weight' => 0, + )) + ->save(); } /** diff --git a/core/modules/user/lib/Drupal/user/Tests/UserRegistrationTest.php b/core/modules/user/lib/Drupal/user/Tests/UserRegistrationTest.php index 38ca376..db649b0 100644 --- a/core/modules/user/lib/Drupal/user/Tests/UserRegistrationTest.php +++ b/core/modules/user/lib/Drupal/user/Tests/UserRegistrationTest.php @@ -209,6 +209,9 @@ function testRegistrationWithUserFields() { 'settings' => array('user_register_form' => FALSE), ); field_create_instance($instance); + entity_get_form_display('user', 'user', 'default') + ->setComponent('test_user_field', array('type' => 'test_field_widget')) + ->save(); // Check that the field does not appear on the registration form. $this->drupalGet('user/register'); diff --git a/core/modules/user/user.install b/core/modules/user/user.install index c7fd16b..496c6cc 100644 --- a/core/modules/user/user.install +++ b/core/modules/user/user.install @@ -339,17 +339,20 @@ function user_install_picture_field() { 'min_resolution' => '', 'default_image' => 0, ), - 'widget' => array( - 'module' => 'image', + ); + field_create_instance($instance); + + // Assign form display settings for the 'default' view mode. + entity_get_form_display('user', 'user', 'default') + ->setComponent('user_picture', array( 'type' => 'image_image', 'settings' => array( 'progress_indicator' => 'throbber', 'preview_image_style' => 'thumbnail', ), 'weight' => -1, - ), - ); - field_create_instance($instance); + )) + ->save(); // Assign display settings for the 'default' and 'compact' view modes. entity_get_display('user', 'user', 'default') @@ -735,29 +738,33 @@ function user_update_8011() { 'min_resolution' => '', 'default_image' => !empty($default_image_fid) ? $default_image_fid : 0, ), - 'widget' => array( - 'module' => 'image', - 'type' => 'image_image', + ); + _update_7000_field_create_instance($field, $instance); + + module_load_install('entity'); + if (update_variable_get('user_pictures', 0)) { + $formatter = 'image'; + $widget = 'image_image'; + } + else { + $formatter = 'hidden'; + $widget = 'field_hidden'; + } + + // Assign form settings for the 'default' form mode. + $form_display = _update_8000_entity_get_form_display('user', 'user', 'default'); + $form_display->set('content.user_picture', array( + 'type' => $widget, 'settings' => array( 'progress_indicator' => 'throbber', 'preview_image_style' => 'thumbnail', ), 'weight' => -1, - ), - ); - _update_7000_field_create_instance($field, $instance); - - // Assign display settings for the 'default' and 'compact' view modes. In D7, - // user pictures did not require Image module to work. Image module only - // allowed usage of an image style to format user pictures in the output. - // The 'user_pictures' variable had a global effect on the presence of the - // user picture functionality before. The new user picture image field is - // created regardless of that global setting, which means the field appears - // on the user account form after migration, even if user pictures were - // disabled previously. The picture is only hidden in the output. - $formatter = update_variable_get('user_pictures', 0) ? 'image' : 'hidden'; - module_load_install('entity'); + )) + ->save(); + update_config_manifest_add('entity.form_display', array($form_display->get('id'))); + // Assign display settings for the 'default' and 'compact' view modes. $display = _update_8000_entity_get_display('user', 'user', 'default'); $display->set('content.user_picture', array( 'label' => 'hidden', diff --git a/core/modules/views/lib/Drupal/views/Tests/DefaultViewsTest.php b/core/modules/views/lib/Drupal/views/Tests/DefaultViewsTest.php index c4f90b8..174c598 100644 --- a/core/modules/views/lib/Drupal/views/Tests/DefaultViewsTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/DefaultViewsTest.php @@ -77,16 +77,8 @@ protected function setUp() { 'field_name' => $this->field_name, 'entity_type' => 'node', 'bundle' => 'page', - 'widget' => array( - 'type' => 'options_select', - ), ); field_create_instance($this->instance); - entity_get_display('node', 'page', 'full') - ->setComponent($this->field_name, array( - 'type' => 'taxonomy_term_reference_link', - )) - ->save(); // Create a time in the past for the archive. $time = time() - 3600; 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..bb2696c 100644 --- a/core/modules/views/lib/Drupal/views/Tests/Wizard/TaggedWithTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/Wizard/TaggedWithTest.php @@ -74,12 +74,15 @@ function setUp() { 'field_name' => 'field_views_testing_tags', 'entity_type' => 'node', 'bundle' => $this->node_type_with_tags->type, - 'widget' => array( - 'type' => 'taxonomy_autocomplete', - ), ); field_create_instance($this->tag_instance); + entity_get_form_display('node', $this->node_type_with_tags->type, 'default') + ->setComponent('field_views_testing_tags', array( + 'type' => 'taxonomy_autocomplete', + )) + ->save(); + entity_get_display('node', $this->node_type_with_tags->type, 'default') ->setComponent('field_views_testing_tags', array( 'type' => 'taxonomy_term_reference_link', @@ -185,6 +188,12 @@ function testTaggedWithByNodeType() { $instance = $this->tag_instance; $instance['bundle'] = $this->node_type_without_tags->type; field_create_instance($instance); + entity_get_form_display('node', $this->node_type_without_tags->type, 'default') + ->setComponent('field_views_testing_tags', array( + 'type' => 'taxonomy_autocomplete', + )) + ->save(); + $view['show[type]'] = $this->node_type_with_tags->type; $this->drupalPost('admin/structure/views/add', $view, t('Update "of type" choice')); $this->assertFieldByXpath($tags_xpath); diff --git a/core/profiles/standard/standard.install b/core/profiles/standard/standard.install index 13463b9..8beabcc 100644 --- a/core/profiles/standard/standard.install +++ b/core/profiles/standard/standard.install @@ -127,13 +127,17 @@ function standard_install() { 'label' => 'Tags', 'bundle' => 'article', 'description' => $vocabulary->help, - 'widget' => array( - 'type' => 'taxonomy_autocomplete', - 'weight' => -4, - ), ); field_create_instance($instance); + // Assign widget settings for the 'default' form mode. + entity_get_form_display('node', 'article', 'default') + ->setComponent($instance['field_name'], array( + 'type' => 'taxonomy_autocomplete', + 'weight' => -4, + )) + ->save(); + // Assign display settings for the 'default' and 'teaser' view modes. entity_get_display('node', 'article', 'default') ->setComponent($instance['field_name'], array( @@ -189,17 +193,20 @@ function standard_install() { 'alt_field' => TRUE, 'title_field' => '', ), + ); + field_create_instance($instance); - 'widget' => array( + // Assign widget settings for the 'default' form mode. + entity_get_form_display('node', 'article', 'default') + ->setComponent($instance['field_name'], array( 'type' => 'image_image', 'settings' => array( 'progress_indicator' => 'throbber', 'preview_image_style' => 'thumbnail', ), 'weight' => -1, - ), - ); - field_create_instance($instance); + )) + ->save(); // Assign display settings for the 'default' and 'teaser' view modes. entity_get_display('node', 'article', 'default')