3

With the module "config pages" I've set some variables global to render in templates. In mytheme.theme I've added a function:

function mytheme_preprocess(array &$variables, $hook)
{
    $myConfigPage = \Drupal\config_pages\Entity\ConfigPages::config('config_name');
    $variables['my_settings'] = $myConfigPage->toArray();
}

In the config_pages I've created a link field. I would like to render them, but I get below output. How can I render the link in Twig?

  ^ array:3 [▼
      0 => array:3 [▼
        "uri" => "entity:node/186"
        "title" => "Title 3"
        "options" => []
      ]
      1 => array:3 [▼
        "uri" => "entity:node/189"
        "title" => "Title 1"
        "options" => []
      ]
      2 => array:3 [▼
        "uri" => "entity:node/195"
        "title" => "Title 2"
        "options" => []
      ]
    ]

Twig:

  {{ dd(my_settings.field_links) }}
  {% for link in my_settings.field_links %}
    <div class="field__item">
      {{ dd(link) }}
      {{ link.uri|render }}
      {{ link.title }}
    </div>
  {% endfor %}

None of above are working.

2 Answers 2

4

You can render a link with core's link function:

This helper function accepts as first parameter the text and as second parameter the uri

Examples:

{{ link(item.title, item.uri, { 'class':['foo', 'bar', 'baz']} ) }}

In your case, this might look like:

{% for link_item in my_settings.field_links %}
  <div class="field__item">
    {{ link(link_item['title'], link_item['uri']) }}
    // or
    {{ link("My link text", "entity:node/123") }}
    // or
    {{ link("My link text", "internal:/some/internal/path") }}
    // or
    {{ link("My link text", "route:my.module.route_name") }}
    // or
    {{ link("My link text", "https://drupal.stackexchange.com/") }}
  </div>
{% endfor %}

Alternatively, and preferably imo, you can just render the field from the config_page entity (as with any content entity).

For example, in your preprocess:

// Load your config page.
$my_config_page = \Drupal::entityTypeManager()
  ->getStorage('config_pages')
  ->load('my_config_page_identifier');
// Build the render array for my links per the config page's `default` view mode.
$vars['my_links'] = $my_config_page->field_links->view('default');

Then, in twig:

{{ my_links }}
5
  • Thanks. So far I understand, but I have "entity:node/123" as array variable. So in Twig I need to use the function 'split'. I was wondering if there isn't a better solution.... I don't like to use to much logic in twig...
    – Lance
    Jan 26, 2021 at 17:10
  • Yes there is. See my answer
    – sonfd
    Jan 26, 2021 at 17:13
  • 1
    What do you mean you need to use the function split? You can pass entity:node/123 straight to the link function, e.g. {{ link('My link text', 'entity:node/123') }}
    – sonfd
    Jan 26, 2021 at 18:39
  • I was confused about the drupal_url() function, but indeed the core link function is what I need. Thanks for the support!
    – Lance
    Jan 27, 2021 at 8:04
  • Also the update for the preprocess function is better. Config pages should have to update their documentation to use above.
    – Lance
    Jan 27, 2021 at 8:09
0
{% for link in my_settings.field_links %}
    <div class="field__item">
      {{ link.uri|render }} => {{ link.url|render }} or {{ link.0.url|render }}
      {{ link.title }}
    </div>
  {% endfor %}

use url instead uri as mentioned above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.