You are here

function skinr_ui_form_alter in Skinr 6.2

Implementation of hook_form_alter().

File

./skinr_ui.module, line 329

Code

function skinr_ui_form_alter(&$form, $form_state, $form_id) {

  // Fix for update script.
  if (defined('MAINTENANCE_MODE')) {
    return;
  }
  $skinr_config = skinr_fetch_config();
  $info = skinr_skin_data();
  foreach ($skinr_config as $module => $settings) {
    if (isset($settings['form'][$form_id])) {
      if (!empty($form['skinr']) && $form['skinr']['module']['#value'] !== $module) {
        continue;
      }
      $form_settings = array_merge(_skinr_fetch_config_defaults('form'), $settings['form'][$form_id]);

      // Check for access.
      if (!skinr_handler('access_handler', 'access skinr', $form_settings['access_handler'], $form, $form_state)) {

        // Deny access.
        break;
      }

      // Ensure we have the required preprocess_hook or preprocess_hook_callback.
      if (empty($form_settings['preprocess_hook']) && empty($form_settings['preprocess_hook_callback'])) {
        trigger_error(sprintf("No preprocess_hook or preprocess_hook_callback was found for form_id '%s' in module '%s'.", $form_id, $module), E_USER_ERROR);
      }
      $themes = list_themes();
      ksort($themes);

      // There's a bug that sometimes disables all themes. This will check for
      // the bug in question. http://drupal.org/node/305653
      $enabled_themes = 0;
      foreach ($themes as $theme) {
        if (!$theme->status) {
          continue;
        }
        $enabled_themes++;
        if (!isset($form['skinr_settings'])) {
          $form['skinr_settings'] = array(
            '#tree' => TRUE,
            '#weight' => $form_settings['skinr_weight'],
          );
        }
        $preprocess_hooks = isset($form_settings['preprocess_hook']) ? $form_settings['preprocess_hook'] : skinr_handler('preprocess_hook_callback', '', $form_settings['preprocess_hook_callback'], $form, $form_state);
        if (!is_array($preprocess_hooks)) {
          $preprocess_hooks = array(
            $preprocess_hooks,
          );
        }
        if (!$form_state['submitted']) {
          if ($skinr = skinr_handler('data_handler', 'form', $form_settings['data_handler'], $form, $form_state, $theme->name, $module, $form_settings)) {
            $defaults = $skinr->skins;
            $additional_default = isset($skinr->skins['_additional']) ? $skinr->skins['_additional'] : '';
            $template_default = isset($skinr->skins['_template']) ? $skinr->skins['_template'] : '';
          }
          else {
            $defaults = array();
            $additional_default = '';
            $template_default = '';
          }
        }
        else {

          // Handle preview before submit.
          $defaults = $form_state['values']['widgets'];
          $additional_default = $form_state['values']['_additional'];
          $template_default = $form_state['values']['_template'];
        }
        if (!isset($form['skinr_settings'][$module . '_group'])) {
          $form['skinr_settings'][$module . '_group'] = array(
            '#type' => 'fieldset',
            '#title' => t('@skinr_title @title', array(
              '@skinr_title' => $form_settings['skinr_title'],
              '@title' => $form_settings['title'],
            )),
            '#description' => t($form_settings['description']) . ' <strong>' . implode(', ', $preprocess_hooks) . '</strong>.',
            '#weight' => $form_settings['weight'],
            '#collapsible' => TRUE,
            '#collapsed' => $form_settings['collapsed'],
          );
        }

        // Get current theme, but make sure it's not the admin theme when we're editing with AJAX.
        $current_theme = skinr_current_theme(TRUE);
        $form['skinr_settings'][$module . '_group'][$theme->name] = array(
          '#type' => 'fieldset',
          '#title' => $theme->info['name'] . ($theme->name == $current_theme ? ' (' . t('enabled + default') . ')' : ''),
          '#collapsible' => TRUE,
          '#collapsed' => $theme->name == $current_theme ? FALSE : TRUE,
        );
        if ($theme->name == $current_theme) {
          $form['skinr_settings'][$module . '_group'][$theme->name]['#attributes'] = array(
            'class' => 'skinr-ui-current-theme',
          );
          $form['skinr_settings'][$module . '_group'][$theme->name]['#weight'] = -10;
        }

        // Create individual widgets for each skin.
        $template_options = array();
        if (isset($info[$theme->name]->skins)) {
          foreach ($info[$theme->name]->skins as $skin_id => $skin) {

            // Check if this skin is disabled.
            if (empty($skin['status'][$theme->name])) {
              continue;
            }

            // Check if this skin applies to this hook.
            if (!is_array($skin['features']) || !in_array('*', $skin['features']) && !_skinr_is_featured($preprocess_hooks, $skin['features'])) {
              continue;
            }

            // Create widget.
            switch ($skin['type']) {
              case 'checkboxes':
                $field = array(
                  '#type' => 'checkboxes',
                  '#multiple' => TRUE,
                  '#title' => t($skin['title']),
                  '#options' => skinr_ui_info_options_to_form_options($skin['options']),
                  '#default_value' => isset($defaults[$skin_id]) ? $defaults[$skin_id] : array(),
                  '#description' => t($skin['description']),
                  '#weight' => isset($skin['weight']) ? $skin['weight'] : NULL,
                );
                break;
              case 'radios':
                $field = array(
                  '#type' => 'radios',
                  '#title' => t($skin['title']),
                  '#options' => array_merge(array(
                    '' => '&lt;none&gt;',
                  ), skinr_ui_info_options_to_form_options($skin['options'])),
                  '#default_value' => isset($defaults[$skin_id]) ? $defaults[$skin_id] : '',
                  '#description' => t($skin['description']),
                  '#weight' => isset($skin['weight']) ? $skin['weight'] : NULL,
                );
                break;
              case 'select':
                $field = array(
                  '#type' => 'select',
                  '#title' => t($skin['title']),
                  '#options' => array_merge(array(
                    '' => '<none>',
                  ), skinr_ui_info_options_to_form_options($skin['options'])),
                  '#default_value' => isset($defaults[$skin_id]) ? $defaults[$skin_id] : '',
                  '#description' => t($skin['description']),
                  '#weight' => isset($skin['weight']) ? $skin['weight'] : NULL,
                );
                break;
            }
            if (empty($skin['group'])) {
              $form['skinr_settings'][$module . '_group'][$theme->name]['widgets'][$skin_id] = $field;
            }
            else {
              if (!isset($form['skinr_settings'][$module . '_group'][$theme->name]['widgets'][$skin['group']])) {
                $group = $info[$theme->name]->options['groups'][$skin['group']];
                $form['skinr_settings'][$module . '_group'][$theme->name]['widgets'][$skin['group']] = array(
                  '#type' => 'fieldset',
                  '#title' => t($group['title']),
                  '#description' => t($group['description']),
                  '#collapsible' => $group['collapsible'],
                  '#collapsed' => $group['collapsed'],
                  '#weight' => isset($group['weight']) ? $group['weight'] : NULL,
                );
              }
              $form['skinr_settings'][$module . '_group'][$theme->name]['widgets'][$skin['group']][$skin_id] = $field;
            }

            // Prepare templates.
            $templates = skinr_ui_info_templates_filter($skin['templates'], $preprocess_hooks);
            $template_options = array_merge($template_options, skinr_ui_info_templates_to_form_options($templates));
          }
        }

        // Check for access.
        if (skinr_handler('access_handler', 'access skinr classes', $form_settings['access_handler'], $form, $form_state)) {
          $form['skinr_settings'][$module . '_group'][$theme->name]['_additional'] = array(
            '#type' => 'textfield',
            '#title' => t('CSS classes'),
            '#size' => 40,
            '#description' => t('To add CSS classes manually, enter classes separated by a single space i.e. <code>first-class second-class</code>'),
            '#default_value' => $additional_default,
          );
          $form['skinr_settings'][$module . '_group'][$theme->name]['_template'] = array(
            '#type' => !empty($template_options) ? 'select' : 'hidden',
            '#title' => t('Template file'),
            '#options' => array_merge(array(
              '' => 'Default',
            ), $template_options),
            '#default_value' => $template_default,
            '#description' => t('Optionally, select a template file to use. The "Default" option allows Drupal to handle this.'),
          );

          // Only add validation handler once.
          if (!isset($form['#validate']) || !in_array('skinr_ui_form_validate', $form['#validate'])) {
            $form['#validate'][] = 'skinr_ui_form_validate';
          }

          // Special for views.
          if (isset($form['buttons']['submit']['#validate']) && !in_array('skinr_ui_form_validate', $form['buttons']['submit']['#validate'])) {
            $form['buttons']['submit']['#validate'][] = 'skinr_ui_form_validate';
          }
        }
      }
      if ($enabled_themes == 0) {
        drupal_set_message(t('Skinr has detected that none of your themes are enabled. This is likely related the Drupal bug: <a href="http://drupal.org/node/305653">Themes disabled during update</a>. Please re-enable your theme to continue using Skinr.'), 'warning', FALSE);
      }

      // Only add submit handler once.
      if (is_string($form_settings['submit_handler_attach_to'])) {

        // Backwards compatibility with previous versions
        $form_settings['submit_handler_attach_to'] = explode('][', preg_replace('/(^\\[|\\]$|\'|")/', '', $form_settings['submit_handler_attach_to']));
      }

      // Find the property to check.
      $attr =& $form;
      foreach ($form_settings['submit_handler_attach_to'] as $part) {
        $attr =& $attr[$part];
      }
      if (!empty($attr) && !in_array('skinr_ui_form_submit', $attr)) {
        $string = $attr[] = 'skinr_ui_form_submit';
      }

      // Keep looping, there might be other modules that implement the same form_id.
    }
  }
}