Add bulk enable "Generate automatic URL alias"/force option in bulk generate

Created on 1 February 2016, almost 9 years ago
Updated 5 May 2023, over 1 year ago

Bulk generate won't work if the existing nodes or terms didn't enable "Generate automatic URL alias". The work around for assigning alias to existing content is to do bulk "Update URL-Alias" at /admin/content. But there is no way to bulk enable "Generate automatic URL alias" for taxonomy terms. It would be nice to have bulk enable "Generate automatic URL alias"/force option in bulk generate so existing content/terms can have alias assigned to them.

Feature request
Status

Fixed

Version

1.0

Component

Code

Created by

🇨🇦Canada Weilinggu

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • @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']);
      }
    }
    
    
Production build 0.71.5 2024