AmpProcessedText.php in Accelerated Mobile Pages (AMP) 8.2
File
src/Element/AmpProcessedText.php
View source
<?php
namespace Drupal\amp\Element;
use Drupal\filter\Element\ProcessedText;
use Lullabot\AMP\AMP;
use Drupal\amp\Service\AMPService;
use Symfony\Component\HttpFoundation\Request;
class AmpProcessedText extends ProcessedText {
public function getInfo() {
$class = get_class($this);
return array(
'#text' => '',
'#format' => NULL,
'#filter_types_to_skip' => array(),
'#langcode' => '',
'#pre_render' => array(
array(
$class,
'preRenderText',
),
array(
$class,
'preRenderAmpText',
),
),
'#cache' => [
'contexts' => [
'url.query_args:amp',
'url.query_args:warnfix',
],
'tags' => [
'config:amp.settings',
],
],
);
}
public static function warningsOn() {
$amp_config = self::configFactory()
->get('amp.settings');
if ($amp_config
->get('amp_library_warnings_display')) {
return true;
}
$request = \Drupal::request();
$user_wants_amp_library_warnings = $request
->get('warnfix');
if (isset($user_wants_amp_library_warnings)) {
return true;
}
return false;
}
public static function preRenderAmpText($element) {
$amp_service = \Drupal::getContainer()
->get('amp.utilities');
$amp = $amp_service
->createAMPConverter();
$amp
->loadHtml($element['#markup']);
$element['#markup'] = $amp
->convertToAmpHtml();
$warning_message = "<pre>" . $amp
->warningsHumanHtml() . "</pre>";
if (self::warningsOn()) {
$element['#markup'] .= $warning_message;
}
if (!empty($amp
->getComponentJs())) {
$element['#attached']['library'] = $amp_service
->addComponentLibraries($amp
->getComponentJs());
}
return $element;
}
}