0

I'm afraid the title isn't clear: I am passing a value from hook_node_view to my javascript in $build['#attached']['drupalSettings'][]. But the value that is set there is not the value that the js is using. I've checked in several places to make sure that this value is indeed the one set in the variable; nevertheless, the js script is getting a different value. I'm sure I'm missing something easy, at least something I should be aware of. The stack trace in the js debugger isn't helpful, so it seems there is some processing happening after the hook is called and the js getting the value in drupal settings.

  • You should share same code. hook_node_view() is cached, so if you have something very dynamic, that will not be refreshed on every view, otherwise this should work fine, see for example \statistics_node_view() which does exactly this – Berdir Apr 17 '18 at 6:06
  • Need to see code. – Jaypan Apr 17 '18 at 11:31
0

I had strongly suspected a caching issue while ignoring something more obvious, that the hook was being called several times and passing the value of the last time it's called to the javascript. Just added a check to make sure it's passing the correct value and it solved the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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