You are here

public function SvgImageFieldFormatter::settingsForm in SVG Image Field 2.0.x

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

Class

SvgImageFieldFormatter
Plugin implementation of the 'svg_formatter' formatter.

Namespace

Drupal\svg_image_field\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $form = parent::settingsForm($form, $form_state);
  $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.
       Notice only trusted users should use fields with this option enabled because of
       <a href="@svg_security_link">inline svg security</a>', [
      '@svg_security_link' => 'https://www.w3.org/wiki/SVG_Security',
    ]),
  ];
  $inline_name = '[settings_edit_form][settings][inline]';
  $form['apply_dimensions'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Apply dimensions.'),
    '#default_value' => $this
      ->getSetting('apply_dimensions'),
    '#states' => [
      'visible' => [
        ':input[name$="' . $inline_name . '"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $dimensions_name = '[settings_edit_form][settings][apply_dimensions]';
  $form['width'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Image width.'),
    '#default_value' => $this
      ->getSetting('width'),
    '#states' => [
      'visible' => [
        ':input[name$="' . $inline_name . '"]' => [
          'checked' => TRUE,
        ],
        ':input[name$="' . $dimensions_name . '"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['height'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Image height.'),
    '#default_value' => $this
      ->getSetting('height'),
    '#states' => [
      'visible' => [
        ':input[name$="' . $inline_name . '"]' => [
          'checked' => TRUE,
        ],
        ':input[name$="' . $dimensions_name . '"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['force_fill'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Force the fill to currentColor'),
    '#description' => $this
      ->t('This can allow the SVG to inherit coloring from the enclosing tag, such as a link tag.'),
    '#default_value' => $this
      ->getSetting('force_fill'),
    '#states' => [
      'visible' => [
        ':input[name$="' . $inline_name . '"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['sanitize'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Sanitize SVG code'),
    '#description' => $this
      ->t('Sanitize the SVG XML code and prevent XSS attacks.'),
    '#default_value' => $this
      ->getSetting('sanitize'),
    '#states' => [
      'visible' => [
        ':input[name$="' . $inline_name . '"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $sanitize_name = '[settings_edit_form][settings][sanitize]';
  $form['sanitize_remote'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Sanitize: Remove remote references'),
    '#description' => $this
      ->t('Remove attributes that reference remote files, this will stop HTTP leaks but will add an overhead to the sanitizer.'),
    '#default_value' => $this
      ->getSetting('sanitize_remote'),
    '#states' => [
      'visible' => [
        ':input[name$="' . $inline_name . '"]' => [
          'checked' => TRUE,
        ],
        ':input[name$="' . $sanitize_name . '"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['link'] = [
    '#title' => $this
      ->t('Link image to'),
    '#type' => 'select',
    '#default_value' => $this
      ->getSetting('link'),
    '#empty_option' => $this
      ->t('Nothing'),
    '#options' => $this
      ->getLinkTypes(),
  ];
  $form['enable_alt'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable alt attribute.'),
    '#default_value' => $this
      ->getSetting('enable_alt'),
  ];
  $form['enable_title'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable title attribute.'),
    '#default_value' => $this
      ->getSetting('enable_title'),
  ];
  $form['notice'] = [
    '#type' => 'markup',
    '#markup' => '<div><small>' . $this
      ->t('Alt and title attributes will be created from an image filename by removing file extension and replacing eventual underscores and dashes with spaces.') . '</small></div>',
  ];
  return $form;
}