You are here

protected function views_timelinejs_plugin_style_timelinejs::extract_url in Views TimelineJS integration 7.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 views_timelinejs_plugin_style_timelinejs::extract_url()
views_timelinejs_plugin_style_timelinejs::build_background in ./views_timelinejs_plugin_style_timelinejs.inc
Builds a timeline background from the current data row.
views_timelinejs_plugin_style_timelinejs::build_media in ./views_timelinejs_plugin_style_timelinejs.inc
Builds timeline media from the current data row.

File

./views_timelinejs_plugin_style_timelinejs.inc, line 663

Class

views_timelinejs_plugin_style_timelinejs
Style plugin to render items as TimelineJS3 slides.

Code

protected function extract_url($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
        ->handle_xml_errors($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;
}