hehe, this is the shit in terms of getting URL from field (Twig version of getting that URL)
If you want the URL (and title) that is in a field you have to get to object of class:
Drupal\Core\Url
If you got the node by drupal_entity (Twig Tweaks) then you have: (I'll use "tab" as that node) tab['#node'].field_link
This gives you by using:
tab['#node'].field_link.uri -> the URI (URI is not URL)
tab['#node'].field_link.title -> the title
but, that's not the end.
it's better to acess Drupal\link\Plugin\Field\FieldType\LinkItem by:
tab['#node'].field_link.get(0)
tab['#node'].field_link[0]
(both do the same)
then you can access what's most interesting:
tab['#node'].field_tab_link[0].getUrl() a Drupal\Core\Url object.
You can access this object also by using this line below, from within that's node template file: content.field_link[0]['#url'] (this is also Drupal\Core\Url object)
That object has methods:
- isExternal() -> is link to outside (ie. http://cnn.com)
- isRouted() -> is link to inside of your site
- getRouteName() -> name of the route
- getRouteParameters() -> which gives you an array of values, here(we get ID of that node) you would have ['node' => ID]
You should use it like that:
{% set tabURL = tab['#node'].field_link[0].getUrl() %}
{% if tabURL.isExternal() %}
{% set link_path = tab['#node'].field_tab_link.uri %}
{% elseif tabURL.isRouted() %}
{% set link_path = path(tabURL.getRouteName(), tabURL.getRouteParameters()) %} {# /node/12 #}
{% endif %}