Heya!

I was overwriting the template eu_cookie_compliance_popup_info.html.twig from within my custom theme, when I realised that adding a library in the file, didn't do anything.

So I went to investigate and realised that when using eu_cookie_compliance module, and having template suggestions turned on, you can't actually discover the template as a frontend developer.

I had a peek in to the module's code and would suggest that we do an actual ->render() by rewriting the hook_page_attachments in to a hook_page_preprocess.

I wrote a patch to show what I mean, and it looks like it's working: when I now overwrite the template the added library is included too + the theme_suggestions are now visible when rendering the page!

Let me know what you think! Patch incoming :)

Comments

MathieuSpil created an issue. See original summary.

mathieuspil’s picture

StatusFileSize
new9.84 KB
svenryen’s picture

Thanks for the patch. I don't think this change will make it into the next version (GDPR issues, currently overdue), and the patch may need a re-roll to also include the mobile banner and then "withdraw consent" banner that are about to be added to the -dev branch.

svenryen’s picture

+++ b/eu_cookie_compliance.module
@@ -145,13 +148,32 @@ function eu_cookie_compliance_page_attachments(&$attachments) {
+    if($config->get('popup_enabled')) {

Does this actually work? You're rendering either the html_info version OR the html_agreed version and then including them in the javascript settings array. Correct me if I'm wrong, but normally we would need both rendered, since the user may see the Thank you message after he has dismissed the first (info) message.

+++ b/eu_cookie_compliance.module
@@ -145,13 +148,32 @@ function eu_cookie_compliance_page_attachments(&$attachments) {
+    } elseif($config->get('popup_agreed_enabled')) {

See above.

I haven't looked into the rest of the patch in detail, so there may be more issues. It would also be nice if when you think the code requires cleanup, you can open a separate issue on that, so there's less "noise" in the proposed patch.

svenryen’s picture

Also, code style: For drupal modules there should be a space between "if" and the opening parentheses.

mathieuspil’s picture

StatusFileSize
new7.3 KB

aha, in my current project, we are not using the popup_agreed_enabled option, so its likely your tought of #3 would make the first patch wrong indeed.

Cleaned up a bit, so it has less meaningless lines, sorry about that :)

svenryen’s picture

Thanks, looks better now (though haven't tested it). It still will need a re-roll in a couple of days.

svenryen’s picture

Status: Active » Needs review

The last submitted patch, 2: 2974324-2.patch, failed testing. View results

svenryen’s picture

Status: Needs review » Needs work

The patch failed to apply. Can you check out the current -dev and update the patch?

mathieuspil’s picture

StatusFileSize
new10.28 KB

Updated the patch to reflect the latest dev version!

This went off my radar for a bit too long :)

Same things, but now doing it for multiple templates within the .module file

And now without messing with indentation in the template files

svenryen’s picture

Status: Needs work » Needs review
nitebreed’s picture

StatusFileSize
new7.61 KB

Re-rolled the patch against the latest dev

Status: Needs review » Needs work

The last submitted patch, 13: 2974324-13.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

svenryen’s picture

Status: Needs work » Needs review
aaronmchale’s picture

Patch in #13 seems to work for me, before applying the patch template files in my theme weren't being used and no theme hook suggestions were being displayed when Twig debug mode was on, now template from my theme is used and theme hook suggestions show.

abaier’s picture

I can verify that patch #13 lets the template suggestions show up. But to mention is, that I was also able to override the original templates before, by copying them to my theme. I did not try to include a library though.

Unfortunately I get the following warning/notice after applying the patch:

Warning: strlen() expects parameter 1 to be string, array given in Drupal\Component\Utility\Unicode::validateUtf8() (line 599 of core/lib/Drupal/Component/Utility/Unicode.php).
Drupal\Component\Utility\Unicode::validateUtf8(Array) (Line: 63)
Drupal\Component\Utility\Xss::filter(Array, Array) (Line: 91)
Drupal\filter\Plugin\Filter\FilterHtml->process(Array, ) (Line: 118)
Drupal\filter\Element\ProcessedText::preRenderText(Array)
call_user_func(Array, Array) (Line: 378)
Drupal\Core\Render\Renderer->doRender(Array, 1) (Line: 195)
Drupal\Core\Render\Renderer->render(Array, 1) (Line: 151)
Drupal\Core\Render\Renderer->Drupal\Core\Render\{closure}() (Line: 582)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 152)
Drupal\Core\Render\Renderer->renderPlain(Array) (Line: 302)
check_markup(Array, 'basic_html', ) (Line: 283)
eu_cookie_compliance_preprocess_page(Array, 'page', Array) (Line: 287)
Drupal\Core\Theme\ThemeManager->render('page', Array) (Line: 437)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 490)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 123)
__TwigTemplate_ba018c7bfd2b1ecd1ff0f2f2a97e12a63fe2dc6ca66dc1b9a874932c9f310f25->doDisplay(Array, Array) (Line: 427)
Twig\Template->displayWithErrorHandling(Array, Array) (Line: 398)
Twig\Template->display(Array) (Line: 406)
Twig\Template->render(Array) (Line: 64)
twig_render_template('themes/mytheme/templates/layout/html.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('html', Array) (Line: 437)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 147)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 582)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 148)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
call_user_func(Array, Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 156)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 184)
Drupal\page_cache\StackMiddleware\PageCache->fetch(Object, 1, 1) (Line: 121)
Drupal\page_cache\StackMiddleware\PageCache->lookup(Object, 1, 1) (Line: 75)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 693)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Notice: Array to string conversion in Drupal\Component\Utility\Html::load() (line 285 of core/lib/Drupal/Component/Utility/Html.php).
Drupal\Component\Utility\Html::load(Array) (Line: 112)
Drupal\filter\Plugin\Filter\FilterHtml->filterAttributes(Array) (Line: 94)
Drupal\filter\Plugin\Filter\FilterHtml->process(Array, ) (Line: 118)
Drupal\filter\Element\ProcessedText::preRenderText(Array)
call_user_func(Array, Array) (Line: 378)
Drupal\Core\Render\Renderer->doRender(Array, 1) (Line: 195)
Drupal\Core\Render\Renderer->render(Array, 1) (Line: 151)
Drupal\Core\Render\Renderer->Drupal\Core\Render\{closure}() (Line: 582)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 152)
Drupal\Core\Render\Renderer->renderPlain(Array) (Line: 302)
check_markup(Array, 'basic_html', ) (Line: 283)
eu_cookie_compliance_preprocess_page(Array, 'page', Array) (Line: 287)
Drupal\Core\Theme\ThemeManager->render('page', Array) (Line: 437)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 490)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 123)
__TwigTemplate_ba018c7bfd2b1ecd1ff0f2f2a97e12a63fe2dc6ca66dc1b9a874932c9f310f25->doDisplay(Array, Array) (Line: 427)
Twig\Template->displayWithErrorHandling(Array, Array) (Line: 398)
Twig\Template->display(Array) (Line: 406)
Twig\Template->render(Array) (Line: 64)
twig_render_template('themes/mytheme/templates/layout/html.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('html', Array) (Line: 437)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 147)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 582)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 148)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
call_user_func(Array, Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 156)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 184)
Drupal\page_cache\StackMiddleware\PageCache->fetch(Object, 1, 1) (Line: 121)
Drupal\page_cache\StackMiddleware\PageCache->lookup(Object, 1, 1) (Line: 75)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 693)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
a.dmitriiev’s picture

StatusFileSize
new14.25 KB

Re-rolled patch for version 1.5. I also fixed some coding standards and added missing variables to desktop template.

svenryen’s picture

Thanks for the re-roll. I'll try to look at this patch soon to get this fixed.

svenryen’s picture

Status: Needs review » Needs work

Thanks again for the patch, @a.dmitriiev.

Comments:

  1. +++ b/eu_cookie_compliance.module
    @@ -343,21 +337,20 @@ function eu_cookie_compliance_page_attachments(&$attachments) {
    -    $cache_tags = isset($attachments['#cache']['tags']) ? $attachments['#cache']['tags'] : [];
    -    $attachments['#cache']['tags'] = Cache::mergeTags($cache_tags, $config->getCacheTags());
    

    Is there any reason why we're dropping cache tags from the changed file?

  2. +++ b/eu_cookie_compliance.module
    @@ -453,14 +446,14 @@ function eu_cookie_compliance_user_in_eu() {
    +    $smart_ip_session = \Drupal\smart_ip\SmartIp::query($ip_address);
    

    I think use statements are preferred to including the paths in code. (This code has been changed in the -dev branch, so it doesn't really matter that much any more.)

a.dmitriiev’s picture

I think there is no reason to remove the cache tags, sorry, I've not seen that. It was kind of silly re-roll. Of course, they should stay.

majid.ali’s picture

StatusFileSize
new15.05 KB

Re-rolled patch for version 1.8. As discussed in #20 and #21 i did not removed the cache tags and kept the use statements for including path.

majid.ali’s picture

Status: Needs work » Needs review
driskell’s picture

StatusFileSize
new15.43 KB

I tried to re-roll this onto 1.9 looks to be applying.

driskell’s picture

StatusFileSize
new15.43 KB

Somehow the patch was broken. Trying this one.

driskell’s picture

brendan.smith’s picture

Tested the patch in Comment #25 with v1.9 of the module and v8.8.6 of Drupal Core.

It fixed an issue we had where the template eu_cookie_compliance_popup_info.html.twig was not being overridden in a Base Theme that we inherit in sub-themes.

svenryen’s picture

StatusFileSize
new15.54 KB

I rerolled the patch and it seems to work well.

  • svenryen committed c8445f7 on 8.x-1.x
    Issue #2974324 by MathieuSpil, Driskell, svenryen, a.dmitriiev, majid....
svenryen’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.