You are here

public function DrupalTextMetaTag::getValue in Metatag 7

Get the string value of this meta tag.

Return value

string The value of this meta tag.

Overrides DrupalDefaultMetaTag::getValue

4 calls to DrupalTextMetaTag::getValue()
DrupalLinkMetaTag::getElement in ./metatag.inc
Get the HTML tag for this meta tag.
DrupalMaskIconMetaTag::getValue in metatag_favicons/metatag_favicons.mask-icon.class.inc
Get the string value of this meta tag.
DrupalSchemaMetaTag::getElement in metatag_google_plus/metatag_google_plus.inc
Get the HTML tag for this meta tag.
DrupalTitleMetaTag::getElement in ./metatag.inc
Get the HTML tag for this meta tag.
1 method overrides DrupalTextMetaTag::getValue()
DrupalMaskIconMetaTag::getValue in metatag_favicons/metatag_favicons.mask-icon.class.inc
Get the string value of this meta tag.

File

./metatag.inc, line 514
Metatag primary classes.

Class

DrupalTextMetaTag
Text-based meta tag controller.

Code

public function getValue(array $options = array()) {
  $options += array(
    'instance' => '',
    'token data' => array(),
    // Remove any remaining token after the string is parsed.
    'clear' => TRUE,
    'sanitize' => variable_get('metatag_token_sanitize', FALSE),
    'raw' => FALSE,
  );

  // If the value wasn't set there's no point in proceeding.
  if (!isset($this->data['value'])) {
    return '';
  }
  $value = $this->data['value'];
  if (empty($options['raw'])) {

    // Keep a copy of the original body value from before the summary string
    // is extracted, so that this doesn't break output from other modules.
    $old_value = NULL;

    // There can be problems extracting the [node:summary] token due to
    // certain modules using custom placeholders, e.g. Media WYSIWYG. To avoid
    // that problem, the string needs to be filtered using tidyValue() before
    // the tokens are processed.
    if (strpos($value, '[node:summary]') !== FALSE) {

      // Make sure there is a node to work with.
      if (isset($options['token data']['node'])) {

        // Get language to use for selecting body field value.
        $lang = field_language('node', $options['token data']['node'], 'body');
        if (!empty($options['token data']['node']->body[$lang][0]['value'])) {
          $old_value = $options['token data']['node']->body[$lang][0]['value'];

          // Pre-tidy the node body for token_replace if it's not empty.
          $options['token data']['node']->body[$lang][0]['value'] = $this
            ->tidyValue($old_value);
        }
      }
    }

    // Give other modules the opportunity to use hook_metatag_pattern_alter()
    // to modify defined token patterns and values before replacement.
    drupal_alter('metatag_pattern', $value, $options['token data'], $this->info['name']);
    $value = token_replace($value, $options['token data'], $options);

    // Put back the original value, if one was retained earlier.
    if (!is_null($old_value)) {
      $options['token data']['node']->body[$lang][0]['value'] = $old_value;
    }
  }

  // Special handling for language meta tags.
  if (!empty($this->info['is_language'])) {

    // If the meta tag value equals LANGUAGE_NONE, i.e. "und", then don't
    // output it.
    if (is_string($value) && $value == LANGUAGE_NONE) {
      $value = '';
    }
  }

  // Special handling for images and other URLs.
  if (!empty($this->info['image']) || !empty($this->info['url'])) {

    // Support multiple items, whether it's needed or not. Also remove the
    // empty values and reindex the array.
    $values = array_values(array_filter(explode(',', $value)));

    // If this meta tag does *not* allow multiple items, only keep the first
    // one.
    if (empty($this->info['multiple']) && !empty($values[0])) {
      $values = array(
        $values[0],
      );
    }
    foreach ($values as &$image_value) {

      // Remove any unwanted whitespace around the value.
      $image_value = trim($image_value);

      // If this contains embedded image tags, extract the image URLs.
      if (!empty($this->info['image']) && strip_tags($image_value) != $image_value) {
        $matches = array();
        preg_match('/src="([^"]*)"/', $image_value, $matches);
        if (!empty($matches[1])) {
          $image_value = $matches[1];
        }
      }

      // Convert the URL to an absolute URL.
      $image_value = $this
        ->convertUrlToAbsolute($image_value);

      // Replace spaces the URL encoded entity to avoid validation problems.
      $image_value = str_replace(' ', '%20', $image_value);
    }

    // Combine the multiple values into a single string.
    $value = implode(',', $values);
  }

  // Clean up the string a bit.
  $value = $this
    ->tidyValue($value);

  // Optionally truncate the value.
  $value = $this
    ->truncate($value);

  // Translate the final output string prior to output. Use the
  // 'output' i18n_string object type, and pass along the meta tag's
  // options as the context so it can be handled appropriately.
  $value = metatag_translate_metatag($value, $this->info['name'], $options, NULL, TRUE);
  return $value;
}