Extract Url & Title from link field in Drupal 8?

Extract Url & Title from link field in Drupal 8?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Extracting URL and Title from a Link Field in Drupal 8

When working with custom field types like field_link in Drupal 8, developers often encounter a common frustration: the raw data structure doesn't immediately expose the properties you expect. In many cases, accessing $entity->field_name->value returns a structured array rather than a simple string or URL object. This discrepancy stems from how Drupal wraps complex field types to handle both display and storage logic internally.

To extract the actual URL and title associated with a Link field, you must understand that the field value is not a direct property but an entity wrapper. Specifically, for field_link, the value returned by $entity->field_name->value is typically an associative array containing keys such as #uri, #title, and sometimes #description.

Accessing these values requires navigating this nested structure correctly. Instead of expecting a direct URL object, you should look for the '#uri' key within the value array. If your field configuration allows for a custom title, that information resides under the '#title' key.

Here is how you can modify your preprocessing loop to extract these specific values:

foreach ($partners as $key => $partner) {
    // Access the raw value of the link field
    $linkValue = $partner->field_link->value;

    // Check if the field actually has data before accessing properties
    if (!empty($linkValue)) {
        $extractedUrl = is_array($linkValue) ? $linkValue['#uri'] : null;
        $extractedTitle = is_array($linkValue) ? $linkValue['#title'] : null;

        // Handle cases where the URI might be an array in some configurations
        if (is_array($extractedUrl)) {
            $extractedUrl = url('none', [
                'path' => $extractedUrl,
            ]);
        }

        $variables['partners'][] = array(
            'image'   => $partner->field_logo->entity->url(),
            'url'     => $extractedUrl ? $extractedUrl->toString() : '#',
            'title'   => $extractedTitle ?: null,
        );
    } else {
        // Handle empty link fields gracefully
        $variables['partners'][] = array(
            'image'   => $partner->field_logo->entity->url(),
            'url'     => '#',
            'title'   => null,
        );
    }
}

The core challenge here is that the Link field type in Drupal 8 stores its data as an internal entity. When you access the .value property directly, you are retrieving the stored data representation, which includes metadata keys prefixed with #. These keys are not automatically exposed as top-level properties like you would find on a standard text or image field.

One potential pitfall is assuming that the #uri value is always a simple string. Depending on your module configuration and how the link was entered in the UI, the URI might be stored as an array containing path components. This is why checking the type of the extracted URL and potentially wrapping it with url() or converting it to a string via toString() is crucial before assigning it to your view variables.

Additionally, always include a null check or an empty check for the field value itself. In scenarios where a node exists but the link field was left empty during content creation, attempting to access $linkValue['#uri'] directly will throw a fatal error because the array keys simply do not exist.

If you are building a theme that needs to display these links dynamically, passing the extracted url and title into your view template allows you to render them using standard Twig logic:

{% if partner.title %}
  <a href="{{ partner.url }}" title="{{ partner.title }}">{{ partner.title }}</a>
{% endif %}

This approach ensures that your theme remains robust even when the underlying field data structure varies slightly between different content types or configurations. By explicitly handling the array structure of the field_link value, you avoid brittle code that breaks as soon as Drupal's internal storage format shifts in a minor patch.

Our network