首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >示例hook_pathauto_pattern_alter

示例hook_pathauto_pattern_alter
EN

Drupal用户
提问于 2021-07-16 07:55:21
回答 1查看 809关注 0票数 2

我不明白钩子的意思。我希望,"pattern_alter“将允许我”改变“”模式“。但情况似乎并非如此。

我也试图改变化名..。这似乎也没有效果。

能给我举个例子吗?

代码语言:javascript
复制
function xxx_pathauto_pattern_alter(\Drupal\pathauto\PathautoPatternInterface &$pattern, array $context) {

  /* //////////////////////////////////////////////////////////////////////////////////
  ////    article_type - override path logic for *archived* nodes ('/archiv/[node::title]')
  /* ///////////////////////////////////////////////////////////////////////////////////*/
  if( $context['op'] === "update" && $context['bundle'] === "news") {

    $node = $context['data']['node'];
    $archived = $node->get('field_archived')->getString() === "1";
    if ($archived) {

      $pattern->setPattern('/archive/[node::title]');
      dpm($pattern); //WORKS !!! in object only. Pattern will not change in real world.
    }
  }
}

function xxx_pathauto_alias_alter(&$alias, array &$context) {
  /* //////////////////////////////////////////////////////////////////////////////////
    ////    article_type - override path logic for *archived* nodes ('/archive/[node::title]')
    /* ///////////////////////////////////////////////////////////////////////////////////*/
  if ($context['op'] === "update" && $context['bundle'] === "news") {

    $node = $context['data']['node'];
    $archived = $node->get('field_archived')->getString() === "1";
    if ($archived) {
      $alias = "/archive/nope";
      dpm($alias); //WORKS !!! in function only. Alias will not change in real world.
    }
  }
EN

回答 1

Drupal用户

回答已采纳

发布于 2021-07-16 09:17:56

只有在以下情况下才会调用hook_pathauto_pattern_alter()

  • 该实体具有路径字段。
  • 已启用路径自动处理。
  • 对于支持修订的实体,该实体使用默认修订。
  • 已为该实体设置了默认模式。
  • $entity->toUrl()->getInternalPath()不引发EntityMalformedExceptionUndefinedLinkTemplateExceptionUnexpectedValueException异常

只在更新实体时才调用显示的钩子,而不是在创建实体时调用。当路径自动模块被设置为不更改现有路径别名时,即使调用了hook_pathauto_pattern_alter(),路径别名也不会更改。

另外,钩子应该首先检查调用钩子的实体是一个节点,这就是pathauto.api.php中示例钩子所做的。

代码语言:javascript
复制
/**
 * Alter the pattern to be used before an alias is generated by Pathauto.
 *
 * This hook will only be called if a default pattern is configured (on
 * admin/config/search/path/patterns).
 *
 * @param \Drupal\pathauto\PathautoPatternInterface $pattern
 *   The Pathauto pattern to be used.
 * @param array $context
 *   An associative array of additional options, with the following elements:
 *   - 'module': The module or entity type being aliased.
 *   - 'op': A string with the operation being performed on the object being
 *     aliased. Can be either 'insert', 'update', 'return', or 'bulkupdate'.
 *   - 'source': A string of the source path for the alias (e.g. 'node/1').
 *   - 'data': An array of keyed objects to pass to token_replace().
 *   - 'bundle': The sub-type or bundle of the object being aliased.
 *   - 'language': A string of the language code for the alias (e.g. 'en').
 *     This can be altered by reference.
 */
function hook_pathauto_pattern_alter(\Drupal\pathauto\PathautoPatternInterface $pattern, array $context) {
  // Switch out any [node:created:*] tokens with [node:updated:*] on update.
  if ($context['module'] == 'node' && ($context['op'] == 'update')) {
    $pattern->setPattern(preg_replace('/\[node:created(\:[^]]*)?\]/', '[node:updated$1]', $pattern->getPattern()));
  }
}

所示钩子的代码类似于下面的代码。

代码语言:javascript
复制
use Drupal\pathauto\PathautoPatternInterface;

function mymodule_pathauto_pattern_alter(PathautoPatternInterface $pattern, array $context) {
  if ($context['module'] == 'node' && $context['op'] === "update" && $context['bundle'] === "news") {
    $node = $context['data']['node'];
    $archived = $node->get('field_archived')->getString() === "1";
    if ($archived) {
      $pattern->setPattern('/archive/[node::title]');
    }
  }
}

对象已经通过引用传递;没有必要将第一个参数定义为&$pattern

我还将验证没有实现hook_pathauto_pattern_alter()的其他模块在问题中显示的实现之后调用它们的实现,这就是改变同一个实体和同一个包的模式。

参考资料

票数 3
EN
页面原文内容由Drupal提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://drupal.stackexchange.com/questions/304123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档