Hello,
I'm a seasoned D7 developer, and new to D8, but after searching online for quite some time and pouring through the debugger variable values, I cannot find a clear method to manipulate data to pass to a twig template.
Specifically in this case, the client wants a Bootstrap carousel, and I have all the relevant fields being output in a standard Views unordered list to a Views block. The default Views output is fine. However, in my theme's HOOK_preprocess_block() function, nowhere do I see any of the field data (e.g., content name, image filename, etc.). Also, in my .twig template's $content variable, again, no specific content data.
Incidentally, in my theme, HOOK_block_view_alter() isn't called, but it may not matter since the variables inside the .twig template don't apparently contain any of the specific content, so I doubt that HOOK_block_view_alter() would help.
(In D7, I would find the field data in the render array and re-arrange it for insertion in to the Bootstrap carousel structure inside a template file.)
So, in D8, how is one supposed to go about altering or re-arranging the data for a unique display template .twig file? Where is the data to be manipulated?
thanks!
Comments
After some more digging, in
After some more digging, in case it helps someone else, I eventually found the data deep in the $variables array of the HOOK_preprocess_block() function. Specifically, in my case, it was located at:
$variables['content']['#view']->result[0]->_entity
...which is a node object and to get the data, one can call the member functions (methods?) like get() or toArray(). The result[0] is the first result node, and likewise, result[1] would be the second, and so on. Then, one can get the render arrays by calling the objects' view methods like so (for in this case the body field):
$variables['content']['#view'] -> result[0] -> _entity -> body -> get(0) -> view()
One could then create a new variable in the $variables array to insert the data after manipulation, and retrieve that same variable later in the twig template.
I'm not sure if this is the best strategy, but it gets the job done.