You are here

function themekey_validator_role in ThemeKey 7.2

Same name and namespace in other branches
  1. 7.3 themekey_validators.inc \themekey_validator_role()
  2. 7 themekey_validators.inc \themekey_validator_role()

Validates a Theme Switching Rule. Allowed Operators: "=", "!", "~" Allowed values:

  • valid regular expression if operator is "~"
  • a valid Drupal role if operator is "=" or "!"

Parameters

$rule: A Theme Switching Rule as associative array:

  • property: ThemeKey property as string (e.g., "drupal:path")
  • wildcard: optional string, only used if property is "drupal:path:wildcard"
  • operator: ThemeKey operator as string ("=", "!", "*", "!*", "<", "<=", ">", ">=", "~", "!~")
  • value: ThemeKey property value as string

Return value

An associative array of errors:

  • property: translated error message as string describing a problem with the property
  • wildcard: translated error message as string describing a problem with the wildcard
  • operator: translated error message as string describing a problem with the operator
  • value: translated error message as string describing a problem with the value

If no errors detected the array is empty.

1 string reference to 'themekey_validator_role'
themekey_user_themekey_properties in modules/themekey.user.inc
Implements hook_themekey_properties().

File

./themekey_validators.inc, line 748
Provides set of validators which can be used to validate ThemeKey Theme Switching Rules.

Code

function themekey_validator_role($rule) {
  $errors = array();
  switch ($rule['operator']) {
    case '=':
    case '!':
      $roles = array();
      $result = db_select('role', 'r')
        ->fields('r')
        ->orderBy('name', 'ASC')
        ->execute();
      foreach ($result as $role) {
        $roles[$role->rid] = $role->name;
      }
      if (!in_array($rule['value'], $roles)) {
        $errors['value'] = t('The entered user role %value is not valid. Possible roles are "%roles".', array(
          '%value' => $rule['value'],
          '%roles' => implode('", "', $roles),
        ));
      }
      break;
    case '~':
    case '!~':
      $errors = themekey_validator_regex($rule);
      break;
    default:
      $errors['operator'] = t('Possible operators are "=", "!", "*", "!*", "~", "!~"');
      break;
  }
  return $errors;
}