You are here

public function ContainerManager::saveSnippets in GoogleTagManager 8

Saves JS snippet files based on current settings.

Parameters

Drupal\Core\Config\Entity\ConfigEntityInterface $container: The container configuration entity.

Return value

bool Whether the files were saved.

Overrides ContainerManagerInterface::saveSnippets

1 call to ContainerManager::saveSnippets()
ContainerManager::createAssets in src/Entity/ContainerManager.php
Prepares directory for and saves snippet files for a container.

File

src/Entity/ContainerManager.php, line 105

Class

ContainerManager
Defines the Google tag container manager.

Namespace

Drupal\google_tag\Entity

Code

public function saveSnippets(ConfigEntityInterface $container) {
  $include_script_as_file = $this->config
    ->get('include_file');

  // Save the altered snippets after hook_google_tag_snippets_alter().
  $result = TRUE;
  $snippets = $container
    ->snippets();
  foreach ($snippets as $type => $snippet) {
    if ($include_script_as_file && $type != 'noscript') {

      // Write to file.
      $uri = $container
        ->snippetURI($type);
      $path = $this->fileSystem
        ->saveData($snippet, $uri, FileSystemInterface::EXISTS_REPLACE);
      $result = !$path ? FALSE : $result;
    }
    else {

      // Write to cache (noscript is always inline).
      $cid = $container
        ->snippetCid($type);
      $this->cache
        ->set($cid, $snippet, CacheBackendInterface::CACHE_PERMANENT, $container
        ->getCacheTags());
    }
  }
  $args = [
    '@count' => count($snippets),
    '%container' => $container
      ->get('label'),
  ];
  if (!$result) {
    $message = 'An error occurred saving @count snippet files for %container container. Contact the site administrator if this persists.';
    $this
      ->displayMessage($message, $args, MessengerInterface::TYPE_ERROR);
    $this->logger
      ->error($message, $args);
  }
  else {
    $message = 'Created @count snippet files for %container container based on configuration.';
    $this
      ->displayMessage($message, $args);

    // Reset the URL query argument so browsers reload snippet files.
    _drupal_flush_css_js();
  }
  return $result;
}