You are here

amp_context.module in Accelerated Mobile Pages (AMP) 7

Main module file.

File

modules/amp_context/amp_context.module
View source
<?php

/**
 * @file
 * Main module file.
 */

/**
 * Implements hook_context_registry().
 */
function amp_context_context_registry() {
  return array(
    'conditions' => array(
      'is_amp_request' => array(
        'title' => t('AMP page'),
        'description' => t('Will be triggered if the page being viewed is an AMP page.'),
        'plugin' => 'amp_context_context_condition_is_amp_request',
      ),
    ),
  );
}

/**
 * Implements hook_context_plugins().
 */
function amp_context_context_plugins() {
  $plugins = array();
  $module_path = drupal_get_path('module', 'amp_context');
  $plugins['amp_context_context_condition_is_amp_request'] = array(
    'handler' => array(
      'path' => $module_path . '/plugins',
      'file' => 'amp_context_context_condition_is_amp_request.inc',
      'class' => 'amp_context_context_condition_is_amp_request',
      'parent' => 'context_condition',
    ),
  );
  return $plugins;
}

/**
 * Implements hook_context_page_condition().
 */
function amp_context_context_page_condition() {
  if ($plugin = context_get_plugin('condition', 'is_amp_request')) {
    $plugin
      ->execute();
  }
}

/**
 * Implements hook_form_id_alter().
 */
function amp_context_form_amp_admin_form_alter(&$form, &$form_state, $form_id) {
  $form['amp_context'] = array(
    '#title' => t('AMP Context'),
    '#type' => 'fieldset',
  );
  $form['amp_context']['amp_context_disable_non_amp_contexts'] = array(
    '#type' => 'checkbox',
    '#title' => t('Disable non AMP contexts'),
    '#default_value' => variable_get('amp_context_disable_non_amp_contexts', TRUE),
    '#description' => t('Disable any context that DOES NOT have "AMP Page" condition.'),
  );
}

/**
 * Implements hook_context_load_alter().
 */
function amp_context_context_load_alter(&$context) {
  if (!amp_is_amp_request()) {
    return;
  }

  // Keep only contexts that have is_amp_request condition.
  $disable_non_amp = variable_get('amp_context_disable_non_amp_contexts', TRUE);
  if ($disable_non_amp && !isset($context->conditions['is_amp_request'])) {

    // Condition not found, remove the context.
    $context = new StdClass();
    $context->name = NULL;
  }
}