You are here

protected function TimelineJS::extractUrl in Views TimelineJS integration 8.3

Searches a string for HTML attributes that contain URLs and returns them.

This will search a string which is presumed to contain HTML for anchor or image tags. It will return the href or src attribute of the first one it finds.

This is basically special handling for core Image fields. There is no built-in field formatter for outputting a raw URL from an image. This method allows image fields to "just work" as sources for TimelineJS media and background image URLs. Anchor tag handling was added for people who forget to output link fields as plain text URLs.

Parameters

string $html: A string that contains HTML.

Return value

string A URL if one was found in the input string, the original string if not.

2 calls to TimelineJS::extractUrl()
TimelineJS::buildBackground in src/Plugin/views/style/TimelineJS.php
Builds a timeline background from the current data row.
TimelineJS::buildMedia in src/Plugin/views/style/TimelineJS.php
Builds timeline media from the current data row.

File

src/Plugin/views/style/TimelineJS.php, line 739

Class

TimelineJS
Style plugin to render items as TimelineJS3 slides.

Namespace

Drupal\views_timelinejs\Plugin\views\style

Code

protected function extractUrl($html) {
  if (!empty($html)) {

    // Disable libxml errors.
    $previous_use_errors = libxml_use_internal_errors(TRUE);
    $document = new DOMDocument();
    $document
      ->loadHTML($html);

    // Handle XML errors.
    foreach (libxml_get_errors() as $error) {
      $this
        ->handleXmlErrors($error, $html);
    }

    // Restore the previous error setting.
    libxml_use_internal_errors($previous_use_errors);

    // Check for anchor tags.
    $anchor_tags = $document
      ->getElementsByTagName('a');
    if ($anchor_tags->length) {
      return $anchor_tags
        ->item(0)
        ->getAttribute('href');
    }

    // Check for image tags.
    $image_tags = $document
      ->getElementsByTagName('img');
    if ($image_tags->length) {
      return $image_tags
        ->item(0)
        ->getAttribute('src');
    }
  }
  return $html;
}