Hi All,
Loving the module. I am using DomPDF and with a few minor issues getting along well. One issue I am having, not sure if it is a bug or me doing something wrong. I am trying to print a date in the PDF. So what I have is this in my Twig file:
{{ entity_print_css }}
{% set displayContent = content.0.0.0 %}
{% set datePoint = displayContent['#view'].filter.myfilter.value[0] %}
<div class='frontpage'>
<p>
Some text here using the dates {{datePoint|date('jS M Y')}} and {{now|date('jS M Y')}}
</p>
</div>
<header>
...
</header>
{{ content }}
In debug view it all works perfect, dats are shown fine. But when creating the real PDF it shows both dates as today. So question is, is this a bug or am I doing something wrong?
Comments
Comment #2
benjy commentedInteresting one, I'm guessing the default behaviour of the twig date function is to use today's date if the input date is null, which implies maybe datePoint is null when you actually render the PDF?
Comment #3
nickbits commented@benjy, thanks for the reply.
I did some more digging about and yes it is null. It would appear that you cannot access any value inside content within the PDF itself. I have tried and all work in debug view but not in the PDF itself. I thought Iwould try a different aproach. I read somewhere (docs?) about the preprocess specifically tor entity print. So created the code, accessed the variable required and set it. Code is:
Works fine. Now in the entity-print--view--view.html.twig file I access that variable with {{datePoint}}. Again works fine in debug view but in PDF it prints "1st Jan 1970" which I assume is default for a null value.
So the question is am I being really stupid and missing something obvious?
I have created a front page to the PDF in the twig file and want to get this value to print on that front page. Everything else works fine, just this value I cannot access.
Comment #4
benjy commentedYeah it sounds like you are different everything right and there is a bug but i'm not sure what exactly it is. Funny that it hasn't been reported previously.
Do any other fields not print correctly from your entity?
Comment #5
nickbits commented@benjy, sorry for the delay. So far beyond {{content}} and {{ entity_print_css }} I have had little success in getting anything else to print. I assumed if it was a bug it would have been reported, but as you say at the same time can't see anything else being done wrong. I will test it on a fresh install later and ensure it is not a conflict with another module. I am assumeing not as debug mode works.
Comment #6
nickbits commentedI have been experementing more. So first thought, is this restricted to one variable or many? I tested various other EXISTING variables. All I have tested seem to work, such as:
<img src="/{{ base_path ~ directory }}/logo.png" />So second thought, if I set a variable with a string will that work. So I did:
And then in the twig file:
Both print fine in debug view, in PDF view wibble prints fine but the date still remains wrong as described previously. So this narrows it down, or at least it does to me. Before going further I wanted to double check so I have altered the preprocess file to:
Ignore the fact that PDF throws an error when using dump, and the fact I should have looked at this earlier, I get the info I need to figure it out. So on debug view works as it should, in PDF view though, date is wrong. So indicating at this point there is an issue with $variables['content'][0][0]['#view']. Using that and dump I think I have figured it out:
In debug view use:
$datePoint = $variables['content'][0][0]['#view']->filter['myfilter']->value[0];but PDF use:
$datePoint = $variables['content'][0]['#view']->filter['myfilter']->value[0];And that works fine, except now debug view shows wrong date.
So I have a solution that works for the real PDF. This leads to two questions:
Comment #7
benjy commentedHa, that's an annoying bug, obviously we're wrapping something in an additional array somewhere. Took a quick look at the code, try this change below, untested but could be it?
Comment #8
nickbits commentedHi Benjy,
Give me a few days and I will give it a try and report back.
Nick
Comment #9
benjy commentedCan you test this patch #2972590: savePrintable() and hook_preprocess_HOOK() - it could also fix your issue.
Comment #10
benjy commented