You are here

function skinr_form_alter in Skinr 6

Implementation of hook_form_alter().

File

./skinr.module, line 78

Code

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

  // Fix for update script.
  if (defined('MAINTENANCE_MODE')) {
    return;
  }
  $skinr_data = skinr_fetch_data();
  $info = skinr_process_info_files();
  foreach ($skinr_data as $module => $settings) {
    if (isset($settings['form'][$form_id])) {
      $form_settings = array_merge(_skinr_fetch_data_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++;
        $preprocess_hook = !empty($form_settings['preprocess_hook']) ? $form_settings['preprocess_hook'] : skinr_handler('preprocess_hook_callback', '', $form_settings['preprocess_hook_callback'], $form, $form_state);
        if (!$form_state['submitted']) {
          $skinr_data = skinr_handler('data_handler', 'form', $form_settings['data_handler'], $form, $form_state, $theme->name, $module, $form_settings);
          $defaults = $skinr_data;
          $additional_default = isset($skinr_data['_additional']) ? $skinr_data['_additional'] : '';
          $template_default = isset($skinr_data['_template']) ? $skinr_data['_template'] : '';
        }
        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'])) {
          $form['skinr_settings'] = array(
            '#tree' => TRUE,
            '#weight' => $form_settings['skinr_weight'],
          );
        }
        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>' . $preprocess_hook . '</strong>.',
            '#weight' => $form_settings['weight'],
            '#collapsible' => TRUE,
            '#collapsed' => $form_settings['collapsed'],
          );
        }
        $form['skinr_settings'][$module . '_group'][$theme->name] = array(
          '#type' => 'fieldset',
          '#title' => $theme->info['name'] . ($theme->name == skinr_current_theme() ? ' (' . t('enabled + default') . ')' : ''),
          '#collapsible' => TRUE,
          '#collapsed' => $theme->name == skinr_current_theme() ? FALSE : TRUE,
        );
        if ($theme->name == skinr_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 applies to this hook.
            if (!is_array($skin['features']) || !in_array('*', $skin['features']) && !in_array($preprocess_hook, $skin['features'])) {
              continue;
            }

            // Create widget.
            switch ($skin['type']) {
              case 'checkboxes':
                $form['skinr_settings'][$module . '_group'][$theme->name]['widgets'][$skin_id] = array(
                  '#type' => 'checkboxes',
                  '#multiple' => TRUE,
                  '#title' => t($skin['title']),
                  '#options' => skinr_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':
                $form['skinr_settings'][$module . '_group'][$theme->name]['widgets'][$skin_id] = array(
                  '#type' => 'radios',
                  '#title' => t($skin['title']),
                  '#options' => array_merge(array(
                    '' => '&lt;none&gt;',
                  ), skinr_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':
                $form['skinr_settings'][$module . '_group'][$theme->name]['widgets'][$skin_id] = array(
                  '#type' => 'select',
                  '#title' => t($skin['title']),
                  '#options' => array_merge(array(
                    '' => '<none>',
                  ), skinr_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;
            }

            // Prepare templates.
            $templates = skinr_info_templates_filter($skin['templates'], $preprocess_hook);
            $template_options = array_merge($template_options, skinr_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]['advanced'] = array(
            '#type' => 'fieldset',
            '#title' => t('Advanced options'),
            '#collapsible' => TRUE,
            '#collapsed' => empty($additional_default),
          );
          $form['skinr_settings'][$module . '_group'][$theme->name]['advanced']['_additional'] = array(
            '#type' => 'textfield',
            '#title' => t('Apply additional CSS classes'),
            '#description' => t('Optionally add additional CSS classes. Example: <em>my-first-class my-second-class</em>'),
            '#default_value' => $additional_default,
          );
          $form['skinr_settings'][$module . '_group'][$theme->name]['advanced']['_template'] = array(
            '#type' => 'select',
            '#title' => t('Template file'),
            '#options' => array_merge(array(
              '' => 'Default',
            ), $template_options),
            '#default_value' => $template_default,
            '#description' => t('Optionally, select a template file to associate with this <strong>!hook</strong>. Selecting "Default" will let Drupal handle this.', array(
              '!hook' => $preprocess_hook,
            )),
          );

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

          // Special for views.
          if (isset($form['buttons']['submit']['#validate']) && !in_array('skinr_form_validate', $form['buttons']['submit']['#validate'])) {
            $form['buttons']['submit']['#validate'][] = 'skinr_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_form_submit', $attr)) {
        $string = $attr[] = 'skinr_form_submit';
      }

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