How to use multiple paragraph custom fields in twig field template in Drupal 8?

1.2k Views Asked by At

I have two fields in a custom paragraph type that I want to use together in a twig field template. I was able to access these properties from the page template, but from my understanding this is bad practice.

<a href="{{ content.field_link[0]['#url'] }}" target="_blank" class="btn">
   {{ content.field_button_text[0] }}
</a>

I was able to achieve this by using a pre-processor, but I'm not sure it's the best way.

function theme_preprocess_field__paragraph__field_link__homepage_hero(&$variables) {
   $paragraph = $variables['element']['#object'];
   kint($paragraph->get('field_button_text')->value);
}

Looking for the best practice approach, seems a bit arbitrary to choose one of the two fields and attach the other one.

1

There are 1 best solutions below

1
bdrupal On

Why do you need 2 fields for outputting the link URL and link Title? They already are present in the field type link as "URL" and "Link text".