How to program mass creation / updating of aliases in Drupal 7

How to programmatically combine a node alias using only the drupal 7 kernel (with it a great batch API!)?

My question is actually how to make drupal use and recognize aliases stored in url_alias table ?

History:

The project I'm working on contains more than 200,000 nodes (Drupal 7), and the default aliasing for all of these nodes literally takes years with the pathauto module (10 aliases every 20 minutes). I tried everything to improve these indicators, but could not (I tried different servers, various mysql optimizations, different templates).

I already have the functions of the batch process up and ready, they are for 200,000 nodes in 20 minutes, they create pure aliases stored in the table "url_alias". I looked at pathauto code for a long time, but could not find or understand how the module gave drupal the order of recognition of mass updated paths.

Thanks for your tips, answers or ideas. Very grateful!

+6
source share
4 answers

Here is a function that will update aliases for all nodes of the specified type

<?php module_load_include('inc', 'pathauto'); module_load_include('inc', 'pathauto.pathauto'); // Get all nodes that need to be updated $query = db_select('node', 'n'); $query->addField('n', 'nid'); $query->condition('n.type', array('TYPE1', 'TYPE2'), 'IN'); $nids = $query->execute()->fetchCol(); // Save current action for new aliases and change it to delete old one. $alias_action = variable_get('pathauto_update_action', 0); variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_DELETE); pathauto_node_update_alias_multiple($nids, 'bulkupdate'); // Restore original action for new aliases. variable_set('pathauto_update_action', $alias_action); ?> 
+8
source

If you do this in the hook_node_update file or in a rule or something else, the new $ node will not be available for other modules such as token, pathauto, etc., and therefore you will not get the expected results. The solution is to reset the cached $node :

 <?php // Reset the cached $node. entity_get_controller('node')->resetCache(array($node->nid)); // Get all nids that reference this node. This is just an example. $nids = db_query("SELECT entity_id FROM field_data_field_reference WHERE field_reference_target_id = {$node->nid}")->fetchCol(); // Include necessary Pathauto files. module_load_include('inc', 'pathauto'); module_load_include('inc', 'pathauto.pathauto'); // Save current action for new aliases and change it to delete old one. $alias_action = variable_get('pathauto_update_action', 0); variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_DELETE); pathauto_node_update_alias_multiple($nids, 'bulkupdate'); // Restore original action for new aliases. variable_set('pathauto_update_action', $alias_action); // Clear path cache. cache_clear_all('*', 'cache_path', TRUE); ?> 
+2
source

Verify that the variable has been set for this package for pathauto.

The variable name is pathauto_ [entity] _ [bundle] _pattern, so pathauto_node_ [bundle] _pattern

+1
source

This code is based on Eugene Fidelin, but the $ conf global variable is used instead of $ set.

  module_load_include('inc', 'pathauto'); module_load_include('inc', 'pathauto.pathauto'); // Get all nodes that need to be updated. $query = db_select('node', 'n'); $query->addField('n', 'nid'); $query->condition('n.type', array('TYPE1', 'TYPE2'), 'IN'); $nids = $query->execute()->fetchCol(); global $conf; // Store old value. $old_pathauto_var = $conf['pathauto_update_action']; // Set new value. $conf['pathauto_update_action'] = PATHAUTO_UPDATE_ACTION_DELETE; // Generate aliases. pathauto_node_update_alias_multiple($nids, 'bulkupdate'); // Restore original action for new aliases. $conf['pathauto_update_action'] = $old_pathauto_var; 
0
source

Source: https://habr.com/ru/post/915893/


All Articles