Updated: Comment #N

Problem/Motivation

#2153891: Add a Url value object added the new value object, but Entity is still passing around an array.

Proposed resolution

Switch the return signature.

Remaining tasks

N/A

User interface changes

N/A

API changes

Entity::urlInfo() now returns \Drupal\Core\Url

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tim.plunkett’s picture

FileSize
52.83 KB

Status: Needs review » Needs work

The last submitted patch, 1: entity-url-2215961-1.patch, failed testing.

The last submitted patch, 1: entity-url-2215961-1.patch, failed testing.

tim.plunkett’s picture

Status: Needs work » Needs review

1: entity-url-2215961-1.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, 1: entity-url-2215961-1.patch, failed testing.

Berdir’s picture

Status: Needs work » Needs review
FileSize
53.57 KB
754 bytes

This makes the login work for me...

Status: Needs review » Needs work

The last submitted patch, 6: entity-url-2215961-6.patch, failed testing.

tim.plunkett’s picture

Status: Needs work » Needs review
FileSize
63.81 KB
16.18 KB

Thanks @Berdir! I could install and login just fine without that, no idea why.
That led me to a bunch of fixes.

Status: Needs review » Needs work

The last submitted patch, 8: entity-uri-2215961-8.patch, failed testing.

tim.plunkett’s picture

Status: Needs work » Needs review
FileSize
757 bytes
64.55 KB

Whoops, missed one when picture was renamed.

dawehner’s picture

OH and the *** tag.

  1. +++ b/core/lib/Drupal/Core/Entity/Entity.php
    @@ -399,27 +399,4 @@ protected function onUpdateBundleEntity() {
     
    -  /**
    -   * Wraps the URL generator.
    -   *
    -   * @return \Drupal\Core\Routing\UrlGeneratorInterface
    -   *   The URL generator.
    -   */
    -  protected function urlGenerator() {
    -    if (!$this->urlGenerator) {
    -      $this->urlGenerator = \Drupal::urlGenerator();
    -    }
    -    return $this->urlGenerator;
    -  }
    -
    

    This is awesome!

  2. +++ b/core/lib/Drupal/Core/Entity/EntityFormController.php
    @@ -315,7 +315,9 @@ public function delete(array $form, array &$form_state) {
    +        $redirect_query = $form_state['redirect_route']->getOption('query') ?: array();
    

    Are we sue that 'redirect_route' is available all the time?

  3. +++ b/core/lib/Drupal/Core/Entity/EntityInterface.php
    @@ -118,10 +118,7 @@ public function label();
       public function urlInfo($rel = 'canonical');
    

    Are we sure $form_state['redirect_route'] always exists?

aspilicious’s picture

+    $uri = $this->urlInfo($rel);
+    $options += $uri->getOptions();
+    $uri->setOptions($options);

I'm not a huge fan of fetching/changing/putting. I prefer methods on the object.
For example:

+    $uri = $this->urlInfo($rel);
+    $uri->mergeOptions($options);

Or does that mess with the options because it gets added in the other direction?

tim.plunkett’s picture

There are cases where a mergeOptions would be nice, like in comment.module. But yes, in this case we're merging the opposite direction...

dawehner’s picture

Issue tags: +Needs change record

.

tim.plunkett’s picture

Well https://drupal.org/node/2221879 hasn't been finished yet, we can clean that up and publish it when this goes in.

tim.plunkett’s picture

FileSize
63.59 KB

Status: Needs review » Needs work

The last submitted patch, 16: urlinfo-2215961-16.patch, failed testing.

tim.plunkett’s picture

Status: Needs work » Needs review
FileSize
63.57 KB

Doh forgot to rebase

sun’s picture

I wanted to RTBC this... I know this is a bit nitpicky, but...

+++ b/core/lib/Drupal/Core/Entity/Entity.php
@@ -174,14 +168,18 @@ public function urlInfo($rel = 'canonical') {
+        throw new \UnexpectedValueException(String::format('No link template "@rel" found for the "@entity_type" entity type', array(
Exception thrown if a value does not match with a set of values. Typically this happens when a function calls another function and expects the return value to be of a certain type or value (not including arithmetic or buffer related errors).

In other words, UnexpectedValueException is supposed to be used within a chain of function calls when an argument (or return value) suddenly is not of an expected data type.

For example, DirectoryIterator::__construct():

throws a UnexpectedValueException if the path cannot be opened.

I first wanted to suggest InvalidArgumentException, but that's not completely right either — valid arguments depend on the particular domain object for which URL info is retrieved.

Based on that, DomainException apparently comes closest:

Exception thrown if a value does not adhere to a defined valid data domain.

The example in the user comment essentially is a simplified code logic example of what we're doing here?

tim.plunkett’s picture

FileSize
2.25 KB
65.08 KB

I don't think any of those are exactly what we want.
So why not write our own!

Also opened #2228505: Clean up Entity exception classes to clean up the other exception classes.

sun’s picture

Status: Needs review » Reviewed & tested by the community

RTBC on the assumption that testbot agrees. :)

tim.plunkett’s picture

There's a lot of changes here, but mostly just the same ones over and over.
Here are some highlights:

  1. +++ b/core/modules/comment/lib/Drupal/comment/Controller/CommentController.php
    @@ -91,9 +91,8 @@ public function commentApprove(CommentInterface $comment) {
    -    $permalink_uri['options']['absolute'] = TRUE;
    -    $url = $this->urlGenerator()->generateFromRoute($permalink_uri['route_name'], $permalink_uri['route_parameters'], $permalink_uri['options']);
    -    return new RedirectResponse($url);
    +    $permalink_uri->setAbsolute();
    +    return new RedirectResponse($permalink_uri->toString());
    
  2. +++ b/core/modules/comment/lib/Drupal/comment/Form/DeleteForm.php
    @@ -62,9 +62,7 @@ protected function actions(array $form, array &$form_state) {
    -    $uri = $this->commentManager->getParentEntityUri($this->entity);
    -    $actions['cancel']['#route_name'] = $uri['route_name'];
    -    $actions['cancel']['#route_parameters'] = $uri['route_parameters'];
    +    $actions['cancel'] += $this->commentManager->getParentEntityUri($this->entity)->toRenderArray();
    
  3. +++ b/core/modules/image/lib/Drupal/image/Form/ImageEffectFormBase.php
    @@ -86,7 +86,6 @@ public function buildForm(array $form, array &$form_state, ImageStyleInterface $
    -    $image_style_uri = $this->imageStyle->urlInfo('edit-form');
    
    @@ -95,9 +94,7 @@ public function buildForm(array $form, array &$form_state, ImageStyleInterface $
         $form['actions']['cancel'] = array(
           '#type' => 'link',
           '#title' => $this->t('Cancel'),
    -      '#route_name' => $image_style_uri['route_name'],
    -      '#route_parameters' => $image_style_uri['route_parameters'],
    -    );
    +    ) + $this->imageStyle->urlInfo('edit-form')->toRenderArray();
         return $form;
    
webchick’s picture

Status: Reviewed & tested by the community » Fixed

Overall this looks fine to me, apart from:

+++ b/core/modules/comment/lib/Drupal/comment/CommentBreadcrumbBuilder.php
@@ -53,8 +53,7 @@ public function build(array $attributes) {
-    $breadcrumb[] = \Drupal::l($entity->label(), $uri['route_name'], $uri['route_parameters'], $uri['options']);
+    $breadcrumb[] = \Drupal::linkGenerator()->generateFromUrl($entity->label(), $entity->urlInfo());

:( Going from \Drupal::l() to \Drupal::linkGenerator()->generateFromUrl() is not nice DX. :(

But Tim pointed out that this code is now doing something different from what l() does internally, although we could open an issue to either switch l()'s behaviour wholesale from ->generate() to ->generateFromUrl() or introduce some new helper function (like Drupal::lurl()...lol :P)

I asked Tim about the change notice, since there's no draft here. He said that the draft notice in https://drupal.org/node/2221879 needs a bit of love, and also needs updating based on this change, so he plans to do both post-commit.

Ok, then. Committed and pushed to 8.x. Thanks!

  • Commit 4a8bbfb on 8.x by webchick:
    Issue #2215961 by tim.plunkett, Berdir: Entity::urlInfo() should return...
tim.plunkett’s picture

Updated https://drupal.org/node/2221879, needs review

tim.plunkett’s picture

Assigned: tim.plunkett » Unassigned
jessebeach’s picture

Issue tags: -Needs change record

Published the change notice. Removed 'Needs change record' tag.

Status: Fixed » Closed (fixed)

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