You are here

protected function SchemaNameBase::parseImageUrlValue in Schema.org Metatag 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/metatag/Tag/SchemaNameBase.php \Drupal\schema_metatag\Plugin\metatag\Tag\SchemaNameBase::parseImageUrlValue()

Parse the image url out of image markup.

A copy of the base method of the same name, but where $value is passed in instead of assumed to be $this->value().

1 call to SchemaNameBase::parseImageUrlValue()
SchemaNameBase::processItem in src/Plugin/metatag/Tag/SchemaNameBase.php

File

src/Plugin/metatag/Tag/SchemaNameBase.php, line 190

Class

SchemaNameBase
All Schema.org tags should extend this class.

Namespace

Drupal\schema_metatag\Plugin\metatag\Tag

Code

protected function parseImageUrlValue($value, $explode) {

  // If this contains embedded image tags, extract the image URLs.
  if ($this
    ->type() === 'image') {

    // If image tag src is relative (starts with /), convert to an absolute
    // link.
    global $base_root;
    if (strpos($value, '<img src="/') !== FALSE) {
      $value = str_replace('<img src="/', '<img src="' . $base_root . '/', $value);
    }
    if (strip_tags($value) != $value) {
      if ($explode) {
        $values = explode(',', $value);
      }
      else {
        $values = [
          $value,
        ];
      }

      // Check through the value(s) to see if there are any image tags.
      foreach ($values as $key => $val) {
        $matches = [];
        preg_match('/src="([^"]*)"/', $val, $matches);
        if (!empty($matches[1])) {
          $values[$key] = $matches[1];
        }
      }
      $value = implode(',', $values);

      // Remove any HTML tags that might remain.
      $value = strip_tags($value);
    }
  }
  return $value;
}