Link field type: Add is_external computed property

Created on 19 September 2024, about 2 months ago
Updated 20 September 2024, about 2 months ago

Problem/Motivation

LinkItem field type has 3 properties, all stored:

$properties['uri'] = DataDefinition::create('uri') ->setLabel(new TranslatableMarkup('URI'));
$properties['title'] = DataDefinition::create('string')->setLabel(new TranslatableMarkup('Link text'));
$properties['options'] = MapDataDefinition::create()->setLabel(new TranslatableMarkup('Options'));

And implements LinkItemInterface::isExternal().

Being a PHP method, this information (is the URL external or not?) is a low-level implementation, usable in custom PHP code, but not "known" by Drupal and not usable by API leveraging the field properties:

  • Field Formatters, especially when they are generic (applied to many field types)
  • Views Filters
  • Views Sorts
  • Content translation (when we pick the translatable properties in /admin/config/regional/content-language)
  • Serializers
  • ...

Proposed resolution

Add a is_external boolean computed property to LinkItem, calling LinkItemInterface::isExternal(), so we can use this information with Drupal API, in the admin pages (manage display, views...)

User interface changes

None

API changes

Only small addition, no breaks, no removal.

Data model changes

The new property is computed, so no change for data stored in DB.

Feature request
Status

Active

Version

11.0 🔥

Component
Link 

Last updated about 23 hours ago

Created by

🇫🇷France pdureau Paris

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @pdureau
  • First commit to issue fork.
  • Pipeline finished with Failed
    about 2 months ago
    Total: 538s
    #289039
  • 🇮🇳India KumudB Ahmedabad

    After checking out the branch where @walli made changes, I applied a custom hook in the module file. As a result, the is_external field now appears as a filter option. I have attached a screenshot for reference and custom module code.

    Steps to Reproduce:
    Step 1: Set Up a Content Type with a Link Field
    1.Create a Custom Content Type:

    • Go to Structure -> Content types.
    • Click on Add content type and create a new content type (e.g., "Article with Link").
    • Add necessary fields like "Title" and "Body."

    2. Add a Link Field:

    • While editing the content type, click on the Manage fields tab.
    • Add a new field, select Link as the field type, and name it something like "External Link."

    3.Create Content:

    • Create a few nodes for this content type.
    • Populate the link field with both external links (e.g., https://example.com) and internal links (e.g., /node/1).

    Step 2: Check for Limitations in Default Views
    1.Create a View:

    • Go to Structure -> Views.
    • Click on Add view.
    • Create a new view based on the "Article with Link" content type.
    • In the view, try to add filters or sorts based on whether the link is external or internal.

    2.Observation:

    • Notice that there is no built-in way to filter or sort content based on whether the link is external or internal.

    The MR code remian as it is and just apply this hook

    /**
     * Implements hook_views_data_alter().
     */
    function customTest_views_data_alter(array &$data) {
      $field_name = 'field_external'; // Replace with your link field's machine name.
    
      if (isset($data["node__{$field_name}"])) {
        // Define the custom field handler.
        $data["node__{$field_name}"]['is_external'] = [
          'title' => t('Is External'),
          'help' => t('Indicates if the link is external.'),
          'field' => [
            'id' => 'is_external_field', // Use the ID from the @ViewsField annotation in IsExternalField.
          ],
          'filter' => [
            'id' => 'boolean',
          ],
          'sort' => [
            'id' => 'standard',
          ],
          'group' => t('Custom'),
        ];
      }
    }

  • 🇬🇧United Kingdom andrew.farquharson

    I see no test in the MR. A test is required.

  • 🇫🇷France pdureau Paris

    Indeed Oily. We already know about that but adding the tag is a good idea.

  • 🇫🇷France pdureau Paris

    Thanks you @kumudb for the proposal. We will have a look and credit you if we integrate it into the MR.

Production build 0.71.5 2024