You are here

function themekey_form_alter in ThemeKey 6.3

Same name and namespace in other branches
  1. 6.4 themekey_admin.inc \themekey_form_alter()
  2. 6.2 themekey_admin.inc \themekey_form_alter()

Implements hook_form_alter().

Alters

Validation will happen here when the form displays the stored configuration. Otherwise all the drag'n'drop stuff will not work.

See also

themekey_rule_chain_form()

themekey_rule_chain_form_set_error()

File

./themekey_admin.inc, line 223

Code

function themekey_form_alter(&$form, $form_state, $form_id) {
  if ('themekey_rule_chain_form' == $form_id && empty($form_state['post'])) {
    module_invoke_all('themekey_load_validators');
    $attributes = variable_get('themekey_attributes', array());
    if (!empty($form['old_items'])) {
      foreach ($form['old_items'] as $key_1 => $value_1) {
        if ($value_1['enabled']['#default_value']) {
          if (!themekey_check_theme_enabled($value_1['theme']['#default_value'])) {
            themekey_rule_chain_form_set_error($form, $key_1, 'theme', t('Theme is not activated'));
          }
          if (0 == drupal_strlen($value_1['value']['#default_value'])) {
            themekey_rule_chain_form_set_error($form, $key_1, 'value', t('You must enter a value'));
          }
          elseif (!empty($attributes[$value_1['property']['#default_value']]['validator'])) {

            //validate rule with custom validator
            $validator = $attributes[$value_1['property']['#default_value']]['validator'];
            if (function_exists($validator)) {
              $rule = array(
                'property' => $value_1['property']['#default_value'],
                'wildcard' => $value_1['wildcard']['#default_value'],
                'operator' => $value_1['operator']['#default_value'],
                'value' => $value_1['value']['#default_value'],
              );
              $errors = $validator($rule);
              foreach ($errors as $element => $msg) {
                themekey_rule_chain_form_set_error($form, $key_1, $element, $msg);
              }
            }
            else {
              themekey_rule_chain_form_set_error($form, $key_1, 'property', t('ThemeKey requested an unknown validator called %validator to validate property %property', array(
                '%validator' => $validator,
                '%property' => $value_1['property']['#default_value'],
              )));
            }
          }
          foreach ($form['old_items'] as $key_2 => $value_2) {
            if ($key_2 == $key_1) {
              continue;
            }
            if ($value_2['enabled']['#default_value'] && !empty($value_1['value']['#default_value']) && $value_2['property']['#default_value'] == $value_1['property']['#default_value'] && $value_2['operator']['#default_value'] == $value_1['operator']['#default_value'] && $value_2['value']['#default_value'] == $value_1['value']['#default_value'] && ($value_2['parent']['#default_value'] == $value_1['parent']['#default_value'] || $value_2['parent']['#default_value'] == $value_1['id']['#value'])) {
              if ('drupal:path:wildcard' != $value_2['property']['#default_value'] || 'drupal:path:wildcard' == $value_2['property']['#default_value'] && $value_2['wildcard']['#default_value'] == $value_1['wildcard']['#default_value']) {

                // We have two identical rules with the same 'indention' in a chain.
                // This is allowed only if the first rule has children and second has none and one isn't the parent of the other
                if (!$value_2['parent']['#default_value'] == $value_1['id']['#value']) {
                  $has_childs_1 = FALSE;
                  $has_childs_2 = FALSE;
                  foreach ($form['old_items'] as $key_3 => $value_3) {
                    if ($value_3['parent']['#default_value'] == $value_1['id']['#value']) {
                      $has_childs_1 = TRUE;
                    }
                    if ($value_3['parent']['#default_value'] == $value_2['id']['#value']) {
                      $has_childs_2 = TRUE;
                    }
                    if ($has_childs_1 && $has_childs_2) {
                      break;
                    }
                  }
                  if ($value_1['weight']['#default_value'] < $value_2['weight']['#default_value'] && $has_childs_1 && !$has_childs_2 || $value_1['weight']['#default_value'] > $value_2['weight']['#default_value'] && !$has_childs_1 && $has_childs_2) {

                    // no error
                    continue;
                  }
                  elseif ($value_1['weight']['#default_value'] > $value_2['weight']['#default_value'] && $has_childs_1 && !$has_childs_2) {
                    themekey_rule_chain_form_set_error($form, $key_1, 'property', t('Theme switching rule could never be reached'));
                    continue;
                  }
                  elseif ($value_1['weight']['#default_value'] < $value_2['weight']['#default_value'] && !$has_childs_1 && $has_childs_2) {
                    themekey_rule_chain_form_set_error($form, $key_1, 'property', t('Theme switching rule hides a later one'));
                    continue;
                  }
                  elseif ($value_1['weight']['#default_value'] < $value_2['weight']['#default_value'] && $has_childs_1 && $has_childs_2) {
                    themekey_rule_chain_form_set_error($form, $key_1, 'property', t('Theme switching rule should be combined with an identical one below'));
                    continue;
                  }
                  elseif ($value_1['weight']['#default_value'] > $value_2['weight']['#default_value'] && $has_childs_1 && $has_childs_2) {
                    themekey_rule_chain_form_set_error($form, $key_1, 'property', t('Theme switching rule should be combined with an identical one above'));
                    continue;
                  }
                }
                themekey_rule_chain_form_set_error($form, $key_1, 'property', t('You entered two identical theme switching rules in the chain'));
                themekey_rule_chain_form_set_error($form, $key_2, 'property', t('You entered two identical theme switching rules in the chain'));
              }
            }
          }
        }
      }
    }

    // dump all errors
    themekey_rule_chain_form_set_error($form);
  }
}