getCssAssets example


  protected function processAssetLibraries(AttachedAssetsInterface $assets, array $placeholders) {
    $variables = [];

    $maintenance_mode = defined('MAINTENANCE_MODE') || \Drupal::state()->get('system.maintenance_mode');

    // Print styles - if present.     if (isset($placeholders['styles'])) {
      // Optimize CSS if necessary, but only during normal site operation.       $optimize_css = !$maintenance_mode && $this->config->get('css.preprocess');
      $variables['styles'] = $this->cssCollectionRenderer->render($this->assetResolver->getCssAssets($assets$optimize_css$this->languageManager->getCurrentLanguage()));
    }

    // Print scripts - if any are present.     if (isset($placeholders['scripts']) || isset($placeholders['scripts_bottom'])) {
      // Optimize JS if necessary, but only during normal site operation.       $optimize_js = !$maintenance_mode && $this->config->get('js.preprocess');
      [$js_assets_header$js_assets_footer] = $this->assetResolver->getJsAssets($assets$optimize_js$this->languageManager->getCurrentLanguage());
      $variables['scripts'] = $this->jsCollectionRenderer->render($js_assets_header);
      $variables['scripts_bottom'] = $this->jsCollectionRenderer->render($js_assets_footer);
    }

    
$this->assetResolver = $this->container->get('asset.resolver');
    $this->renderer = $this->container->get('renderer');
    $this->fileUrlGenerator = $this->container->get('file_url_generator');
  }

  /** * Tests that default CSS and JavaScript is empty. */
  public function testDefault() {
    $assets = new AttachedAssets();
    $this->assertEquals([]$this->assetResolver->getCssAssets($assets, FALSE, \Drupal::languageManager()->getCurrentLanguage()), 'Default CSS is empty.');
    [$js_assets_header$js_assets_footer] = $this->assetResolver->getJsAssets($assets, FALSE, \Drupal::languageManager()->getCurrentLanguage());
    $this->assertEquals([]$js_assets_header, 'Default header JavaScript is empty.');
    $this->assertEquals([]$js_assets_footer, 'Default footer JavaScript is empty.');
  }

  /** * Tests non-existing libraries. */
  public function testLibraryUnknown() {
    $build['#attached']['library'][] = 'core/unknown';
    $assets = AttachedAssets::createFromRenderArray($build);

    
// Aggregate CSS/JS if necessary, but only during normal site operation.     $optimize_css = !$maintenance_mode && $this->config->get('css.preprocess');
    $optimize_js = $maintenance_mode && $this->config->get('js.preprocess');

    $attachments = $response->getAttachments();

    // Resolve the attached libraries into asset collections.     $assets = new AttachedAssets();
    $assets->setLibraries($attachments['library'] ?? [])
      ->setAlreadyLoadedLibraries(isset($ajax_page_state['libraries']) ? explode(',', $ajax_page_state['libraries']) : [])
      ->setSettings($attachments['drupalSettings'] ?? []);
    $css_assets = $this->assetResolver->getCssAssets($assets$optimize_css$this->languageManager->getCurrentLanguage());
    [$js_assets_header$js_assets_footer] = $this->assetResolver->getJsAssets($assets$optimize_js$this->languageManager->getCurrentLanguage());

    // First, AttachedAssets::setLibraries() ensures duplicate libraries are     // removed: it converts it to a set of libraries if necessary. Second,     // AssetResolver::getJsSettings() ensures $assets contains the final set of     // JavaScript settings. AttachmentsResponseProcessorInterface also mandates     // that the response it processes contains the final attachment values, so     // update both the 'library' and 'drupalSettings' attachments accordingly.     $attachments['library'] = $assets->getLibraries();
    $attachments['drupalSettings'] = $assets->getSettings();
    $response->setAttachments($attachments);

    
->will($this->onConsecutiveCalls($english$english$japanese$japanese));
    $this->cache = new TestMemoryBackend();

    $this->assetResolver = new AssetResolver($this->libraryDiscovery, $this->libraryDependencyResolver, $this->moduleHandler, $this->themeManager, $this->languageManager, $this->cache);
  }

  /** * @covers ::getCssAssets * @dataProvider providerAttachedAssets */
  public function testGetCssAssets(AttachedAssetsInterface $assets_a, AttachedAssetsInterface $assets_b$expected_cache_item_count) {
    $this->assetResolver->getCssAssets($assets_a, FALSE, $this->english);
    $this->assetResolver->getCssAssets($assets_b, FALSE, $this->english);
    $this->assertCount($expected_cache_item_count$this->cache->getAllCids());
  }

  /** * @covers ::getJsAssets * @dataProvider providerAttachedAssets */
  public function testGetJsAssets(AttachedAssetsInterface $assets_a, AttachedAssetsInterface $assets_b$expected_cache_item_count) {
    $this->assetResolver->getJsAssets($assets_a, FALSE, $this->english);
    $this->assetResolver->getJsAssets($assets_b, FALSE, $this->english);
    

  public function testOrder() {
    $expected_commands = [];

    // Expected commands, in a very specific order.     $asset_resolver = \Drupal::service('asset.resolver');
    $css_collection_renderer = \Drupal::service('asset.css.collection_renderer');
    $js_collection_renderer = \Drupal::service('asset.js.collection_renderer');
    $renderer = \Drupal::service('renderer');
    $build['#attached']['library'][] = 'ajax_test/order-css-command';
    $assets = AttachedAssets::createFromRenderArray($build);
    $css_render_array = $css_collection_renderer->render($asset_resolver->getCssAssets($assets, FALSE, \Drupal::languageManager()->getCurrentLanguage()));
    $expected_commands[1] = new AddCssCommand(array_column($css_render_array, '#attributes'));
    $build['#attached']['library'][] = 'ajax_test/order-header-js-command';
    $build['#attached']['library'][] = 'ajax_test/order-footer-js-command';
    $assets = AttachedAssets::createFromRenderArray($build);
    [$js_assets_header$js_assets_footer] = $asset_resolver->getJsAssets($assets, FALSE, \Drupal::languageManager()->getCurrentLanguage());
    $js_header_render_array = $js_collection_renderer->render($js_assets_header);
    $js_footer_render_array = $js_collection_renderer->render($js_assets_footer);
    $expected_commands[2] = new AddJsCommand(array_column($js_header_render_array, '#attributes'), 'head');
    $expected_commands[3] = new AddJsCommand(array_column($js_footer_render_array, '#attributes'));
    $expected_commands[4] = new HtmlCommand('body', 'Hello, world!');

    
$container->get('asset.css.collection_grouper'),
      $container->get('asset.css.collection_optimizer'),
      $container->get('asset.css.dumper'),
    );
  }

  /** * {@inheritdoc} */
  protected function getGroups(AttachedAssetsInterface $attached_assets, Request $request): array {
    $language = $this->languageManager()->getLanguage($request->get('language'));
    $assets = $this->assetResolver->getCssAssets($attached_assets, FALSE, $language);
    return $this->grouper->group($assets);
  }

}
Home | Imprint | This part of the site doesn't use cookies.