You are here

public function AddressDisplayFormatter::settingsForm in Address Display 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/AddressDisplayFormatter.php, line 124

Class

AddressDisplayFormatter
Plugin implementation of the 'Address Display' formatter.

Namespace

Drupal\address_display\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $form = parent::settingsForm($form, $form_state);
  $labels = $this
    ->getLabels();
  $group_class = 'group-order-weight';
  $items = $this
    ->getSetting('address_display');

  // Build table.
  $form['address_display'] = [
    '#type' => 'table',
    '#caption' => $this
      ->t('Address display'),
    '#header' => [
      $this
        ->t('Label'),
      $this
        ->t('Display'),
      $this
        ->t('Glue'),
      $this
        ->t('Weight'),
    ],
    '#empty' => $this
      ->t('No items.'),
    '#tableselect' => FALSE,
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => $group_class,
      ],
    ],
  ];

  // Build rows.
  foreach ($items as $key => $value) {
    $form['address_display'][$key]['#attributes']['class'][] = 'draggable';
    $form['address_display'][$key]['#weight'] = $value['weight'];

    // Label col.
    $form['address_display'][$key]['label'] = [
      '#plain_text' => $labels[$key],
    ];

    // ID col.
    $form['address_display'][$key]['display'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Display'),
      '#default_value' => $value['display'],
    ];

    // Glue col.
    $form['address_display'][$key]['glue'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Glue'),
      '#default_value' => $value['glue'],
    ];

    // Weight col.
    $form['address_display'][$key]['weight'] = [
      '#type' => 'weight',
      '#title' => $this
        ->t('Weight for @title', [
        '@title' => $key,
      ]),
      '#title_display' => 'invisible',
      '#default_value' => $value['weight'],
      '#attributes' => [
        'class' => [
          $group_class,
        ],
      ],
    ];
  }
  return $form;
}