I have nodes with puzzles. I store extra info on the puzzle (say puzzle_sol) in a different table. I add this extra info to node in 'hook_node_storage_load' in my module. I then attach a special library to the page in page_attachments hook. There I use the above extra info to decide whether to attach the library or not. (if puzzle_sol was added, then attach the library ... else not)

This whole strategy is working without absolutely any issue.

Now the problem.

I wish to use different rendering of the puzzle (by means of a special template) when there is a puzzle_sol added as above.

My understanding is that I need to add a template suggestion depending on this extra info (if puzzle_sol was added then add the template suggestion ... else not). I tried to use hook_theme_suggestions_node_alter for this purpose ... but I do not have any clue how to proceed ... How to find out whether the extra info (puzzle_sol) was added on the node or not?

Please let me know how to approach this.

Thanks in advance

Comments

mvelanka’s picture

I am adding this info (puzzle_sol) to node in hook_node_storage_load and was expecting to locate it in a foreach loop as follows:

$node = $variables['elements']['#node'];
foreach($node as $key => $value){
...
if (isset ($node['puzzle_sol']) {

etc etc

however this loop was not catching it (probably it was attached as object property ... not a class property ... I guess that is the reason)
When I tried to access it as $node->puzzle_sol .........I got it.

I now should be able to add template suggestion based on this.

(Please let me know if you think of any better way to achieve this)

Thanks