You are here

public function GeneralEmailFormatter::settingsForm in Formatter Suite 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/GeneralEmailFormatter.php, line 148

Class

GeneralEmailFormatter
Formats an email address.

Namespace

Drupal\formatter_suite\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $formState) {
  $this
    ->sanitizeSettings();
  $isMultiple = $this->fieldDefinition
    ->getFieldStorageDefinition()
    ->isMultiple();

  // Below, some checkboxes and select choices show/hide other form
  // elements. We use Drupal's obscure 'states' feature, which adds
  // Javascript to elements to auto show/hide based upon a set of
  // simple conditions.
  //
  // Those conditions need to reference the form elements to check
  // (e.g. a checkbox), but the element ID and name are automatically
  // generated by the parent form. We cannot set them, or predict them,
  // so we cannot use them. We could use a class, but this form may be
  // shown multiple times on the same page, so a simple class would not be
  // unique. Instead, we create classes for this form only by adding a
  // random number marker to the end of the class name.
  $marker = rand();

  // Add branding.
  $elements = [];
  $elements = Branding::addFieldFormatterBranding($elements);
  $elements['#attached']['library'][] = 'formatter_suite/formatter_suite.fieldformatter';

  // Add description.
  //
  // Use a large negative weight to insure it comes first.
  $elements['description'] = [
    '#type' => 'html_tag',
    '#tag' => 'div',
    '#value' => $this
      ->getDescription(),
    '#weight' => -1000,
    '#attributes' => [
      'class' => [
        'formatter_suite-settings-description',
      ],
    ],
  ];
  $weight = 0;

  // Prompt for each setting.
  $elements['emailStyle'] = [
    '#title' => $this
      ->t('Link style'),
    '#type' => 'select',
    '#options' => $this
      ->getEmailStyles(),
    '#default_value' => $this
      ->getSetting('emailStyle'),
    '#weight' => $weight++,
    '#wrapper_attributes' => [
      'class' => [
        'formatter_suite-general-email-style',
      ],
    ],
    '#attributes' => [
      'class' => [
        'emailStyle-' . $marker,
      ],
    ],
  ];
  $elements['linkText'] = [
    '#title' => $this
      ->t('Custom text'),
    '#type' => 'textfield',
    '#size' => 10,
    '#default_value' => $this
      ->getSetting('linkText'),
    '#weight' => $weight++,
    '#wrapper_attributes' => [
      'class' => [
        'formatter_suite-general-email-title-custom-text',
      ],
    ],
    '#states' => [
      'visible' => [
        '.emailStyle-' . $marker => [
          'value' => 'custommailto',
        ],
      ],
    ],
  ];
  $elements['sectionBreak1'] = [
    '#markup' => '<div class="formatter_suite-section-break"></div>',
    '#weight' => $weight++,
  ];
  $elements['classes'] = [
    '#title' => $this
      ->t('Custom classes'),
    '#type' => 'textfield',
    '#size' => 10,
    '#default_value' => $this
      ->getSetting('classes'),
    '#weight' => $weight++,
    '#attributes' => [
      'autocomplete' => 'off',
      'autocapitalize' => 'none',
      'spellcheck' => 'false',
      'autocorrect' => 'off',
    ],
    '#wrapper_attributes' => [
      'class' => [
        'formatter_suite-general-email-classes',
      ],
    ],
  ];
  if ($isMultiple === TRUE) {
    $elements['sectionBreak3'] = [
      '#markup' => '<div class="formatter_suite-section-break"></div>',
      '#weight' => $weight++,
    ];
    $elements['listStyle'] = [
      '#title' => $this
        ->t('List style'),
      '#type' => 'select',
      '#options' => $this
        ->getListStyles(),
      '#default_value' => $this
        ->getSetting('listStyle'),
      '#weight' => $weight++,
      '#wrapper_attributes' => [
        'class' => [
          'formatter_suite-general-email-list-style',
        ],
      ],
      '#attributes' => [
        'class' => [
          'listStyle-' . $marker,
        ],
      ],
    ];
    $elements['listSeparator'] = [
      '#title' => $this
        ->t('Separator'),
      '#type' => 'textfield',
      '#size' => 10,
      '#default_value' => $this
        ->getSetting('listSeparator'),
      '#weight' => $weight++,
      '#attributes' => [
        'autocomplete' => 'off',
        'autocapitalize' => 'none',
        'spellcheck' => 'false',
        'autocorrect' => 'off',
      ],
      '#wrapper_attributes' => [
        'class' => [
          'formatter_suite-general-email-list-separator',
        ],
      ],
      '#states' => [
        'visible' => [
          '.listStyle-' . $marker => [
            'value' => 'span',
          ],
        ],
      ],
    ];
  }
  return $elements;
}