Problem/Motivation

Part of #2856744: [META] Add trigger_error(..., E_USER_DEPRECATED) to deprecated code

Explicitly mark methods like ::url(), ::link() and so on as deprecated, fix remaining usages.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

Berdir created an issue. See original summary.

berdir’s picture

Title: Add @trigger_error() to deprecated EntityInterface methods » Add @trigger_error() to deprecated url/link EntityInterface methods
Status: Active » Needs review
StatusFileSize
new236.15 KB

Turns out we have a few more calls left than I expected, but I guess this was a good relaxation exercise or something ;)

Status: Needs review » Needs work

The last submitted patch, 2: entity-url-methods-3019834-2.patch, failed testing. View results

berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new236.15 KB

Fixed syntax error in user.api.php.

Status: Needs review » Needs work

The last submitted patch, 4: entity-url-methods-3019834-3.patch, failed testing. View results

berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new237.22 KB
new6.52 KB

Lets see what's left now. Interestingly, toLink() on ConfigEntityBase() does *not* default to edit-form, unlike link(), url() and toUrl().

Status: Needs review » Needs work

The last submitted patch, 6: entity-url-methods-3019834-6.patch, failed testing. View results

berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new238.12 KB
new926 bytes

I think @Wim and I discussed about that call in the issue when we refactored getEntityUri(), I would have liked to remove it already then as it means we *are* calling a deprecated method.

Question is if this is breaking BC, but then only if someone is relying on a method that was deprecated already before 8.0 so they already had inconsistent behavior. And just like File does, they can override the normalizer then.

The only alternative would be to flag all these tests as @legacy I think but they're not.

alexpott’s picture

+++ b/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php
@@ -186,10 +186,9 @@ public function denormalize($data, $class, $format = NULL, array $context = [])
-      return $entity->url('canonical', []);
+      return '';

Looking at the code in \Drupal\Core\Entity\Entity::toUrl() shouldn't we be calling that here to give things that implement uri_callback a chance?

berdir’s picture

Maybe, uri_callback is also pretty much dead and broken and deprecated :)

Definitely not for new/not having an ID, that's an immediate exception. and if there is no uri_callback, it will also throw an exception, I don't like having to wrap it in try/catch.

alexpott’s picture

Ah I see the following code is in the url method already...

    // While self::toUrl() will throw an exception if the entity has no id,
    // the expected result for a URL is always a string.
    if ($this->id() === NULL || !$this->hasLinkTemplate($rel)) {
      return '';
    }

So yeah the whole not having a link template situation is meh and the change in #8 seems good to me.

wim leers’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +@deprecated

👏

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/comment/src/CommentTypeForm.php
@@ -158,7 +158,7 @@ public function save(array $form, FormStateInterface $form_state) {
-    $edit_link = $this->entity->toLink($this->t('Edit'))->toString();
+    $edit_link = $this->entity->toLink($this->t('Edit'), 'edit-form')->toString();

I think this change will break contrib. We need to think a bit harder about config entities ::toLink() method.

alexpott’s picture

  public function testUrlInfo($rel, $options) {
    $entity = $this->getEntity(Entity::class, [], ['toUrl']);
    $entity->expects($this->once())
      ->method('toUrl')
      ->with($rel, $options);

    $entity->toUrl($rel, $options);
  }

test needs renaming. And so does providerTestUrlInfo - or maybe this should be the legacy urlInfo test as with the current change it does not appear to be testing anything and we need a legacy test to for urlInfo().

We also don't appear to have any test coverage of the ::link() deprecation.

The existing testUrl and testUrlEmpty are good coverage for the url method.

berdir’s picture

> I think this change will break contrib. We need to think a bit harder about config entities ::toLink() method.

Yes, it's unfortunate that this is inconsistent, but it has been like this since 8.0, anyone who used the non-deprecated method already had this problem.

I was never a fan of the hack that ConfigEntityBase is doing and I know that for example webform is actively undoing that again.

Changing the default *now* would be a bigger problem IMHO. The thing would I guess be a note on the change record about this behavior change.

Nothing happens automatically to their existing deprecated code, *only* when they convert to toLink().

alexpott’s picture

Ah phew... I thought somehow the behaviour of toLink() was changing. So things seems okay - yes inconsistent but that is already the case and not caused by this patch. It does mean we have to review all the link() -> toLink() conversions very carefully - especially any that are generic.

berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new239.57 KB
new1.97 KB

Fixed legacy test coverage of urlInfo() and added something similar for link.

kim.pepper’s picture

Status: Needs review » Reviewed & tested by the community

Thanks @Berdir. Looks like all feedback has been addressed.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

I think it worth mentioning that the defaults for configuration entities have changed on the deprecation message for ::link(). This seems something that couyld catch people out and documenting this is helpful.

Also the ::url() deprecation notice could be helpful and mention the return type change from string to a Url object and to use toString() if that is needed. The other thing is not handling new entities.

I think these notices need to help people avoid errors in their code.

berdir’s picture

Status: Needs work » Needs review

@alexpott I'm not quite sure how to get that information into the fairly limited space we have for deprecation messages.

I expanded https://www.drupal.org/node/2614344 with notes on the changes that callers need to be aware of, is that enough?

I feel like this should not be a huge issue for contrib/custom as these deprecations happened years ago before 8.0 and I would assume most people used the new ones usually. That said, http://grep.xnddx.ru/search finds plenty of cases where they still appear. e.g. 20 pages of urlInfo( and 16 of ->url( vs 60+ of ->toUrl(,

Setting to needs review to get feedback on that.

alexpott’s picture

I suggest:

@trigger_error("EntityInterface::link() is deprecated in Drupal 8.0.0 and will be removed in Drupal 9.0.0. EntityInterface::toLink() instead. Note, the default relationship for configuration entities changes from 'edit-form' to 'canonical'. See https://www.drupal.org/node/2614344", E_USER_DEPRECATED);
@trigger_error('EntityInterface::url() is deprecated in Drupal 8.0.0 and will be removed in Drupal 9.0.0. EntityInterface::toUrl() instead. Note, a \Drupal\Core\Url object is returned. See https://www.drupal.org/node/2614344', E_USER_DEPRECATED);

Yes the lines are long but the detail is really helpful.

berdir’s picture

Thanks, updated the patch.

alexpott’s picture

Status: Needs review » Reviewed & tested by the community

Thanks @Berdir - let's get this done. This has been rtbc in #18 and was only sent back for improvements to the deprecation messages.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 60505f3 and pushed to 8.7.x. Thanks!

  • alexpott committed 60505f3 on 8.7.x
    Issue #3019834 by Berdir, alexpott: Add @trigger_error() to deprecated...

Status: Fixed » Closed (fixed)

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