Amp for views pages by path

--- sites/all/modules/amp/amp.admin.inc	(revision )
+++ sites/all/modules/amp/amp.admin.inc	(revision )
@@ -91,6 +91,29 @@

'#size' => 20,
      '#description' => t('The Network ID to use on all tags. This value should begin with a /.'),
    );
  }

+  $form['amp_views_group'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Views pages'),
+  );
+  $form['amp_views_group']['amp_views_en'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Enable AMP Views'),
+    '#default_value' => variable_get('amp_views_en'),
+  );
+
+  $form['amp_views_group']['amp_views_pages'] = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Checked views pages'),
+    '#options' => amp_get_views_pages(),
+    '#default_value' => variable_get('amp_views_pages'),
+    '#states' => array(
+      // Only show this field when the 'toggle_me' checkbox is enabled.
+      'visible' => array(
+        ':input[name="amp_views_en"]' => array('checked' => TRUE),
+      ),
+    ),
+  );
+
   $form['pixel_group'] = array(
     '#type' => 'fieldset',
     '#title' => t('amp-pixel'),
@@ -180,6 +203,43 @@
 
   $form['#submit'] = array('amp_admin_form_submit');
   return system_settings_form($form);
+}
+
+/*
+ * get all views pages
+ */
+function amp_get_views_pages() {
+  $views = views_get_all_views();
+  //dpm($views);
+  $a_v = array();
+  if (!empty($views)) {
+    foreach ($views as $view => $data) {
+      if (!$data->disabled && ($view != 'feeds_log' && $view != 'rss')) {
+        $title_option = $data->human_name;
+        $value_option = $data->name;
+        foreach ($data->display as $key => $display) {
+          if (!empty($display->display_options['path'])) {
+            $a_v[$value_option . '#' . $key] = $title_option . ': ' . $display->display_title;
+          }
+        }
+      }
+    }
+  }
+  return $a_v;
+}
+
+function amp_compare_views_page_with_current(){
+  $checked_views = variable_get('amp_views_pages');
+  foreach($checked_views as $view){
+    $view = explode('#', $view);
+    if (count($view) == 2) {
+      $ob_view = views_get_view($view[0]);
+      if ($ob_view->display[$view[1]]->display_options['path'] == current_path()) {
+        return TRUE;
+      }elseif($ob_view->display[$view[1]]->display_options['path'] == 'taxonomy/term/%'){
+         $pos = strpos(current_path(), 'taxonomy/term/');
+         if ($pos !== false) {
+           return TRUE;
+         }
+       }
+    }
+  }
+  return FALSE;
 }
 /**
  * Submit handler for the amp_admin_form

--- sites/all/modules/amp/amp.module	(revision )
+++ sites/all/modules/amp/amp.module	(revision )
@@ -349,13 +347,17 @@
           // Only if all of the above conditions are true is this a valid AMP
           // request.
           $result = TRUE;
        }
+      }elseif(variable_get('amp_views_en')){
+        $result = amp_compare_views_page_with_current();
        }
     }
  }
   return $result;
 }
@@ -772,6 +774,24 @@
       '#value' => json_encode($metadata_json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT), array('node' => $node),
     );
     drupal_add_html_head($element, 'amp_metadata');
  }
}
+
+
+/**
+ * implements hook_views_pre_view
+ */
+function amp_views_pre_view(&$view, &$display_id, &$args){
+  if(variable_get('amp_views_en') && !isset($_GET['amp']) && amp_compare_views_page_with_current()){
+    $uri = array();
+    $uri['path'] = current_path();
+    $uri['options']['query']['amp'] = NULL;
+    $uri['options']['absolute'] = TRUE;
+    drupal_add_html_head_link(array('rel' => 'amphtml', 'href' => url($uri['path'], $uri['options'])), TRUE);
+   }
+}
 

after apply patch you get new option in admin/config/content/amp with you 'views' pages.

Only I not understand which right will scheme for this pages:
'Article', 'NewsArticle', 'BlogPosting'

Comments

mozh92 created an issue. See original summary.

mozh92’s picture

Issue summary: View changes
chegor’s picture

It does not work with contextual filters?

venkatesh rajan.j’s picture

The patch worked for me.

Thanks!

mozh92’s picture

It does not work with contextual filters?

I think works. It works with taxonomy/term/% views

chegor’s picture

StatusFileSize
new3.92 KB

Just a small step forward. I changed slightly code in function amp_compare_views_page_with_current() so that it works with exposed filters.
Patch attached.

venkatesh rajan.j’s picture

The patch did not provide option to enable AMP on individual taxonomy pages

Farreres’s picture

@mozh92: One question, in the original post in this issue you attached an image where I can see "enable AMP front page", but in my dev version I cannot find this option. How can I have it?

mozh92’s picture

@Farreres, You need see this issue https://www.drupal.org/node/2880037

mykhailo.levchenko’s picture

Hi everyone. What about adding this functionality to Drupal 8?

proweb.ua’s picture

amp 7.x-1.1
#6 patch

patching file amp.admin.inc
Hunk #1 succeeded at 73 (offset -8 lines).
Hunk #2 FAILED at 194.
1 out of 2 hunks FAILED -- saving rejects to file amp.admin.inc.rej
patching file amp.module
Hunk #1 succeeded at 315 with fuzz 2 (offset -35 lines).
Hunk #2 succeeded at 764 (offset -5 lines).

chegor’s picture

Status: Active » Needs review
StatusFileSize
new3.5 KB

Patch based on the last dev.

Status: Needs review » Needs work

The last submitted patch, 12: ampviews-2882636-12.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.