4

So I've created a small subtheme called bartik_graphql. It just overrides the 'Powered by Drupal' block. Besides it's bartik_graphql.info.yml file it has a templates directory containing one file block--bartik-graphql-powered.html.twig.

Contents of block--bartik-graphql-powered.html.twig:

{#graphql
query {
  admin:userById(id: "1") {
    name
  }
}
#}

{% extends "@bartik/block" %}

{% block content %}
  <div{{ content_attributes.addClass('content') }}>
    {{ content }} and some guy named {{ graphql.data.admin.name }}
  </div>
{% endblock %}

I've enabled my subtheme and set it to default. However I receive the following error:

Twig\Error\LoaderError: Template "@bartik/block" is not defined. in Twig\Loader\ChainLoader->getCacheKey() (line 122 of web/core/themes/bartik/templates/page.html.twig).

What I expect to happen is have my subtheme's template override what is used by default to display the 'Powered by Drupal' message.

Drupal: 8.7.8
PHP: 7.2.17

Am I using extends wrong here?

1 Answer 1

1

I didn't know of Twig namespacing just until now. But what always worked for extends and includes is to point to the template you want to extend/include directly, with the path starting from Drupal web root. So in your case it needs to say:

{% extends "core/themes/bartik/templates/block.html.twig" %}

You could also try the following which seems to follow theme inheritance but only up to the final base theme (which would be Classy).

{% extends "@block/block.html.twig" %}
7
  • 1
    Thanks @leymannx that has appeared to do the trick. In my searches to find a solution I found similar suggestions but the particular example I was looking at on YouTube showed him successfully using {% extends @bartik/block" %}. Could that perhaps be that the tutorial was from mid-2018 and that's no longer how it's done?
    – frmt
    Commented Oct 14, 2019 at 20:35
  • 1
    @frmt – Oh wow, I've never recognized Twig namespacing is actually used in a few places even in core. I just tried the same but for me it also doesn't work. Whereas Drupal root-relative paths work just fine...
    – leymannx
    Commented Oct 14, 2019 at 20:51
  • It's even in the documentation in places like GraphQL Twig. I posted in their Slack channel about it, hopefully I can gain some more insight as to what they are doing.
    – frmt
    Commented Oct 14, 2019 at 21:03
  • 1
    Will do. Once I get back in the office in the morning I'll try that out and update you. There is a similar issue I'm having related to the same webinar on GraphQL Twig, so I also commented on that issue. It hasn't been addressed in three months so hopefully pinging it again will bring it to the forefront again.
    – frmt
    Commented Oct 15, 2019 at 0:08
  • 2
    Here is an update @leymannx. @block/block.html.twig works too, although the resulting output is minimal, I'm assuming Classy is pretty bare bones. When using your original suggestion I get a lot of stuff inherited from Bartik.
    – frmt
    Commented Oct 15, 2019 at 16:12

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.