I have a content type that has a reference to a webform. In the template for this content type, I'd like to check if the referenced webform is open or closed to conditionally display some content.

I have a content.field_ballot[0], but this appears to just be a render array without access to the underlying webform settings.

Is it possible to access it through this reference? Or should I be investigating a different route?



1 Answer 1


You should avoid any complex PHP code in template files. I recommend you use a THEME_preprocess_node_MY_CONTENT_TYPE(&$vars) hook to perform your checks, then pass a variable to the template file for your basic validation. In the preprocess function you pass the variable using $vars['my_custom_variable'] = XYZ and in the template you can call the variable with $my_custom_variable.

Make sure Drupal looks for content type node templates, or you can just use the default THEME_preprocess_node(&$vars) and put a check for your content type in there to keep the full validation code from running. https://www.drupal.org/node/337022

Stackoverflow discussion along similar lines https://stackoverflow.com/questions/40645297/pass-variable-to-preprocess-node-function-for-use-in-node-template

  • Thanks! I was able to pull the status in a preprocess_node function via $variables['node']->get('field_ballot')->entity->isOpen() (with additional conditional checks to make sure the field is populated).
    – j.szwedko
    Mar 8, 2018 at 23:30
  • Another approach that's more object-oriented would be to write your own field formatter, extend the default one and then add your additional stuff there. That only works if your additional content is directly above or below the webform as you can just control that part of the output.
    – Berdir
    Mar 9, 2018 at 2:31

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.