From cabc9b7bb0e21520ddaf31d8bf221aec870e375e Mon Sep 17 00:00:00 2001
From: juampy <juampy72@gmail.com>
Date: Thu, 18 Oct 2012 02:10:39 +0200
Subject: [PATCH] Issue #1407176 by Dave Reid and juampy: Add support for Bulk
 update path aliases.

---
 pathauto_entity.module       |   14 ++++++++-----
 pathauto_entity.pathauto.inc |   46 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+), 5 deletions(-)
 create mode 100644 pathauto_entity.pathauto.inc

diff --git a/pathauto_entity.module b/pathauto_entity.module
index 9c10e37..5e69da2 100644
--- a/pathauto_entity.module
+++ b/pathauto_entity.module
@@ -1,4 +1,8 @@
 <?php
+/**
+ * @file
+ * Implements custom entity support with Pathauto module.
+ */
 
 /**
  * Helper function: return a list of supported entity types.
@@ -28,13 +32,13 @@ function pathauto_entity_pathauto($op) {
     case 'settings':
       $settings = array();
       foreach (pathauto_entity_supported_entity_types() as $entity_type => $entity_info) {
-        $settings[$entity_type] = (object) array(
+        $settings[] = (object) array(
           'module' => $entity_type,
           'token_type' => $entity_info['token type'],
-          'groupheader' => $entity_info['label'],
-          'patterndescr' => t('Default pattern'),
+          'groupheader' => t('Entity paths'),
+          'patterndescr' => t('Default pattern for @name', array('@name' => $entity_info['label'])),
           'patterndefault' => '',
-          'batch_update_callback' => 'user_pathauto_bulk_update_batch_process',
+          'batch_update_callback' => 'path_auto_entity_bulk_update_batch_process',
           'batch_file' => drupal_get_path('module', 'pathauto_entity') . '/pathauto_entity.pathauto.inc',
         );
       }
@@ -74,7 +78,7 @@ function pathauto_entity_update_alias($entity_type, $entity, $op) {
     return;
   }
 
-  // Skip processing if the user has disabled pathauto for the node.
+  // Skip processing if the user has disabled pathauto for the entity.
   if (isset($entity->path['pathauto']) && empty($entity->path['pathauto'])) {
     return;
   }
diff --git a/pathauto_entity.pathauto.inc b/pathauto_entity.pathauto.inc
new file mode 100644
index 0000000..a066dea
--- /dev/null
+++ b/pathauto_entity.pathauto.inc
@@ -0,0 +1,46 @@
+<?php
+/**
+ * @file
+ * Pathauto callback implementations.
+ */
+
+/**
+ * Batch processing callback; Generate aliases for entities.
+ */
+function path_auto_entity_bulk_update_batch_process(&$context) {
+  if (!isset($context['sandbox']['count'])) {
+    // First pass. Build an array with all the pairs of id and entity type to process.
+    $context['sandbox']['count'] = 0;
+    $context['sandbox']['items'] = array();
+    foreach (pathauto_entity_supported_entity_types() as $entity_type => $entity_info) {
+      $query = new EntityFieldQuery();
+      $query->entityCondition('entity_type', $entity_type);
+      $result = $query->execute();
+      $ids = isset($result[$entity_type]) ? array_keys($result[$entity_type]) : array();
+      foreach ($ids as $id) {
+        $context['sandbox']['items'][] = array('id' => $id, 'type' => $entity_type);
+      }
+    }
+    $context['sandbox']['total'] = count($context['sandbox']['items']);
+
+    // If there are no items to update, stop immediately.
+    if (!$context['sandbox']['total']) {
+      $context['finished'] = 1;
+      return;
+    }
+  }
+
+  // Extract 25 items to be processed in this pass.
+  $items_to_process = array_slice($context['sandbox']['count'], 25, $context['sandbox']['items']);
+  module_load_include('inc', 'pathauto');
+  foreach ($items_to_process as $item) {
+    $entity = entity_load($item['type'], $item['id']);
+    pathauto_entity_update_alias($item['type'], $entity, 'bulkupdate');
+  }
+
+  // Update progress stats.
+  $context['sandbox']['count'] += count($items_to_process);
+  if ($context['sandbox']['count'] != $context['sandbox']['total']) {
+    $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
+  }
+}
-- 
1.7.9.5

