Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By swentel on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.x
Issue links:
Description:
In Drupal 7, hook_field_formatter_settings_summary() needed to return a string. This has changed in Drupal 8 where the formatter::settingsSummary() now needs to return an array. This affects hook_field_formatter_settings_summary_alter() which now receives an array instead of a string.
Drupal 7
/**
* Implements hook_field_formatter_settings_summary().
*/
function text_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = '';
if (strpos($display['type'], '_trimmed') !== FALSE) {
$summary = t('Trim length') . ': ' . $settings['trim_length'];
}
return $summary;
}
Drupal 8
// From \Drupal\text\Plugin\field\formatter\TextTrimmedFormatter.
public function settingsSummary() {
$summary = array();
$summary[] = t('Trim length: @trim_length', array('@trim_length' => $this->getSetting('trim_length')));
return $summary;
}
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') {
if ($context['formatter']->getSetting('mysetting')) {
$summary[] = t('My setting enabled.');
}
}
}