Drupal 8 |使用了错误的别名

Drupal 8 | Wrong alias used

你好,

我有一个关于 Drupal 8 的问题我无法解决,这就是我拜访你的原因。

同一个节点我有 2 个别名:

我有一个只出现在“/public/*”页面上的 block_1 和一个出现在“/pro/*”页面上的 block_2。

当我访问url“/pro/event/10”时,显示block_1而不是block_2。

我得出结论,当我在“/pro/event/10”页面上时,Drupal 选择了别名“/public/event/10”(可能是他找到的第一个)。

如何以编程方式告诉 Drupal 要使用的正确别名?

预先感谢您的帮助。

您可能希望通过实施 OutboundPathProcessorInterface 来创建自己的 path_processor_outbound 服务。
如果当前请求路径匹配 /public/event/**/pro/event/**.
,此实现可能适用于 /node/{id} 路径 分析节点实体的类型(包):如果它是 event 生成并且 return 您想要的路径;如果不是 event,请不要操纵路径和 return 原始路径。

在 PHP 代码中编写实际实现可能是您自己的乐趣 ;-)

这里是代码,如果它可以帮助某人

class OOutboundPathProcessor implements OutboundPathProcessorInterface 
{
    function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) 
    {
        // Only for nodes
        if (!isset($options['entity_type']) OR $options['entity_type'] !== 'node') 
        {
            return $path;
        }

        // Get current 'space'
        $espace = \Drupal::service('session')->get('espace');   

        // Get the node to process
        $node = $options['entity'];

        // New path
        $str_path = "/%s/%s/%s";
        $new_path = sprintf($str_path, $espace, $node->bundle(), $node->id());

        // Check new path
        $isValid = \Drupal::service('path.validator')->isValid($new_path);

        if ($isValid === true) return $new_path;

        return $path;
    }
}