1

I've been working on a D7 to D9 migrate project, and encountered an issue around field_image's alt value.

The D7 site does not have all the field_image alt tag setup, so we'd like to use the imported Node Title as a replacement if the value was empty.

The migration config file works, all other fields are imported perfectly. Just the alt tag didn't make use of the replacement of '@title' instead (I saw other usage of @ syntax on other migration config files). All I saw was @title string within my alt field after importation.

How should I make it work? Thanks!

config/sync/migrate_plus.migration.upgrade_d7_node_complete_article.yml

langcode: en
status: true
dependencies: {  }
id: upgrade_d7_node_complete_article
class: Drupal\node\Plugin\migrate\D7NodeTranslation
field_plugin_method: null
cck_plugin_method: null
migration_tags:
  - 'Drupal 7'
  - Content
migration_group: migrate_drupal_7
label: 'Node complete (News)'
source:
  plugin: d7_node_complete
  node_type: article
process:
  nid:
    -
      plugin: get
      source: tnid
  vid:
    -
      plugin: get
      source: vid
  langcode:
    -
      plugin: default_value
      source: language
      default_value: und
  title:
    -
      plugin: get
      source: title
  field_image:
    -
      plugin: sub_process
      source: field_image
      process:
        target_id: fid
        alt:
          plugin: default_value
          source: alt
          default_value: '@title'
        title: title
        width: width
        height: height
destination:
  plugin: 'entity_complete:node'
  translations: true
  default_bundle: article
migration_dependencies:
  required:
    - upgrade_d7_user
    - upgrade_d7_node_type
    - upgrade_language
  optional:
    - upgrade_d7_field_instance

1 Answer 1

0

The @ notation can only be used where property names are entered.

Since it is not possible to write property names in default_value plugin's default_value, other methods must be considered.

For example, the following code should work as expected.

process:
  field_image:
    -
      plugin: sub_process
      source: field_image
      include_source: true
      process:
        _alt_or_title:
          # Extract non-empty values from alt and title
          - plugin: callback
            callable: array_filter
            source:
              - alt
              - source/title
          # Retrieve only the first value from the previous array
          - plugin: callback
            callable: array_shift

        target_id: fid
        alt: '@_alt_or_title'  # `@` can be used here (shorthand for `plugin: get`)
        title: title
        width: width
        height: height

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.