Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract Url & Title from link field in Drupal 8?

Tags:

I'm trying to retrieve the URL and the Title values of a Link field in Drupal 8.

In my custom controller, I retrieve the nodes with:

$storage = \Drupal::entityManager()->getStorage('node');
$nids = $storage->getQuery()
    ->condition('type', 'partners')
    ->condition('status', 1)
    ->execute();

$partners = $storage->loadMultiple($nids);

When I loop throught all my nodes, to preprocess vars I'll give to my view, I would like to retrieve the URL and the Title.

foreach ($partners as $key => $partner) {
    $variables['partners'][] = array(
        'image' => $partner->field_logo->entity->url(),
        'url'   => $partner->field_link->value, // Can't retrieve values of link field
    );
}

Unfortunately, I don't found how to retrieve the URL and the Title of field_link.

Thanks for your help.

like image 880
Kevin Wenger Avatar asked Dec 28 '15 19:12

Kevin Wenger


People also ask

How do I extract a URL?

Extract the Hyperlink URL with a Keyboard ShortcutSelect the cell containing the hyperlink and press Ctrl + K to open the Edit Hyperlink menu. This will open the Edit Hyperlink menu and you can copy and paste the URL from the Address just like before.

How do I extract text from a URL?

Click and drag to select the text on the Web page you want to extract and press “Ctrl-C” to copy the text. Open a text editor or document program and press “Ctrl-V” to paste the text from the Web page into the text file or document window. Save the text file or document to your computer.

How do I separate text from URL in Excel?

Click Insert > Module, and paste the following code in the Module Window. 3. Save the code and close the window, select a blank cell to type this formula =GetURL(A2) (A2 is the cell that the hyperlink in), and press Enter button. You can see the real hyperlink address is extracted.


1 Answers

At the node level, inside your Twig template you can use:

{{ content.field_link.0['#url'] }} & {{ content.field_link.0['#title'] }}

For example:

<a href="{{ content.field_link.0['#url'] }}">{{ content.field_link.0['#title'] }}</a>

field_link being the name of the link field in question.

like image 147
Mark Conroy Avatar answered Nov 07 '22 18:11

Mark Conroy