Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
Migrate provides MigrateDefaultFieldHandler, which handles most fields pretty well. However, one oddity that I haven't quite figured out a reason for is that in order to set subfields (individual parts of a field), you have to pass arguments like this:
<?php
// Example from the Name module.
$arguments = array(
'title' => array('source_field' => 'profile_title'),
'middle' => array('source_field' => 'profile_middle_name'),
'family' => array('source_field' => 'profile_last_name'),
);
// The given component should be passed in as the primary value, additional components must be passed as arguments.
$this->addFieldMapping('field_name', 'profile_first_name')
->arguments($arguments);
// Since the excerpt is mapped via an argument, add a null mapping so it's
// not flagged as unmapped.
$this->addFieldMapping(NULL, 'profile_title');
$this->addFieldMapping(NULL, 'profile_middle_name');
$this->addFieldMapping(NULL, 'profile_last_name');
?>
This is really ungainly- for instance, you have to add the hackish dummy mappings at the end to stop the field from getting flagged as unmapped.
Why can't we simply map subfields directly, like so:
<?php
$this->addFieldMapping('field_name:given', 'profile_first_name');
$this->addFieldMapping('field_name:family', 'profile_last_name');
// etc...
?>
Having to use arguments instead of this relatively simple mapping is counterintuitive.
Comment | File | Size | Author |
---|---|---|---|
#4 | migrate-2223095-4.patch | 1.55 KB | Dane Powell |
Comments
Comment #1
Dane Powell CreditAttribution: Dane Powell commentedComment #2
Dane Powell CreditAttribution: Dane Powell commentedApparently I've overlooked something, because I just tested this and it turns out you can map directly to field parts / subfields.
Maybe the problem here is just a lack of documentation. I think we should add a docblock to MigrateDefaultFieldHandler better describing how to map field parts like this, and if there's any other user documentation out there, make sure it's up-to-date too.
Would you accept a patch for this?
Comment #3
13rac1 CreditAttribution: 13rac1 commentedThe documentation seems straightforward: https://drupal.org/node/1133448
A patch for code docs would be go though!
Comment #4
Dane Powell CreditAttribution: Dane Powell commentedThanks for the link to that documentation. I think I was confused because all of the handlers I was dealing with were a bit older, and included outdated docs.
I updated the documentation page to clarify mapping subfields just a little. Here's a patch the cleans up the docs in code.
Comment #5
mikeryanThe subfield support has nothing specifically to do with MigrateDefaultFieldHandler, it's a general feature of field mappings (not field handlers) implemented in Migration::applyMappings(). Migration::addFieldMapping() would probably be the most useful place for code documentation, since that's the call where you actually use that syntax.
Comment #6
pifagor