You are here

function nodewords_metatag_from_node_content in Nodewords: D6 Meta Tags 6

Create the content of a meta tag from a node teaser.

Parameters

$node: The node object the meta tag refers to.

$content: The meta tag content.

$options: An array of options; currently, the only option used is the maximum allowed length.

Return value

The string used as meta tag content.

1 call to nodewords_metatag_from_node_content()
_nodewords_prepare_description in ./nodewords.module
Extract or generate the description for the current object.

File

./nodewords.module, line 1024
Implement an API that other modules can use to implement meta tags.

Code

function nodewords_metatag_from_node_content($node, $content, $options = array()) {

  // The method used to generate the summary string.
  $method = variable_get('nodewords_metatags_generation_method_' . $node->type, NODEWORDS_GENERATION_WHEN_EMPTY);

  // If not generating an automatic description, return immediately.
  if ($method == NODEWORDS_GENERATION_NEVER || $method == NODEWORDS_GENERATION_WHEN_EMPTY && !empty($content)) {
    return $content;
  }

  // Proceed as normal.
  $result = '';
  $source = variable_get('nodewords_metatags_generation_source_' . $node->type, NODEWORDS_GENERATION_TEASER);

  // If generating an automatic description, determine the source.
  if (!empty($node->teaser) && ($source == NODEWORDS_GENERATION_TEASER || $source == NODEWORDS_GENERATION_TEASER_BODY)) {
    $result = $node->teaser;
  }
  elseif (!empty($node->body) && ($source == NODEWORDS_GENERATION_BODY || $source == NODEWORDS_GENERATION_TEASER_BODY && empty($node->teaser))) {
    $result = $node->body;
  }

  // Clean up the text by running it through applicable filters.
  if (!empty($result)) {

    // Check for the presence of the PHP evaluator filter in the current format.
    // If the text contains PHP code, do not split it up to prevent parse errors.
    $filters = filter_list_format($node->format);
    if (isset($filters['php/0']) && strpos($result, '<?') !== FALSE) {
      $result = '';
    }
    else {

      // Run all of the normal text filters on the summary text.
      $result = check_markup($result, $node->format, FALSE);

      // Ensure there's a setting for controlling the maximum summary length.
      if (!isset($options['size'])) {
        $options['size'] = variable_get('nodewords_max_size', 350);
      }

      // Optionally replace the tag IMG with its ALT attribute.
      if (variable_get('nodewords_use_alt_attribute', FALSE)) {
        $result = preg_replace("/<img\\s[^>]*alt=[\"']([^\"']*)[\"'][^>]*>/i", ' ($1) ', $result);
      }

      // Strip off all the HTML tags.
      $result = strip_tags($result);

      // Remove the strings added from third-party modules.
      $modules = array_filter(variable_get('nodewords_filter_modules_output_' . $node->type, array()));
      $regexps = array(
        'imagebrowser' => '/\\[ibimage[^\\]]*\\]/i',
        'img_assist' => '/\\[img_assist[^\\]]*\\]/i',
      );
      foreach ($regexps as $module => $regexp) {
        if (isset($modules[$module])) {
          $result = preg_replace($regexp, '', $result);
        }
      }

      // Remove the text matching the type-specific regular expression.
      if ($regexp = trim(variable_get('nodewords_filter_regexp_' . $node->type, ''))) {
        $result = preg_replace('/' . $regexp . '/', '', $result);
      }

      // Remove line breaks.
      $result = preg_replace('/(\\r\\n?|\\n)/', ' ', $result);

      // Remove any leading & trailing whitespace.
      $result = trim($result);

      // Remove excess inline whitespace.
      $result = preg_replace('/\\s\\s+/', ' ', $result);

      // Trim the filtered text to the maximum length allowed.
      $result = node_teaser($result, $node->format, $options['size']);
    }
  }
  return $result;
}