首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >哪个钩子将允许我改变一个页面的链接?

哪个钩子将允许我改变一个页面的链接?
EN

Drupal用户
提问于 2023-04-03 21:05:12
回答 1查看 48关注 0票数 0

哪个钩子将允许我更改内容页和菜单中的每个链接(在标记中给出)并更改它们的属性?

我需要在一个主题中实现这一点,这个主题使用Olivero作为父主题,而显而易见的MYTHEME_preprocess_links()会导致一个空白的白屏(WSOD)。

我唯一能够用来更改页面内容的钩子就是这个。

代码语言:javascript
复制
function MYTHEME_preprocess_field(&$variables): void {
    $content = $variables['items'][0]['content'] ?? false;
    $type = $content['#type'] ?? '';

    if ($content && $type === 'processed_text') {
        $dom = new DomDocument();

        libxml_use_internal_errors(true);
        $dom->loadHTML($variables['items'][0]['content']['#text']);
        $errors = libxml_get_errors();
        libxml_clear_errors();

        if (count($errors) === 0) {
            $edited = 0;

            foreach ($dom->getElementsByTagName('a') as $link) {
            }
        }
    }
}
EN

回答 1

Drupal用户

发布于 2023-04-04 19:42:59

多亏了评论者,我才能想出如何在内容页和菜单中编辑DOM。

关于版本:Drupal 10

将"MYTHEME“替换为主题的机器名称。打开MYTHEME.info.yml文件,查看主题的名称。这是区分大小写的。

在您的/themes/MYTHEME/目录中,您可能有一个MYTHEME.theme文件,您可以在其中声明钩子函数。这些钩子包括允许您在数据构建到模板之前对其进行预处理的钩子。在这里,您可以根据需要调整HTML字符串。

这些钩子包括一个$variables参数,如果您试图通过var_dump打印它,它将占用所有可用内存。

编辑内容

内容包括在/admin/content中生成的内容和在/admin/structure/block/block-content中生成的内容。

用于调整内容页的HTML的钩子是MYTHEME_preprocess_field(&$variables)

检查$variables['entity_type']以确定您是在编辑内容还是块内容。

将用于构建内容的HTML在$variables['items'][0]['content']['#text']中。通过字符串操作或使用DOMDocument进行DOM操作来调整该字符串,然后用您编辑的字符串覆盖$variables['items'][0]['content']['#text']

如果使用DOMDocument,您可能需要忽略loadHTML()生成的所有错误,因为这个特性不理解HTML5元素,但仍然可以使用它们。

代码语言:javascript
复制
function MYTHEME_preprocess_field(&$variables): void {
    $content = $variables['items'][0]['content'] ?? false;
    $type = $content['#type'] ?? '';
    // Blocks are the pieces that are not content. These include the sidebar, search, header, and footer.
    $block = $variables['entity_type'] === 'block_content';

    if ($content && $type === 'processed_text') {
        $dom = new DomDocument();

        libxml_use_internal_errors(true);
        $options = LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOERROR;
        $dom->loadHTML('' . $content['#text'] . '', $options);
        // Use this variable to inspect the errors when troubleshooting.
        // $errors = libxml_get_errors();
        libxml_clear_errors();

        $parsedHTML = $dom->saveHTML();

        // Errors are fine as long as the HTML string was successfully parsed by DOMDocument.
        if ($parsedHTML) {
            // Your code here to manipulate DOM.
        }
    }
}

编辑菜单

用来调整菜单HTML的钩子是MYTHEME_preprocess_menu(&$variables)

Drupal页面中有各种菜单。其中包括adminmainaccount

要调整的菜单项在$variables['items']中。该数组中每个元素的结构如下:

代码语言:javascript
复制
items(array) 
 - menu_link_content:{unique_id}(array)
 -  - is_expanded(boolean)
 -  - is_collapsed(boolean)
 -  - in_active_trail(boolean)
 -  - attributes(object)
 -  - title(string) Sample menu item
 -  - url(object)
 -  - below(array)
 -  -  - menu_link_content:{unique_id}(array)
 -  -  -  - is_expanded(boolean)
 -  -  -  - is_collapsed(boolean)
 -  -  -  - in_active_trail(boolean)
 -  -  -  - attributes(object)
 -  -  -  - title(string) Sample sub-menu item
 -  -  -  - url(object)
 -  -  -  - below(array)
 -  -  -  - original_link(object)

每个菜单项都可能有子项.它们作为数组保存在['below']键中。

如果要调整url,则['url']元素包含一个Drupal对象。关于如何调整Drupal对象,请参考文献资料

下面是一个让您开始的例子:

代码语言:javascript
复制
function adjustMenuItem($item) {
    // Your code to adjust the elements of the $item array.

    // Handle recursive array elements.
    if (isset($item['below']) && is_array($item['below']) && count($item['below']) > 0) {
        foreach ($item['below'] as $key => $value) {
            $item['below'][$key] = adjustMenuItem($value);
        }
    }

    return $item;
}

function MYTHEME_preprocess_menu(&$variables): void {
    $name = $variables['menu_name'] ?? '';

    if ($name === 'main' && is_array($variables['items'])) {
        foreach ($variables['items'] as $key => $value) {
            $variables['items'][$key] = adjustMenuItem($value);
        }
    }
}
票数 0
EN
页面原文内容由Drupal提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

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

复制
相关文章

相似问题

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