【问题标题】:Generate url aliasing based on taxonomy term根据分类术语生成 url 别名
【发布时间】:2023-04-07 19:11:01
【问题描述】:

我有一个词汇类别和四个术语。我想要做的是,如果内容被标记为特别是“term1”以将 url 生成为 word1/[node:title] 而对于所有其他标签,则只是标准的 url 格式。

如果我想要 url 中的术语显然 id 使用模式替换,但如果使用特定标签,我希望使用另一个词

【问题讨论】:

    标签: drupal drupal-7 drupal-modules drupal-taxonomy


    【解决方案1】:

    我想不出一种简单的即插即用方式来实现这一点。您可能需要在 Pathauto 的 URL 别名设置中为“默认路径模式”创建自己的令牌:

    /**
     * Implementation of hook_token_info().
     */
    function MODULE_token_info() {
      $info['tokens']['node']['node-term-path'] = array(
        'name'        => t('Node path by term'),
        'description' => t('The path to a node based on its taxonomy terms.'),
      );
      return $info;
    }
    
    /**
     * Implementation of hook_tokens().
     */
    function MODULE_tokens($type, $tokens, array $data = array(), array $options = array()) {
      $replacements = array();
      if ($type == 'node' && !empty($data['node'])) {
        $node = $data['node'];
    
        foreach ($tokens as $name => $original) {
          switch ($name) {
            case 'node-term-path':
              $items = field_get_items('node', $node, 'TAXONOMY_FIELD_NAME');
              foreach ($items as $item) {
                $tids[] = $item['tid'];
              }
              if (in_array(TID_OF_TERM1, $tids)) {
                // Path for nodes with term1
                $replacements[$original] = 'word1/'. pathauto_cleanstring($node->title);
              }
              else {
                // Path for other nodes
                $replacements[$original] = 'content/'. pathauto_cleanstring($node->title);
              }
              break;
          }
        }
      }
      return $replacements;
    }
    

    【讨论】:

      【解决方案2】:

      为任何需要类似解决方案的人找到了一种简单的方法,可以使用模块实体参考。

      http://drupal.org/project/entityreference

      我刚刚为用户帐户选择实体引用创建了一个新字段,然后您可以选择 drupal 中的任何实体进行引用。 (即你可以选择一个术语/内容/任何东西)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-13
        • 1970-01-01
        • 2014-01-04
        • 2023-03-27
        • 1970-01-01
        • 2017-08-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多