You are here

public function SkinsEditForm::submitForm in Skinr 8.2

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 FormInterface::submitForm

File

skinr_ui/src/Form/SkinsEditForm.php, line 350
Contains Drupal\skinr_ui\Form\SkinsEditForm.

Class

SkinsEditForm

Namespace

Drupal\skinr_ui\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $element_type = $form_state
    ->getValue('element_type');
  $element = $form_state
    ->getValue('element');
  if ($form_state
    ->hasValue('skinr_settings')) {
    foreach ($form_state
      ->getValue('skinr_settings') as $section_theme_name => $section_theme) {

      // Process widgets.
      if (!empty($section_theme) && is_array($section_theme)) {
        foreach ($section_theme as $skin_name => $options) {
          if ($skin_name == '_additional' && !\Drupal::currentUser()
            ->hasPermission('edit advanced skin settings')) {

            // This user doesn't have access to alter these options.
            continue;
          }

          // Ensure options is an array.
          if (!is_array($options)) {
            $options = $skin_name == '_additional' ? explode(' ', $options) : array(
              $options,
            );
          }

          // Sanitize options.
          $options = _skinr_array_strip_empty($options);

          // Find existing skin.
          unset($skin);
          $properties = array(
            'theme' => $section_theme_name,
            'element_type' => $element_type,
            'element' => $element,
            'skin' => $skin_name,
          );

          /** @var Skin[] $skins */
          $skins = \Drupal::entityManager()
            ->getStorage('skin')
            ->loadByProperties($properties);
          if ($skins) {
            $skin = reset($skins);
          }
          if (empty($options)) {
            if (!empty($skin)) {

              // Delete this skin configuration.
              $skin
                ->delete();
            }
            continue;
          }
          if (empty($skin)) {

            // It doesn't exist, so create a new skin.
            $skin = Skin::create(array(
              'element_type' => $element_type,
              'element' => $element,
              'theme' => $section_theme_name,
              'skin' => $skin_name,
            ));
          }
          $skin
            ->setOptions($options);
          $skin
            ->enable();

          // Save skin.
          $skin
            ->save();
        }
      }
    }
  }
}