You are here

class JsCollectionRenderer in Advanced CSS/JS Aggregation 8.2

Hierarchy

Expanded class hierarchy of JsCollectionRenderer

1 string reference to 'JsCollectionRenderer'
advagg.services.yml in ./advagg.services.yml
advagg.services.yml
1 service uses JsCollectionRenderer
asset.js.collection_renderer in ./advagg.services.yml
Drupal\advagg\Asset\JsCollectionRenderer

File

src/Asset/JsCollectionRenderer.php, line 14

Namespace

Drupal\advagg\Asset
View source
class JsCollectionRenderer extends CoreJsCollectionRenderer implements AssetCollectionRendererInterface {

  /**
   * A config object for the advagg configuration.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $config;

  /**
   * {@inheritdoc}
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   A config factory for retrieving required config objects.
   */
  public function __construct(StateInterface $state, ConfigFactoryInterface $config_factory) {
    $this->state = $state;
    $this->config = $config_factory
      ->get('advagg.settings');
  }

  /**
   * {@inheritdoc}
   */
  public function render(array $js_assets) {

    // Check assets array to ensure that it isn't malformed.
    if (isset($js_assets['-1']) && empty($js_assets[-1])) {
      unset($js_assets['-1']);
    }
    $elements = [
      'prefetch' => [],
    ];
    $prefetch = $this->config
      ->get('dns_prefetch');

    // A dummy query-string is added to filenames, to gain control over
    // browser-caching. The string changes on every update or full cache
    // flush, forcing browsers to load a new copy of the files, as the
    // URL changed. Files that should not be cached get REQUEST_TIME as
    // query-string instead, to enforce reload on every page request.
    $default_query_string = $this->state
      ->get('system.css_js_query_string') ?: '0';

    // Defaults for each element.
    $element_defaults = [
      '#type' => 'html_tag',
      '#tag' => 'script',
      '#value' => '',
    ];
    $prefetch_element_defaults = [
      '#type' => 'html_tag',
      '#tag' => 'link',
      '#attributes' => [
        'rel' => 'dns-prefetch',
      ],
    ];

    // Loop through all JS assets.
    foreach ($js_assets as $js_asset) {

      // Element properties that do not depend on JS asset type.
      $element = $element_defaults;
      $element['#browsers'] = $js_asset['browsers'];

      // Element properties that depend on item type.
      switch ($js_asset['type']) {
        case 'setting':
          $element['#attributes'] = [
            // This type attribute prevents this from being parsed as an
            // inline script.
            'type' => 'application/json',
            'data-drupal-selector' => 'drupal-settings-json',
          ];
          $element['#value'] = Json::encode($js_asset['data']);
          break;
        case 'file':
          $query_string = $js_asset['version'] == -1 ? $default_query_string : 'v=' . $js_asset['version'];
          $query_string_separator = strpos($js_asset['data'], '?') !== FALSE ? '&' : '?';
          $element['#attributes']['src'] = file_url_transform_relative(file_create_url($js_asset['data']));

          // Only add the cache-busting query string if this isn't an aggregate
          // file.
          if (!isset($js_asset['preprocessed'])) {
            $element['#attributes']['src'] .= $query_string_separator . ($js_asset['cache'] ? $query_string : REQUEST_TIME);
          }
          $element['#inline'] = !empty($js_asset['inline']) ? TRUE : FALSE;
          break;
        case 'external':
          $element['#attributes']['src'] = $js_asset['data'];
          if ($prefetch) {
            $pre_element = $prefetch_element_defaults;
            $pre_element['#attributes']['href'] = '//' . parse_url($js_asset['data'], PHP_URL_HOST);
            $elements['prefetch'][] = $pre_element;
          }
          break;
        default:
          throw new \Exception('Invalid JS asset type.');
      }

      // Attributes may only be set if this script is output independently.
      if (!empty($element['#attributes']['src']) && !empty($js_asset['attributes'])) {
        $element['#attributes'] += $js_asset['attributes'];
      }
      $elements[] = $element;
    }
    if (empty($elements['prefetch'])) {
      unset($elements['prefetch']);
    }
    return $elements;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
JsCollectionRenderer::$config protected property A config object for the advagg configuration.
JsCollectionRenderer::$state protected property The state key/value store.
JsCollectionRenderer::render public function This class evaluates the aggregation enabled/disabled condition on a group by group basis by testing whether an aggregate file has been made for the group rather than by testing the site-wide aggregation setting. This allows this class to work… Overrides JsCollectionRenderer::render
JsCollectionRenderer::__construct public function Overrides JsCollectionRenderer::__construct