Problem/Motivation

Right now, the array returned from toRenderArray() is just the URL parts of a render array.
It is *always* added to '#type' => 'link' and a #title in HEAD, no exceptions.

Proposed resolution

Despite the current restricted use, a Url is not a link.

Make the method name more accurate by calling it: renderArrayProperties()

Remaining tasks

User interface changes

API changes

method rename

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tim.plunkett’s picture

FileSize
12.61 KB
tim.plunkett’s picture

Status: Active » Needs review
pwolanin’s picture

Looks good, though I'd make the $title param optional $title = '' perhaps? If it's missing maybe put in the link or generate the URL?

tim.plunkett’s picture

Status: Needs work » Needs review
FileSize
672 bytes
13.26 KB

We don't have any usages for that, and I don't see why they couldn't just pass in '' themselves. Generating the URL is not a bad idea either, but honestly either of those could be added later without an API change.

amateescu’s picture

How about introducing a Link value object instead of muddying the waters in the Url class? That would be similar to the distinction we had previously with l() and url() (which made a lot of sense, imo).

pwolanin’s picture

@amateescu - this came out of my concern that the current function doesn't currently return a valid render array, and the link element is the only thing that makes sense in terms of the context where it's used.

tim.plunkett’s picture

#6, I'm not against that, but it'd be helpful to see a PoC, because as I envision it, it'd be rather ugly.

If we choose to not do either what I have in the patch or what #6 says, we can just s/toRenderArray/toRenderArrayFragment/ and be done with it.

amateescu’s picture

I think renaming the method to toRenderArrayFragment() would be the non-controversial change at this point. This method could be used (potentially) for the autocompete form element, something like:

$form['stuff'] = array(
  ...
  '#autocomplete' => TRUE,
) + $url->toRenderArrayFragment();

That's why I'd prefer to not limit its functionality to a link element.

tim.plunkett’s picture

Title: Make \Drupal\Core\Url::toRenderArray() take a title and return a valid render array » Rename \Drupal\Core\Url::toRenderArray() to toRenderArrayFragment()
Status: Needs review » Needs work
Issue tags: +Novice

Agreed that it's non-controversial and make sense. I like the look of the code in #5, but I agree its a weird coupling of links and URLs.

andriyun’s picture

Issue tags: +dcdn-sprint
denismosolov’s picture

Hello, folks!
I've just renamed the method. I hope I understood the task correct.

Thanks

andriyun’s picture

Issue tags: +dcdn-sprint
tvn’s picture

Status: Needs work » Needs review
pwolanin’s picture

How about just toRenderfragment()?

cilefen’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll
hottaco’s picture

Assigned: Unassigned » hottaco
hotpizzas’s picture

Assigned: hottaco » Unassigned
Status: Needs work » Needs review
FileSize
9.61 KB

Rerolled patch.

Encountered simple conflicts in the following files:
core/modules/user/src/Form/UserCancelForm.php
core/modules/node/src/Form/NodeDeleteForm.php
core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceLabelFormatter.php
core/lib/Drupal/Core/Form/ConfirmFormHelper.php

No longer exists in head:
core/modules/comment/lib/Drupal/comment/Form/DeleteForm.php

mrjmd’s picture

Issue tags: -Needs reroll
tim.plunkett’s picture

Thanks for rerolling!

  1. +++ b/core/lib/Drupal/Core/Entity/EntityForm.php
    @@ -234,7 +234,7 @@ protected function actions(array $form, FormStateInterface $form_state) {
    +      $actions['delete'] += $route_info->toRenderArrayFragment('');
    
    +++ b/core/lib/Drupal/Core/Url.php
    @@ -382,18 +382,25 @@ public function toArray() {
    +  public function toRenderArrayFragment($title) {
    

    That's not really in scope, is it?
    And if so, let's make it $title = '' in the method signature.

  2. +++ b/core/modules/node/src/Form/NodeDeleteForm.php
    @@ -58,7 +58,11 @@ public function getQuestion() {
    +<<<<<<< HEAD:core/modules/node/src/Form/NodeDeleteForm.php
       public function getCancelUrl() {
    +=======
    +  public function getCancelRoute() {
    +>>>>>>> apply coreurl:core/modules/node/lib/Drupal/node/Form/NodeDeleteForm.php
    

    Whoops!

  3. +++ b/core/tests/Drupal/Tests/Core/Form/ConfirmFormHelperTest.php
    @@ -30,7 +30,7 @@ public function testCancelLinkTitle() {
    -    $link = ConfirmFormHelper::buildCancelLink($form, new Request());
    +    $link = ConfirmFormHelper::buildCancelLink($form, new Request(array('destination' => 'foo')));
    

    What's the reason for this change?

cilefen’s picture

Status: Needs work » Needs review
FileSize
2.98 KB
9.06 KB

@tim.plunkett I addressed your comments. It looks like we really need title='' in the signature because there are some existing usages that don't include $title. Also I tried to return ConfirmFormHelper to the original intent in #5.

tim.plunkett’s picture

My question was why we're introducing #title in an issue that is only about renaming the toRenderArray method.

cilefen’s picture

Status: Needs work » Needs review
FileSize
457 bytes
9.04 KB
cilefen’s picture

Status: Needs work » Needs review
FileSize
557 bytes
9.05 KB
cilefen’s picture

Status: Needs work » Needs review
FileSize
1.03 KB
10.21 KB

Wow, core moves fast!

cilefen queued 29: rename-2235495-29.patch for re-testing.

tim.plunkett’s picture

Component: other » routing system
Status: Needs review » Reviewed & tested by the community
Parent issue: » #2339219: [meta] Finalize URL generation API (naming, docs, deprecation)

Okay, let's just get this in. It will be useful for the conversions we need to do.

webchick’s picture

Status: Reviewed & tested by the community » Needs review
+++ b/core/lib/Drupal/Core/Url.php
@@ -371,18 +371,25 @@ public function toArray() {
-  public function toRenderArray() {
+  public function toRenderArrayFragment($title = '') {
     if ($this->isExternal()) {
       return array(
+        '#type' => 'link',
+        '#title' => $title,
         '#href' => $this->getPath(),
         '#options' => $this->getOptions(),
       );

I'm showing it to eff and neither of us understand why, now that this *is* returning a full render array with a #type => link, it makes sense to rename the thing to RenderArrayFragment. Seems like this is now returning a complete render array, no?

In addition, the word "Fragment" is overloaded. Not only because of HTML Page fragments, but also because of URL fragments, and this is on the Url class. :)

If we get a re-roll of this without the rename, this should be safe for anytime post-beta. But setting to needs review first because I don't quite parse peter's concerns in #7.

webchick’s picture

If, on the other hand, we decide to not have the Url class making assumptions about #type being link, then effulgentsia suggests maybe toRenderArrayProperties().

pwolanin’s picture

I'm fine with it being #type => link, but in that case I agree that we shouldn't change the method name. The whole motivation was since it didn't include a #type

pwolanin’s picture

Status: Needs review » Needs work
pwolanin’s picture

Status: Needs work » Needs review
Issue tags: -Novice, -dcdn-sprint +Amsterdam2014
FileSize
8.45 KB
8.92 KB

change the method name back.

cilefen’s picture

Status: Needs review » Reviewed & tested by the community

This satisfies the requirements of #32 and #34 and it is green.

dawehner’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/lib/Drupal/Core/Url.php
    @@ -379,18 +379,25 @@ public function toArray() {
    +   * @param string $title
    +   *   The title for the render array.
    +   *
    ...
    -  public function toRenderArray() {
    +  public function toRenderArray($title = '') {
    

    I don't think this is a sane idea, if you want to add it, just use += later or earlier. We are basically treating easy to use vs. sanity. Why should a url care about titles. If you want that, move this code to the link class

  2. +++ b/core/lib/Drupal/Core/Url.php
    @@ -379,18 +379,25 @@ public function toArray() {
    +        '#type' => 'link',
    +        '#title' => $title,
    

    Same thing as before, a URL is not a link

amateescu’s picture

Re: #38: Yup. Also #6, #9 and #10 above.

pwolanin’s picture

Title: Rename \Drupal\Core\Url::toRenderArray() to toRenderArrayFragment() » Rename \Drupal\Core\Url::toRenderArray() to renderArrayProperties()
Issue summary: View changes

In the face of a lack of consensus - I sought a final decision from alexpott. This is the decision:

The method should be renamed to renderArrayProperties()

It will otherwise stay functionally the same and not provide a #type or #title.

pwolanin’s picture

Issue summary: View changes
cilefen’s picture

Issue summary: View changes
Status: Needs work » Needs review
FileSize
9.5 KB
8.25 KB

Let's see how well phpstorm handles this.

pwolanin’s picture

Status: Needs review » Reviewed & tested by the community

simple method name fix - looks good

tim.plunkett’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
8.24 KB

Rerolled.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 45: 2235495-toRenderArray-45.patch, failed testing.

tim.plunkett’s picture

Interestingly enough, #2347465: Convert all instances of #type link/links to convert to use routes removes all but 3 usages of toRenderArray (since '#url' => $this is used in its place)

cilefen’s picture

Status: Needs work » Needs review
FileSize
8.24 KB

Rerolled.

pwolanin’s picture

@tim.plunkett - should we remove the method after that goes in instead?

dawehner’s picture

I really kinda feel that this issue is pointless.

Fabianx queued 48: rename-2235495-48.patch for re-testing.

Status: Needs review » Needs work

The last submitted patch, 48: rename-2235495-48.patch, failed testing.

dawehner’s picture

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

Let's do that in 9, its not worth to do that at all anymore in D8.

kgoel’s picture

Status: Needs work » Postponed
catch’s picture

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

We could do it in a late minor release of 8.1.x with a deprecation, if we're really going to make the full change in 9.x.

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

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now 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.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now 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.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now 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.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now 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.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now 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.

dawehner’s picture

Status: Postponed » Needs work

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

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now 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.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.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.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). 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.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now 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.

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

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.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.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.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.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.