How to display a Paragraph field with a condition

Nora: 2 weeks ago

Hello here is my problem, I have a paragrah type with an illimited value image field in which I store image(s) to display them in 200*200 but also a single Json file with mapping data linked to the image(s) in the paragraph.

But in the drupal conf I can not hide the json file name since it's stored in the same field, they would both be invisible (json and image).

However I'd like to display in the Frontpage view and so (/node/xxx) only the image(s) and not the json file name even though they are in the same paragraph field, is that possible directly in drupal configuration or do I have to make a template ?

Hudson: 2 weeks ago

Depending on how complex your logic is, you can either write it inside the paragraph preprocess function, as in:

function THEME-NAME_preprocess_paragraph_PARAGRAPH-TYPE($variables, $paragraph) {
  if(some_condition) {
    $variables['field_name'] = $paragraph->get('field_name')->value;
  // ...
  return $variables;

or write the same logic inside twig template if its simpler, as in:

{# paragraph--PARAGRAPH-TYPE.html.twig #}

{% if condition %}
  {{ content.custom_var }}
{% endif %}

I can elaborate and edit this answer if you are more specific.