Correct way of defining "attr_source"

Created on 17 March 2020, almost 5 years ago
Updated 23 February 2023, almost 2 years ago

This module is exactly what I was looking for, because I'm trying to import a lot of links with classes attached.

But unfortunately I can't really get it going as I'd like. The links are getting migrated just fine, but I just can't get the classes imported correctly.

My source data is in a CSV file and looks like this:

link_field_URL;link_field_class
https://twitter.com/example/|https://www.youtube.com/example/;twitter|youtube
https://facebook.com/example/|https://www.instagram.com/example/;facebook|instagram

So what I'm trying to achieve is to import these links into a multiple value link field and attach the corresponding class to the link - so for example, the Link to Twitter should get the CSS class "twitter".

My migration.yml looks like this:

pseudo_class_helper_field:
    plugin: link_array_build
    source: link_field_class
  pseudo_link_helper_field:
    plugin: explode
    source: link_field_URL
    delimiter: '|'
  field_link_with_class:
    -
      plugin: skip_on_invalid_url
      method: process
      source: '@pseudo_link_helper_field'
    -
      plugin: field_link_generate
      attr_source: '@pseudo_class_helper_field'
    -
      plugin: field_link
      uri_scheme: 'https://'

The plugin "link_array_build" is in a custom module I wrote myself, which explodes the string with delimiter "|" and basically returns an array of the serialized values for the attributes, like this:

array(2) {
  [0]=>
  array(1) {
    ["class"]=>
    string(7) "twitter"
  }
  [1]=>
  array(1) {
    ["class"]=>
    string(7) "youtube"
  }
}

So the idea was that, if I have an array in every pseudo field, then this should get imported into two values in the link field, with an URL and the corresponding link class.

But the problem is - the attributes array is obviously not getting treated as multiple values, but as one value - so I end up with multiple URLs in the link field, but they don't have the correct serialized attributes array attached, but every value in the link field has all classes attached - which is invalid and doesn't work.

In my example, this results in two values like:

field_link_uri: https://twitter.com/example/
field_link_options: a:1:{s:10:"attributes";a:2:{i:0;a:0:{s:5:"class";s:7:"twitter";}i:1;a:1:{s:5:"class";s:7:"youtube";}}}

field_link_uri: https://www.youtube.com/example/
field_link_options: a:1:{s:10:"attributes";a:2:{i:0;a:0:{s:5:"class";s:7:"twitter";}i:1;a:1:{s:5:"class";s:7:"youtube";}}}

Expected result would be this:

field_link_uri: https://twitter.com/example/
field_link_options: a:1:{s:10:"attributes";a:1:{s:5:"class";s:7:"twitter";}}

field_link_uri: https://www.youtube.com/example/
field_link_options: a:1:{s:10:"attributes";a:1:{s:5:"class";s:7:"youtube";}}

I'm pretty stuck with this - can anyone help me out or tell me what I'm doing wrong?

Feature request
Status

Active

Version

1.5

Component

Code

Created by

🇩🇪Germany majusz

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.

Production build 0.71.5 2024