0
votes

I've been attempting to solve this issue for the past few hours but haven't gotten any closer to a resolution. I'm attempting to return the string value for a link target attribute inside of a D8 paragraph template:

enter image description here

When I do a var dump on the field using machine name (field_link_cta), doing the following

{{ dump(content.field_link_cta['#items'].getValue()) }} I get the resulting return values:

enter image description here

I can traverse the variables to return the title ok, but for some reason can't get to the link attributes?

{{ content.field_link_cta[0]['#title'] }} - Works, returns value.

{{ content.field_link_cta[0]['#options']['#attributes']['#target'] }} - Does not work, returns no value.

What am I missing here? Given that I've stepped through the variables listed in the variable dump, shouldn't the 'target' attribute string be returned, exactly like the title? Is there a better way of checking context here for this variable?

Any pointers would be greatly appreciated.

Thanks for your help!

Mark.

1

1 Answers

2
votes

There are two options:

Option 1 (better):

{{ content.field_link_cta.0['#options']['attributes']['target'] }}

Option 2:

{{ content.field_link_cta['#items'].getValue().0['options']['attributes']['target']) }}

Explanation:

You did {{ dump(content.field_link_cta['#items'].getValue()) }} and you can see that options attributes and target array keys do not have #. However you did never use this option (Option 2) except for dump.

It would be better to do {{ dump(content.field_link_cta[0]) }} and you would see different dump, something like:

array (size=4)
  '#type' => string 'link' (length=4)
  '#title' => string 'link/text' (length=9)
  '#options' => 
    array (size=1)
      'attributes' => 
        array (size=3)
          'target' => string '_blank' (length=6)
          'rel' => string 'rel' (length=3)
          'class' => 
            array (size=1)
              ...

Here it is clearly that title array key has # and this is why {{ content.field_link_cta[0]['#title'] }} works.

Attributes and target array keys do not have # and this is why {{ content.field_link_cta[0]['#options']['#attributes']['#target'] }} does not work and correct code would be the one displayed under "Option 1".