You are here

public function NameFormatter::settingsSummary in Name Field 8

Returns a short summary for the current formatter settings.

If an empty result is returned, a UI can still be provided to display a settings form in case the formatter has configurable settings.

Return value

string[] A short summary of the formatter settings.

Overrides FormatterBase::settingsSummary

File

src/Plugin/Field/FieldFormatter/NameFormatter.php, line 208

Class

NameFormatter
Plugin implementation of the 'name' formatter.

Namespace

Drupal\name\Plugin\Field\FieldFormatter

Code

public function settingsSummary() {
  $settings = $this
    ->getSettings();
  $summary = [];

  // Name format.
  $machine_name = isset($settings['format']) ? $settings['format'] : 'default';
  $name_format = $this->entityTypeManager
    ->getStorage('name_format')
    ->load($machine_name);
  if ($name_format) {
    $summary[] = $this
      ->t('Format: @format (@machine_name)', [
      '@format' => $name_format
        ->label(),
      '@machine_name' => $name_format
        ->id(),
    ]);
  }
  else {
    $summary[] = $this
      ->t('Format: <strong>Missing format.</strong><br/>This field will be displayed using the Default format.');
  }

  // List format.
  if (!isset($settings['list_format']) || $settings['list_format'] == '') {
    $summary[] = $this
      ->t('List format: Individually');
  }
  else {
    $machine_name = isset($settings['list_format']) ? $settings['list_format'] : 'default';
    $name_format = $this->entityTypeManager
      ->getStorage('name_list_format')
      ->load($machine_name);
    if ($name_format) {
      $summary[] = $this
        ->t('List format: @format (@machine_name)', [
        '@format' => $name_format
          ->label(),
        '@machine_name' => $name_format
          ->id(),
      ]);
    }
    else {
      $summary[] = $this
        ->t('List format: <strong>Missing list format.</strong><br/>This field will be displayed using the Default list format.');
    }
  }

  // Additional options.
  $markup_options = $this->parser
    ->getMarkupOptions();
  $summary[] = $this
    ->t('Markup: @type', [
    '@type' => $markup_options[$this
      ->getSetting('markup')],
  ]);
  if (!empty($settings['link_target'])) {
    $targets = $this
      ->getLinkableTargets();
    $summary[] = $this
      ->t('Link: @target', [
      '@target' => empty($targets[$settings['link_target']]) ? $this
        ->t('-- invalid --') : $targets[$settings['link_target']],
    ]);
  }
  $this
    ->settingsNameAdditionalPreferredSummary($summary);

  // Provide an example of the selected format.
  if ($name_format) {
    $names = $this->generator
      ->loadSampleValues(1, $this->fieldDefinition);
    if ($name = reset($names)) {
      $formatted = $this->parser
        ->parse($name, $name_format
        ->get('pattern'));
      if (empty($formatted)) {
        $summary[] = $this
          ->t('Example: <em>&lt;&lt;empty&gt;&gt;</em>');
      }
      else {
        $summary[] = $this
          ->t('Example: @example', [
          '@example' => $formatted,
        ]);
      }
    }
  }
  return $summary;
}