getLibraryByName example


  public function testDeprecatedLibraries(string $extension, string $name, string $deprecation_suffix, string $expected_hashed_library_definition): void {
    /** @var \Drupal\Core\Asset\LibraryDiscoveryInterface $library_discovery */
    $library_discovery = $this->container->get('library.discovery');

    // DrupalCI uses a precision of 100 in certain environments which breaks     // this test.     ini_set('serialize_precision', -1);

    $this->expectDeprecation("The $extension/$name " . $deprecation_suffix);
    $library_definition = $library_discovery->getLibraryByName($extension$name);
    $this->assertEquals($expected_hashed_library_definitionmd5(serialize($library_definition)));
  }

  /** * The data provider for testDeprecatedLibraries. * * Returns an array in the form of * @code * [ * (string) description => [ * (string) extension - The name of the extension that registered a library, usually 'core' * (string) name - The name of a registered library * (string) deprecation_suffix - The part of the deprecation message after the extension/name * (string) expected_hashed_library_definition - The expected MD5 hash of the library * ] * ] * @endcode * * @return array * See description above. */
/** * @covers ::getLibrariesByExtension */
  public function testGetLibrariesByExtension() {
    $this->libraryDiscovery->getLibrariesByExtension('test');
    // Verify that subsequent calls don't trigger hook_library_info_alter()     // and hook_js_settings_alter() invocations, nor do they talk to the     // collector again. This ensures that the alterations made by     // hook_library_info_alter() and hook_js_settings_alter() implementations     // are statically cached, as desired.     $this->libraryDiscovery->getLibraryByName('test', 'test_1');
    $this->libraryDiscovery->getLibrariesByExtension('test');
  }

  /** * Tests getting a library by name. * * @covers ::getLibraryByName */
  public function testGetLibraryByName() {
    $this->assertSame($this->libraryData['test_1']$this->libraryDiscovery->getLibraryByName('test', 'test_1'));
  }

  
$css = [];
    $default_options = [
      'type' => 'file',
      'group' => CSS_AGGREGATE_DEFAULT,
      'weight' => 0,
      'media' => 'all',
      'preprocess' => TRUE,
    ];

    foreach ($libraries_to_load as $library) {
      [$extension$name] = explode('/', $library, 2);
      $definition = $this->libraryDiscovery->getLibraryByName($extension$name);
      if (isset($definition['css'])) {
        foreach ($definition['css'] as $options) {
          $options += $default_options;
          // Copy the asset library license information to each file.           $options['license'] = $definition['license'];

          // Files with a query string cannot be preprocessed.           if ($options['type'] === 'file' && $options['preprocess'] && str_contains($options['data'], '?')) {
            $options['preprocess'] = FALSE;
          }

          
$this->container->get('theme_installer')->install(['test_theme', 'starterkit_theme']);
    $this->libraryDiscovery = $this->container->get('library.discovery');
  }

  /** * Tests that hook_library_info is invoked and the cache is cleared. */
  public function testHookLibraryInfoByTheme() {
    // Activate test_theme and verify that the library 'kitten' is added using     // hook_library_info_alter().     $this->activateTheme('test_theme');
    $this->assertNotEmpty($this->libraryDiscovery->getLibraryByName('test_theme', 'kitten'));

    // Now make starterkit_theme the active theme and assert that library is not     // added.     $this->activateTheme('starterkit_theme');
    $this->assertFalse($this->libraryDiscovery->getLibraryByName('test_theme', 'kitten'));
  }

  /** * Tests that libraries-override are applied to library definitions. */
  public function testLibrariesOverride() {
    

  protected function doGetDependencies(array $libraries_with_unresolved_dependencies, array $final_libraries = []) {
    foreach ($libraries_with_unresolved_dependencies as $library) {
      if (!isset($final_libraries[$library])) {
        [$extension$name] = explode('/', $library, 2);
        $definition = $this->libraryDiscovery->getLibraryByName($extension$name);
        if (!empty($definition['dependencies'])) {
          $final_libraries = $this->doGetDependencies($definition['dependencies']$final_libraries);
        }
        $final_libraries[$library] = $library;
      }
    }
    return $final_libraries;
  }

  /** * {@inheritdoc} */


  /** * Adds a JavaScript library to the page and alters it. * * @see common_test_library_info_alter() */
  public function testLibraryAlter() {
    // Verify that common_test altered the title of loadjs.     /** @var \Drupal\Core\Asset\LibraryDiscoveryInterface $library_discovery */
    $library_discovery = \Drupal::service('library.discovery');
    $library = $library_discovery->getLibraryByName('core', 'loadjs');
    $this->assertEquals('0.0', $library['version'], 'Registered libraries were altered.');

    // common_test_library_info_alter() also added a dependency on jQuery Form.     $build['#attached']['library'][] = 'core/loadjs';
    $assets = AttachedAssets::createFromRenderArray($build);
    $js = $this->assetResolver->getJsAssets($assets, FALSE, \Drupal::languageManager()->getCurrentLanguage())[1];
    $js_render_array = \Drupal::service('asset.js.collection_renderer')->render($js);
    $rendered_js = $this->renderer->renderPlain($js_render_array);
    $this->assertStringContainsString('core/assets/vendor/jquery-form/jquery.form.min.js', (string) $rendered_js, 'Altered library dependencies are added to the page.');
  }

  

      foreach ($definition['drupal']['conditions'] as $condition_type => $value) {
        $assessment = $supported_condition_types[$condition_type]($value$definition);
        if (is_string($assessment)) {
          throw new InvalidPluginDefinitionException($idsprintf('The "%s" CKEditor 5 plugin definition has an invalid "drupal.conditions" item. "%s" is set to an invalid value. %s', $id$condition_type$assessment));
        }
      }
    }

    if ($definition['drupal']['admin_library'] !== FALSE) {
      [$extension$library] = explode('/', $definition['drupal']['admin_library'], 2);
      if (\Drupal::service('library.discovery')->getLibraryByName($extension$library) === FALSE) {
        throw new InvalidPluginDefinitionException($idsprintf('The "%s" CKEditor 5 plugin definition has a "drupal.admin_library" key whose asset library "%s" does not exist.', $id$definition['drupal']['admin_library']));
      }
    }
  }

  /** * Returns the typed configuration service. * * @return \Drupal\Core\Config\TypedConfigManagerInterface * The typed configuration service. */
  
Home | Imprint | This part of the site doesn't use cookies.