Issue #945524 by Jelle_S, tim.plunkett, Dave Reid, pfrenssen, xjm, swentel: Added Field formatter settings hooks in core.
parent
2edfc475e5
commit
35a3762014
|
@ -280,3 +280,22 @@ function field_test_query_efq_table_prefixing_test_alter(&$query) {
|
|||
// exception if the EFQ does not properly prefix the base table.
|
||||
$query->join('test_entity','te2','%alias.ftid = test_entity.ftid');
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_formatter_settings_form_alter().
|
||||
*/
|
||||
function field_test_field_formatter_settings_form_alter(&$element, &$form_state, $context) {
|
||||
$settings = $context['instance']['display'][$context['view_mode']]['settings'];
|
||||
$element['field_test_formatter_settings_form_alter'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Formatter settings form alter'),
|
||||
'#default_value' => isset($settings['field_test_formatter_settings_form_alter']) ? $settings['field_test_formatter_settings_form_alter'] : '',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_formatter_settings_summary_alter().
|
||||
*/
|
||||
function field_test_field_formatter_settings_summary_alter(&$summary, $context) {
|
||||
$summary .= '<br />field_test_field_formatter_settings_summary_alter';
|
||||
}
|
||||
|
|
|
@ -1066,6 +1066,17 @@ function field_ui_display_overview_form($form, &$form_state, $entity_type, $bund
|
|||
$settings_form = $function($field, $instance, $view_mode, $form, $form_state);
|
||||
}
|
||||
|
||||
// Allow other modules to alter the formatter settings form.
|
||||
$context = array(
|
||||
'module' => $formatter['module'],
|
||||
'formatter' => $formatter,
|
||||
'field' => $field,
|
||||
'instance' => $instance,
|
||||
'view_mode' => $view_mode,
|
||||
'form' => $form,
|
||||
);
|
||||
drupal_alter('field_formatter_settings_form', $settings_form, $form_state, $context);
|
||||
|
||||
if ($settings_form) {
|
||||
$table[$name]['format']['#cell_attributes'] = array('colspan' => 3);
|
||||
$table[$name]['format']['settings_edit_form'] = array(
|
||||
|
@ -1101,6 +1112,15 @@ function field_ui_display_overview_form($form, &$form_state, $entity_type, $bund
|
|||
else {
|
||||
// Display a summary of the current formatter settings.
|
||||
$summary = module_invoke($formatter['module'], 'field_formatter_settings_summary', $field, $instance, $view_mode);
|
||||
|
||||
// Allow other modules to alter the summary.
|
||||
$context = array(
|
||||
'field' => $field,
|
||||
'instance' => $instance,
|
||||
'view_mode' => $view_mode,
|
||||
);
|
||||
drupal_alter('field_formatter_settings_summary', $summary, $context);
|
||||
|
||||
$table[$name]['settings_summary'] = array();
|
||||
$table[$name]['settings_edit'] = array();
|
||||
if ($summary) {
|
||||
|
|
|
@ -127,6 +127,56 @@ function hook_field_formatter_settings_form($field, $instance, $view_mode, $form
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Alter the formatter settings form.
|
||||
*
|
||||
* @param $element
|
||||
* Form array as returned by hook_field_formatter_settings_form().
|
||||
* @param $form_state
|
||||
* The form state of the (entire) configuration form.
|
||||
* @param $context
|
||||
* An associative array with the following elements:
|
||||
* - 'module': The module that contains the definition of this formatter.
|
||||
* - 'formatter': The formatter type description array.
|
||||
* - 'field': The field structure being configured.
|
||||
* - 'instance': The instance structure being configured.
|
||||
* - 'view_mode': The view mode being configured.
|
||||
* - 'form': The (entire) configuration form array.
|
||||
*/
|
||||
function hook_field_formatter_settings_form_alter(&$element, &$form_state, $context) {
|
||||
// Add a mysetting checkbox to the settings form for foo_field fields.
|
||||
if ($context['field']['type'] == 'foo_field') {
|
||||
$display = $context['instance']['display'][$context['view_mode']];
|
||||
$element['mysetting'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('My setting'),
|
||||
'#default_value' => $display['settings']['mysetting'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Alter the field formatter settings summary.
|
||||
*
|
||||
* @param $summary
|
||||
* The summary as returned by hook_field_formatter_settings_summary().
|
||||
* @param $context
|
||||
* An associative array with the following elements:
|
||||
* - 'field': The field structure being configured.
|
||||
* - 'instance': The instance structure being configured.
|
||||
* - 'view_mode': The view mode being configured.
|
||||
*/
|
||||
function hook_field_formatter_settings_summary_alter(&$summary, $context) {
|
||||
// Append a message to the summary when an instance of foo_field has
|
||||
// mysetting set to TRUE for the current view mode.
|
||||
if ($context['field']['type'] == 'foo_field') {
|
||||
$display = $context['instance']['display'][$context['view_mode']];
|
||||
if ($display['settings']['mysetting']) {
|
||||
$summary .= '<br />' . t('My setting enabled.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a short summary for the current formatter settings of an instance.
|
||||
*
|
||||
|
|
|
@ -19,7 +19,7 @@ class ManageDisplayTest extends FieldUiTestBase {
|
|||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('search');
|
||||
public static $modules = array('search', 'field_test');
|
||||
|
||||
public static function getInfo() {
|
||||
return array(
|
||||
|
@ -75,6 +75,24 @@ class ManageDisplayTest extends FieldUiTestBase {
|
|||
$current_setting_value = $instance['display']['default']['settings'][$setting_name];
|
||||
$this->assertEqual($current_format, $format, t('The formatter was updated.'));
|
||||
$this->assertEqual($current_setting_value, $setting_value, t('The setting was updated.'));
|
||||
|
||||
// Assert that hook_field_formatter_settings_summary_alter() is called.
|
||||
$this->assertText('field_test_field_formatter_settings_summary_alter');
|
||||
|
||||
// Click on the formatter settings button to open the formatter settings
|
||||
// form.
|
||||
$this->drupalPostAJAX(NULL, array(), "field_test_formatter_settings_edit");
|
||||
|
||||
// Assert that the field added in
|
||||
// field_test_field_formatter_settings_form_alter() is present.
|
||||
$fieldname = 'fields[field_test][settings_edit_form][settings][field_test_formatter_settings_form_alter]';
|
||||
$this->assertField($fieldname, 'The field added in hook_field_formatter_settings_form_alter() is present on the settings form.');
|
||||
$edit = array($fieldname => 'foo');
|
||||
$this->drupalPostAJAX(NULL, $edit, "field_test_formatter_settings_update");
|
||||
|
||||
// Confirm that the settings are updated on the settings form.
|
||||
$this->drupalPostAJAX(NULL, array(), "field_test_formatter_settings_edit");
|
||||
$this->assertFieldByName($fieldname, 'foo');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue