You are here

function google_tag_update_8103 in GoogleTagManager 8

Update the snippet parent URI and add the flush_snippets setting.

File

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

Code

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.');
}