Suppress template not found error for modular pages

I am currently building a theme compatibility behavior where a modular page, even those not supported by the theme, can dump the content of its modules into the page. I got the behavior I wanted, with a catch. When {{ module.content }} or {{ module.content|raw}} is used, in addition to the content of the page, the following error markup is being added:

<h3 style="color:red">ERROR: <code>modular/text.html.twig</code> template not found for page: <code>/demo/modular/_text</code></h3> <h1>_text</h1> 

I couldn’t isolate these values with a dump in the debugbar, nor I can find a twig filter or function to do the trick. And for this scenario, renaming the modules to something theme-supported is not an option.

Is there anyway to force dump a module content without the errors popping up? Or for a way for me to access, restyle, or move the error in anyway? Thanks.

I found the culprit. The output is defined on `system/templates/modular/default.html.twig’, which renders if the module template isn’t found.

The next step is to figure out how to make sure that if your theme has a modular/default.html.twig, the theme will use that instead of the system fallback template.

Update: if you did define a modular/default.html.twig, the theme does use that in place of the system fallback. I just needed to clear all cache to see the changes.

However, when dumping the module object, the content is defined in #raw_content, not in content. module.raw_content only outputs the tags in the template, but not the raw content in the module object.

Problem solved.

I’ve found that the best way to handle compatibility support for modular templates is by creating two files: a modular.html.twig and a modular/default.html.twig.

My modular.html.twig contains this basic collection loop:

{% embed 'partials/base.html.twig' %}
{% block content %}
    {% for module in page.collection() %}
        {% include 'modular/default.html.twig' %}    
    {% endfor %} 
{% endblock content %}
{% endembed %}

There is a caveat though. I encountered a weird caching bug that does not let page.content()|raw work if page.content()|raw and module.content()|raw is not present in the template. After so many trial and errors, this is the only consistent workaround I found for my modular/default.html.twig

<section>
    {% if module.content()|raw %}
        {{ module.content()|raw }}
    {% elseif page.content()|raw %}
        {{ page.content()|raw }}
    {% endif %}
</section>