Same name in other branches
- 5.0.x advagg_mod/src/Asset/DeferJs.php \Drupal\advagg_mod\Asset\DeferJs
- 6.0.x advagg_mod/src/Asset/DeferJs.php \Drupal\advagg_mod\Asset\DeferJs
- 8.x-4.x advagg_mod/src/Asset/DeferJs.php \Drupal\advagg_mod\Asset\DeferJs
Add defer tag to scripts.
Hierarchy
- class \Drupal\advagg_mod\Asset\DeferJs
Expanded class hierarchy of DeferJs
1 file declares its use of DeferJs
- InitSubscriber.php dans advagg_mod/
src/ EventSubscriber/ InitSubscriber.php
2 string references to 'DeferJs'
- advagg_mod.services.yml dans advagg_mod/
advagg_mod.services.yml - advagg_mod/advagg_mod.services.yml
- InitSubscriber::getSubscribedEvents dans advagg_mod/
src/ EventSubscriber/ InitSubscriber.php
1 service uses DeferJs
Fichier
-
advagg_mod/
src/ Asset/ DeferJs.php, line 12
Namespace
Drupal\advagg_mod\AssetView source
class DeferJs {
/**
* The defer type to use from advagg_mod configuration.
*
* @var int
*/
protected $deferType;
/**
* The global count to use from advagg configuration.
*
* @var int
*/
protected $counter;
/**
* The Drupal module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* A list of file cids to skip.
*
* @var array
*/
protected $skipList;
/**
* DeferCss constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The Drupal module handler.
*/
public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
$this->deferType = $config_factory->get('advagg_mod.settings')
->get('css_defer_js_code');
$this->counter = $config_factory->get('advagg.settings')
->get('global_counter');
$this->moduleHandler = $module_handler;
$this->skipList = [];
// Admin Toolbar 8x fails when deferred.
if ($this->moduleHandler
->moduleExists('admin_toolbar')) {
$this->skipList[] = Crypt::hashBase64(drupal_get_path('module', 'admin_toolbar') . '/js/admin_toolbar.js' . $this->counter);
}
}
/**
* Add defer attribute to script tags.
*
* @param string $content
* The response content.
*
* @return string
* Updated content.
*/
public function defer($content) {
// Admin Toolbar 8x fails when deferred.
foreach ($this->skipList as $cid) {
if (strstr($content, $cid)) {
return $content;
}
}
// Only defer local scripts.
if ($this->deferType === 2) {
$pattern = '/<script src="\\/[a-zA-Z0-0].*"/';
}
else {
$pattern = '/<script src=".*"/';
}
return preg_replace_callback($pattern, [
$this,
'callback',
], $content);
}
/**
* Callback to replace individual stylesheet links.
*
* @param array $matches
* Array from matches from preg_replace_callback.
*
* @return string
* Updated html string.
*/
protected function callback(array $matches) {
return "{$matches[0]} defer";
}
}
Members
Titre Trier par ordre décroissant | Modifiers | Object type | Résumé |
---|---|---|---|
DeferJs::$counter | protected | property | The global count to use from advagg configuration. |
DeferJs::$deferType | protected | property | The defer type to use from advagg_mod configuration. |
DeferJs::$moduleHandler | protected | property | The Drupal module handler. |
DeferJs::$skipList | protected | property | A list of file cids to skip. |
DeferJs::callback | protected | function | Callback to replace individual stylesheet links. |
DeferJs::defer | public | function | Add defer attribute to script tags. |
DeferJs::__construct | public | function | DeferCss constructor. |