You are here

function google_tag_requirements in GoogleTagManager 7.2

Same name and namespace in other branches
  1. 8 google_tag.install \google_tag_requirements()
  2. 7 google_tag.install \google_tag_requirements()

Implements hook_requirements().

File

./google_tag.install, line 67
Provides install, update, and uninstall functions.

Code

function google_tag_requirements($phase) {
  $t = get_t();
  $requirements = array();
  if ($phase == 'runtime') {
    $manager = \GTMContainerManager::getInstance();
    $containers = $manager
      ->loadContainers();
    if (empty($containers)) {

      // Google Tag Manager container ID has not been set.
      $args = array(
        '@url1' => '/admin/config/system/google_tag/settings',
        '@url2' => '/admin/config/system/google_tag',
      );
      $description = $t('Configure default settings on the <a href="@url1">module settings page</a>. Afterwards, add a container on the <a href="@url2">container management page</a>.', $args);
      $requirements['google_tag'] = array(
        'title' => $t('Google Tag Manager'),
        'description' => $description,
        'severity' => REQUIREMENT_WARNING,
        'value' => $t('Not configured'),
      );
    }
  }
  if ($phase == 'runtime' || $phase == 'update' || $phase == 'install') {
    $phase == 'install' ? require_once __DIR__ . '/google_tag.module' : '';

    // Adapted from system_requirements().
    $directory = $phase == 'install' ? 'public:/' : \GTMSettings::getInstance()
      ->get('uri');
    if (empty($directory)) {
      $requirements['google_tag_snippet_parent_directory'] = array(
        'title' => t('Google Tag Manager'),
        'description' => $t('The snippet parent directory is not set. Configure default settings on the <a href="@url1">module settings page</a>.', array(
          '@url1' => '/admin/config/system/google_tag/settings',
        )),
        'severity' => REQUIREMENT_ERROR,
        'value' => t('Not configured'),
      );
      return $requirements;
    }
    $directory .= '/google_tag';
    if (!is_dir($directory) || !_google_tag_is_writable($directory) || !_google_tag_is_executable($directory)) {
      _file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
    }
    $is_executable = _google_tag_is_executable($directory);
    $is_writable = _google_tag_is_writable($directory);
    $is_directory = is_dir($directory);
    if (!$is_executable || !$is_writable || !$is_directory) {

      // The snippet directory does not exist or is not writable or searchable.
      // If applicable, get the directory path of stream wrapper.
      $wrapper = file_stream_wrapper_get_instance_by_uri($directory);
      if (method_exists($wrapper, 'getDirectoryPath') && ($path = $wrapper
        ->getDirectoryPath())) {

        // getDirectoryPath() is not defined in StreamWrapperInterface; it
        // exists in LocalStream and the local storage replacement classes in
        // google_appengine; s3fs returns an empty string.
        $path .= '/google_tag';
      }
      elseif (!($path = $wrapper
        ->getExternalUrl())) {
        $path = $directory;
      }
      if (!$is_directory) {
        $error = $t('The directory %directory does not exist.', array(
          '%directory' => $path,
        ));
        $description = $t('An automated attempt to create the directory failed, possibly due to a permissions problem. Create the directory and make it writable.');
        $value = $t('Does not exist');
      }
      elseif (!$is_writable) {
        $error = $t('The directory %directory is not writable.', array(
          '%directory' => $path,
        ));
        $description = $t('An automated attempt to make the directory writable failed, possibly due to a permissions problem. Make the directory writable.');
        $value = $t('Not writable');
      }
      else {
        $error = $t('The directory %directory is not searchable.', array(
          '%directory' => $path,
        ));
        $description = $t('An automated attempt to make the directory searchable failed, possibly due to a permissions problem. Make the directory searchable.');
        $value = $t('Not searchable');
      }
      if ($phase == 'install') {
        $description .= $t(' For more information, see INSTALL.txt or the <a href="!handbook_url">online handbook</a>.', array(
          '!handbook_url' => 'https://www.drupal.org/server-permissions',
        ));
        $value = '';
      }
      $requirements['google_tag_snippet_directory'] = array(
        'title' => $t('Google Tag Manager snippet directory'),
        'description' => "{$error} {$description}",
        'severity' => REQUIREMENT_ERROR,
        'value' => $value,
      );
    }
  }
  return $requirements;
}