0

We are using the Sitemap module for creating a sitemap generated from three menus (Footer, Main nav and a 3rd arbitrary one).

In the main nav we have a placeholder LI element which we use to insert a view.

In the sitemap template we need to replace this placeholder with some content without affecting the main nav.

In the sitemap overridden template where {{ item.content }} normally appears I tried replacing with the following:

{{ item.content|replace('>LRGPH_BRANCHES_BY_SERVICE<', ('>' ~ allBranches ~ '<')) }}

Note: allBranches is a concatenation of the output of 4 views (Essentially pure HTML)

but Twig/Drupal seems to have issue with this as I get an error, as per below:

Warning: strtr() expects parameter 1 to be string, array given in twig_replace_filter() (line 521 of /var/www/vendor/twig/twig/src/Extension/CoreExtension.php). twig_replace_filter(Array, '>LRGPH_BRANCHES_BY_SERVICE<', '>

I next tried using the contributed modules hook, namely function TEMPLATE_preprocess_sitemap(&$variables) but although I can see the HTML output of the sitemap I cannot change it using this as far as I can tell.

Is there any way to achieve this change at all without altering the Main navigation?

1 Answer 1

0

{{ something.content }} is a render array, you can't do a string replace on an array. But you can store the computed string output of the render array, and do a string replace on the rendered output:

{# compute the output of the render array and store it #}
{% set rendered_html_string = item.content|render %}

{# do the replacement #}
{% set modified_string = rendered_html_string|replace('seachtext', 'replacetext') %}

{# print the modified string #}
{{ modified_html_string }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.