You are here

function context_ui_context_items in Context 5

Implementation of hook_context_items().

Allows modules to integrate with context_ui and provide their native objects as options for setting/getting 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', 'radio', 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_ui_set(), so the integrating module should use a unique (within its namespace) / usable identifier. '#context_ui': Either 'setter' or 'getter'. Determines where this item will appear on the context_ui form.

File

context_ui/context_ui.module, line 49

Code

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

  // Content Types
  $nodetypes = array();
  foreach (node_get_types() as $type) {
    $nodetypes[$type->type] = t($type->name);
  }
  $items['node'] = array(
    '#title' => t('Content Types'),
    '#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',
    '#context_ui' => 'setter',
  );

  // Menu
  if (module_exists('menu')) {

    // grab menu cache
    global $_menu;
    $menus = array();
    $errors = array();

    // build options using root menus
    foreach (array_reverse(menu_get_root_menus(), true) as $root_mid => $root_menu) {

      // build menu options from children of each root menu
      $menu = array();
      $options = menu_parent_options(0, $root_mid, 0);
      unset($options[key($options)]);
      foreach ($options as $mid => $title) {
        $item = menu_get_item($mid);
        $path = $item['path'];

        // If path is unique, enter into options
        if (!isset($menu[$path])) {
          $menu[$path] = $title;
        }
        else {
          $errors[] = t('!item at !path', array(
            '!item' => str_replace('-', '', $menu[$path]),
            '!path' => $path,
          ));
          $errors[] = t('!item at !path', array(
            '!item' => str_replace('-', '', $title),
            '!path' => $path,
          ));
        }
      }
      $path = $_menu['items'][$root_mid]['path'];
      $menus[$root_mid] = "<strong>" . $root_menu . "</strong>";
      $menus = $menus + $menu;
    }
    $items['menu'] = array(
      '#title' => t('Menus'),
      '#description' => t('Display the selected menu item as active when this context is set.'),
      '#options' => $menus,
      '#type' => 'radios',
      '#context_ui' => 'getter',
    );
    if (count($errors)) {
      $items['menu']['#description'] .= '<p>' . t('<strong>Note:</strong> context_ui will not work properly with menu items that share the same path. You can work around this issue by pointing custom menu items at !alias. The following duplicates were found:', array(
        '!alias' => l(t('path aliases'), 'admin/build/path'),
      )) . '</p>';
      $items['menu']['#description'] .= theme('item_list', $errors);
    }
  }

  // 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',
    '#context_ui' => 'setter',
  );

  // Book
  if (module_exists('book')) {
    $result = db_query("SELECT b.nid, nr.title FROM {book} b JOIN {node} n ON n.nid = b.nid AND n.vid = b.vid JOIN {node_revisions} nr ON nr.vid = n.vid WHERE n.status = 1 AND b.parent = 0");
    $options = array();
    while ($book = db_fetch_object($result)) {
      $options[$book->nid] = $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',
      '#context_ui' => 'setter',
    );
  }
  return $items;
}