You are here

function read_more_node_view in Read More Link 7

Implements hook_node_view().

File

./read_more.module, line 52
Customize the "Read More" link shown in teasers.

Code

function read_more_node_view($node, $view_mode, $langcode) {
  $attachments = variable_get('read_more_' . $node->type . '_view_modes', array());
  if (!empty($attachments[$view_mode])) {

    /* Teaser */
    $display = variable_get('read_more_placement', READ_MORE_PLACEMENT_DEFAULT);

    // Don't do anything if placing the link is disabled.
    if ($display == 'disable') {
      return;
    }

    // Is this an RSS feed?
    if ($view_mode == 'rss') {

      // If link replacement in RSS feeds is enabled, prevent core from
      // adding a second link by setting the readmore flag to NULL.
      if (variable_get('read_more_rss', TRUE)) {
        $node->readmore = NULL;
      }
      else {
        return;
      }
    }
    $read_more_link = read_more_link($node, $display, $view_mode);

    // Try to insert the link inline.
    if ($display == 'inline') {
      $elements_array = variable_get('read_more_elements', array(
        'p',
      ));
      $elements = '(?:' . implode('|', $elements_array) . ')';

      // Only add a link if there's a body field.
      if (!variable_get('read_more_require_body_field', FALSE) || isset($node->content['body'])) {

        // Get last position of the last closing marker in teaser, if the body
        // field exists.
        if (isset($node->content['body'])) {
          preg_match('!</?' . $elements . '[^>]*>\\s*$!i', $node->content['body'][0]['#markup'], $match, PREG_OFFSET_CAPTURE);

          // Recalculate the position in $teaser. We do this because there may be extra CCK fields appended to the teaser.
          $insert_point = empty($match) ? -1 : strpos($view_mode, $node->content['body'][0]['#markup']) + $match[0][1];

          // Insert the link.
          $node->content['body'][0]['#markup'] = substr_replace($node->content['body'][0]['#markup'], drupal_render($read_more_link), $insert_point, 0);
        }
        else {
          $display = 'after';
        }
      }
    }

    // Append the link to the end of the teaser.
    if ($display == 'after') {
      $node->content['read_more'] = read_more_link($node, $display, $view_mode) + array(
        '#weight' => 1,
      );
    }
  }
  if ($view_mode == 'full' && variable_get('read_more_anchor', FALSE)) {

    /* Full node and anchoring is enabled */
    $body = field_get_items('node', $node, 'body');
    $teaser = field_view_value('node', $node, 'body', $body[0], 'teaser');
    $teaser_rendered = $teaser['#markup'];
    $node->content['body'][0]['#markup'] = substr_replace($node->content['body'][0]['#markup'], $teaser_rendered . '<a name="more"></a>', 0, strlen($teaser_rendered));
  }
}