Problem/Motivation

Library API can find files also into the /libraries folder so it could be used for manage external libraries defined in contrib modules (i.e. to hold javascript file with a different license than Drupal). These modules require a way to verify the existence of the required files (maybe in hook_requirements) so they can notify the user to download the library into the /libraries folder.

Proposed resolution

We could add a new method into LibraryDiscovery class to verify that required files exist.

Remaining tasks

  • The patch is ready for review
  • Tests to be written
  • Documentation to be written

User interface changes

None

API changes

None (just a new method added to the class LibraryDiscovery)

Issue fork drupal-2231385

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

lussoluca’s picture

StatusFileSize
new1.44 KB
dawehner’s picture

Status: Active » Needs review

I guess we also want to add this method to the interface?

lussoluca’s picture

StatusFileSize
new2.09 KB

Yes, I've only modified the class to see if this feature request made sense. I reattach the patch with both the class and the interface.

dawehner’s picture

Is there any usecase for this directly in core?

lussoluca’s picture

If a module want to use library API to manage external js libraries it needs a way to check if all required assets are present into /libraries folder. This check could be done into .install file but if different modules use this technique we'll have a lot of duplicated code. For me it make sense to have an API to check the existence of those files.

andypost’s picture

+1, useful method. Just needs tests

+++ b/core/lib/Drupal/Core/Asset/LibraryDiscovery.php
@@ -73,6 +73,43 @@ public function getLibraryByName($extension, $name) {
+  public function verifyLibrary($extension, $name) {

Maybe isLibraryValid() better?

nod_’s picture

Status: Needs review » Needs work

I'm not sure how much we need this but at least
+ $parts = explode('/', $dependency);
Needs to be

+ $parts = explode('/', $dependency, 2);

To follow what's in core.

lussoluca’s picture

StatusFileSize
new4.13 KB

New patch with corrections in #6 and #7 and a simple test (not sure if enough).

lussoluca’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 8: 2231385-8.patch, failed testing.

theodorosploumis’s picture

+++ b/core/lib/Drupal/Core/Asset/LibraryDiscovery.php
@@ -48,4 +48,41 @@ public function getLibraryByName($extension, $name) {
+          if (!file_exists(DRUPAL_ROOT . '/' . $js['data'])) {

What if the libraries are not under DRUPAL_ROOT/libraries folder but under DRUPAL_ROOT/profiles/PROFILE_NAME/libraries? The verification will fail. Probably we need to include all these libraries paths when doing the condition. Such paths are:

  • [DRUPAL_ROOT]/libraries
  • [DRUPAL_ROOT]/sites/all/libraries
  • [DRUPAL_ROOT]/profiles/[profile_name]/libraries
  • [module_name]/libraries
  • [DRUPAL_ROOT]/sites/[site_domain]/libraries
wim leers’s picture

Your description matches the Drupal 7 Libraries API. That's not what this is.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

gease’s picture

Why cannot we remove the condition https://git.drupalcode.org/project/drupal/-/blob/9.2.x/core/lib/Drupal/C...?
If LibrariesDirectoryFileFinder->find() returns FALSE, that means js file is missing, path in library definition is not changed, so it's still pointing to the missing file, and there's no hint that file is missing. If we put there FALSE instead, it would be obvious that the file is missing, and won't (as far as I've basically checked) break js on page more than missing file does.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
anybody’s picture

Version: 9.3.x-dev » 9.4.x-dev

Still this would be very very helpful as each module using a library now copies the logic to verify it is present.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

grimreaper’s picture

Hi,

Regarding patch from comment 8, when looking for dependencies:

    if (array_key_exists('dependencies', $library)) {
      foreach ($library['dependencies'] as $dependency) {
        $parts = explode('/', $dependency, 2);
        $dependencyLibrary = $this->getLibraryByName($parts[0], $parts[1]);
        $valid = $this->isLibraryValid($dependencyLibrary);
      }
    }

I think it should return FALSE as soon as a dependency is not satisfied.

grimreaper’s picture

And also before the dependencies, as soon as file is not found it should return FALSE.

grimreaper’s picture

MR created from patch 8 + my suggestion.

Test not copied because a test file with the same name exists now in Core.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.