@lubwn you could use an update hook instead that let's you do bulk updates without timing out if you have many entities and you can also leave the code because it will only execute once on each environment.
function mymodule_update_9001(&$sandbox) { // Initialize variables on the first run. if (!isset($sandbox['total'])) { $entity_list = \Drupal::entityQuery('node') ->condition('type', 'my_entity') ->execute(); $langcodes = \Drupal::languageManager()->getLanguages(); $sandbox['langcodes'] = array_keys($langcodes); $sandbox['total'] = count($entity_list); $sandbox['current'] = 0; } // Batch cycle. $batch_nids = \Drupal::entityQuery('node') ->condition('type', 'my_entity') ->range($sandbox['current'], 15) ->execute(); foreach ($batch_nids as $nid) { $my_entity = \Drupal::entityTypeManager()->getStorage('node')->load($nid); // Loop through all languages. foreach ($sandbox['langcodes'] as $langcode) { if ($my_entity->hasTranslation($langcode)) { $my_entity_translation = $my_entity->getTranslation($langcode); // Avoid re-saving nodes that don't need to be updated. if ($my_entity_translation->path->pathauto !== 1) { $my_entity_translation->path->pathauto = 1; $my_entity_translation->save(); } } } $sandbox['current']++; } if ($sandbox['current'] >= $sandbox['total']) { $sandbox['#finished'] = 1; \Drupal::messenger()->addMessage('A total of ' . $sandbox['total'] . ' entities were updated.'); } else { $sandbox['#finished'] = ($sandbox['current'] / $sandbox['total']); // Output a message to inform about the ongoing process. \Drupal::messenger()->addMessage($sandbox['current'] . ' entities were updated out of ' . $sandbox['total']); } }
- 🇳🇴Norway vegardjo
Using Drupal 10.4, and running the "Update URL Alias" action does not enable the "Generate automatic URL alias" setting on the nodes, so I also don't see that this is a closed task..?
I did this for a one off, test locally and use at own risk!
drush php-eval "use Drupal\node\Entity\Node; \$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadByProperties(['type' => 'my_node_bundle']); foreach (\$nodes as \$node) { if (\$node->hasField('path')) { \$node->path->pathauto = 1; \$node->save(); } }"
- 🇳🇴Norway efpapado
Following up vegardjo's suggestion, another code snippet that does not trigger a node save:
$connection = \Drupal::database(); $bundles = [ 'my_node_bundle', 'my_other_node_bundle', ]; $nodes = \Drupal::entityQuery('node') ->condition('type', $bundles, 'IN') ->accessCheck(FALSE) ->execute(); foreach ($nodes as $nid) { $connection->merge('key_value') ->keys([ 'collection' => "pathauto_state.node", 'name' => $nid, ]) ->fields([ 'value' => serialize(1), ]) ->execute(); }
This only updates the key_value table, which immitates the
class PathautoState
's logic.And here too: use at own risk!