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
Comment #2
mozh92 commentedComment #3
chegor commentedIt does not work with contextual filters?
Comment #4
venkatesh rajan.j commentedThe patch worked for me.
Thanks!
Comment #5
mozh92 commentedI think works. It works with taxonomy/term/% views
Comment #6
chegor commentedJust 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.
Comment #7
venkatesh rajan.j commentedThe patch did not provide option to enable AMP on individual taxonomy pages
Comment #8
Farreres commented@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?
Comment #9
mozh92 commented@Farreres, You need see this issue https://www.drupal.org/node/2880037
Comment #10
mykhailo.levchenko commentedHi everyone. What about adding this functionality to Drupal 8?
Comment #11
proweb.ua commentedamp 7.x-1.1
#6 patch
Comment #12
chegor commentedPatch based on the last dev.