google_tag.install in GoogleTagManager 8
Same filename and directory in other branches
Provides install, update, and uninstall functions.
@author Jim Berry ("solotandem", http://drupal.org/user/240748)
File
google_tag.installView source
<?php
/**
* @file
* Provides install, update, and uninstall functions.
*
* @author Jim Berry ("solotandem", http://drupal.org/user/240748)
*/
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Url;
/**
* Implements hook_requirements().
*/
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;
}
/**
* Implements hook_install().
*/
function google_tag_install() {
global $_google_tag_display_message;
$_google_tag_display_message = TRUE;
_google_tag_assets_create();
}
/**
* Implements hook_uninstall().
*/
function google_tag_uninstall() {
if (\Drupal::config('google_tag.settings')
->get('flush_snippets')) {
$directory = \Drupal::config('google_tag.settings')
->get('uri');
if (!empty($directory)) {
// Remove snippet file directory.
\Drupal::service('file_system')
->deleteRecursive($directory . '/google_tag');
}
}
// Reset the URL query argument so browsers reload snippet files.
_drupal_flush_css_js();
}
/**
* Convert config item to separate module settings and container config items.
*/
function google_tag_update_8101(&$sandbox) {
$data = \Drupal::config('google_tag.settings')
->get();
if (!empty($data['_default_container'])) {
// Config appears to be updated; do nothing.
return t('Config appears to be updated; no changes made');
}
// Create a container configuration item.
$container_config = \Drupal::service('config.factory')
->getEditable('google_tag.container.primary');
if (!empty($container_config
->get())) {
// Config appears to be updated; do nothing.
return t('Config appears to be updated; no changes made');
}
$keys = array_flip([
'uri',
'compact_snippet',
'include_file',
'rebuild_snippets',
'debug_output',
'_core',
]);
$data = array_diff_key($data, $keys);
$container_data = [
'status' => TRUE,
'id' => 'primary',
'label' => 'Primary',
'weight' => 0,
] + $data;
$container_config
->setData($container_data)
->save();
// Update the module configuration item.
$module_config = \Drupal::service('config.factory')
->getEditable('google_tag.settings');
$module_data = $module_config
->get();
unset($keys['_core']);
$data['container_id'] = '';
$module_data = array_intersect_key($module_data, $keys);
$module_data = [
'uri' => 'public://google_tag',
] + $module_data + [
'_default_container' => $data,
];
$module_config
->setData($module_data)
->save();
return t('Converted config item to separate settings and container config items');
}
/**
* Install the container configuration entity type.
*/
function google_tag_update_8102(&$sandbox) {
$type_manager = \Drupal::entityTypeManager();
$type_manager
->clearCachedDefinitions();
$entity_type = $type_manager
->getDefinition('google_tag_container');
\Drupal::entityDefinitionUpdateManager()
->installEntityType($entity_type);
return t('Installed the google_tag_container entity type');
}
/**
* Update the snippet parent URI and add the flush_snippets setting.
*/
function google_tag_update_8103(&$sandbox) {
$module_config = \Drupal::service('config.factory')
->getEditable('google_tag.settings');
$module_data = $module_config
->get();
// Update the module settings.
$snippet_uri_changed = TRUE;
$uri = isset($module_data['uri']) ? $module_data['uri'] : 'public:/';
if (substr($uri, -11) == '/google_tag') {
// Remove the default directory as this will be appended in code.
$uri = substr($uri, 0, -11);
$snippet_uri_changed = FALSE;
}
if (substr($uri, -3) == '://') {
// Remove the last slash from a bare stream wrapper.
$uri = substr($uri, 0, -1);
}
$module_data = [
'uri' => $uri,
'flush_snippets' => FALSE,
] + $module_data;
$keys = array_flip([
'uri',
'compact_snippet',
'include_file',
'rebuild_snippets',
'flush_snippets',
'debug_output',
]);
$module_data = array_merge($keys, $module_data);
$module_config
->setData($module_data)
->save();
if ($snippet_uri_changed) {
return t('Updated the snippet parent URI and added the flush_snippets setting. The old snippet directory was not deleted.');
}
return t('Added the flush_snippets setting and retained the snippet parent URI.');
}
Functions
Name | Description |
---|---|
google_tag_install | Implements hook_install(). |
google_tag_requirements | Implements hook_requirements(). |
google_tag_uninstall | Implements hook_uninstall(). |
google_tag_update_8101 | Convert config item to separate module settings and container config items. |
google_tag_update_8102 | Install the container configuration entity type. |
google_tag_update_8103 | Update the snippet parent URI and add the flush_snippets setting. |