You are here

bs_shortcodes.module in Bootstrap 3 shortcodes 8

Same filename and directory in other branches
  1. 7 bs_shortcodes.module

File

bs_shortcodes.module
View source
<?php

/**
 *
 * Parse data-attributes for shortcodes
 *
 */
use Drupal\Component\Utility\Html;
function _bs_shortcodes_parse_data_attributes($data) {
  $data_props = '';
  if ($data) {
    $data = explode('|', $data);
    foreach ($data as $d) {
      $d = explode(',', $d);
      $data_props .= sprintf('data-%s="%s" ', Html::escape($d[0]), Html::escape(trim($d[1])));
    }
  }
  else {
    $data_props = FALSE;
  }
  return $data_props;
}
function _bs_shortcodes_scrape_dom_element($tag, $content, $class, $title, $data = '') {
  if ($content && is_array($tag)) {
    $previous_value = libxml_use_internal_errors(TRUE);
    $dom = new DOMDocument();
    $dom
      ->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'));
    libxml_clear_errors();
    libxml_use_internal_errors($previous_value);
    foreach ($tag as $find) {
      $tags = $dom
        ->getElementsByTagName($find);
      foreach ($tags as $find_tag) {
        $outputdom = new DOMDocument();
        $new_root = $outputdom
          ->importNode($find_tag, TRUE);
        $outputdom
          ->appendChild($new_root);
        if (is_object($outputdom->documentElement)) {
          $outputdom->documentElement
            ->setAttribute('class', $outputdom->documentElement
            ->getAttribute('class') . ' ' . Html::escape($class));
          if ($title) {
            $outputdom->documentElement
              ->setAttribute('title', $title);
          }
          if ($data) {
            $data = explode('|', $data);
            foreach ($data as $d) {
              $d = explode(',', $d);
              $outputdom->documentElement
                ->setAttribute('data-' . $d[0], trim($d[1]));
            }
          }
        }
        return $outputdom
          ->saveHTML($outputdom->documentElement);
      }
    }
  }
  else {
    return '';
  }
}