You are here

public function SlideConfigurationForm::submitForm in Nivo Slider 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides ConfigFormBase::submitForm

File

src/Form/SlideConfigurationForm.php, line 201

Class

SlideConfigurationForm

Namespace

Drupal\nivo_slider\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $slides = [];
  $values = $form_state
    ->getValues();
  $config = $this
    ->config('nivo_slider.settings');
  if (is_array($values['order']) && is_array($values['images'])) {

    // Merge in order settings.
    foreach ($values['order'] as $slide => $settings) {
      if (is_numeric($slide)) {
        if (is_array($settings)) {
          $slides[$slide] = isset($slides[$slide]) ? array_merge($slides[$slide], $settings) : $settings;
        }
      }
    }

    // Merge in images settings.
    foreach ($values['images'] as $slide => $settings) {
      if (is_numeric($slide)) {
        if (is_array($settings)) {
          $slides[$slide] = isset($slides[$slide]) ? array_merge($slides[$slide], $settings) : $settings;
        }
      }
    }
  }

  // Remove any deleted slides.
  foreach ($slides as $slide => $settings) {

    // Delete the slide if required.
    if ($settings['delete']) {
      unset($slides[$slide]);
    }
  }

  // Update / create translation source for user defined slide strings.
  if (function_exists('i18n_string_update')) {
    nivo_slider_locale_refresh($slides);
  }

  // Store slide images in a folder named 'banner'.
  $banner_folder = 'public://banner';

  // Create the banner directory if it does not currently exist.
  file_prepare_directory($banner_folder, FILE_CREATE_DIRECTORY);

  // Create a new slide if an image was uploaded.
  if ($file = file_save_upload('upload', [], $banner_folder, 0)) {

    // Create a new slide.
    $slides[] = [
      'fid' => $file
        ->id(),
      'title' => '',
      'description' => [
        'value' => '',
        'format' => filter_fallback_format(),
      ],
      'url' => '',
      'visibility' => '*',
      'transition' => '',
      'weight' => 1,
      'published' => 1,
      'delete' => 0,
    ];
  }

  // Sort the slides by weight.
  usort($slides, 'drupal_sort_weight');

  // Save the slides.
  $config
    ->set('slides', $slides)
    ->save();
  parent::submitForm($form, $form_state);
}