You are here

public function SvgFormatter::settingsForm in SVG Formatter 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/SvgFormatter.php, line 108

Class

SvgFormatter
Plugin implementation of the 'svg_formatter' formatter.

Namespace

Drupal\svg_formatter\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $form = parent::settingsForm($form, $form_state);
  $token_module = $this->moduleHandler
    ->moduleExists('token');
  $form['inline'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Output SVG inline'),
    '#default_value' => $this
      ->getSetting('inline'),
    '#description' => $this
      ->t('Check this option if you want to manipulate the SVG image with CSS and Javascript.'),
  ];
  $sanitize_attributes = $this
    ->isSanitizerInstalled() ? [] : [
    'disabled' => 'disabled',
  ];
  $form['sanitize'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Sanitize inline SVG'),
    '#default_value' => $this
      ->getSetting('sanitize'),
    '#description' => $this
      ->t('For this to work you must install "enshrined/svg-sanitize" library with composer.'),
    '#states' => [
      'visible' => [
        ':input[name="fields[' . $this->fieldName . '][settings_edit_form][settings][inline]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
    '#attributes' => $sanitize_attributes,
  ];
  $form['apply_dimensions'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Set image dimensions.'),
    '#default_value' => $this
      ->getSetting('apply_dimensions'),
  ];
  $form['width'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Image width.'),
    '#default_value' => $this
      ->getSetting('width'),
    '#states' => [
      'visible' => [
        ':input[name="fields[' . $this->fieldName . '][settings_edit_form][settings][apply_dimensions]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['height'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Image height.'),
    '#default_value' => $this
      ->getSetting('height'),
    '#states' => [
      'visible' => [
        ':input[name="fields[' . $this->fieldName . '][settings_edit_form][settings][apply_dimensions]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['enable_alt'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable alt attribute.'),
    '#default_value' => $this
      ->getSetting('enable_alt'),
    '#states' => [
      'visible' => [
        ':input[name="fields[' . $this->fieldName . '][settings_edit_form][settings][inline]"]' => [
          'checked' => FALSE,
        ],
      ],
    ],
  ];
  $form['alt_string'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Token with alt value'),
    '#description' => $token_module ? $this
      ->t('Use the token help link below to select the token.') : $this
      ->t('Install token module to see available tokens.'),
    '#default_value' => $this
      ->getSetting('alt_string'),
    '#states' => [
      'visible' => [
        ':input[name="fields[' . $this->fieldName . '][settings_edit_form][settings][enable_alt]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['enable_title'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable title attribute.'),
    '#default_value' => $this
      ->getSetting('enable_title'),
    '#states' => [
      'visible' => [
        ':input[name="fields[' . $this->fieldName . '][settings_edit_form][settings][inline]"]' => [
          'checked' => FALSE,
        ],
      ],
    ],
  ];
  $form['title_string'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Token with title value'),
    '#description' => $token_module ? $this
      ->t('Use the token help link below to select the token.') : $this
      ->t('Install token module to see available tokens.'),
    '#default_value' => $this
      ->getSetting('title_string'),
    '#states' => [
      'visible' => [
        ':input[name="fields[' . $this->fieldName . '][settings_edit_form][settings][enable_title]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  if ($token_module) {
    $form['token_help'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => [
        $this->fieldDefinition
          ->getTargetEntityTypeId(),
        'file',
      ],
    ];
  }
  return $form;
}