You are here

amp.install in Accelerated Mobile Pages (AMP) 8.3

Same filename and directory in other branches
  1. 8 amp.install
  2. 8.2 amp.install
  3. 7 amp.install

File

amp.install
View source
<?php

use Drupal\image\Entity\ImageStyle;
use Drupal\image\ImageStyleInterface;

/**
 * Implements hook_install().
 */
function amp_install() {
}

/**
 * Implements hook_requirements().
 */
function amp_requirements($phase) {
  $requirements = [];
  if (!class_exists('\\Lullabot\\AMP\\AMP')) {
    $requirements['amp_library'] = [
      'title' => t('AMP'),
      'value' => t('Not available'),
      'description' => t('The AMP module requires the PHP <a href="@library">AMP library</a>.', [
        '@library' => 'https://github.com/Lullabot/amp-library',
      ]),
      'severity' => REQUIREMENT_ERROR,
    ];
  }
  if ($phase == 'runtime') {
    $theme_handler = \Drupal::service('theme_handler');

    // Check the version of AMPTheme by looking for a deprecated function.
    if (function_exists('amptheme_preprocess_username')) {
      $requirements['amptheme'] = [
        'title' => t('AMPTheme 8.3'),
        'value' => t('Not installed'),
        'description' => t('The AMP 8.3 will not work with <a href="@theme">AMPTheme 8.1</a>. Please confirm that the right version is installed.', [
          '@theme' => 'https://www.drupal.org/project/amptheme',
        ]),
        'severity' => REQUIREMENT_WARNING,
      ];
    }
    elseif ($theme_handler
      ->themeExists('amptheme')) {
      $requirements['amptheme'] = [
        'title' => t('AMPTheme 8.3'),
        'value' => t('Installed'),
        'description' => t('AMP 8.3 will not work with <a href="@theme">AMPTheme 8.1</a>. ', [
          '@theme' => 'https://www.drupal.org/project/amptheme',
        ]),
        'severity' => REQUIREMENT_OK,
      ];
    }
    $module_handler = \Drupal::service('module_handler');

    // Schema Metatag has been added as a dependency, but if AMP was
    // installed earlier this could still be missing.
    if (!$module_handler
      ->moduleExists('schema_metatag')) {
      $requirements['amp_schema_metatag'] = [
        'title' => t('Schema.org Metatag'),
        'value' => t('Not installed'),
        'description' => t('To create valid AMP you will also need to download, install, and configure the <a href="@module">Schema.org Metatag</a> module.', [
          '@module' => 'https://www.drupal.org/project/schema_metatag',
        ]),
        'severity' => REQUIREMENT_ERROR,
      ];
    }
    else {
      $requirements['amp_schema_metatag'] = [
        'title' => t('Schema.org Metatag'),
        'value' => t('Installed'),
        'description' => t('To create valid AMP you will also need to download, install, and configure the <a href="@module">Schema.org Metatag</a> module.', [
          '@module' => 'https://www.drupal.org/project/schema_metatag',
        ]),
        'severity' => REQUIREMENT_OK,
      ];
    }
    if ($module_handler
      ->moduleExists('rdf') && !$module_handler
      ->moduleExists('amp_rdf')) {
      $requirements['amp_rdf'] = [
        'title' => t('AMP RDF'),
        'value' => t('Not installed'),
        'description' => t('AMP RDF required by AMP when the RDF module is enabled.'),
        'severity' => REQUIREMENT_ERROR,
      ];
    }
    elseif ($module_handler
      ->moduleExists('rdf')) {
      $requirements['amp_rdf'] = [
        'title' => t('AMP RDF'),
        'value' => t('Installed'),
        'description' => t('AMP RDF required by AMP when the RDF module is enabled.'),
        'severity' => REQUIREMENT_OK,
      ];
    }
    if ($module_handler
      ->moduleExists('toolbar') && !$module_handler
      ->moduleExists('amp_toolbar')) {
      $requirements['amp_toolbar'] = [
        'title' => t('AMP Toolbar'),
        'value' => t('Not installed'),
        'description' => t('AMP Toolbar required by AMP when the Toolbar module is enabled.'),
        'severity' => REQUIREMENT_ERROR,
      ];
    }
    elseif ($module_handler
      ->moduleExists('toolbar')) {
      $requirements['amp_toolbar'] = [
        'title' => t('AMP Toolbar'),
        'value' => t('Installed'),
        'description' => t('AMP Toolbar required by AMP when the Toolbar module is enabled.'),
        'severity' => REQUIREMENT_OK,
      ];
    }
  }
  return $requirements;
}

/**
 * Implementation of hook_update_last_removed().
 */
function amp_update_last_removed() {
  return 8002;
}

/**
 * Remove node_types from amp.settings.
 */
function amp_update_8001(&$sandbox) {
  $config = \Drupal::service('config.factory')
    ->getEditable('amp.settings');
  $config
    ->clear('node_types')
    ->save();
}

/**
 * REMOVED.
 * @see https://www.drupal.org/project/amp/issues/2867636
 */
function amp_update_8002() {
}

/**
 * Fix Amp's image style dependencies.
 */
function amp_update_8003() {
  $config_factory = \Drupal::configFactory();
  $styles = [
    'amp_metadata_content_image_min_696px_wide',
    'amp_metadata_logo_600x60',
  ];
  foreach ($styles as $style) {
    $image_style = \Drupal::entityTypeManager()
      ->getStorage('image_style')
      ->load($style);
    if ($image_style) {
      $config = $config_factory
        ->getEditable('image.style.' . $style);
      $config
        ->set('dependencies.enforced.module', [
        'amp',
      ]);
      $config
        ->save();
    }
  }
}

/**
 * Remove AMP metatadata, deferring to the Schema.org Metadata module now.
 */
function amp_update_8004() {
  $config_factory = \Drupal::configFactory();
  $config = $config_factory
    ->getEditable('amp.metadata');
  $config
    ->delete();
}

/**
 * Replace deprecated image and file formatter settings.
 */
function amp_update_8005() {

  // Find all file fields that use the outdated formatters.
  $all_fields = \Drupal::service('entity_field.manager')
    ->getFieldMapByFieldType('file');
  foreach ($all_fields['node'] as $field_name => $info) {
    foreach ($info['bundles'] as $bundle) {
      $properties = array(
        'targetEntityType' => 'node',
        'bundle' => $bundle,
      );
      if ($view_displays = \Drupal::service('entity_type.manager')
        ->getStorage('entity_view_display')
        ->loadByProperties($properties)) {
        foreach ($view_displays as $view_display) {
          if ($component = $view_display
            ->getComponent($field_name)) {

            // Map old formatter settings to new ones.
            $changed = FALSE;
            switch ($component['type']) {
              case 'amp_video':
                $type = 'amp_video';
                $settings = array(
                  'use_description_as_link_text' => $component['settings']['use_description_as_link_text'],
                  'layout' => 'responsive',
                  'width' => $component['settings']['amp_video_width'],
                  'height' => $component['settings']['amp_video_height'],
                  'autoplay' => FALSE,
                  'controls' => TRUE,
                  'loop' => FALSE,
                );
                $changed = TRUE;
                break;
            }
            if ($changed) {
              $view_display
                ->setComponent($field_name, array(
                'type' => $type,
                'settings' => $settings,
              ) + $component)
                ->save();
            }
          }
        }
      }
    }
  }

  // Find all image fields that use the outdated formatters.
  $all_fields = \Drupal::service('entity_field.manager')
    ->getFieldMapByFieldType('image');
  foreach ($all_fields['node'] as $field_name => $info) {
    foreach ($info['bundles'] as $bundle) {
      $properties = array(
        'targetEntityType' => 'node',
        'bundle' => $bundle,
      );
      if ($view_displays = \Drupal::service('entity_type.manager')
        ->getStorage('entity_view_display')
        ->loadByProperties($properties)) {
        foreach ($view_displays as $view_display) {
          if ($component = $view_display
            ->getComponent($field_name)) {

            // Map old formatter settings to new ones.
            $changed = FALSE;
            switch ($component['type']) {
              case 'amp_image':
                $type = 'amp_image';
                $settings = array(
                  'image_style' => $component['settings']['image_style'],
                  'image_link' => $component['settings']['image_link'],
                  'layout' => $component['settings']['amp_layout'],
                  'width' => NULL,
                  'height' => $component['settings']['amp_fixed_height'],
                );
                $changed = TRUE;
                break;
            }
            if ($changed) {
              $view_display
                ->setComponent($field_name, array(
                'type' => $type,
                'settings' => $settings,
              ) + $component)
                ->save();
            }
          }
        }
      }
    }
  }
}

/**
 * Clear Power User settings which are moved to new AMP Replace module. Power
 * User is known to be outdated and may strip valid AMP markup, so it is not
 * recommended for now.
 */
function amp_update_8006() {
  $config = \Drupal::configFactory()
    ->getEditable('amp.settings');
  if (!$config
    ->isNew()) {
    $config
      ->clear('amp_library_process_full_html')
      ->clear('amp_library_warnings_display')
      ->clear('amp_library_process_full_html_warnings')
      ->clear('amp_library_process_statistics')
      ->save();
    return (string) t("Power User mode has been moved to new AMP Replace module. If you want to use that please enable and configure that module. Note that the full replacement done by Power User mode is known to be outdated and won't work correctly unless the AMP library is rewritten. You can use the AMP module without this option.");
  }
}

/**
 * Clear amp_render_css.
 */
function amp_update_8007() {
  $config = \Drupal::configFactory()
    ->getEditable('amp.settings');
  if (!$config
    ->isNew()) {
    $config
      ->clear('amp_render_css')
      ->save();
  }
  return (string) t("Removed the css everywhere setting, css is always rendered that way in 8.3.");
}

/**
 * Set module weight to ensure AMP runs after most modules.
 */
function amp_update_8008() {
  module_set_weight('amp', 10);
}

/**
 * Set module weight to ensure AMP runs after most modules.
 */
function amp_update_8009() {
  module_set_weight('amp', 0);
}

Functions

Namesort descending Description
amp_install Implements hook_install().
amp_requirements Implements hook_requirements().
amp_update_8001 Remove node_types from amp.settings.
amp_update_8002 REMOVED.
amp_update_8003 Fix Amp's image style dependencies.
amp_update_8004 Remove AMP metatadata, deferring to the Schema.org Metadata module now.
amp_update_8005 Replace deprecated image and file formatter settings.
amp_update_8006 Clear Power User settings which are moved to new AMP Replace module. Power User is known to be outdated and may strip valid AMP markup, so it is not recommended for now.
amp_update_8007 Clear amp_render_css.
amp_update_8008 Set module weight to ensure AMP runs after most modules.
amp_update_8009 Set module weight to ensure AMP runs after most modules.
amp_update_last_removed Implementation of hook_update_last_removed().