You are here

public function SmartTrimFormatter::settingsForm in Smart Trim 8

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/SmartTrimFormatter.php, line 58

Class

SmartTrimFormatter
Plugin implementation of the 'smart_trim' formatter.

Namespace

Drupal\smart_trim\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $element = parent::settingsForm($form, $form_state);
  $element['trim_length'] = [
    '#title' => $this
      ->t('Trim length'),
    '#type' => 'textfield',
    '#size' => 10,
    '#default_value' => $this
      ->getSetting('trim_length'),
    '#min' => 0,
    '#required' => TRUE,
  ];
  $element['trim_type'] = [
    '#title' => $this
      ->t('Trim units'),
    '#type' => 'select',
    '#options' => [
      'chars' => $this
        ->t("Characters"),
      'words' => $this
        ->t("Words"),
    ],
    '#default_value' => $this
      ->getSetting('trim_type'),
  ];
  $element['trim_suffix'] = [
    '#title' => $this
      ->t('Suffix'),
    '#type' => 'textfield',
    '#size' => 10,
    '#default_value' => $this
      ->getSetting('trim_suffix'),
  ];
  $element['wrap_output'] = [
    '#title' => $this
      ->t('Wrap trimmed content?'),
    '#type' => 'checkbox',
    '#default_value' => $this
      ->getSetting('wrap_output'),
    '#description' => $this
      ->t('Adds a wrapper div to trimmed content.'),
  ];
  $element['wrap_class'] = [
    '#title' => $this
      ->t('Wrapped content class.'),
    '#type' => 'textfield',
    '#size' => 20,
    '#default_value' => $this
      ->getSetting('wrap_class'),
    '#description' => $this
      ->t('If wrapping, define the class name here.'),
    '#states' => [
      'visible' => [
        ':input[name="fields[body][settings_edit_form][settings][wrap_output]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $element['more_link'] = [
    '#title' => $this
      ->t('Display more link?'),
    '#type' => 'checkbox',
    '#default_value' => $this
      ->getSetting('more_link'),
    '#description' => $this
      ->t('Displays a link to the entity (if one exists)'),
  ];
  $element['more_text'] = [
    '#title' => $this
      ->t('More link text'),
    '#type' => 'textfield',
    '#size' => 20,
    '#default_value' => $this
      ->getSetting('more_text'),
    '#description' => $this
      ->t('If displaying more link, enter the text for the link.'),
    '#states' => [
      'visible' => [
        ':input[name="fields[body][settings_edit_form][settings][more_link]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $element['more_aria_label'] = [
    '#title' => $this
      ->t('More link aria-label'),
    '#type' => 'textfield',
    '#size' => 30,
    '#default_value' => $this
      ->getSetting('more_aria_label'),
    '#description' => $this
      ->t('If displaying more link, provide additional context for screen-reader users. Tokens supported. In most cases, the aria-label value will be announced instead of the link text.'),
    '#states' => [
      'visible' => [
        ':input[name="fields[body][settings_edit_form][settings][more_link]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $element['token_browser'] = [
    '#type' => 'item',
    '#theme' => 'token_tree_link',
    '#token_types' => [
      $this->fieldDefinition
        ->getTargetEntityTypeId(),
    ],
    '#states' => [
      'visible' => [
        ':input[name="fields[body][settings_edit_form][settings][more_link]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $element['more_class'] = [
    '#title' => $this
      ->t('More link class'),
    '#type' => 'textfield',
    '#size' => 20,
    '#default_value' => $this
      ->getSetting('more_class'),
    '#description' => $this
      ->t('If displaying more link, add a custom class for formatting.'),
    '#states' => [
      'visible' => [
        ':input[name="fields[body][settings_edit_form][settings][more_link]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  if ($this->fieldDefinition
    ->getType() == 'text_with_summary') {
    $element['summary_handler'] = [
      '#title' => $this
        ->t('Summary'),
      '#type' => 'select',
      '#options' => [
        'full' => $this
          ->t("Use summary if present, and do not trim"),
        'trim' => $this
          ->t("Use summary if present, honor trim settings"),
        'ignore' => $this
          ->t("Do not use summary"),
      ],
      '#default_value' => $this
        ->getSetting('summary_handler'),
    ];
  }
  $trim_options_value = $this
    ->getSetting('trim_options');
  $element['trim_options'] = [
    '#title' => $this
      ->t('Additional options'),
    '#type' => 'checkboxes',
    '#options' => [
      'text' => $this
        ->t('Strip HTML'),
      'trim_zero' => $this
        ->t('Honor a zero trim length'),
    ],
    '#default_value' => empty($trim_options_value) ? [] : array_keys(array_filter($trim_options_value)),
  ];
  return $element;
}