setAlreadyLoadedLibraries example

$attachments = $response->getAttachments();

    // First, gather the BigPipe placeholders that must be replaced.     $placeholders = $attachments['big_pipe_placeholders'] ?? [];
    $nojs_placeholders = $attachments['big_pipe_nojs_placeholders'] ?? [];

    // BigPipe sends responses using "Transfer-Encoding: chunked". To avoid     // sending already-sent assets, it is necessary to track cumulative assets     // from all previously rendered/sent chunks.     // @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.41     $cumulative_assets = AttachedAssets::createFromRenderArray(['#attached' => $attachments]);
    $cumulative_assets->setAlreadyLoadedLibraries($attachments['library']);

    $this->performPreSendTasks();

    // Find the closing </body> tag and get the strings before and after. But be     // careful to use the latest occurrence of the string "</body>", to ensure     // that strings in inline JavaScript or CDATA sections aren't used instead.     $parts = explode('</body>', $content);
    $post_body = array_pop($parts);
    $pre_body = implode('', $parts);

    $this->sendPreBody($pre_body$nojs_placeholders$cumulative_assets);
    
if (!$include_string) {
      throw new BadRequestHttpException('The libraries to include are encoded incorrectly.');
    }
    $attached_assets->setLibraries(explode(',', $include_string));

    if ($request->query->has('exclude')) {
      $exclude_string = UrlHelper::uncompressQueryParameter($request->query->get('exclude'));
      if (!$exclude_string) {
        throw new BadRequestHttpException('The libraries to exclude are encoded incorrectly.');
      }
      $attached_assets->setAlreadyLoadedLibraries(explode(',', $exclude_string));
    }
    $groups = $this->getGroups($attached_assets$request);

    $group = $this->getGroup($groups$request->query->get('delta'));
    // Generate a hash based on the asset group, this uses the same method as     // the collection optimizer does to create the filename, so it should match.     $generated_hash = $this->generateHash($group);
    $data = $this->optimizer->optimizeGroup($group);

    // However, the hash from the library definitions in code may not match the     // hash from the URL. This can be for three reasons:
// If we don't have any placeholders, there is no need to proceed.     if (!empty($attached['html_response_attachment_placeholders'])) {
      // Get the placeholders from attached and then remove them.       $attachment_placeholders = $attached['html_response_attachment_placeholders'];
      unset($attached['html_response_attachment_placeholders']);

      $assets = AttachedAssets::createFromRenderArray(['#attached' => $attached]);
      // Take Ajax page state into account, to allow for something like       // Turbolinks to be implemented without altering core.       // @see https://github.com/rails/turbolinks/       $ajax_page_state = $this->requestStack->getCurrentRequest()->get('ajax_page_state');
      $assets->setAlreadyLoadedLibraries(isset($ajax_page_state) ? explode(',', $ajax_page_state['libraries']) : []);
      $variables = $this->processAssetLibraries($assets$attachment_placeholders);
      // $variables now contains the markup to load the asset libraries. Update       // $attached with the final list of libraries and JavaScript settings, so       // that $response can be updated with those. Then the response object will       // list the final, processed attachments.       $attached['library'] = $assets->getLibraries();
      $attached['drupalSettings'] = $assets->getSettings();

      // Since we can only replace content in the HTML head section if there's a       // placeholder for it, we can safely avoid processing the render array if       // it's not present.
$maintenance_mode = defined('MAINTENANCE_MODE') || \Drupal::state()->get('system.maintenance_mode');

    // 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();
    
$this->assertCount($expected_cache_item_count$this->cache->getAllCids());

    $this->assetResolver->getJsAssets($assets_a, FALSE, $this->japanese);
    $this->assetResolver->getJsAssets($assets_b, FALSE, $this->japanese);
    $this->assertCount($expected_cache_item_count * 2, $this->cache->getAllCids());
  }

  public function providerAttachedAssets() {
    $time = time();
    return [
      'same libraries, different timestamps' => [
        (new AttachedAssets())->setAlreadyLoadedLibraries([])->setLibraries(['core/drupal'])->setSettings(['currentTime' => $time]),
        (new AttachedAssets())->setAlreadyLoadedLibraries([])->setLibraries(['core/drupal'])->setSettings(['currentTime' => $time + 100]),
        1,
      ],
      'different libraries, same timestamps' => [
        (new AttachedAssets())->setAlreadyLoadedLibraries([])->setLibraries(['core/drupal'])->setSettings(['currentTime' => $time]),
        (new AttachedAssets())->setAlreadyLoadedLibraries([])->setLibraries(['core/drupal', 'core/jquery'])->setSettings(['currentTime' => $time]),
        2,
      ],
    ];
  }

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