How to add code logic to Display Suite fields without PHP in Drupal 8? Twig? 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. #}
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(&amp;$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.