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.
With pathauto enabled, how can we programmatically set “Generate automatic URL alias” to TRUE for existing nodes, using PHP or SQL?
I have tried all patches and PHP or SQL solutions suggested and nothing has worked for the latest D8 + pathauto releases.
After having studied various options, I believe this could be the fastest way to do it:
use Drupal\node\Entity\Node;
$query = \Drupal::entityQuery('node');
$query->condition('status', 1);
$query->condition('type', 'my_content_type');
$entity_ids = $query->execute();
foreach ($entity_ids AS $nid) {
$node = Node::load($nid);
$node->set("path", ["pathauto" => TRUE]); //this line is not doing anything
$node->save();
}
No errors but when I check my nodes after the execution of this code, they still have the "Generate automatic URL alias" disabled/invisible.
Comments
Comment #2
David Fiaty CreditAttribution: David Fiaty commentedComment #3
BerdirYou should use the constant, but something like that should work.
\Drupal\pathauto\Tests\PathautoNodeWebTest::testNodeState() has code that tests both saving to explicitly generate aliases and to not do that.
Comment #5
TOMY MOHAN CreditAttribution: TOMY MOHAN commented