All aliases are deleted if entity with <nolink> url is deleted

Created on 15 June 2023, over 1 year ago
Updated 20 July 2023, over 1 year ago

Problem/Motivation

I'm using a bundle class to override the toUrl() method on a particular node type. If some condition is met (the specifics are not important) I don't want the node to be visible on its own page and I don't want teasers and cards to render any links to the node

  public function toUrl($rel = 'canonical', array $options = []) {
    if ($rel === 'canonical') {
      if (//some condition) {
        return Url::fromRoute('<nolink>');
      }
    }
    return parent::toUrl($rel, $options);
  }

Let's assume I have created a node that meets the condition to have <nolink> as its canonical url. If I delete that node, every single url alias gets deleted.

That's because AliasStorageHelperInterface::deleteBySourcePrefix() gets called with / as the prefiz, which of course matches all aliases.

Proposed resolution

Add some check somewhere to prevent accidentally deleting all url aliases if a canonical url is <nolink>. Not sure if that would be best in AliasStorageHelper or pathauto.module.

One way to prevent this mass deletion from happening is to override the hasLinkTemplate() method in the bundle class such that it returns false if the node has a canonical url.

  /**
   * {@inheritdoc}
   */
  public function hasLinkTemplate($rel) {
    if ($rel === 'canonical') {
      if (//some condition) {
        return FALSE;
      }
    }
    return parent::hasLinkTemplate($rel);
  }

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States danflanagan8 St. Louis, US

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024