Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By tce on
I've created some nodes programmatically and set the path alias. But when I then go into edit the node the 'Generate automatic URL alias' option is still checked, even though my alias is shown in the 'URL alias' textfield underneath, so consequently gets overwritten on save.
Also, when editing the node using the Entity module and entity_metadata_wrapper(), the alias is generated again and my alias is overwritten because of this problem.
When I load a node using node_load() I expect to see $node->path['pathauto'] but I don't, so I can't seem to disable it this way. Anyone got any advice?
Comments
how to turn off auto aliasing programmatically
$node->path['pathauto'] = FALSE;
not working for me
Nate,
I'm trying the above and it's not turning off the automatic aliases. My situation is pretty much that I'm using pathauto for future pages, had a bunch of pages that needed migrated from a legacy site, along with the url slugs they used, and now pathauto is interfering with those old urls by replacing them.
I created a new file in my theme (will eventually delete), wherein Drupal is bootstrapped and then I'm loading all nodes of type secondary or promotion page, via this code:
I've verified that it loads the nodes by creating an array and pushing each loaded node onto the array, then var_dump'ing it. It shows all the nodes that need the automatic urls turned off, so I know it's getting the nodes, but when it saves, if I refresh, the admin page still tells me that it's using automatic URLs.
Any more suggestions?
existing aliases have to be removed
You need to delete the existing aliases from the url_alias table as well as turning off pathauto for the node.
I don't know how this will interact with your entity metadata, but I found that setting pathauto to 0 didn't work until I deleted the old aliases too.
Can be done without saving?!
Is there any way of programmatically unchecking 'Generate automatic URL alias' option for all nodes WITHOUT saving?
Because saving could invoke other mechanism, and updates the modified date etc.
The reason we are looking into this is to stop the relationship between the Title and the path after a day...
This is what I ended up
This is what I ended up implementing that doesn't change the updated time. It essentially is only run when any page is updated.
In $node (Object) there isn't
In $node (Object) there isn't a $node->path property.
The only thing I may advice to you is this:
the 'Generate automatic URL alias' option is checked if the node alias value is equal to the result of pathauto pattern only, i.e.
[node-title] -> this is the 'pattern' for node-type=page
My test page -> this is the title of my page
if and only if the alias value is 'my-test-page', then the 'Generate automatic URL alias' is checked, otherwise it doesn't.
You can go to check pathauto
You can go to check pathauto.module pathauto_field_attach_form()
This shows that this option only detect whether the entity alias stored in database is the same as pathauto generates.
If not, Then it will be FALSE. Or it will show TRUE.
For you I think you just don't want using pathauto function.
Tip1:Turn pathauto module off will help you.
Tip2:Go to /admin/config/search/path/patterns, Delete the alias pattern for the specify entity type.
You can choose one of the tips, that should work.
Thx
hook_form_alter approach
The code by bfodeke will definitely work and may be preferable depending on how strict you need to be with disabling alias generation.
We wanted to share an alternate approach that's a little less restrictive. For us, the two issues with the node_presave approach were...
1) "Generate automatic URL alias" checkbox stays checked until second save.
We wanted the checkbox to be off after the first save since a path already exists.
2) Automatic URL alias functionality is lost after first save.
If "Generate automatic URL alias" is manually re-checked, this would not do anything. We still wanted automatic alias generation to work if someone explicitly checked the box. (An example: In the event of a total rewrite of the entity title, auto generation could be nicer than manually typing the new path.)
---
With the above issues and goals in mind, we wrote a custom after_build function to check for a path pid. If one exists “Generate automatic URL alias" will be unchecked.
End result: "Generate automatic URL alias" checkbox will always be UNCHECKED if a path exists for that node/entity OR if the user has explicitly re-checked it.
The code...
We can configure the same using Url alias path settings.
This way you can uncheck 'Generate automatic URL alias' option for all nodes instead of any form alters (using Path auto module - version 7.x-1.2).
Change pathauto generation option for all nodes in Drupal 8
Anyone looking to do this with Drupal 8 the following db query seem to do the job
and for particular content type in D8
hope this helps somebody
It did! :-)
It did! :-)
Thanks for sharing!
Update all entities in a bundle
Here's a `drush scr` script I use to update the state for entities. See https://gist.github.com/ahebrank/be91b2bba61496ea5b31d4d38e6687a3
Using code and Pathauto internals
I put this method together based on Pathauto internals.
You can select your entities using an Entity Query, passing the $entity and $state = PathautoState::SKIP to disable.