5.x-3.x branch
Implementation of the Hierarchical Select API for the Book module.
| Name | Description |
|---|---|
| hs_book_admin_settings | Form definition; admin settings. |
| hs_book_form_alter | Implementation of hook_form_alter(). |
| hs_book_hierarchical_select_children | Implementation of hook_hierarchical_select_children(). |
| hs_book_hierarchical_select_implementation_info | Implementation of hook_hierarchical_select_implementation_info(). |
| hs_book_hierarchical_select_item_get_label | Implementation of hook_hierarchical_select_item_get_label(). |
| hs_book_hierarchical_select_lineage | Implementation of hook_hierarchical_select_lineage(). |
| hs_book_hierarchical_select_params | Implementation of hook_hierarchical_select_params(). |
| hs_book_hierarchical_select_root_level | Implementation of hook_hierarchical_select_root_level(). |
| hs_book_hierarchical_select_valid_item | Implementation of hook_hierarchical_select_valid_item(). |
| hs_book_menu | Implementation of hook_menu(). |
- <?php
-
- /**
- * @file
- * Implementation of the Hierarchical Select API for the Book module.
- */
-
-
- //----------------------------------------------------------------------------
- // Drupal core hooks.
-
- /**
- * Implementation of hook_menu().
- */
- function hs_book_menu($may_cache) {
- $items = array();
-
- if ($may_cache) {
- $items[] = array(
- 'path' => 'admin/settings/hierarchical_select/book',
- 'title' => t('Book'),
- 'description' => t('Hierarchical Select configuration for Book'),
- 'callback' => 'drupal_get_form',
- 'callback arguments' => array('hs_book_admin_settings'),
- 'type' => MENU_LOCAL_TASK,
- );
- }
- return $items;
- }
-
- /**
- * Implementation of hook_form_alter().
- */
- function hs_book_form_alter($form_id, &$form) {
- if ($form_id == 'book_node_form' && isset($form['parent'])) {
- unset($form['parent']['#options']);
- $form['parent']['#type'] = 'hierarchical_select';
- $form['parent']['#config'] = array(
- 'module' => 'hs_book',
- 'params' => array(
- 'nid' => (isset($form['nid'])) ? $form['nid']['#value'] : NULL,
- ),
- 'save_lineage' => 0,
- 'enforce_deepest' => 0,
- 'resizable' => variable_get('hs_book_resizable', 1),
- );
- $form['parent']['#default_value'] = (!is_numeric($form['parent']['#default_value'])) ? 0 : $form['parent']['#default_value'];
- }
- }
-
-
- //----------------------------------------------------------------------------
- // Menu callbacks.
-
- /**
- * Form definition; admin settings.
- */
- function hs_book_admin_settings() {
- $form['hs_book_resizable'] = array(
- '#type' => 'radios',
- '#title' => t('Resizable'),
- '#description' => t(
- "When enabled, a handle appears below the Hierarchical Select to allow
- the user to dynamically resize it. Double clicking will toggle between
- the smallest and a sane 'big size'."
- ),
- '#options' => array(
- 0 => t('Disabled'),
- 1 => t('Enabled'),
- ),
- '#default_value' => variable_get('hs_book_resizable', 1),
- );
-
- return system_settings_form($form);
- }
-
-
- //----------------------------------------------------------------------------
- // Hierarchical Select hooks.
-
- /**
- * Implementation of hook_hierarchical_select_params().
- */
- function hs_book_hierarchical_select_params() {
- $params = array(
- 'nid', // The nid of the given book page.item. This book page and its children will be excluded from the hierarchy.
- );
- return $params;
- }
-
- /**
- * Implementation of hook_hierarchical_select_root_level().
- */
- function hs_book_hierarchical_select_root_level($params) {
- $root_level = array();
-
- // If the user has permission to create new books, add the top-level book page to the menu;
- if (user_access('create new books')) {
- $root_level[0] = '<'. t('top-level') .'>';
- }
- $root_level += hs_book_hierarchical_select_children(-1, $params);
-
- return $root_level;
- }
-
- /**
- * Implementation of hook_hierarchical_select_children().
- */
- function hs_book_hierarchical_select_children($parent, $params) {
- // TRICKY: because the book form assumes that the value "0" corresponds to
- // the <top-level page> option (for creating a new menu item at the root
- // level) *AND* 0 is also a pseudo-value in the actual hierarchy (it
- // represents the root), we have to pretend in the code that -1 is the root
- // in the hierarchy instead of 0.
- if ($parent == 0) {
- return array();
- }
- if ($parent == -1) {
- $parent = 0;
- }
-
- $result = db_query(db_rewrite_sql('SELECT n.nid, n.title FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 AND b.parent = %d ORDER BY b.weight, n.title'), $parent);
-
- $children = array();
- while ($node = db_fetch_object($result)) {
- // Don't include the given book page in the hierarchy!
- if (isset($params['nid']) && $params['nid'] == $node->nid) {
- continue;
- }
-
- $children[$node->nid] = $node->title;
- }
-
- return $children;
- }
-
- /**
- * Implementation of hook_hierarchical_select_lineage().
- */
- function hs_book_hierarchical_select_lineage($item, $params) {
- $lineage = array($item);
-
- while (TRUE) {
- $parent = db_result(db_query("SELECT parent FROM {book} WHERE nid = %d", $item));
-
- // 0 is the root menu item, so if $parent == 0, the lineage is complete!
- if ($parent == 0)
- break;
-
- array_unshift($lineage, $parent);
- $item = $parent;
- }
-
- return $lineage;
- }
-
- /**
- * Implementation of hook_hierarchical_select_valid_item().
- */
- function hs_book_hierarchical_select_valid_item($item, $params) {
- if (!is_numeric($item) || $item < 0) {
- return FALSE;
- }
-
- // Item 0 corresponds to the special <top-level page> option.
- if ($item == 0) {
- return TRUE;
- }
-
- $count = db_result(db_query(db_rewrite_sql("SELECT COUNT(n.nid) FROM {book} n WHERE n.nid = %d"), $item));
- return ($count == 1);
- }
-
- /**
- * Implementation of hook_hierarchical_select_item_get_label().
- */
- function hs_book_hierarchical_select_item_get_label($item, $params) {
- static $labels = array();
-
- if (!isset($labels[$item])) {
- $labels[$item] = t(db_result(db_query("SELECT n.title FROM {book} b INNER JOIN {node} n ON b.vid = n.vid WHERE b.nid = %d", $item)));
- }
-
- return $labels[$item];
- }
-
- /**
- * Implementation of hook_hierarchical_select_implementation_info().
- */
- function hs_book_hierarchical_select_implementation_info() {
- $info = node_get_types('type', 'book');
- return array(
- 'hierarchy type' => t('Book'),
- 'entity type' => t('Node (!book_page)', array('!book_page' => $info->name)),
- );
- }
-