You are here

public function LibraryListForm::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/LibraryListForm.php, line 169
Contains \Drupal\skinr_ui\Form\LibraryListForm.

Class

LibraryListForm
Provides skinr plugin installation interface.

Namespace

Drupal\skinr_ui\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $skin_infos = skinr_get_skin_info();
  $theme = $form_state
    ->getValue('edited_theme');
  $theme_info = system_get_info('theme', $theme);
  $triggering_element =& $form_state
    ->getTriggeringElement();
  $reset = $triggering_element['#id'] == 'edit-reset' ? TRUE : FALSE;
  if ($reset) {

    // Reset all values to their default.
    foreach ($form_state
      ->getValue('skin_infos') as $category => $data) {
      foreach ($data as $skin => $enabled) {
        $default_status = isset($skin_infos[$skin]['status'][$theme]) ? $skin_infos[$skin]['status'][$theme] : $skin_infos[$skin]['default status'];
        $form_state
          ->setValue([
          'skin_infos',
          $category,
          $skin,
          'enable',
        ], $default_status);
      }
    }
  }
  if ($triggering_element['#id'] == 'edit-submit' || $reset) {

    // Make sure we don't disable skins for which configuration exists. Ask to
    // disable all related skin configurations so we can disable the skin.
    $affected_skins = array();
    $disable_sids = array();
    $rebuild = FALSE;
    foreach ($form_state
      ->getValue('skin_infos') as $category => $data) {
      foreach ($data as $skin => $enabled) {
        $enabled = $enabled['enable'];
        $status = skinr_skin_info_status_get($skin_infos[$skin]);
        if (!empty($status[$theme]) && !$enabled) {

          // This skin is being disabled.
          $affected_skins[] = $skin;

          // Find all enabled configurations for this skin.
          // @todo
          $params = array(
            'theme' => $theme,
            'skin' => $skin,
            'status' => 1,
          );
          $sids = skinr_skin_get_sids($params);
          if (count($sids)) {
            $disable_sids += $sids;
            $rebuild = TRUE;
          }
        }
      }
    }
    if ($rebuild) {
      $storage = array(
        'status' => $form_state
          ->getValue('skin_infos'),
        'skin_infos' => $affected_skins,
        'sids' => $disable_sids,
        'reset' => $reset,
      );
      $form_state
        ->setStorage($storage);
      $form_state
        ->setRebuild();
      drupal_set_message(t('Rebuilding skins.'));
      return;
    }
  }
  $storage =& $form_state
    ->getStorage();
  $changed_status = array();
  if (!empty($storage['sids'])) {

    // Disable any configurations for skins that are being disabled.
    // @todo
    db_update('skinr_skins')
      ->fields(array(
      'status' => 0,
    ))
      ->condition('sid', $storage['sids'])
      ->execute();

    // Clear skinr_skin_load_multiple cache.
    drupal_static_reset('skinr_skin_load_multiple');
    foreach ($storage['skins'] as $skin) {
      drupal_set_message(t('Disabled all skin configurations for skin %skin and theme %theme.', array(
        '%skin' => $skin,
        '%theme' => $theme_info['name'],
      )));
    }
    $changed_status = $storage['status'];
    $reset = $storage['reset'];
  }
  else {
    $changed_status = $form_state
      ->getValue('skin_infos');
  }

  // Save new status.
  foreach ($changed_status as $category => $data) {
    foreach ($data as $skin => $enabled) {
      $enabled = $enabled['enable'];
      $status = skinr_skin_info_status_get($skin_infos[$skin]);
      if (!isset($status[$theme]) || $status[$theme] != $enabled) {

        // Update status.
        $status[$theme] = $enabled;
        skinr_skin_info_status_set($skin_infos[$skin], $status);
      }
    }
  }
  if ($reset) {
    drupal_set_message(t("Statuses for %theme's skins have been reset to their defaults.", array(
      '%theme' => $theme_info['name'],
    )));
  }
  else {
    drupal_set_message(t("Statuses for %theme's skins have been updated.", array(
      '%theme' => $theme_info['name'],
    )));
  }
}