function GlobalTemplateCollectionManager::createGlobalCollectionForLocalCollection in Courier 8
Same name and namespace in other branches
- 2.x src/Service/GlobalTemplateCollectionManager.php \Drupal\courier\Service\GlobalTemplateCollectionManager::createGlobalCollectionForLocalCollection()
Create a global template collection and associate it with a template collection.
Parameters
\Drupal\courier\TemplateCollectionInterface $template_collection: A local template collection entity.
array $defaults: Default values to add to the new global template collection. This value must contain a 'id' key which does not conflict with existing global template collections.
Return value
\Drupal\courier\Entity\GlobalTemplateCollectionInterface A new and saved global template collection.
Throws
\Drupal\courier\Exception\GlobalTemplateCollectionException Thrown if passed template collection is unsaved.
Overrides GlobalTemplateCollectionManagerInterface::createGlobalCollectionForLocalCollection
File
- src/Service/ GlobalTemplateCollectionManager.php, line 74 
Class
- GlobalTemplateCollectionManager
- The global template collection manager.
Namespace
Drupal\courier\ServiceCode
function createGlobalCollectionForLocalCollection(TemplateCollectionInterface $template_collection, $defaults = []) {
  if ($template_collection
    ->isNew()) {
    throw new GlobalTemplateCollectionException('Template collection must be saved to instantiate a new global template collection.');
  }
  $global_template_collection = GlobalTemplateCollection::create($defaults);
  foreach ($template_collection
    ->getTemplates() as $template) {
    $contents = $template
      ->exportTemplate();
    $global_template_collection
      ->setTemplate($template
      ->getEntityTypeId(), $contents);
  }
  $global_template_collection
    ->save();
  $this->keyValueStore
    ->set($global_template_collection
    ->id(), $template_collection
    ->id());
  return $global_template_collection;
}