Problem/Motivation

On #2676182: In the documentation, change all instances of "url" to "URL"., we are fixing the word "url" in documentation to its correct spelling, "URL".

This issue is something slightly different: the problem is that there are quite a few spots in the documentation that are mentioning the url() function [note the parens after url!], which does not exist any more in Drupal 8.

Note that there is still a Twig url() function, and a CSS url() function, and a JavaScript url() function, and several classes that have url() methods. However, there is no more bare PHP url() function in Drupal 8, so places in the docs that mention this function need to be replaced with something else.

Here's a grep command that checks for these, eliminating at least some of the false positives:

grep -R "url()" * | grep -v "::url()" | grep -v "_url()" | grep -v ">url()"

Proposed resolution

The question is what to replace the url() mentions with. For that, you'd probably need to look at the code and see what it's calling instead of url(). For instance, in system.module you find this:

 * @param array $options
 *   Optional array of options to pass to url().
 * @return \Drupal\Core\Url
 *   The full URL to authorize.php, using HTTPS if available.
 *
 * @see system_authorized_init()
 */
function system_authorized_get_url(array $options = array()) {
  // core/authorize.php is an unrouted URL, so using the base: scheme is
  // the correct usage for this case.
  $url = Url::fromUri('base:core/authorize.php');
  $url_options = $url->getOptions();
  $url->setOptions($options + $url_options);
  return $url;
}

The second line of docs is what mentions the url() function in error.

So, looking at the code, you can see that the options are actually added to the Url object by calling $url->setOptions(), and therefore a good way to change the documentation line (the second line above) would be something like this:

Optional array of options to set on the \Drupal\Core\Url object.

Similar fixes should be found for the other approximately 10 or 20 places in the docs that need fixing. Just be careful not to fix references to CSS or Twig functions, or class methods named url().

Remaining tasks

Make a patch.

User interface changes

None.

API changes

None.

Data model changes

None.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jhodgdon created an issue. See original summary.

aditya_anurag’s picture

Assigned: Unassigned » aditya_anurag
aditya_anurag’s picture

Status: Active » Needs review
FileSize
54.11 KB
jhodgdon’s picture

Status: Needs review » Needs work

This patch seems to be fixing the word "url" to be "URL". That is the wrong patch for this issue. Please read the issue summary more carefullly.

jhodgdon’s picture

Actually deleting this file because it's just not related to this issue at all.

Next person to make a patch, please do not make an interdiff to the previous patch, as it is not relevant to this issue at all.

jhodgdon’s picture

Issue summary: View changes

Hopefully clarifying the issue summary.

AjitS’s picture

Status: Needs work » Needs review
FileSize
3.77 KB

Here is the result for the command

grep -R "url()" * | grep -v "::url()" | grep -v "_url()" | grep -v ">url()"

against branch 8.0.x

$ grep -R "url()" * | grep -v "::url()" | grep -v "_url()" | grep -v ">url()"
core/assets/vendor/backbone/backbone.js:  // to the model's `url()`. Some possible customizations could be:
core/includes/common.inc: * Prepares a 'destination' URL query parameter for use with url().
core/includes/form.inc: *   - url_options: options passed to url() when constructing redirect URLs for
core/lib/Drupal/Core/Asset/CssOptimizer.php:   * returns the stylesheet content with all url() paths corrected.
core/lib/Drupal/Core/Asset/CssOptimizer.php:    // the url() path.
core/lib/Drupal/Core/Asset/CssOptimizer.php:    // Alter all internal url() paths. Leave external paths alone. We don't need
core/lib/Drupal/Core/Asset/CssOptimizer.php:   *   url() references in CSS files, except for external or absolute ones.
core/modules/menu_link_content/src/Plugin/migrate/source/MenuLink.php:      'options' => t('A serialized array of options to be passed to the url() or l() function, such as a query string or HTML attributes.'),
core/modules/system/src/Tests/Theme/EngineTwigTest.php:    // Verify that url() has the ability to bubble cacheability metadata:
core/modules/system/system.module: *   Optional array of options to pass to url().
core/modules/system/system.module: *   Optional array of options to pass to url().
core/modules/user/templates/username.html.twig: * - link_options: Options to pass to the url() function's $options parameter if
core/tests/Drupal/Tests/Core/DrupalTest.php:   * Tests the url() method.
core/tests/Drupal/Tests/Core/Entity/EntityUrlTest.php:   * Tests the url() method.
core/themes/classy/templates/user/username.html.twig: * - link_options: Options to pass to the url() function's $options parameter if
core/themes/stable/templates/user/username.html.twig: * - link_options: Options to pass to the url() function's $options parameter if
vendor/mikey179/vfsStream/CHANGELOG.md:   * implemented issue #34: provide `url()` method on all `vfsStreamContent` instances
vendor/mikey179/vfsStream/src/main/php/org/bovigo/vfs/vfsStreamAbstractContent.php:    public function url()
vendor/mikey179/vfsStream/src/main/php/org/bovigo/vfs/vfsStreamContent.php:    public function url();

Attaching initial version of the patch, making changes about which I was confident. I made changes in the files:

core/includes/common.inc
core/includes/form.inc
core/modules/menu_link_content/src/Plugin/migrate/source/MenuLink.php
core/modules/system/system.module

I did not change any functions in the JS files or twig templates as mentioned in the issue summary.

I was particularly not sure about core/lib/Drupal/Core/Asset/CssOptimizer.php. The code

// the url() path.
    $directory = $directory == '.' ? '' : $directory .'/';

    // Alter all internal url() paths. Leave external paths alone. We don't need
    // to normalize absolute paths here because that will be done later.
    return preg_replace('/url\(\s*([\'"]?)(?![a-z]+:|\/+)([^\'")]+)([\'"]?)\s*\)/i', 'url(\1' . $directory . '\2\3)', $file);

seems to make use of the css() function. So, I thought it would be wrong to make changes in the comments unless done in the code first. Please review :)

jhodgdon’s picture

Status: Needs review » Needs work

I think that the stuff in the CSS classes is referring to the ability to use url(...) in CSS to indicate a background image URL and things like that, so I do not think it needs to be updated.

Anyway, this patch looks pretty good, thanks! A few things to fix:

  1. +++ b/core/includes/common.inc
    @@ -133,7 +133,8 @@
     /**
    - * Prepares a 'destination' URL query parameter for use with url().
    + * Prepares a 'destination' URL query parameter for use with
    + * the \Drupal\Core\Url object.
    

    This is first-line documentation for a function.

    So it needs to be 1 line ending in . and followed by a blank line, not two lines.

    Also... I don't think that the docs are really right... I am not sure this is used "with the Url object".

    How about just taking out the whole "for use with url()" part entirely?

  2. +++ b/core/modules/menu_link_content/src/Plugin/migrate/source/MenuLink.php
    @@ -51,7 +51,7 @@ public function fields() {
    +      'options' => t('A serialized array of options to set on the \Drupal\Core\Url object or l() function, such as a query string or HTML attributes.'),
    

    Let's make this just say "... options to set on the URL, such as..."

    The thing is, the l() function doesn't exist either.

  3. I think we do need to fix the various username.html.twig templates. They are actually referring to the old PHP url() function, not the Twig url() function -- which you can see if you look at how link_options is used in template_preprocess_username() in user.module (they are being passed in as options in a call to Url::fromUri()).
  4. The rest looks good, as far as what to include and what not to include.
mikebell_’s picture

I've fixed the first two issues mentioned by @jhodgdon, I'm not sure where to start with the twig files though.

The \Drupal\Core\Url class doesn't contain any reference to the drupal_get_destination() function so I've removed that from the comment.

jhodgdon’s picture

Please when making a new patch for an existing issue, make an interdiff file so it is clear what you changed. I have not reviewed the new patch because this wasn't done...

Leaving this at needs work for (a) interdiff file and (b) Twig files. I don't know what to tell you about the Twig template files... they need similar fixes to the rest of the url() changes that were already in the patch. The files that need fixing are username.html.twig in various directories (see comment #7).

AjitS’s picture

Status: Needs work » Needs review
FileSize
1.94 KB
3.69 KB

Thank you @jhodgdon.
Updated patch as per #8.

Interdiff is against the patch from #7.

jhodgdon’s picture

Status: Needs review » Needs work

OK, but the username.html.twig files are still not in the patch. Please fix them too.

imalabya’s picture

Hi jennifer, have added a patch for the username.html.twig files.

imalabya’s picture

Status: Needs work » Needs review
jhodgdon’s picture

Status: Needs review » Needs work

Thanks! Getting pretty close...

+++ b/core/modules/user/templates/username.html.twig
@@ -9,8 +9,8 @@
+ * - link_options: Options to pass to the \Drupal\Core\Url object $options
+ *   property if linking the user's name to the user's page.

Hm. You don't really "pass" the options to the "options property".... Maybe we could say:

Options to set on the \Drupal\Core\Url object if linking the user's name to the user's page.

Thoughts?

subharanjan’s picture

Status: Needs work » Needs review
FileSize
842 bytes
6.23 KB

Made changes as per suggestions by @jhodgdon above. Thanks !

jhodgdon’s picture

Status: Needs review » Needs work

Almost!

  1. +++ b/core/themes/classy/templates/user/username.html.twig
    @@ -9,8 +9,8 @@
    + * - link_options: Options to pass to the \Drupal\Core\Url object $options
    + *   property if linking the user's name to the user's page.
    

    This template needs the same updates as the core/modules/user/templates one.

  2. +++ b/core/themes/stable/templates/user/username.html.twig
    @@ -9,8 +9,8 @@
    + * - link_options: Options to pass to the \Drupal\Core\Url object $options
    + *   property if linking the user's name to the user's page.
    

    This one too.

subharanjan’s picture

Status: Needs work » Needs review
FileSize
2.51 KB
6.19 KB

I am sorry about missing the other two instances of the same. Thanks @jhodgdon for pointing out.

imalabya’s picture

link_options: Options to set on the \Drupal\Core\Url object if linking
+ *   the user's name to the user's page.

I think the word "the" can be wrapped in the previous line for the username.html.twig files.

jhodgdon’s picture

Status: Needs review » Needs work

Yes, I think that is true about the wrapping. Otherwise, looks good!

imalabya’s picture

Updated the patch

imalabya’s picture

Status: Needs work » Needs review
jhodgdon’s picture

Version: 8.0.x-dev » 8.2.x-dev
Status: Needs review » Reviewed & tested by the community
Issue tags: +needs backport to 8.1.x, +needs backport to 8.0.x

Looks good, thanks!

So, let's see. This patch includes user interface text changes in one place. So I think it can be applied to 8.2.x and 8.1.x. Then we will need to "backport" it to 8.0.x -- taking out the user interface text changes in file core/modules/menu_link_content/src/Plugin/migrate/source/MenuLink.php

So. Setting this to 8.2.x, with backport tags. Please wait until this is committed to 8.2 and 8.1 before making the 8.0 patch.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 750f647 and pushed to 8.0.x, 8.1.x and 8.2.x. Thanks!

I tried to find more instances where url() was incorrect in the codebase and I could not - nice to see this cleaned up.

  • alexpott committed 5278698 on 8.2.x
    Issue #2679953 by malavya, subharanjan, AjitS, aditya_anurag, mikebell_...

  • alexpott committed 750f647 on 8.0.x
    Issue #2679953 by malavya, subharanjan, AjitS, aditya_anurag, mikebell_...

Status: Fixed » Closed (fixed)

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