You are here

public function TextimageTextFieldFormatter::settingsForm in Textimage 8.4

Same name and namespace in other branches
  1. 8.3 src/Plugin/Field/FieldFormatter/TextimageTextFieldFormatter.php \Drupal\textimage\Plugin\Field\FieldFormatter\TextimageTextFieldFormatter::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/TextimageTextFieldFormatter.php, line 135

Class

TextimageTextFieldFormatter
Plugin implementation of the Textimage text field formatter.

Namespace

Drupal\textimage\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {

  // Image style setting.
  $image_styles = $this->textimageFactory
    ->getTextimageStyleOptions(TRUE);
  if (empty($image_styles)) {
    $image_styles[''] = $this
      ->t('No Textimage style available');
  }
  $description_link = Link::fromTextAndUrl($this
    ->t('Configure Image Styles'), Url::fromRoute('entity.image_style.collection'));
  $element['image_style'] = [
    '#title' => $this
      ->t('Image style'),
    '#type' => 'select',
    '#default_value' => $this
      ->getSetting('image_style'),
    '#options' => $image_styles,
    '#required' => TRUE,
    '#description' => $description_link
      ->toRenderable() + [
      '#access' => $this->currentUser
        ->hasPermission('administer image styles'),
    ],
  ];

  // Multi-value text field image generation settings.
  if ($this->fieldDefinition
    ->getFieldStorageDefinition()
    ->getCardinality() != 1) {
    $options = [
      'merge' => $this
        ->t("Build one single image, styling together text values."),
      'itemize' => $this
        ->t("Build multiple images, styling each text value in a separate image."),
    ];
    $element['image_text_values'] = [
      '#title' => $this
        ->t('Multiple values text field'),
      '#type' => 'radios',
      '#default_value' => $this
        ->getSetting('image_text_values'),
      '#options' => $options,
      '#required' => TRUE,
      '#description' => $this
        ->t("Text values are styled following the sequence of 'Text overlay' effects in the image style."),
    ];
  }

  // Link setting.
  $link_types = [
    'content' => $this
      ->t('Content'),
    'file' => $this
      ->t('Styled image'),
  ];
  $element['image_link'] = [
    '#title' => $this
      ->t('Link image to'),
    '#type' => 'select',
    '#default_value' => $this
      ->getSetting('image_link'),
    '#empty_option' => $this
      ->t('Nothing'),
    '#options' => $link_types,
  ];

  // Image alt and title attribute settings.
  $description = $this
    ->t('This text will be used by screen readers, search engines, or when the image cannot be loaded.');
  $description .= ' ' . $this
    ->t('Tokens can be used.');
  if ($this->fieldDefinition
    ->getType() == 'image') {
    $description .= ' ' . $this
      ->t('Leave empty to use the alternative text set on content level.');
  }
  $element['image_alt'] = [
    '#title' => $this
      ->t('Alternative text'),
    '#type' => 'textfield',
    '#default_value' => $this
      ->getSetting('image_alt'),
    '#description' => $description,
    '#maxlength' => 512,
  ];
  $description = $this
    ->t('The title is used as a tool tip when the user hovers the mouse over the image.');
  $description .= ' ' . $this
    ->t('Tokens can be used.');
  if ($this->fieldDefinition
    ->getType() == 'image') {
    $description .= ' ' . $this
      ->t('Leave empty to use the title set on content level.');
  }
  $element['image_title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Title'),
    '#default_value' => $this
      ->getSetting('image_title'),
    '#description' => $description,
    '#maxlength' => 1024,
  ];
  return $element;
}