Link: support target attribute (_blank)

Created on 12 May 2021, about 4 years ago
Updated 2 June 2025, 29 days ago

Problem/Motivation

Hi,
my editor has a page with a "Link" field where it is possible to choose the link target (Allow the user to choose in the field settings).
In the destination website I have the same settings in the content type, but there is not this option in the Mapping of "Feeds importers", only Title and URL.

How can I import the target "_blank"? Any ideas?
Thank you very much.

Ste

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Feature request
Status

Active

Version

3.0

Component

Code

Created by

🇨🇭Switzerland steva1982

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇦🇺Australia marc.groth

    I had a similar requirement and I implemented it by following the instructions on this page: https://www.drupal.org/docs/contributed-modules/feeds/feeds-howtos/alter...

    In particular, these were the files I added/updated:

    1. EventSubscriber file

    I opted for the first option of the aforementioned link. My alterItems() function looked something like this:

    $link_target = $item->get('link_target');
    if (!empty($link_target)) {
      $options = [
        [
          'attributes' => [
            'target' => $link_target,
          ],
        ],
      ];
      $item->set('link_target', $options);
    }
    

    The 'options' data for the link field needs to be an associative array and in my case I only needed to update the 'target'... So I was able to create the attributes as above... But you could easily have multiple attributes in the CSV file (one per column for example) and then combine them using the above code as a guide/starting point.

    2. Feed config file

    In my CSV I had a column that was used for the target. I just needed to update my existing link field mapping in the config file and add the following line:

    options: link_target

    Where 'link_target' is the ID of the relevant 'Custom source' mapping config. So the full entry for the link field in the config file was as follows:

    -
        target: field_link
        map:
          uri: link
          title: ''
          options: link_target
        settings:
          language: null

    For some reason I had to add a 'Skip if empty' feeds tamper for the 'options' portion of the link... Otherwise I'd get PHP warnings.

    After that all I had to do was import configuration, clear the cache and import the feeds items. The attributes should then be updated as expected.

    Hopefully this helps anyone else who may be trying to achieve a similar thing!

Production build 0.71.5 2024