You are here

function paging_node_load in Paging 7

Implements hook_node_load().

1 call to paging_node_load()
paging_build_names in ./paging.module
Returns a rendered list of page links.

File

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

Code

function paging_node_load($nodes, $types) {

  // We can use $types to figure out if we need to process any of these nodes.
  $our_types = array();
  foreach ($types as $type) {
    if (variable_get('paging_enabled_' . $type, FALSE)) {
      $our_types[] = $type;
    }
  }

  // Now $our_types contains all the types from $types that we want
  // to deal with. If it's empty, we can safely return.
  if (!count($our_types)) {
    return;
  }
  foreach ($nodes as $node) {

    // Load the separator for the content type (backwards compatability w 1.0)
    $paging_separator = variable_get('paging_separator_' . $node->type, FALSE);

    // Load the global separator.
    if (!$paging_separator) {
      $paging_separator = variable_get('paging_separator', '<!--pagebreak-->');
    }
    $field = variable_get('paging_field_' . $node->type, 0);
    if (!empty($node->{$field})) {
      foreach ($node->{$field} as $language => $item) {
        $body = $item[0]['value'];

        // Check if manual page separators were used.
        if (strpos($body, $paging_separator) !== FALSE) {
          $node->paging[$language]['pages'] = explode($paging_separator, $body);
          $node->paging[$language]['page_count'] = count($node->paging[$language]['pages']);
        }
        else {
          $body_parts = $body;

          // Automatic paging based on character count.
          if (variable_get('paging_automatic_method_' . $node->type, 0) == 'chars' && ($max_chars = variable_get('paging_automatic_chars_' . $node->type, 4000)) != 0) {
            $orphan_size = variable_get('paging_automatic_chars_orphan_' . $type, 100);
            $total_chars = drupal_strlen($body);

            // Check if pagination is possible.
            if ($total_chars > $max_chars) {
              $body = $body;
              $breaks = (int) ($total_chars / $max_chars);
              $bodypart = array();
              for ($i = 0; $i <= $breaks; $i++) {

                // Pick off the next body part.
                $bodypart[$i] = truncate_utf8($body, $max_chars, TRUE);

                // Now pull that off the body.
                $bodycount = drupal_strlen($bodypart[$i]);
                $body = drupal_substr($body, $bodycount);

                // Check for orphans.
                if (drupal_strlen($body) < $orphan_size) {
                  $bodypart[$i] .= $body;
                  break;
                }
              }
              $body_parts = implode($paging_separator, $bodypart);
            }
          }
          elseif (variable_get('paging_automatic_method_' . $node->type, 0) == 'words' && ($max_words = variable_get('paging_automatic_words_' . $node->type, 400)) != 0) {
            $orphan_size = variable_get('paging_automatic_words_orphan_' . $type, 100);
            $words = explode(' ', $body);
            $total_words = count($words);
            $words_remaining = $total_words - $max_words;

            // Check if pagination is possible.
            if ($total_words > $max_words) {
              $breaks = (int) ($total_words / $max_words);
              for ($i = 1; $i < $breaks; $i++) {
                $index = $i * $max_words;
                $words_remaining -= $max_words;

                // Orphan check.
                if ($words_remaining >= $orphan_size) {

                  // Not an orphan, treat normally.
                  $words[$index] .= $paging_separator;
                }
              }
            }
            $body_parts = implode(' ', $words);
          }
          $node->paging[$language]['pages'] = explode($paging_separator, $body_parts);
          $node->paging[$language]['page_count'] = count($node->paging[$language]['pages']);
        }
      }
    }
  }
}