Commit feac53fd authored by John Fiala's avatar John Fiala
Browse files

Issue #1010850 by benjifisher: Update MigrateFieldHandler to work with migrate-7.x-2.4 and later.

parent 84f56b3f
......@@ -2,9 +2,39 @@
/**
* @file
* Support for migrate module
* Support for migrate module.
*
* With Migrate 2.4 or later, you can use the subfield syntax to set the title
* and attributes:
*
* @code
* $this->addFieldMapping('field_my_link', 'source_url');
* $this->addFieldMapping('field_my_link:title', 'source_title');
* $this->addFieldMapping('field_my_link:attributes', 'source_attributes');
* @endcode
*
* With earlier versions of Migrate, you must pass an arguments array:
*
* @code
* $link_args = array(
* 'title' => array('source_field' => 'source_title'),
* 'attributes' => array('source_field' => 'source_attributes'),
* );
* $this->addFieldMapping('field_my_link', 'source_url')
* ->arguments($link_args);
* @endcode
*/
/**
* Implements hook_migrate_api().
*/
function link_migrate_api() {
return array(
'api' => 2,
'field handlers' => array('MigrateLinkFieldHandler'),
);
}
class MigrateLinkFieldHandler extends MigrateFieldHandler {
public function __construct() {
$this->registerTypes(array('link_field'));
......@@ -24,6 +54,26 @@ class MigrateLinkFieldHandler extends MigrateFieldHandler {
return $arguments;
}
/**
* Implementation of MigrateFieldHandler::fields().
*
* @param $type
* The field type.
* @param $instance
* Instance info for the field.
* @param Migration $migration
* The migration context for the parent field. We can look at the mappings
* and determine which subfields are relevant.
* @return array
*/
public function fields($type, $instance, $migration = NULL) {
return array(
'title' => t('Subfield: The link title attribute'),
'attributes' => t('Subfield: The attributes for this link'),
'language' => t('Subfield: The language for the field'),
);
}
public function prepare($entity, array $field_info, array $instance, array $values) {
if (isset($values['arguments'])) {
$arguments = $values['arguments'];
......@@ -34,8 +84,9 @@ class MigrateLinkFieldHandler extends MigrateFieldHandler {
}
$language = $this->getFieldLanguage($entity, $field_info, $arguments);
$values = array_filter($values);
foreach($values as $delta => $value) {
foreach ($values as $delta => $value) {
$item = array();
if (isset($arguments['title'])) {
if (!is_array($arguments['title'])) {
......@@ -50,7 +101,6 @@ class MigrateLinkFieldHandler extends MigrateFieldHandler {
}
$item['url'] = $value;
$return[$language][$delta] = $item;
$delta++;
}
return isset($return) ? $return : NULL;
......
......@@ -1051,13 +1051,6 @@ function link_views_api() {
);
}
/**
* Implements hook_migrate_api().
*/
function link_migrate_api() {
return array('api' => 2);
}
/**
* Forms a valid URL if possible from an entered address.
* Trims whitespace and automatically adds an http:// to addresses without a protocol specified
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment