0

I have this template file for a view:

views-view--ir-news--page.tpl.php

But right now, I need to use Google AMP only for this view (Machine name: ir_news) What I want to know is if something like this it's possible:

html-views-view--ir-news.tpl.php

Because I need to change, for this view the html tag from this to this only for this view.

Is this possible? Is that the way? Thanks for the answers.

2
  • which html tag? can you show in inspect element screenshot?
    – No Sssweat
    Commented Apr 24, 2019 at 11:19
  • I need to implement the google amp tags, styles and scripts. But, I don't want to use the amp theme, because I already have a theme (template), I need to include only the html amp tag, the metatags, the styles and the scripts. This is a little hard for me for two reasons, first I don't understand very well drupal 7, and second, the most of the documentation it's on english and it's not my first language. Commented Apr 24, 2019 at 13:28

2 Answers 2

0

if you want to use AMP for specific pages, you might want to take a look at the AMP module.

But if you still want to add the amp attribut to the <HTML> tag manually, here's one way:

Overwrite the html.tpl.php template by copying the original tpl file (modules/system/html.tpl.php) to your current theme (sites/all/themes/your_theme/). Then add the following code at the top of the file:

$amp = '';
if (arg(0) == 'node' && is_numeric(arg(1))) {
    $nid = arg(1);
    $node = node_load($nid);
    $amp = $node->type == 'ir_news' ? 'amp' : '';
}  

Then, you just print the $amp variable inside the <html> tag.

The amp attribute will only be added to your ir_news pages.

0

This is my html.tpl.php

<?php global $base_url, $theme_path ?>
<!DOCTYPE html>
<html class="no-js" lang="<?= $language->language ?>" dir="<?= $language->dir ?>"<?= $rdf_namespaces, !empty($schemaorg) ? $schemaorg : ''; ?>>
    <head>
        <?= $head; ?>
        <!-- Set the viewport width to device width for mobile -->
        <meta name="viewport" content="width=device-width">
        <meta name="format-detection" content="telephone=no">
        <meta http-equiv="X-UA-Compatible" content="IE=Edge">
        <title><?= $head_title; ?></title>
        <?= $styles; ?>
        <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
        <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
        <?= $scripts; ?>
        <?php dri_config_output_gtm_head() ?>
    </head>
    <body class="<?= $classes; ?>" <?= $attributes; ?>>
    </body>
</html>

And this is what I need exclusively for this view: views-view--ir-news--page.tpl.php

<?php global $base_url, $theme_path ?>
<!doctype html>
<html amp lang="<?php print $language->language; ?>" dir="<?php print $language->dir; ?>">
  <head>
    <meta charset="utf-8">
    <title><?php print $head_title; ?></title>
    <?php print $head; ?>
    <?php include $theme_path_file . '/templates/amp-css/amp-boilerplate-styles-min.inc' ?>
    <?php if (!empty($ampsubtheme_path_file)): ?>
      <style amp-custom>
        <?php include $theme_path_file . '/css/amp-custom-styles.css' ?>
      </style>
    <?php endif; ?>
    <script async src="https://cdn.ampproject.org/v0.js"></script>
  </head>
  <body class="<?php print $classes; ?>" <?php print $attributes;?>>
  </body>
</html>

And finally I need to add this function for replace the img tag for a

function theme_image($variables) {
  // Remove RDF properties incompatible with AMP specification.
  if (isset($variables['attributes']['typeof'])) {
    unset($variables['attributes']['typeof']);
  }

  $attributes = $variables['attributes'];
  $attributes['src'] = file_create_url($variables['path']);

  foreach (array('width', 'height', 'alt', 'title') as $key) {

    if (isset($variables[$key])) {
      if (empty($attributes[$key])) {
        $attributes[$key] = $variables[$key];
      }
    }
  }

  return '<amp-img' . drupal_attributes($attributes) . '></amp-img>';
}

But I'm not sure if I can add this function to this file views-view--ir-news--page.tpl.php or it's only for the template.php file.

Thanks for the answers. Sorry for my english.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.