amp-8.x-3.5/modules/amp_rdf/amp_rdf.module
modules/amp_rdf/amp_rdf.module
<?php
use Drupal\Core\Template\Attribute;
/**
* Implements hook_preprocess_html() for HTML document templates.
*/
function amp_rdf_preprocess_html(&$variables) {
// Remove RDF and Metatag properties incompatible with AMP specification.
$attribute_list= [
'prefix',
'xmlns:dc',
'xmlns:og',
'xmlns:article',
'xmlns:book',
'xmlns:product',
'xmlns:profile',
'xmlns:video',
'itemtype',
'itemscope'
];
}
/**
* Implements hook_preprocess_node() for node templates.
*/
function amp_rdf_preprocess_node(&$variables) {
$amp_context = \Drupal::service('router.amp_context');
if (!$amp_context->isAmpRoute()) {
return;
}
// Remove RDF properties incompatible with AMP specification.
if (isset($variables['attributes']['about'])) {
unset($variables['attributes']['about']);
}
if (isset($variables['attributes']['typeof'])) {
unset($variables['attributes']['typeof']);
}
if (isset($variables['title_suffix']['rdf_meta_title'])) {
unset($variables['title_suffix']['rdf_meta_title']);
}
if (isset($variables['metadata'])) {
unset($variables['metadata']);
}
if (isset($variables['title_suffix']['rdf_meta_comment_count'])) {
unset($variables['title_suffix']['rdf_meta_comment_count']);
}
}
/**
* Implements hook_preprocess_user() for user templates.
*/
function amp_rdf_preprocess_user(&$variables) {
$amp_context = \Drupal::service('router.amp_context');
if (!$amp_context->isAmpRoute()) {
return;
}
// Remove RDF properties incompatible with AMP specification.
if (isset($variables['attributes']['about'])) {
unset($variables['attributes']['about']);
}
if (isset($variables['attributes']['typeof'])) {
unset($variables['attributes']['typeof']);
}
if (isset($variables['#attached']['html_head']['rdf_user_username'])) {
unset($variables['#attached']['html_head']['rdf_user_username']);
}
}
/**
* Implements hook_preprocess_username() for username templates.
*/
function amp_rdf_preprocess_username(&$variables) {
$amp_context = \Drupal::service('router.amp_context');
if (!$amp_context->isAmpRoute()) {
return;
}
// Remove RDF properties incompatible with AMP specification.
if (isset($variables['attributes']['about'])) {
unset($variables['attributes']['about']);
}
if (isset($variables['attributes']['typeof'])) {
unset($variables['attributes']['typeof']);
}
if (isset($variables['attributes']['property'])) {
unset($variables['attributes']['property']);
}
if (isset($variables['attributes']['content'])) {
unset($variables['attributes']['content']);
}
if (isset($variables['attributes']['datatype'])) {
unset($variables['attributes']['datatype']);
}
}
/**
* Implements hook_preprocess_comment() for comment templates.
*/
function amp_rdf_preprocess_comment(&$variables) {
$amp_context = \Drupal::service('router.amp_context');
if (!$amp_context->isAmpRoute()) {
return;
}
// Remove RDF properties incompatible with AMP specification.
if (isset($variables['attributes']['about'])) {
unset($variables['attributes']['about']);
}
if (isset($variables['attributes']['typeof'])) {
unset($variables['attributes']['typeof']);
}
if (isset($variables['author']['#theme']['rdf_wrapper'])) {
$variables['author'] = $variables['author']['#content'];
}
foreach ($variables['submitted'] as $key => $submitted) {
if (isset($submitted['#theme']) && ($submitted['#theme'] == 'rdf_wrapper')) {
$variables['submitted'][$key] = $variables['submitted'][$key]['#content'];
}
}
if (isset($variables['title_attributes']['property'])) {
unset($variables['title_attributes']['property']);
}
if (isset($variables['title_attributes']['datatype'])) {
unset($variables['title_attributes']['datatype']);
}
if (isset($variables['rdf_metadata_attributes'])) {
unset($variables['rdf_metadata_attributes']);
}
if (isset($variables['content']['comment_body']['#prefix'])) {
unset($variables['content']['comment_body']['#prefix']);
}
}
/**
* Implements hook_preprocess_taxonomy_term() for taxonomy term templates.
*/
function amp_rdf_preprocess_taxonomy_term(&$variables) {
$amp_context = \Drupal::service('router.amp_context');
if (!$amp_context->isAmpRoute()) {
return;
}
// Remove RDF properties incompatible with AMP specification.
if (isset($variables['attributes']['about'])) {
unset($variables['attributes']['about']);
}
if (isset($variables['attributes']['typeof'])) {
unset($variables['attributes']['typeof']);
}
if (isset($variables['title_suffix']['taxonomy_term_rdfa'])) {
unset($variables['title_suffix']['taxonomy_term_rdfa']);
}
}
/**
* Implements hook_preprocess_image() for image templates.
*/
function amp_rdf_preprocess_image(&$variables) {
$amp_context = \Drupal::service('router.amp_context');
if (!$amp_context->isAmpRoute()) {
return;
}
// Remove RDF properties incompatible with AMP specification.
if (isset($variables['attributes']['typeof'])) {
unset($variables['attributes']['typeof']);
}
}
/**
* Implements hook_preprocess_rdf_metadata() for rdf metadata templates.
*/
function amp_rdf_preprocess_rdf_metadata(&$variables) {
$amp_context = \Drupal::service('router.amp_context');
if (!$amp_context->isAmpRoute()) {
return;
}
// Remove RDF properties incompatible with AMP specification.
foreach ($variables['metadata'] as $key => $attributes) {
$variables['metadata'][$key] = new Attribute();
}
}
/**
* Implements hook_preprocess_field() for field templates.
*/
function amp_rdf_preprocess_field(&$variables) {
$amp_context = \Drupal::service('router.amp_context');
if (!$amp_context->isAmpRoute()) {
return;
}
// Remove RDF properties incompatible with AMP specification.
if (isset($variables['attributes']['rel'])) {
unset($variables['attributes']['rel']);
}
if (isset($variables['attributes']['rev'])) {
unset($variables['attributes']['rev']);
}
if (isset($variables['attributes']['property'])) {
unset($variables['attributes']['property']);
}
if (isset($variables['attributes']['content'])) {
unset($variables['attributes']['content']);
}
if (isset($variables['attributes']['datatype'])) {
unset($variables['attributes']['datatype']);
}
}
