You are here

public function LightGallery::buildOptionsForm in Lightgallery 8

Provide a form to edit options for this plugin.

Overrides StylePluginBase::buildOptionsForm

File

src/Plugin/views/style/LightGallery.php, line 100

Class

LightGallery
Style plugin to render each item in an ordered or unordered list.

Namespace

Drupal\lightgallery\Plugin\views\style

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  parent::buildOptionsForm($form, $form_state);

  // Get the active field options.
  $this->fieldSources = $this
    ->confGetFieldSources();
  $field_images = $this
    ->getImageFields();
  $field_others = $this
    ->getNonImageFields();
  $missing_field_warning = '';
  if (empty($field_images)) {
    $missing_field_warning = $this
      ->t('<strong>You must add a field of type image to your view display before this value can be set.</strong><br/>');
  }
  $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($form[$group
      ->getName()])) {

      // Attach group to form.
      $form[$group
        ->getName()] = [
        '#type' => 'details',
        '#title' => $group
          ->getTitle(),
        '#open' => !empty($group
          ->getOpenValue()) ? $this->options['lightgallery'][$group
          ->getOpenValue()] : $group
          ->isOpen(),
      ];
    }
    if ($field
      ->appliesToViews()) {

      // Attach field to group and form.
      $form[$group
        ->getName()][$field
        ->getName()] = [
        '#type' => $field
          ->getType(),
        '#title' => $field
          ->getTitle(),
        '#default_value' => isset($this->options['lightgallery'][$field
          ->getName()]) ? $this->options['lightgallery'][$field
          ->getName()] : $field
          ->getDefaultValue(),
        '#description' => $field
          ->getDescription(),
        '#required' => $field
          ->isRequired(),
      ];
      if ($field
        ->getName() == 'thumb_field' || $field
        ->getName() == 'image_field') {

        // Add exception for these fields.
        $form[$group
          ->getName()][$field
          ->getName()]['#suffix'] = $missing_field_warning;
      }
      if (!empty($field
        ->getOptions())) {

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