Commit 93a004ca authored by neffets's avatar neffets
Browse files

Issue #2861387 by bomoko, flocondetoile, marcvangend: Use a Twig template for...

Issue #2861387 by bomoko, flocondetoile, marcvangend: Use a Twig template for generating html output
parent 65d6944d
Loading
Loading
Loading
Loading
+15 −1
Original line number Diff line number Diff line
@@ -65,4 +65,18 @@ function iframe_help($route_name, RouteMatchInterface $route_match) {
  }
}

/**
 * Implements hook_theme().
 */
function iframe_theme($existing, $type, $theme, $path) {
  return [
    'iframe' => [
      'variables' => [
        'src' => NULL,
        'attributes' => NULL,
        'options' => NULL,
        'text' => NULL,
      ]
    ]
  ];
}
+19 −27
Original line number Diff line number Diff line
@@ -108,11 +108,8 @@ class IframeDefaultFormatter extends FormatterBase {
      if (!isset($item->title)) {
        $item->title = '';
      }
      $elements[$delta] = array(
        '#markup' => self::iframe_iframe($item->title, $item->url, $item),
        '#allowed_tags' => array('iframe', 'a', 'h3'),
      );
      # tokens can be dynamic, so its not cacheable
      $elements[$delta] = self::iframe_iframe($item->title, $item->url, $item);
      # Tokens can be dynamic, so its not cacheable.
      if (isset($settings['tokensupport']) && $settings['tokensupport']) {
        $elements[$delta]['cache'] = array('max-age' => 0);
      }
@@ -125,7 +122,7 @@ class IframeDefaultFormatter extends FormatterBase {
   * form the iframe code
   */
  static public function iframe_iframe($text, $path, $item) {
    $options = array();
    $options = [];
    $options['width'] = !empty($item->width)? $item->width : '100%';
    $options['height'] = !empty($item->height)? $item->height : '701';

@@ -143,9 +140,9 @@ class IframeDefaultFormatter extends FormatterBase {
        $options['transparency'] = 0;
    }

    $htmlid = '';
    if (isset($item->htmlid) && !empty($item->htmlid)) {
      $htmlid = ' id="' . htmlspecialchars($item->htmlid) . '" name="' . htmlspecialchars($item->htmlid) . '"';
      $options['id'] = htmlspecialchars($item->htmlid);
      $options['name'] = htmlspecialchars($item->htmlid);
    }

    // Append active class.
@@ -157,16 +154,12 @@ class IframeDefaultFormatter extends FormatterBase {
    if (!empty($options['title']) && strpos($options['title'], '<') !== FALSE) {
      $options['title'] = strip_tags($options['title']);
    }
    $options_link = array(); $options_link['attributes'] = array();
    $options_link['attributes']['title'] = $options['title'];

    // policy attribute
    if (!empty($item->allowfullscreen) && $item->allowfullscreen) {
        $options['allow'] = (!empty($options['allow'])? $options['allow'] . "," : "") . "fullscreen";
    }

    $drupal_attributes = new Attribute($options);

    if (\Drupal::moduleHandler()->moduleExists('token')) {
      // Token Support for field "url" and "title"
      $tokensupport = $item->getTokenSupport();
@@ -181,20 +174,19 @@ class IframeDefaultFormatter extends FormatterBase {
        $path = \Drupal::token()->replace($path, $tokencontext);
      }
    }
    $src = Url::fromUri($path, $options)->toString();
    $options['src'] = $src;

    $output =
      '<div class="' . (!empty($options['class'])? new HtmlEscapedText($options['class']) : '') . '">'
        . (empty($text)? '' : '<h3 class="iframe_title">' . (isset($options['html']) && $options['html'] ? $text : new HtmlEscapedText($text)) . '</h3>')
        . '<iframe src="' . htmlspecialchars(Url::fromUri($path, $options)->toString()) . '"'
          . $drupal_attributes->__toString()
          . $htmlid
        . '>'
        . t('Your browser does not support iframes, but you can use the following link:') . ' ' . Link::fromTextAndUrl('Link', Url::fromUri($path, $options_link))->toString()
        . '</iframe>'
      . '</div>'
    ;
    return $output;
  }
    $drupal_attributes = new Attribute($options);

    $render_array = [
      '#theme' => 'iframe',
      '#options' => $options,
      '#attributes' => $drupal_attributes,
      '#text' => (isset($options['html']) && $options['html'] ? $text : new HtmlEscapedText($text)),
      '#src' => $src,
    ];
    return $render_array;
  }

}
+21 −0
Original line number Diff line number Diff line
{#
/**
* @file
* Default theme implementation to display an iframe field.
*
* Available variables:
* - src: Url being loaded into the iframe.
* - attributes: An array of HTML attributes, intended to be added to the
*   iframe tag.
* - options: Field options.
* - text: Title text.
*/
#}
<div{% if attributes.class is not empty %} class="{{ attributes.class }}"{% endif %}>
  {% if text is not empty %}
    <h3 class="iframe_title">{{ text }}</h3>
  {% endif %}
  <iframe {{ attributes }}>
    {{ 'Your browser does not support iframes, but you can visit <a href=":url">@text</a>'|t({ ':url': src, '@text': text }) }}
  </iframe>
</div>