0

I tried to find a solution to this problem, but couldn't. I will describe what I would like to achieve, and hope someone could help. Sorry if my description is a bit lengthy, I am trying to be detailed in order to help those who could provide advice.

I am building a website for a school. I have a content type of 'staff'. I control its appearance by a template file, specifically:

/templates/node/node--staff.html.twig

In it, I have a

{% if teaser == true %}

that controls appearance for teaser mode .. Now that works fine, and code for teaser mode gets picked up by views.

The problem is, I use some 'block type' views of staff in 3-4 specific pages, and in there, I would like some custom code each time - according to which page the 'teaser code' is being used by the view.

I tried with {{ node.id }} but of course this doesn't work (it gives me the respective node-id of each 'staff'.

So the question is .. How can I have the information on which page each block view is currently being used ? To be more precise perhaps, how can I have the information in the 'teaser code' of the content type, so I can customise, according to which view is using the template code?

I guess I could have different template files for every case, but I would rather avoid it if I can - code will basically be the same, with only a minor string being added each time, so if I can do it with if statements in one file, I 'd rather do that.

My block views are obviously different for each case (to pick up the staff they need) - they 're different block-type views if you understand what I mean. So if I can somehow put something in the view characteristics that can later help me in the template code, that would be great.

I just don't know how to do it .. It's the first site I ever build with Drupal, so I might be missing something obvious ...

I hope I described the problem sufficiently - any help would be greatly appreciated.

Also, if anyone could suggest a better title for this question, please do - this is the best I could come with.

thanks,

thomas

  • This question seems to be similar to this question about getting the current node from the route match drupal.stackexchange.com/questions/264624/…, this includes already the next issue you are facing, which is caching. – 4k4 Jul 19 '18 at 18:54
  • thanks. I don't really understand what's going on in there - still haven't used preprocess hooks at all, I guess I need to study them and see how they can help (I guess they can 'pass' information to be used by the template code). I was hoping there could be another way to achieve what I want, because I need it fairly fast .. But if not, I will have to use the long road :) – thomas Jul 19 '18 at 18:58
  • Then don't do it, this isn't even good practice, because of caching. I would rather look for a solution in the page template, where {{ node.id }} of the current node is available by default. – 4k4 Jul 19 '18 at 19:03
  • But the {{node.id}} gives me the respective node id of each 'staff' node - i.e., the node-id of each node for which the teaser info is being displayed. So if the block view returns, say, 20 staff members, I get 20 different node IDs. So it cannot be used. I want the page node id within which the block view is being displayed, if you understand what I mean. Sorry for my english, not my native language, I hope it's clear what I mean. That page node id is what I 'm looking for. – thomas Jul 19 '18 at 19:08
  • I mean the {{ node.id }} in page.html.twig. This should be the one you are looking for. – 4k4 Jul 19 '18 at 19:15

Your Answer

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

Browse other questions tagged or ask your own question.