You are here

public function EntityListTrait::settingsForm in Formatter Suite 8

File

src/Plugin/Field/FieldFormatter/EntityListTrait.php, line 117

Class

EntityListTrait
Formats multiple fields as a list.

Namespace

Drupal\formatter_suite\Plugin\Field\FieldFormatter

Code

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

  // Get the parent's form.
  $this
    ->sanitizeListSettings();
  $elements = parent::settingsForm($form, $formState);

  // 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 = 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 = 100;

  // Prompt for each setting.
  $elements['sectionBreak'] = [
    '#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-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-list-separator',
      ],
    ],
    '#states' => [
      'visible' => [
        '.listStyle-' . $marker => [
          'value' => 'span',
        ],
      ],
    ],
  ];
  return $this
    ->postProcessSettingsForm($elements);
}