You are here

function metatag_requirements in Metatag 7

Same name and namespace in other branches
  1. 8 metatag.install \metatag_requirements()

Implements hook_requirements().

File

./metatag.install, line 11
Install, update, and uninstall functions for the metatag module.

Code

function metatag_requirements($phase) {
  $requirements = array();

  // Ensure translations don't break during installation.
  $t = get_t();
  if ($phase == 'install') {

    // Handle scenarios where the site had the legacy "metatags" module
    // installed but then had Metatag installed on top of it.
    if (function_exists('db_table_exists') && function_exists('db_field_exists') && function_exists('db_query') && Database::isActiveConnection()) {

      // Check if the primary table already exists.
      if (db_table_exists('metatag')) {

        // Check to see if all of the fields exist in the table. If one of the
        // fields does not exist, proceed with the fix.
        $fields = array(
          'entity_type',
          'entity_id',
          'revision_id',
          'language',
          'data',
        );
        foreach ($fields as $field) {

          // This field doesn't exist, so determine what to do.
          if (!db_field_exists('metatag', $field)) {

            // The table contains data, so rename it.
            if (db_query("SELECT COUNT(*) FROM {metatag}")
              ->fetchField() > 0) {
              db_query("RENAME TABLE {metatag} TO {metatag}_legacy");
              $message = 'An out-of-date version of the {metatag} table was discovered. As the table contained data it was renamed with a suffix of "_legacy". This will not prevent installation from continuing, but will need to be dealt with later. See <a href="https://www.drupal.org/node/1391554">https://www.drupal.org/node/1391554</a> for further details.';
            }
            else {
              db_query("DROP TABLE {metatag}");
              $message = 'An out-of-date version of the {metatag} table was discovered. As the table was empty it was simply removed so that it could be recreated in the correct format. Installation may now proceed. See <a href="https://www.drupal.org/node/1391554">https://www.drupal.org/node/1391554</a> for further details.';
            }
            $requirements['metatag'] = array(
              'severity' => REQUIREMENT_WARNING,
              'title' => 'Metatag',
              'value' => $t('Legacy data discovered.'),
              'description' => $t($message),
            );
            drupal_set_message($t($message), 'warning');
            break;
          }
        }
      }
    }
  }
  elseif ($phase == 'runtime') {

    // Work out the release of D7 that is currently running.
    list($major, $minor) = explode('.', VERSION);

    // Strip off any suffixes on the version string, e.g. "17-dev".
    if (strpos('-', $minor)) {
      list($minor, $suffix) = explode('-', $minor);
    }

    // Releases of Drupal older than 7.40 support did not project:module syntax
    // for dependencies.
    if ($minor < 40) {
      $requirements['metatag'] = array(
        'severity' => REQUIREMENT_WARNING,
        'title' => 'Metatag',
        'value' => $t('Upgrade Drupal core to v7.40 or newer'),
        'description' => $t("This older version of Drupal core is missing functionality necessary for Metatag to work correctly, it must be upgraded to version 7.40 or newer."),
      );
    }

    // Add a note if Page Title is also installed.
    if (module_exists('page_title')) {
      $requirements['metatag_page_title'] = array(
        'severity' => REQUIREMENT_WARNING,
        'title' => 'Metatag',
        'value' => $t('Page Title module should be removed'),
        'description' => $t('The Metatag module is able to customize page titles, so running the Page Title module simultaneously can lead to complications. Please follow the instructions to <a href="@page">convert the Page Title settings</a> and uninstall the module.', array(
          '@page' => 'https://www.drupal.org/node/2774833',
        )),
      );
    }

    // Add a note if the deprecated metatag.entity_translation.inc file still
    // exists.
    $filename = 'metatag.entity_translation.inc';
    if (file_exists(dirname(__FILE__) . '/' . $filename)) {
      $requirements['metatag_deprecated_et_file'] = array(
        'severity' => REQUIREMENT_ERROR,
        'title' => 'Metatag',
        'value' => $t('Unwanted :filename file found', array(
          ':filename' => $filename,
        )),
        'description' => $t("The :filename file was removed in v7.x-1.0-beta5 but it still exists in the site's Metatag module's directory and will cause problems. This file needs to be removed. The file's path in the Drupal directory structure is:<br /><code>!short_path</code><br />The file's full path is:<br /><code>!full_path</code>", array(
          ':filename' => $filename,
          '!short_path' => drupal_get_path('module', 'metatag') . '/' . $filename,
          '!full_path' => dirname(__FILE__) . $filename,
        )),
      );
    }

    // Check that Entity_Translation is current.
    if (module_exists('entity_translation') && drupal_get_installed_schema_version('entity_translation') < 7004) {
      $requirements['metatag_et_version'] = array(
        'severity' => REQUIREMENT_ERROR,
        'title' => 'Metatag',
        'value' => $t('<a href="@url">Entity_Translation</a> is out of date and requires updating', array(
          '@url' => 'https://www.drupal.org/project/entity_translation',
        )),
        'description' => $t('The Entity_Translation module is out of date and needs to be updated in order to be compatible with Metatag.'),
      );
    }

    // It's recommended to install the Transliteration module to clean up file
    // paths for use with image meta tags.
    if (!module_exists('transliteration')) {
      $requirements['metatag_transliteration'] = array(
        'severity' => REQUIREMENT_INFO,
        'title' => 'Metatag',
        'value' => $t('The Transliteration module is recommended.'),
        'description' => $t("It is recommended to install the <a href=\"@url\">Transliteration module</a> to clean up filenames of uploaded files that may be used with image meta tags.", array(
          '@url' => 'https://drupal.org/project/transliteration',
        )),
      );
    }

    // It's recommended to install the Imagecache Token module to make image
    // tokens easier to do.
    if (!module_exists('imagecache_token')) {
      $requirements['metatag_imagecache_token'] = array(
        'severity' => REQUIREMENT_INFO,
        'title' => 'Metatag',
        'value' => $t('The Imagecache Token module is recommended.'),
        'description' => $t("It is recommended to install the <a href=\"@url\">Imagecache Token module</a> to make it easier to control image meta tags, e.g. og:image. See the Metatag module's README.txt for details.", array(
          '@url' => 'https://drupal.org/project/imagecache_token',
        )),
      );
    }

    // Recommend the Fast Token Browser module.
    if (!module_exists('fast_token_browser')) {
      $requirements['metatag_fasttokenbrowser'] = array(
        'severity' => REQUIREMENT_INFO,
        'title' => 'Metatag',
        'value' => $t('The Fast Token Browser module is recommended.'),
        'description' => $t("Using the !url can help avoid problems with the token browser.", array(
          '!url' => l(t('Fast Token Browser module'), 'https://www.drupal.org/project/fast_token_browser'),
        )),
      );
    }

    // The Admin Language module can cause problems.
    if (module_exists('admin_language') && variable_get('admin_language_force_neutral', 0)) {
      $requirements['metatag_admin_language'] = array(
        'severity' => REQUIREMENT_WARNING,
        'title' => 'Metatag',
        'value' => $t('Conflict with Admin Language module.'),
        'description' => $t("Using the \"@option\" with Metatag can lead to data loss, so it is recommended to <a href=\"@url\">disable that option</a>.", array(
          '@option' => t('Force language neutral aliases'),
          '@url' => url('admin/config/regional/language/admin_language'),
        )),
      );
    }

    // Token v7.x-1.6 is *highly* recommended.
    $token_module_info = system_get_info('module', 'token');

    // If the version string is not present then it means the module is running
    // from git, which means it can't be compared against. Alternatively, look
    // for the test file, which was the last commit of the 1.6 release.
    if (!empty($token_module_info['version']) || empty($token_module_info['files'])) {

      // If there's no test file then this is older than v1.6.
      if (empty($token_module_info['files'])) {
        $minor = 5;
      }
      else {

        // Versions are in the format 7.x-1.y, so split the string up to find
        // the 'y' portion.
        $version = explode('-', $token_module_info['version']);
        if (isset($version[1])) {
          list($major, $minor) = explode('.', $version[1]);

          // Strip off any suffixes on the version string, e.g. "17-dev".
          if (strpos('-', $minor)) {
            list($minor, $suffix) = explode('-', $minor);
          }
        }
        else {
          $minor = 0;
        }
      }

      // If v1.6 is not installed, give a warning.
      if ($minor < 6) {
        $requirements['metatag_token_version'] = array(
          'severity' => REQUIREMENT_WARNING,
          'title' => 'Metatag',
          'value' => $t('Token module is out of date.'),
          'description' => $t('It is highly recommended to install <a href="https://www.drupal.org/project/token">Token module</a> v7.x-1.6 or newer, otherwise there may be problems using certain meta tags.'),
        );
      }
    }

    // If Workbench Moderation is installed, show a message if it is out of
    // date.
    if (module_exists('workbench_moderation')) {
      $wm_module_info = system_get_info('module', 'workbench_moderation');

      // If the version string is not present then it means the module is
      // running from git, which means it can't be compared against.
      if (!empty($wm_module_info['version'])) {

        // Versions are in the format 7.x-1.y, so split the string up to find
        // the 'y' portion.
        $version = explode('-', $wm_module_info['version']);
        if (isset($version[1])) {
          list($major, $minor) = explode('.', $version[1]);
        }
        else {
          $major = 0;
        }
      }

      // If v3.x is not installed, give a message.
      if ($major < 3) {
        $requirements['metatag_wm_version'] = array(
          'severity' => REQUIREMENT_INFO,
          'title' => 'Metatag',
          'value' => $t('Workbench Moderation module is out of date.'),
          'description' => $t('It is recommended to use <a href="https://www.drupal.org/project/workbench_moderation">Workbench Moderation module</a> v7.x-3.0 or newer.'),
        );
      }
    }

    // Recommend the Schema.org Metatag module.
    if (!module_exists('schema_metatag')) {
      $requirements['metatag_schema'] = array(
        'severity' => REQUIREMENT_INFO,
        'title' => 'Metatag',
        'value' => $t('Schema.org Metatag is recommended'),
        'description' => $t('The <a href="@module">Schema.org Metatag</a> module is highly recommended to add <a href="@jsonld">JSON-LD</a> -formatted <a href="@schema">schema.org</a> compatible data structures to the site.', array(
          '@module' => 'https://www.drupal.org/project/schema_metatag',
          '@jsonld' => 'https://json-ld.org',
          '@schema' => 'http://schema.org',
        )),
      );
    }
    else {
      $requirements['metatag_schema'] = array(
        'severity' => REQUIREMENT_OK,
        'title' => 'Metatag',
        'value' => $t('Schema.org Metatag is installed'),
        'description' => $t('The <a href="@module">Schema.org Metatag</a> module is installed.', array(
          '@module' => 'https://www.drupal.org/project/schema_metatag',
        )),
      );
    }
  }
  return $requirements;
}