You are here

public function LightgalleryFormatter::settingsForm in Lightgallery 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/LightgalleryFormatter.php, line 47

Class

LightgalleryFormatter
Light gallery formatter.

Namespace

Drupal\lightgallery\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $fields_settings = LightgalleryManager::getSettingFields();

  /*
   * @var \Drupal\lightgallery\Field\FieldInterface $field
   * @var \Drupal\lightgallery\Group\GroupInterface $group
   */
  foreach ($fields_settings as $field) {
    $group = $field
      ->getGroup();
    if (empty($element[$group
      ->getName()])) {

      // Attach group to form.
      $element[$group
        ->getName()] = [
        '#type' => 'details',
        '#title' => $group
          ->getTitle(),
        '#open' => $group
          ->isOpen(),
      ];
    }
    if ($field
      ->appliesToFieldFormatter()) {

      // Attach field to group and form.
      $element[$group
        ->getName()][$field
        ->getName()] = [
        '#type' => $field
          ->getType(),
        '#title' => $field
          ->getTitle(),
        '#default_value' => isset($this->settings[$group
          ->getName()][$field
          ->getName()]) ? $this->settings[$group
          ->getName()][$field
          ->getName()] : $field
          ->getDefaultValue(),
        '#description' => $field
          ->getDescription(),
        '#required' => $field
          ->isRequired(),
      ];
      if (!empty($field
        ->getOptions())) {

        // Set field options.
        if (is_callable($field
          ->getOptions())) {
          $element[$group
            ->getName()][$field
            ->getName()]['#options'] = call_user_func($field
            ->getOptions());
        }
      }
    }
  }
  return $element;
}