You are here

public function ScriptPlacementManager::getRenderedScriptsForPosition in Script Manager 8

Get the rendered scripts for a given position.

File

src/ScriptPlacementManager.php, line 66

Class

ScriptPlacementManager
Manage script placements.

Namespace

Drupal\script_manager

Code

public function getRenderedScriptsForPosition($position) {
  if ($this->isAdminRoute) {
    return [];
  }
  $scripts = $this->scriptStorage
    ->loadByProperties([
    'position' => $position,
  ]);
  $rendered_scripts = [
    '#cache' => [
      'tags' => [
        'config:script_list',
      ],
    ],
  ];
  foreach ($scripts as $script) {
    $access = $script
      ->access('view', NULL, TRUE);
    $rendered = [
      '#markup' => new FormattableMarkup($script
        ->getSnippet(), []),
      '#access' => $access
        ->isAllowed(),
    ];
    CacheableMetadata::createFromObject($access)
      ->addCacheableDependency($script)
      ->applyTo($rendered);
    $rendered_scripts[] = $rendered;
  }
  $this->moduleHandler
    ->alter('script_manager_scripts', $rendered_scripts);
  return $rendered_scripts;
}