You are here

function context_context_conditions in Context 6.2

Same name and namespace in other branches
  1. 6 context.core.inc \context_context_conditions()

Implementation of hook_context_conditions().

Allows modules to integrate with context and provide their native objects as options for setting a context definition. The hook should return an array of items keyed on the object "type" (e.g. "node", "user", etc.) with key-value pairs corresponding to a FormAPI element array with some restrictions and additional info.

'#title': Required. The title of the object / form option. '#type': Required. The FormAPI element type to use. Currently only 'select', 'checkboxes', 'radios', and 'textfield' are allowed. '#description': Optional. Help text to be displayed on the form. '#options': Required. A key-value array of options. They key will be stored and passed to context_set_by_condition(), so the integrating module should use a unique (within its namespace) / usable identifier.

File

./context.core.inc, line 57

Code

function context_context_conditions() {
  $items = array();

  // Content Types
  $nodetypes = array();
  foreach (node_get_types() as $type) {
    $nodetypes[$type->type] = t(drupal_ucfirst($type->name));
  }
  $items['node'] = array(
    '#title' => t('Node pages'),
    '#description' => t('Set this context when viewing a node page or using the add/edit form of one of these content types.'),
    '#options' => $nodetypes,
    '#type' => 'checkboxes',
    '#help_topic' => 'node-condition',
    '#help_module' => 'context_ui',
  );

  // User
  $items['user'] = array(
    '#title' => t('User pages'),
    '#description' => t('Set this context when a user with selected role(s) is viewed'),
    '#options' => user_roles(true),
    '#type' => 'checkboxes',
  );

  // Book
  if (module_exists('book')) {
    $options = array();
    foreach (book_get_books() as $book) {
      $options[$book['menu_name']] = $book['title'];
    }
    $items['book'] = array(
      '#title' => t('Book'),
      '#description' => t('Set this context when a node in the selected book is viewed.'),
      '#options' => $options,
      '#type' => 'checkboxes',
    );
  }

  // Sitewide context:
  $items['sitewide'] = array(
    '#title' => t('Sitewide context'),
    '#type' => 'radios',
    '#options' => array(
      0 => t('False'),
      1 => t('True'),
    ),
    '#description' => theme('advanced_help_topic', 'context_ui', 'default-contexts') . t('Should this context always be set? If <strong>true</strong>, this context will be active across your entire site.'),
    '#help_topic' => 'default-contexts',
    '#help_module' => 'context_ui',
  );

  // Default context:
  $items['default'] = array(
    '#title' => t('Default context'),
    '#type' => 'radios',
    '#options' => array(
      0 => t('False'),
      1 => t('True'),
    ),
    '#description' => theme('advanced_help_topic', 'context_ui', 'default-context') . t('If no other context is activated in this namespace and attribute, should this one be activated?'),
    '#help_topic' => 'default-context',
    '#help_module' => 'context_ui',
  );

  // Path:
  $items['path'] = array(
    '#title' => t('Path'),
    '#description' => t('Set this context when any of the paths above match the page path. Put each path on a separate line. You can use the "*" character as a wildcard and &lt;front&gt; for the front page.'),
    '#type' => 'textarea',
    '#element_validate' => array(
      'context_condition_text_validate',
    ),
  );

  // Menu trail
  // @TODO: Implement a real handler system : (
  // This condition most clearly shows the architecture problem of
  // storing both conditions and reactions as first-level children
  // on the context object.
  if (module_exists('menu')) {
    $menus = menu_parent_options(array_reverse(menu_get_menus()), NULL);
    $root_menus = array();
    foreach ($menus as $key => $name) {
      $id = explode(':', $key);
      if ($id[1] == '0') {
        $root_menus[$id[0]] = check_plain($name);
      }
      else {
        $link = menu_link_load($id[1]);
        $root_menu = $root_menus[$id[0]];
        $menus[$root_menu][$link['link_path']] = $name;
      }
      unset($menus[$key]);
    }
    array_unshift($menus, "-- " . t('None') . " --");
    $items['menu_trail'] = array(
      '#title' => t('Menu trail'),
      '#description' => t('Set this context when any of the selected menu items belong to the current active menu trail.'),
      '#options' => $menus,
      '#type' => 'select',
      '#multiple' => TRUE,
    );
  }
  else {
    $items['menu_trail'] = array(
      '#type' => 'value',
    );
  }
  if (module_exists('taxonomy')) {
    $taxonomy = array();
    $context_taxonomies = variable_get('context_taxonomy_vocabularies', array());
    foreach (taxonomy_get_vocabularies() as $vid => $vocab) {

      // Is using this vocab active for context:
      if (in_array($vid, $context_taxonomies)) {
        $taxonomy[$vid . ':0'] = '<' . $vocab->name . '>';
        foreach (taxonomy_get_tree($vid) as $term) {
          $taxonomy[$vid . ':' . $term->tid] = str_repeat('-', $term->depth + 1) . ' ' . $vocab->name . ':  ' . $term->name;
        }
      }
    }

    // Terms Selection
    $items['taxonomy'] = array(
      '#title' => t('Taxonomy Terms'),
      '#description' => t('Set this context when a node with a selected term is viewed. Select the vocabulary to set the context when a node contains any term in the vocabulary. Vocabularies and their terms will only appear in this list if you have explicity included them on their settings page.'),
      '#type' => 'select',
      '#options' => $taxonomy,
      '#multiple' => TRUE,
      '#help_topic' => 'taxonomy-condition',
      '#help_module' => 'context_ui',
    );
  }
  return $items;
}