3

Using Display Suite in Drupal 7 I can easily add PHP logic to any custom Display Suite Field. In Drupal 8 PHP is not allowed. How can I program logic in Display Suite fields in Drupal 8?

As an example, I'd like to use a simple if/then statement to show/hide a node field. I have tried embedding Twig into the field but it does not execute. It just renders as plain text.

This is my sample code {# This is a test comment. #}

1

Showing hiding fields in twig is pretty simple. You can check if a field has a value like so:

{% if node.field_foo is not empty %}
  {{ content.field_foo }}
{% endif %}

You cannot execute raw PHP from a twig file, nor can you inject Twig and parse it out through field formatters (probably not without a lot of work, anyway).

Note the difference between checking the node object and rendering the content array.

You want to do checks against the entity object, and never (in my opinion) the render array. In the above code, if the field has a value, then the condition is true. If you want to check the value for a specific string, it is likely better suited for a preprocess hook.

What if the twig template being rendered is a Paragraph, and not a Node template? Not much difference:

{% if paragraph.field_foo is not empty %}
  {{ content.field_foo }}
{% endif %}

If you need more muscle, I would advise moving your logic to a preprocess hook in mytheme.theme where you can use whatever PHP you want, and inject new variables into your Twig file.

function mytheme_preprocess_node(&$variables) {
  $variables['show_foo_field'] = FALSE;

  // if field ...
  // and maybe some more conditions?
  $variables['show_foo_field'] = TRUE;
  // end if...
}

Then in the twig template...

{% if show_foo_field %}
  {{ content.field_foo }}
{% endif %}

Now, you say you are using Display Suite - I personally don't inject condition or code through the UI like what you are asking. But one thing I do know about Display Suite is it (when I last looked) tends to hijack a majority of the preprocess calls - so check its docs or API for the right preprocess hook to call for nodes.

The current hooks for Display suite can be viewed in its .api.php file.

Definitely enable twig debug to be able to check what the Twig template naming pattern should be (suggestions) in the HTML source, and be aware of mytheme_preprocess_node potentially not firing. You will need to locate the correct DS hook.

  • I think the answer I needed (although not the answer I wanted to hear) is that I cannot parse Twig through a field formatter. Thanks for your additional, and comprehensive, advice. I will check out the Display Suite API. – Ben Stark Jun 27 '17 at 23:48
  • If you get a little lost in Display Suite, a good starting point is to setup a vanilla site without it and get a feel for theming under normal circumstances - then come back to Display Suite. When I first used it in 8.x, a lot of my preprocess hooks quit firing, and it threw me for a loop. Maybe you don't even need Display Suite - I find D8 theming easier than before. – Kevin Jun 27 '17 at 23:49
  • I think you're right. I may not even need DS in Drupal 8. At least, not for theme logic. It is still useful for overriding default field code and creating quick stacked layouts. – Ben Stark Jun 27 '17 at 23:52
  • Yeah, it can be used for RAD/prototyping - just be aware that normal theme calls (namely preprocess) and template suggestions that are in the Drupal documentation may not work when Display Suite is enabled. Just so you don't get confused later - it's not immediately clear at first without diving through the call stack and DS docs/code/api. – Kevin Jun 27 '17 at 23:53

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.