You are here

public function FFTFormatter::settingsForm in Field Formatter Template 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/FFTFormatter.php \Drupal\fft\Plugin\Field\FieldFormatter\FFTFormatter::settingsForm()

Returns a form to configure settings for the formatter.

Invoked from \Drupal\field_ui\Form\EntityDisplayFormBase to allow administrators to configure the formatter. The field_ui module takes care of handling submitted form values.

Parameters

array $form: The form where the settings form is being included in.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form elements for the formatter settings.

Overrides FormatterBase::settingsForm

File

src/Plugin/Field/FieldFormatter/FFTFormatter.php, line 55

Class

FFTFormatter
Plugin implementation of the 'fft_formatter' formatter.

Namespace

Drupal\fft\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $form = parent::settingsForm($form, $form_state);
  $settings = $this
    ->getSettings();
  $field['type'] = $this->fieldDefinition
    ->getType();
  $fft_templates = fft_get_templates();
  $options_set = $fft_templates['templates'];
  $form['#attached']['library'][] = 'fft/backend';
  if ($settings['isNew'] == 0) {
    $fft_templates['settings'][$settings['template']] = $settings['settings'];
  }
  $form['#attached']['drupalSettings']['fft'] = !empty($fft_templates['settings']) ? $fft_templates['settings'] : [];
  $form['isNew'] = [
    '#type' => 'hidden',
    '#value' => 0,
  ];
  $form['template'] = [
    '#title' => $this
      ->t('Template'),
    '#type' => 'select',
    '#options' => $options_set,
    '#default_value' => $settings['template'],
    '#attributes' => [
      'class' => [
        'fft-template',
      ],
    ],
  ];
  $form['reset'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Reset'),
    '#options' => [
      0 => 'No',
      1 => 'Yes',
    ],
    '#default_value' => $settings['reset'],
    '#description' => $this
      ->t('Reset default Drupal field wrapper markup.'),
  ];
  switch ($field['type']) {
    case 'image':
      $image_style_options = image_style_options();
      $form['image_style_1'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Image Styles 1'),
        '#options' => $image_style_options,
        '#default_value' => $settings['image_style_1'],
      ];
      $form['image_style_2'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Image Styles 2'),
        '#options' => $image_style_options,
        '#default_value' => $settings['image_style_2'],
      ];
      break;
  }
  $settings_des[] = $this
    ->t('Add settings extras for template, one setting per line with syntax key = value.');
  $settings_des[] = $this
    ->t('Support array like key[] = value or key[name] = value.');
  $form['settings'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Settings Extras'),
    '#default_value' => '',
    '#attributes' => [
      'class' => [
        'fft-settings',
      ],
    ],
  ];
  $form['settings_des'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('More Information'),
    '#open' => FALSE,
  ];
  $form['settings_des']['info'] = [
    '#type' => 'markup',
    '#markup' => nl2br(implode("\r\n", $settings_des)),
  ];
  return $form;
}