You are here

public static function AmpIframe::preRenderAmpIframe in Accelerated Mobile Pages (AMP) 8

Same name and namespace in other branches
  1. 8.2 src/Element/AmpIframe.php \Drupal\amp\Element\AmpIframe::preRenderAmpIframe()

Pre-render callback: Attaches the amp-iframe library and required markup.

File

src/Element/AmpIframe.php, line 56
Contains \Drupal\amp\Element\AmpIframe.

Class

AmpIframe
Provides a render element for an iframe rendered as an amp-iframe.

Namespace

Drupal\amp\Element

Code

public static function preRenderAmpIframe($element) {

  /** @var AMPService $amp_service */
  $amp_service = \Drupal::getContainer()
    ->get('amp.utilities');

  /** @var AMP $amp */
  $amp = $amp_service
    ->createAMPConverter();
  $amp
    ->loadHtml($element['#markup']);
  $element['#markup'] = $amp
    ->convertToAmpHtml();
  $element['#iframe'] = [
    '#markup' => t($element['#markup']),
  ];
  $element['#iframe']['#attached']['library'][] = 'amp/amp.iframe';
  return $element;
}