You are here

function paging_node_view in Paging 7

Implements hook_node_view().

File

./paging.module, line 307
Allows a node to be broken into multiple pages via a tag.

Code

function paging_node_view($node, $view_mode, $langcode) {

  // If paging is enabled for this node type.
  if (variable_get('paging_enabled_' . $node->type, 0) == TRUE) {

    // Get the paging field name.
    $field = variable_get('paging_field_' . $node->type, 0);

    // Fall back to default language if translation doesn't exist.
    if (empty($node->{$field}[$langcode])) {
      $langcode = $node->language;
    }
    if (isset($node->paging[$langcode]['page_count'])) {

      // Get the field to use as the body.
      $body = paging_fetch_body($node, TRUE);

      // Get the summary version of the field to use as the body.
      $summary = paging_fetch_body_summary($node, TRUE);

      // Fetch a structured array containing page names.
      $node->paging[$langcode]['page_names'] = paging_fetch_names($body);

      // Check if view_mode is teaser.
      if ($view_mode != 'full') {

        // Check to see if the summary is longer than our first page.
        if ($node->paging[$langcode]['page_count'] > 1 && strlen($summary) > strlen($node->paging[$langcode]['pages'][0])) {
          $node->paging[$langcode]['pagemore'] = TRUE;
        }
      }

      // Set an element value for this pager.
      $element = 0;

      // Pull page from the URL query string.
      $page = isset($_GET['page']) ? $_GET['page'] : '';

      // Only do paging
      // a) if not in teaser view mode;
      // b) if there is more than one page;
      // c) if a printable version is not being requested; or
      // d) if a non-paged version is not being explicitly requested
      //    e.g. http://www.example.com/node/1?page=full or node/1/full.
      if ($view_mode == 'full' && $node->paging[$langcode]['page_count'] > 1 && arg(2) != 'print' && arg(2) != 'full' && $page != 'full') {
        pager_default_initialize($node->paging[$langcode]['page_count'], 1, $element);

        // Store the page in here, for safe keeping.
        $current_page = explode(',', $page);

        // Clean up page number for use later on.
        $page = $current_page[$element] != '' ? $current_page[$element] : 0;

        // Put the current page contents into the body.
        $lang = isset($node->{$field}[$node->language]) ? $node->language : LANGUAGE_NONE;
        $format = $node->{$field}[$lang][0]['format'];
        $node->content[$field][0]['#markup'] = check_markup($node->paging[$langcode]['pages'][$page], $format, FALSE);

        // Mapping the pages in $node->paging[$langcode]['page_names'] and $node->paging[$langcode]['page_count'] to set number of pages as the array length.
        $fake = array_fill(0, $node->paging[$langcode]['page_count'] - 1 + 1, '');
        $length = count($fake) > count($node->paging[$langcode]['page_names']) ? count($fake) : count($node->paging[$langcode]['page_names']);
        for ($i = 0; $i < $length; ++$i) {
          $merged[$i] = array_key_exists($i, $node->paging[$langcode]['page_names']) ? $node->paging[$langcode]['page_names'][$i] : '';
        }

        // Fill the empty names with node title and page number.
        $node->paging[$langcode]['page_names'] = _paging_populate_empty_names($merged, $node->title);

        // Generate the pager.
        $pager = theme('pager', array(
          'element' => $element,
        ));

        // Add pager to node content.
        $node->content['paging']['#markup'] = $pager;

        // Add the second pager if requested.
        $setting = variable_get('paging_pager_count', 'one');
        if ($setting == 'two') {
          $node->content['paging_above']['#markup'] = $pager;
        }
        if (variable_get('paging_name_title_' . $node->type, 0) && !empty($page)) {

          // Set the browser title to page's name.
          drupal_set_title(t($node->paging[$langcode]['page_names'][$page]));
        }
      }
    }
  }
}