You are here

public function AdvancedTextFormatter::settingsForm in Advanced Text Formatter 8

Same name and namespace in other branches
  1. 2.1.x src/Plugin/Field/FieldFormatter/AdvancedTextFormatter.php \Drupal\advanced_text_formatter\Plugin\Field\FieldFormatter\AdvancedTextFormatter::settingsForm()
  2. 2.0.x src/Plugin/Field/FieldFormatter/AdvancedTextFormatter.php \Drupal\advanced_text_formatter\Plugin\Field\FieldFormatter\AdvancedTextFormatter::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/AdvancedTextFormatter.php, line 56

Class

AdvancedTextFormatter
Plugin implementation of the 'advanced_text_formatter' formatter.

Namespace

Drupal\advanced_text_formatter\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $elTrimLengthId = Html::getUniqueId('advanced_text_formatter_trim');
  $elFilterId = Html::getUniqueId('advanced_text_formatter_filter');
  $element['trim_length'] = [
    '#id' => $elTrimLengthId,
    '#type' => 'number',
    '#title' => $this
      ->t('Trim length'),
    '#description' => $this
      ->t("Set this to 0 if you don't want to cut the text. Otherwise, input a positive integer."),
    '#size' => 10,
    '#default_value' => $this
      ->getSetting('trim_length'),
    '#required' => TRUE,
  ];
  $element['ellipsis'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Ellipsis'),
    '#description' => $this
      ->t('If checked, a "..." will be added if a field was trimmed.'),
    '#default_value' => $this
      ->getSetting('ellipsis'),
    '#states' => [
      'visible' => [
        '#' . $elTrimLengthId => [
          '!value' => '0',
        ],
      ],
    ],
  ];
  $element['word_boundary'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Word Boundary'),
    '#description' => $this
      ->t('If checked, this field be trimmed only on a word boundary.'),
    '#default_value' => $this
      ->getSetting('word_boundary'),
    '#states' => [
      'visible' => [
        '#' . $elTrimLengthId => [
          '!value' => '0',
        ],
      ],
    ],
  ];
  $element['use_summary'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Use Summary'),
    '#description' => $this
      ->t('If a summary exists, use it.'),
    '#default_value' => $this
      ->getSetting('use_summary'),
  ];
  $token_link = _advanced_text_formatter_browse_tokens($this->fieldDefinition
    ->getTargetEntityTypeId());
  $element['token_replace'] = [
    '#type' => 'checkbox',
    '#description' => $this
      ->t('Replace text pattern. e.g %node-title-token or %node-author-name-token, by token values.', [
      '%node-title-token' => '[node:title]',
      '%node-author-name-token' => '[node:author:name]',
    ]) . ' ',
    '#title' => $this
      ->t('Token Replace'),
    '#default_value' => $this
      ->getSetting('token_replace'),
  ];
  $element['filter'] = [
    '#id' => $elFilterId,
    '#title' => $this
      ->t('Filter'),
    '#type' => 'select',
    '#options' => [
      static::FORMAT_NONE => $this
        ->t('None'),
      static::FORMAT_INPUT => $this
        ->t('Selected Text Format'),
      static::FORMAT_PHP => $this
        ->t('Limit allowed HTML tags'),
      static::FORMAT_DRUPAL => $this
        ->t('Drupal'),
    ],
    '#default_value' => $this
      ->getSetting('filter'),
  ];
  $element['format'] = [
    '#title' => $this
      ->t('Format'),
    '#type' => 'select',
    '#options' => [],
    '#default_value' => $this
      ->getSetting('format'),
    '#states' => [
      'visible' => [
        '#' . $elFilterId => [
          'value' => 'drupal',
        ],
      ],
    ],
  ];
  $formats = filter_formats();
  foreach ($formats as $formatId => $format) {
    $element['format']['#options'][$formatId] = $format
      ->get('name');
  }
  $allowedHtml = $this
    ->getSetting('allowed_html');
  if (empty($allowedHtml)) {
    $tags = '';
  }
  elseif (is_string($allowedHtml)) {
    $tags = $allowedHtml;
  }
  else {
    $tags = '<' . implode('> <', $allowedHtml) . '>';
  }
  $element['allowed_html'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Allowed HTML tags'),
    '#description' => $this
      ->t('See <a href="@link" target="_blank">filter_xss()</a> for more information', [
      '@link' => 'http://api.drupal.org/api/drupal/core%21includes%21common.inc/function/filter_xss/8',
    ]),
    '#default_value' => $tags,
    '#element_validate' => [
      '_advanced_text_formatter_validate_allowed_html',
    ],
    '#states' => [
      'visible' => [
        '#' . $elFilterId => [
          'value' => 'php',
        ],
      ],
    ],
  ];
  $element['autop'] = [
    '#title' => $this
      ->t('Converts line breaks into HTML (i.e. &lt;br&gt; and &lt;p&gt;) tags.'),
    '#type' => 'checkbox',
    '#return_value' => 1,
    '#default_value' => $this
      ->getSetting('autop'),
    '#states' => [
      'invisible' => [
        '#' . $elFilterId => [
          '!value' => 'php',
        ],
      ],
    ],
  ];
  $element['br'] = [
    '#markup' => '<br/>',
  ];
  return $element;
}