Same name in other branches
  1. 5.0.x advagg_mod/src/Asset/AsyncJs.php \Drupal\advagg_mod\Asset\AsyncJs
  2. 6.0.x advagg_mod/src/Asset/AsyncJs.php \Drupal\advagg_mod\Asset\AsyncJs
  3. 8.x-4.x advagg_mod/src/Asset/AsyncJs.php \Drupal\advagg_mod\Asset\AsyncJs

Add async tag to scripts.

Hierarchy

  • class \Drupal\advagg_mod\Asset\AsyncJs

Expanded class hierarchy of AsyncJs

1 file declares its use of AsyncJs
InitSubscriber.php in advagg_mod/src/EventSubscriber/InitSubscriber.php
2 string references to 'AsyncJs'
advagg_mod.services.yml in advagg_mod/advagg_mod.services.yml
advagg_mod/advagg_mod.services.yml
InitSubscriber::getSubscribedEvents in advagg_mod/src/EventSubscriber/InitSubscriber.php
1 service uses AsyncJs
advagg_mod.async_js in advagg_mod/advagg_mod.services.yml
Drupal\advagg_mod\Asset\AsyncJs

File

advagg_mod/src/Asset/AsyncJs.php, line 8

Namespace

Drupal\advagg_mod\Asset
View source
class AsyncJs {
    
    /**
     * Add Async attribute to all external script tags.
     *
     * @param string $content
     *   The response content.
     *
     * @return string
     *   Updated content.
     */
    public function async($content) {
        $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]} async";
    }

}

Members

Title Sort descending Modifiers Object type Summary
AsyncJs::async public function Add Async attribute to all external script tags.
AsyncJs::callback protected function Callback to replace individual stylesheet links.