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.

8.0.x
Nathaniel 2011-11-21 16:58:37 +09:00
parent 0546cadbba
commit dfa187c483
3 changed files with 120 additions and 1 deletions

View File

@ -250,3 +250,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;
}
}

View File

@ -1971,7 +1971,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;
}

View File

@ -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().');
}
}