From e59e9d948fa31fbfbf36b08611e04a74aab4dd93 Mon Sep 17 00:00:00 2001 From: webchick Date: Sat, 10 Dec 2011 23:15:51 -0600 Subject: Issue #1301522 by xjm, Rob Loach, David_Rothstein: Fixed field_ui_default_value_widget() does not pass along the entity type when it creates the default value form. --- modules/field/tests/field_test.module | 35 +++++++++++++++ modules/field_ui/field_ui.admin.inc | 2 +- modules/field_ui/field_ui.test | 84 +++++++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 1 deletion(-) diff --git a/modules/field/tests/field_test.module b/modules/field/tests/field_test.module index 7f43fbf09..4a87b060e 100644 --- a/modules/field/tests/field_test.module +++ b/modules/field/tests/field_test.module @@ -248,3 +248,38 @@ function field_test_field_attach_view_alter(&$output, $context) { $output['test_field'][] = array('#markup' => 'field_test_field_attach_view_alter'); } } + +/** + * 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) { + switch ($context['field']['field_name']) { + case 'alter_test_text': + drupal_set_message('Field size: ' . $context['instance']['widget']['settings']['size']); + break; + + case 'alter_test_options': + drupal_set_message('Widget type: ' . $context['instance']['widget']['type']); + break; + } +} diff --git a/modules/field_ui/field_ui.admin.inc b/modules/field_ui/field_ui.admin.inc index 7aff69b7e..e8152ea21 100644 --- a/modules/field_ui/field_ui.admin.inc +++ b/modules/field_ui/field_ui.admin.inc @@ -1947,7 +1947,7 @@ function field_ui_default_value_widget($field, $instance, &$form, &$form_state) $instance['description'] = ''; // @todo Allow multiple values (requires more work on 'add more' JS handler). - $element += field_default_form(NULL, NULL, $field, $instance, LANGUAGE_NONE, $items, $element, $form_state, 0); + $element += field_default_form($instance['entity_type'], NULL, $field, $instance, LANGUAGE_NONE, $items, $element, $form_state, 0); return $element; } diff --git a/modules/field_ui/field_ui.test b/modules/field_ui/field_ui.test index e7c5e18e7..ea8727e5b 100644 --- a/modules/field_ui/field_ui.test +++ b/modules/field_ui/field_ui.test @@ -643,3 +643,87 @@ class FieldUIManageDisplayTestCase extends FieldUITestCase { return $return; } } + +/** + * Tests custom widget hooks and callbacks on the field administration pages. + */ +class FieldUIAlterTestCase extends DrupalWebTestCase { + public static function getInfo() { + return array( + 'name' => 'Widget customization', + 'description' => 'Test custom field widget hooks and callbacks on field administration pages.', + 'group' => 'Field UI', + ); + } + + function setUp() { + parent::setUp(array('field_test')); + + // Create test user. + $admin_user = $this->drupalCreateUser(array('access content', 'administer content types', 'administer users')); + $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', + )); + field_create_instance(array( + 'field_name' => 'alter_test_text', + 'entity_type' => 'node', + 'bundle' => 'article', + 'widget' => array( + 'type' => 'text_textfield', + 'size' => 60, + ), + )); + + // 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().'); + + // 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. + field_create_instance(array( + 'field_name' => 'alter_test_options', + 'entity_type' => 'user', + 'bundle' => 'user', + 'widget' => array( + 'type' => 'options_select', + ) + )); + field_create_instance(array( + 'field_name' => 'alter_test_options', + 'entity_type' => 'node', + 'bundle' => 'page', + 'widget' => array( + 'type' => 'options_select', + ) + )); + + // 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().'); + } +} -- cgit v1.2.3