我不明白钩子的意思。我希望,"pattern_alter“将允许我”改变“”模式“。但情况似乎并非如此。
我也试图改变化名..。这似乎也没有效果。
能给我举个例子吗?
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.
}
}发布于 2021-07-16 09:17:56
只有在以下情况下才会调用hook_pathauto_pattern_alter():
$entity->toUrl()->getInternalPath()不引发EntityMalformedException、UndefinedLinkTemplateException或UnexpectedValueException异常只在更新实体时才调用显示的钩子,而不是在创建实体时调用。当路径自动模块被设置为不更改现有路径别名时,即使调用了hook_pathauto_pattern_alter(),路径别名也不会更改。
另外,钩子应该首先检查调用钩子的实体是一个节点,这就是pathauto.api.php中示例钩子所做的。
/**
* 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()));
}
}所示钩子的代码类似于下面的代码。
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()的其他模块在问题中显示的实现之后调用它们的实现,这就是改变同一个实体和同一个包的模式。
https://drupal.stackexchange.com/questions/304123
复制相似问题