【问题标题】:How to programmatically bulk create/update aliases in Drupal 7如何以编程方式在 Drupal 7 中批量创建/更新别名
【发布时间】:2012-05-16 09:19:38
【问题描述】:

如何通过仅使用 drupal 7 核心(具有出色的批处理 API!)以编程方式批量处理别名节点 URL?

我的问题实际上是如何使用drupal并识别存储在url_alias表中的别名

背景:

我从事的项目有超过 200,000 个节点(Drupal 7),并且使用 pathauto 模块(每 20 分钟 10 个别名)为所有这些节点的系统默认 URL 设置别名实际上需要数年时间。我尝试了一切来提高这些性能,但失败了(尝试了不同的服务器、不同的 mysql 优化、不同的模式)。

我已经准备好批处理功能,它们在 20 分钟内为 200,000 个节点起别名,它们创建了存储在表“url_alias”中的干净别名。 我花了很多时间查看 pathauto 代码,但无法找到或理解该模块如何为 drupal 提供识别批量更新路径的命令。

感谢您的提示、答案或想法。非常感谢!

【问题讨论】:

    标签: drupal drupal-7 pathauto drupal-path-aliases


    【解决方案1】:

    这里是更新所有指定类型节点别名的函数

    <?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);
    
    ?>
    

    【讨论】:

    • 永远不要使用variable_set() 在请求期间临时修改值——竞争条件很容易咬你。而是设置和恢复$conf['pathauto_update_action'],不会影响其他请求。
    【解决方案2】:

    如果您在 hook_node_update 或 Rule 或其他内容中执行此操作,新的 $node 将无法用于其他模块,如 token、pathauto 等,因此您不会得到预期的结果。一个解决方案是重置缓存的$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);
    ?>
    

    【讨论】:

      【解决方案3】:

      检查以确保为 pathauto 包设置了变量。

      变量名是pathauto_[entity]_[bundle]_pattern,所以pathauto_node_[bundle]_pattern

      【讨论】:

        【解决方案4】:

        此代码基于 Eugene Fidelin 的代码,但使用 $conf 全局而不是变量集。

          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;
        

        【讨论】:

          猜你喜欢
          • 2011-12-28
          • 1970-01-01
          • 2014-11-17
          • 1970-01-01
          • 1970-01-01
          • 2018-06-27
          • 2019-08-05
          • 1970-01-01
          • 2016-03-19
          相关资源
          最近更新 更多