You are here

public function SliderPro::validateOptionsForm in Slider Pro 8

Validate the options form.

Overrides StylePluginBase::validateOptionsForm

File

src/Plugin/views/style/SliderPro.php, line 339

Class

SliderPro
Plugin annotation @ViewsStyle( id = "slider_pro", title = @Translation("Slider Pro"), help = @Translation("Displays a view as a Slider Pro, using the Slider Pro jQuery plugin."), theme = "slider_pro_views_style", theme_file =…

Namespace

Drupal\slider_pro\Plugin\views\style

Code

public function validateOptionsForm(&$form, FormStateInterface $form_state) {
  parent::validateOptionsForm($form, $form_state);
  $style_options = $form_state
    ->getValue('style_options');

  // Flatten style options array.
  $nested_options = [
    'layers' => !empty($style_options['layers_wrapper']['layers']) ? $style_options['layers_wrapper']['layers'] : [],
    'fields' => $style_options['general']['fields'],
    'thumbnail_fields' => $style_options['thumbnails']['thumbnail_fields'],
  ];
  unset($style_options['layers_wrapper']['layers']);
  unset($style_options['general']['fields']);
  unset($style_options['thumbnails']['thumbnail_fields']);
  $form_state
    ->setValue(array(
    'style_options',
  ), array_merge($nested_options, SliderProManager::flattenArray($style_options)));

  // Unset nested values.
  $form_state
    ->unsetValue(array(
    'style_options',
    'general',
  ));
  $form_state
    ->unsetValue(array(
    'style_options',
    'thumbnails',
  ));
  $form_state
    ->unsetValue(array(
    'style_options',
    'layers_wrapper',
  ));

  // Validation.
  $optionset = \Drupal\slider_pro\Entity\SliderPro::load($form_state
    ->getValue([
    'style_options',
    'optionset',
  ]));
  $thumbnail_fields = $form_state
    ->getValue([
    'style_options',
    'thumbnail_fields',
  ]);
  if ($optionset
    ->hasThumbnails() && empty(array_filter($thumbnail_fields))) {
    $form_state
      ->setErrorByName('thumbnails][thumbnail_fields][title', $this
      ->t('The "Thumbnails fields" field is required as the optionset has a position for thumbnails configured.'));
  }
}