You are here

function course_book_override_outline_list_item in Course 7.2

Same name and namespace in other branches
  1. 8.3 modules/course_book/course_book.module \course_book_override_outline_list_item()
  2. 8.2 modules/course_book/course_book.module \course_book_override_outline_list_item()
  3. 6 modules/course_book/course_book.module \course_book_override_outline_list_item()
  4. 7 modules/course_book/course_book.module \course_book_override_outline_list_item()
  5. 3.x modules/course_book/course_book.module \course_book_override_outline_list_item()

Overrides a course outline list item.

Parameters

array $item: A course outline list item. The structure mirrors an array element from the $items param from theme_item_list().

CourseObject $courseObject: The instantiated course object that has an outline item to be overridden.

string $type: The type of override to perform. Can be:

  • all_pages: Displays a nested item list of book pages, with all items fully expanded.
  • active_tree: Displays the active menu tree, mirroring the core book outline on book pages. Additionally, the number of pages are appended to the course outline item title, to indicate there are pages when not a page within the active tree.
  • count: Displays the book title, with the number of pages in the book.
1 call to course_book_override_outline_list_item()
CourseObjectBook::overrideOutlineListItem in modules/course_book/course_book.classes.inc
Overrides a course outline list item.

File

modules/course_book/course_book.module, line 92

Code

function course_book_override_outline_list_item(&$item, CourseObject $courseObject, $type) {
  if ($courseObject
    ->getModule() == 'course_book' && $courseObject
    ->getComponent() == 'book') {
    if ($bid = $courseObject
      ->getInstanceId()) {

      // Alter the book outline item differently, depending on configured type.
      switch ($type) {
        case 'all_pages':

          // If users do not have access to take the book course object, display
          // only titles instaed of links.
          $links = $courseObject
            ->access('take');

          // Get the top level item in the tree (the book item).
          $book_items = course_book_items($bid, $links);
          $book_tree = reset($book_items);
          if ($book_tree['children']) {

            // Add a fully expanded list of children below the existing course
            // book outline item.
            $item['children'] = $book_tree['children'];
          }
          break;
        case 'active_tree':

          // @see book_block().
          $book_node = node_load($bid);
          if (!empty($book_node->book)) {
            $tree = menu_tree_page_data($book_node->book['menu_name'], $book_node->book);

            // There should only be one element at the top level.
            $data = array_shift($tree);
            if ($data['below']) {

              //$item['children'] = menu_tree_output($data['below']);
              $output = menu_tree_output($data['below']);

              // Append to the existing book outline item's data output, since we
              // don't have an array but already rendered active menu tree output.
              $item['data'] .= drupal_render($output);
            }
          }

        // Note we do not break here purposefully, to additionally append the
        // number of pages to the course outline item title. We do this
        // because when not a page within the active tree, no children items
        // display, so without this there is no indicator they are there. // Ignore this. Fixes my NetBeans.
        case 'count':
          $count = course_book_count($bid);

          // @kludge replace outline object title with an appended version.
          $subject = $item['data'];
          $pattern = check_plain($courseObject
            ->getTitle());
          if ($count > 1) {
            $replacement = t('!title (@count pages)', array(
              '!title' => $pattern,
              '@count' => $count,
            ));

            // Replace only the first instance of the title (in case the same
            // string also exists elsewhere in the data output.

            //$replaced = str_replace($pattern, $replacement, $subject);
            $replaced = preg_replace('/' . preg_quote($pattern, '/') . '/', $replacement, $subject, 1);
            $item['data'] = $replaced;
          }
          break;
      }
    }
  }
}