class ScriptPlacementManager in Script Manager 8
Manage script placements.
Hierarchy
- class \Drupal\script_manager\ScriptPlacementManager implements ContainerInjectionInterface
Expanded class hierarchy of ScriptPlacementManager
1 file declares its use of ScriptPlacementManager
- script_manager.module in ./
script_manager.module - Manage JavaScript snippets included in your website.
File
- src/
ScriptPlacementManager.php, line 15
Namespace
Drupal\script_managerView source
class ScriptPlacementManager implements ContainerInjectionInterface {
/**
* The script storage controller.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $scriptStorage;
/**
* A flag for if the current route is an admin route.
*
* @var bool
*/
protected $isAdminRoute;
/**
* The module handler to invoke hooks on.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* ScriptPlacementManager constructor.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $scriptStorage
* The script entity storage.
* @param bool $isAdminRoute
* Whether the current route is considered an admin route.
*/
public function __construct(EntityStorageInterface $scriptStorage, $isAdminRoute, ModuleHandlerInterface $moduleHandler) {
$this->scriptStorage = $scriptStorage;
$this->isAdminRoute = $isAdminRoute;
$this->moduleHandler = $moduleHandler;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager')
->getStorage('script'), $container
->get('router.admin_context')
->isAdminRoute(), $container
->get('module_handler'));
}
/**
* Get the rendered scripts for a given position.
*/
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ScriptPlacementManager:: |
protected | property | A flag for if the current route is an admin route. | |
ScriptPlacementManager:: |
protected | property | The module handler to invoke hooks on. | |
ScriptPlacementManager:: |
protected | property | The script storage controller. | |
ScriptPlacementManager:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
ScriptPlacementManager:: |
public | function | Get the rendered scripts for a given position. | |
ScriptPlacementManager:: |
public | function | ScriptPlacementManager constructor. |