You are here

function google_tag_requirements in GoogleTagManager 8

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

Implements hook_requirements().

File

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

Code

function google_tag_requirements($phase) {
  $requirements = [];
  if ($phase == 'runtime') {
    $containers = \Drupal::service('entity_type.manager')
      ->getStorage('google_tag_container')
      ->loadMultiple();
    if (empty($containers)) {

      // Google Tag Manager container ID has not been set.
      $requirements['google_tag'] = [
        'title' => t('Google Tag Manager'),
        '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>.', [
          ':url1' => Url::fromRoute('google_tag.settings_form')
            ->toString(),
          ':url2' => Url::fromRoute('entity.google_tag_container.collection')
            ->toString(),
        ]),
        '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 = \Drupal::config('google_tag.settings')
      ->get('uri');
    if (empty($directory)) {
      if ($phase == 'runtime' || $phase == 'update') {
        $requirements['google_tag_snippet_parent_directory'] = [
          '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>.', [
            ':url1' => Url::fromRoute('google_tag.settings_form')
              ->toString(),
          ]),
          'severity' => REQUIREMENT_ERROR,
          'value' => t('Not configured'),
        ];
        return $requirements;
      }
      $directory = 'public:/';
    }
    $directory .= '/google_tag';
    if (!is_dir($directory) || !_google_tag_is_writable($directory) || !_google_tag_is_executable($directory)) {
      _file_prepare_directory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::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 = \Drupal::service('stream_wrapper_manager')
        ->getViaUri($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.', [
          '%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.', [
          '%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.', [
          '%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');
      }
      $extra = '';
      if ($phase == 'install') {
        $extra = t('For more information, see INSTALL.txt or the <a href=":handbook_url">online handbook</a>.', [
          ':handbook_url' => 'https://www.drupal.org/server-permissions',
        ]);
        $value = '';
      }
      $description = [
        '#type' => 'inline_template',
        '#template' => '{{ error }} {{ description }} {{ extra }}',
        '#context' => [
          'error' => $error,
          'description' => $description,
          'extra' => $extra,
        ],
      ];
      $requirements['google_tag_snippet_directory'] = [
        'title' => t('Google Tag Manager snippet directory'),
        'description' => $description,
        'severity' => REQUIREMENT_ERROR,
        'value' => $value,
      ];
    }
  }
  return $requirements;
}