What is the reason for checking for path field when deleting redirects owned by an entity?

For example, I'm creating redirects for user entities. When I'm deleting the user, I expect that the corresponding redirects to be cleaned-out but they remain as orphan redirects because the user entity lacks the path field.

Comments

claudiu.cristea created an issue. See original summary.

claudiu.cristea’s picture

Status: Active » Needs review
StatusFileSize
new1.24 KB

Patch.

claudiu.cristea’s picture

Issue summary: View changes

IS

claudiu.cristea’s picture

StatusFileSize
new1.23 KB
new654 bytes

Better.

berdir’s picture

Status: Needs review » Needs work

I guess that makes sense, I think this was more or less directly ported from 7.x like this.

However, lets then explicitly use canonical for toUrl() as well, as for config entities, it can default to edit-form.

Also not convinced about moving the entity case out of the if. if there is no route to an entity, then it also won't have entity based redirects?

Also wondering if using predelete() would allow us to delete in bullk with only a single query for a batch of entities, but that's probably better done in a separate issue.

claudiu.cristea’s picture

@Berdir, reading the docs from EntityInterface::toUrl(), my understanding is that the URL follows this fallback cascade:

link template (if exists) > uri callback (if is callable) > standard canonical (entity/{entity_type_id}/{entity_id})

but reading the code in Entity::toUrl(), I don't see the latest fallback step. Or I'm missing something?

Also it seems that we need to catch the exception because ::toUrl() doesn't return NULL/FALSE on failure.

berdir’s picture

Yes, there is no fallback, it throws an exception if the link template is not defined I think.

Catching exception shouldn't be necessary because we do check if the link template exists before.

claudiu.cristea’s picture

Catching exception shouldn't be necessary because we do check if the link template exists before.

@Berdir, then we exclude entities without link template but with uri_callback.

berdir’s picture

Your patch does that already and I'm actually pretty fine with that.

Comment has a uri callback but it also has a canonical route, what forum is trying to do is completely broken and Item for example is an external url which shouldn't use the toUrl() system like that anyway IMHO, it would actually break the code here as we can't delete external redirects like that.

berdir’s picture

Your patch does that already and I'm actually pretty fine with that.

Comment has a uri callback but it also has a canonical route, what forum is trying to do is completely broken and Item for example is an external url which shouldn't use the toUrl() system like that anyway IMHO, it would actually break the code here as we can't delete external redirects like that.

entity:node/1 is just an alternative way to route to the canonical route for node id 1, if there is no canonical link template then entity:$type/$id also can't work. See \Drupal\Core\Url::fromEntityUri

claudiu.cristea’s picture

Status: Needs work » Needs review
StatusFileSize
new1.22 KB
new713 bytes

OK.

Just a note: I think that redirects created by entities should be treated somehow as first-class-citizens and we need to implement a way to know that a certain redirect has a specific entity as dependency. I have this case: I'm migrating a D6 site to D8 and I want to have redirects in the new site for old URLs, including files/images. For this reason I have also this kind of redirects:

sites/default/files/d6/path/to/unmanaged/file.pdf > base:/sites/default/d8/path/to/managed/file.pdf

I'm expecting that this redirect will be automatically cleared when I'm deleting the managed file entity in D8 site. Now, I have to implement custom code to get these redirects removed on file entity removal. If we could mark a redirect being dependent on a specific entity then everything can be simplified and would allow to handle such cases, without custom code. We can, for instance, store also the entity type ID and entity ID in the {redirect} table (and leave blank for non-entity redirects). Or even store them in a new {redirect_entity} table. A new table would not impact performance because it would be used only on creation or deletion of redirects.

berdir’s picture

Status: Needs review » Fixed

Thanks. Yeah, file entities are special/weird, we could add special support for them, but it will always need to be a special I think because the file URL isn't the canonical entity URL.

Status: Fixed » Closed (fixed)

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