Same filename and directory in other branches
- 5.0.x advagg_mod/src/Asset/DeferJs.php
- 6.0.x advagg_mod/src/Asset/DeferJs.php
- 8.x-4.x advagg_mod/src/Asset/DeferJs.php
Namespace
Drupal\advagg_mod\Asset
File
-
advagg_mod/src/Asset/DeferJs.php
View source
<?php
namespace Drupal\advagg_mod\Asset;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Extension\ModuleHandlerInterface;
class DeferJs {
protected $deferType;
protected $counter;
protected $moduleHandler;
protected $skipList;
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 = [];
if ($this->moduleHandler
->moduleExists('admin_toolbar')) {
$this->skipList[] = Crypt::hashBase64(drupal_get_path('module', 'admin_toolbar') . '/js/admin_toolbar.js' . $this->counter);
}
}
public function defer($content) {
foreach ($this->skipList as $cid) {
if (strstr($content, $cid)) {
return $content;
}
}
if ($this->deferType === 2) {
$pattern = '/<script src="\\/[a-zA-Z0-0].*"/';
}
else {
$pattern = '/<script src=".*"/';
}
return preg_replace_callback($pattern, [
$this,
'callback',
], $content);
}
protected function callback(array $matches) {
return "{$matches[0]} defer";
}
}
Classes
| Title |
Deprecated |
Summary |
| DeferJs |
|
Add defer tag to scripts. |