You are here

public static function IframeDefaultFormatter::iframeIframe in Iframe 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/IframeDefaultFormatter.php \Drupal\iframe\Plugin\Field\FieldFormatter\IframeDefaultFormatter::iframeIframe()

Like central function form the iframe code.

2 calls to IframeDefaultFormatter::iframeIframe()
IframeDefaultFormatter::viewElements in src/Plugin/Field/FieldFormatter/IframeDefaultFormatter.php
Builds a renderable array for a field value.
IframeOnlyFormatter::viewElements in src/Plugin/Field/FieldFormatter/IframeOnlyFormatter.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/IframeDefaultFormatter.php, line 77

Class

IframeDefaultFormatter
Class IframeDefaultFormatter.

Namespace

Drupal\iframe\Plugin\Field\FieldFormatter

Code

public static function iframeIframe($text, $path, $item) {

  // \iframe_debug(0, __METHOD__, $item->toArray());
  $options = [];
  $options['width'] = empty($item->width) ? '100%' : $item->width;
  $options['height'] = empty($item->height) ? '701' : $item->height;

  // Collect all allow policies.
  $allow = [];

  // Collect styles, but leave it overwritable.
  $style = '';
  $itemName = $item
    ->getFieldDefinition()
    ->getName();
  $itemParentId = $item
    ->getParent()
    ->getParent()
    ->getEntity()
    ->ID();
  if (!empty($item->frameborder) && $item->frameborder > 0) {
    $style .= '/*frameborder*/ border-width:2px;';
  }
  else {
    $style .= '/*frameborder*/ border-width:0;';
  }
  if (!empty($item->scrolling)) {
    if ($item->scrolling == 'yes') {
      $style .= '/*scrolling*/ overflow:scroll;';
    }
    elseif ($item->scrolling == 'no') {
      $style .= '/*scrolling*/ overflow:hidden;';
    }
    else {

      // Default: auto.
      $style .= '/*scrolling*/ overflow:auto;';
    }
  }
  if (!empty($item->transparency) && $item->transparency > 0) {
    $style .= '/*transparency*/ background-color:transparent;';
  }
  $htmlid = 'iframe-' . $itemName . '-' . $itemParentId;
  if (property_exists($item, 'htmlid') && $item->htmlid !== null && !empty($item->htmlid)) {
    $htmlid = $item->htmlid;
  }
  $htmlid = preg_replace('#[^A-Za-z0-9\\-\\_]+#', '-', $htmlid);
  $options['id'] = $options['name'] = $htmlid;

  // Append active class.
  $options['class'] = empty($item->class) ? '' : $item->class;

  // Remove all HTML and PHP tags from a tooltip.
  // For best performance, we act only
  // if a quick strpos() pre-check gave a suspicion
  // (because strip_tags() is expensive).
  $options['title'] = empty($item->title) ? '' : $item->title;
  if (!empty($options['title']) && strpos($options['title'], '<') !== FALSE) {
    $options['title'] = strip_tags($options['title']);
  }

  // Policy attribute.
  if (!empty($item->allowfullscreen) && $item->allowfullscreen) {
    $allow[] = 'fullscreen';
  }
  $allow[] = 'autoplay';
  $allow[] = 'camera';
  $allow[] = 'microphone';
  $allow[] = 'payment';
  $allow[] = 'accelerometer';
  $allow[] = 'geolocation';
  $allow[] = 'encrypted-media';
  $allow[] = 'gyroscope';
  $options['allow'] = implode(';', $allow);
  if (\Drupal::moduleHandler()
    ->moduleExists('token')) {

    // Token Support for field "url" and "title".
    $tokensupport = $item
      ->getTokenSupport();
    $tokencontext = [
      'user' => \Drupal::currentUser(),
    ];
    if (isset($GLOBALS['node'])) {
      $tokencontext['node'] = $GLOBALS['node'];
    }
    if ($tokensupport > 0) {
      $text = \Drupal::token()
        ->replace($text, $tokencontext);
    }
    if ($tokensupport > 1) {
      $path = \Drupal::token()
        ->replace($path, $tokencontext);
    }
  }
  $options_link = [];
  $options_link['attributes'] = [];
  $options_link['attributes']['title'] = $options['title'];
  try {
    $srcuri = Url::fromUri($path, $options_link);
    $src = $srcuri
      ->toString();
    $options['src'] = $src;
    $drupal_attributes = new Attribute($options);

    // Style attribute is filtered while rendering => use style block.
    $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>') . '<style type="text/css">iframe#' . $htmlid . ' {' . $style . '}</style>' . "\n" . '  <iframe ' . $drupal_attributes
      ->__toString() . '>' . t('Your browser does not support iframes, but you can use the following link:') . ' ' . Link::fromTextAndUrl('Link', $srcuri)
      ->toString() . '</iframe>' . '</div>';
    return $output;
  } catch (\Exception $excep) {
    watchdog_exception(__METHOD__, $excep);
    return '';
  }
}