Child of #2323895: [Meta] Document format/content of various YML files (we have been discussing how to document *.yml files).

There is currently no information on how to define a library in a *.libraries.yml file, on api.drupal.org.

There should be. It should be linked from
https://api.drupal.org/api/drupal/core!modules!system!theme.api.php/grou...
or added to that page.

We do have some documentation on drupal.org : https://www.drupal.org/node/2274843

So the topic on api.drupal.org should give a minimal example and link to this page.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

alexpott’s picture

wadmiraal’s picture

Assigned: Unassigned » wadmiraal

I'll have a look.

wadmiraal’s picture

Documentation for module, theme and profile *.info.yml files is provided directly in core/lib/Drupal/Core/Extension/InfoParserInterface.php::parse(). I suggest we add the *.libraries.yml documentation in core/lib/Core/Asset/LibraryDiscoveryParser::parseLibraryInfo().

wadmiraal’s picture

Status: Active » Needs review
FileSize
3.79 KB

Here is a documentation docblock for the LibraryDiscoveryParser::parseLibraryInfo() method, as well as an added link for theme.api.php to this information.

We should note, however, that the documentation is still incomplete. The documentation at https://www.drupal.org/node/2274843#define-library is not complete at all. A lot of information is missing. In fact, the class method documentation is now more complete than the aforementioned node. We should clarify how the libraries are parsed (especially for the 'data' and 'header' keys). I added a @todo to the method docblock.

jhodgdon’s picture

Status: Needs review » Needs work

Great start, and thanks for reviving this issue!

I have a few suggestions:

  1. +++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php
    @@ -208,6 +208,75 @@ public function buildByExtension($extension) {
    +   * Info files are formatted as YAML. If the 'version' key is set to 'VERSION'
    +   * in any info file, then the value will be substituted with the current
    +   * version of Drupal core. Each library is listed as an entry detailing the
    +   * CSS and JS assets.
    +   *
    

    I would put this information about the version key in the docs below that tell about 'js', 'css', and etc.

  2. +++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php
    @@ -208,6 +208,75 @@ public function buildByExtension($extension) {
    +   * Info files are formatted as YAML. If the 'version' key is set to 'VERSION'
    +   * in any info file, then the value will be substituted with the current
    +   * version of Drupal core. Each library is listed as an entry detailing the
    +   * CSS and JS assets.
    +   *
    

    Actually, maybe this whole paragraph can be omitted? We could just say something like:

    Library information is parsed from *.libraries.yml files; see foo.library.yml for an example. Each entry starts with a machine name, and the information below has the following elements:

    and then list the js, css, etc. as well as the note about VERSION.

  3. +++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php
    @@ -208,6 +208,75 @@ public function buildByExtension($extension) {
    +   * @code
    +   * drupal.editor:
    +   *   version: VERSION
    +   *   js:
    

    I don't think we really need to put in a full example of a libraries.yml file in here. I would instead suggest saying something like "See foo.libraries.yml for an example" (replace 'foo' with the name of an actual library file that exists in Core).

  4. +++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php
    @@ -208,6 +208,75 @@ public function buildByExtension($extension) {
    +   * Information stored in all .libraries.yml entries:
    

    This is the line I'd suggest combining with that previous paragraph.

  5. +++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php
    @@ -208,6 +208,75 @@ public function buildByExtension($extension) {
    +   *   forward slash, in which case it is relative to the Drupal root. If the
    +   *   file path starts with 2 forward slashes, it will be treated as an
    

    Maybe instead of saying "forward slash" just put in '/' and '//'? More concise?

  6. +++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php
    @@ -208,6 +208,75 @@ public function buildByExtension($extension) {
    +   * - 'js': A list of Javascript files to include. Each file is the key of an
    

    Actually, we don't normally want to put '' on list item keys. See https://www.drupal.org/node/1354#lists

  7. +++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php
    @@ -208,6 +208,75 @@ public function buildByExtension($extension) {
    +   *   object definition, which allows modules to set attributes for the
    +   *   particular file, like weight or HTML attributes. The path of the file is
    +   *   relative to the module or theme directory, unless it starts with a
    +   *   forward slash, in which case it is relative to the Drupal root. If the
    +   *   file path starts with 2 forward slashes, it will be treated as an
    +   *   external resource. Required if no 'css' key is present.
    

    This probably needs more details. I cannot figure out what it means.

  8. +++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php
    @@ -208,6 +208,75 @@ public function buildByExtension($extension) {
    +   *   external resource. Required if no 'css' key is present.
    

    Maybe this would be clearer if it said "Every library needs to have at least one js or css entry"?

  9. +++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php
    @@ -208,6 +208,75 @@ public function buildByExtension($extension) {
    +   *   Just like with Javascript files, each CSS file is the key of an object
    +   *   which can define specific attributes. The available categories are:
    +   *   - 'base'
    

    Hm, again not really sure what this means? What are "categories"?

  10. +++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php
    @@ -208,6 +208,75 @@ public function buildByExtension($extension) {
    +   * See editor.libraries.yml for an example of a module .libraries.yml file.
    

    Ah, I see you have an example listed; just move this up into the main paragraph. :)

  11. +++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php
    @@ -208,6 +208,75 @@ public function buildByExtension($extension) {
    +   * See @link https://www.drupal.org/node/2274843#define-library Defining a library @endlink
    

    We can leave out the @link here. Just say "See https://whatever for more information".

wadmiraal’s picture

Status: Needs work » Needs review
FileSize
3.08 KB
4.93 KB

Yeah, I wasn't really sure all that was very clear. How's this? I still feel there should be some example in there. It's pretty hard to understand just based on words.

jhodgdon’s picture

Status: Needs review » Needs work

Great! I agree that the short examples you put in this time are good and necessary, and I think it's much better this way than including a whole example with documentation that is separate.

A few things to address:

  1. +++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php
    @@ -208,6 +208,57 @@ public function buildByExtension($extension) {
    +   * - js: A list of Javascript files to include. Each file is keyed by the file
    

    Nitpick: should be JavaScript not Javascript throughout this patch

  2. +++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php
    @@ -208,6 +208,57 @@ public function buildByExtension($extension) {
    +   *   which can define specific attributes. The format of the file path is the
    

    Nitpick: which -> that

  3. +++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php
    @@ -208,6 +208,57 @@ public function buildByExtension($extension) {
    +   * - version: If the version key is set to "VERSION", then the value will
    +   *   be substituted with the current version of Drupal core.
    

    We should probably start by saying "The library version." before launching into saying about the "VERSION" special value. :) Also I think this would be clearer if it said:

    version: The library version. The string "VERSION" can be used to mean the current Drupal core version.

  4. +++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php
    @@ -208,6 +208,57 @@ public function buildByExtension($extension) {
    +   * @todo Document the following keys and their usage:
    

    We need to take care of this before the patch is finalized. Alternatively, if we think these are not used much, we could just put in another - list item saying something like:

    - Additional information can also be specified, such as the weight. See ... for more information.

nod_’s picture

About the additional stuff there are a few things that shouldn't be used/documented:

  • weight: deprecated, will be removed once we rely on a proper graph to order files (order based on declared dependencies).
  • type: deprecated, used to be there for adding inline JS (which is not supported anymore), and adding settings to Drupal.settings (for that we have a drupalSettings key in the libraries, at the same level as js and CSS, see core/drupalSettings definition).
  • data: same as type. shouldn't be used directly. Still in the API because we didn't want/need to refactor the whole thing. Should be removed at the same time as weight.

Stuff we need to document (see core.libraries.yml for examples):

  • header: we output scripts at the bottom by default now, but using this boolean you can force all scripts in the library (and this libraries' dependencies) to be added to the header.
  • minified: flag the file as minified to avoid minifiying it again (once we get JS minification in core).
  • remote: for third party scripts, the repo URL.
  • license: an important one. Declare the licensing of the repo to be able to minify and document it properly #2258313: Add license information to aggregated assets. It's an object with 3 keys:
    • name: Human readable license name
    • url: the URL of license file/information for the version of the library used (so not pointing to master but a specific tag/branch).
    • gpl-compatble: boolean used in #2258313: Add license information to aggregated assets and for people packaging drupal (like in debian or something).
wadmiraal’s picture

Thanks for the feedback and the additional info. Will update.

wadmiraal’s picture

Updated, taking into account both your comments.

wadmiraal’s picture

Status: Needs work » Needs review
wadmiraal’s picture

Assigned: wadmiraal » Unassigned
jhodgdon’s picture

Status: Needs review » Needs work

Looking good! I'm sitting next to you, so these comments are brief, so others can see the review:

  1. +++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php
    @@ -208,6 +208,61 @@ public function buildByExtension($extension) {
    +   *   The path of the file is relative to the module or theme directory, unless
    +   *   it starts with a /, in which case it is relative to the Drupal root. If
    +   *   the file path starts with //, it will be treated as an external resource.
    +   * - css: A list of categories for which the library provides CSS files. The
    +   *   available categories are:
    

    Need to talk about stream wrappers and http:// stuff here?

  2. +++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php
    @@ -208,6 +208,61 @@ public function buildByExtension($extension) {
    +   *   set this to true. Defaults to false.
    

    Should use TRUE and FALSE in docs, not true/false.

  3. +++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php
    @@ -208,6 +208,61 @@ public function buildByExtension($extension) {
    +   * - remote: If the library is a third-party script, this provides the
    +   *   repository URL.
    +   * - license: If the remote property is set, the license information is
    

    Maybe say something like "for reference"? I think this is just for documentation and/or human readers so they can visit the project page, right?

  4. +++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php
    @@ -208,6 +208,61 @@ public function buildByExtension($extension) {
    +   *   - gpl-compatible: A boolean for whether this library is GPL compatible.
    

    boolean => Boolean

wadmiraal’s picture

Assigned: Unassigned » wadmiraal
wadmiraal’s picture

Assigned: wadmiraal » Unassigned
Status: Needs work » Needs review
FileSize
3.84 KB
1.84 KB

I did not capitalize the boolean TRUE and FALSE values, as in this context we're talking about a YAML format (which should use lowercase true and false).

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

Thanks! I think this is ready to go now.

wadmiraal’s picture

Just setting the organization for the credits.

nod_’s picture

Great, thanks a lot!

Should we update the doc page as well or just refer to the api for more details?

jhodgdon’s picture

Is the page on drupal.org wrong? The point of this issue was to make sure we have docs on api.drupal.org about libraries. I don't think there was a problem with the d.o docs.

nod_’s picture

Fair enough, it's not wrong, just not very detailed.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Yay, Moar Docs! :)

Committed and pushed to 8.0.x. Thanks!

  • webchick committed bd36946 on 8.0.x
    Issue #2390241 by wadmiraal, jhodgdon, nod_: No documentation on how to...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.