name.migrate.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * @file
  4. * Support for migration into Name fields.
  5. */
  6. /**
  7. * Allows using Name fields as Migrate destinations.
  8. *
  9. * The primary value passed to this field handler must be the 'given' component.
  10. *
  11. * Example usage:
  12. * @code
  13. * // The given component should be mapped as the primary value.
  14. * $this->addFieldMapping('field_name', 'profile_first_name');
  15. * // Now map other name components.
  16. * $this->addFieldMapping('field_name:title', 'profile_title');
  17. * $this->addFieldMapping('field_name:middle', 'profile_middle_name');
  18. * $this->addFieldMapping('field_name:family', 'profile_last_name');
  19. * @endcode
  20. *
  21. * @see MigrateDefaultFieldHandler
  22. */
  23. class MigrateNameHandler extends MigrateFieldHandler {
  24. protected $primaryColumn = 'given';
  25. /**
  26. * Declares the types of fields used.
  27. */
  28. public function __construct() {
  29. $this->registerTypes(array('name'));
  30. }
  31. /**
  32. * Implements MigrateFieldHandler::fields().
  33. *
  34. * Returns all available fields except for 'given',
  35. * which is the primary column.
  36. *
  37. * @param string $field_type
  38. * The field type.
  39. * @param array $field_instance
  40. * The field instance.
  41. *
  42. * @return array
  43. * The available fields.
  44. */
  45. public function fields($field_type, $field_instance) {
  46. $field_info = field_info_field($field_instance['field_name']);
  47. $fields = array();
  48. foreach ($field_info['columns'] as $column_name => $column_info) {
  49. // The first column is the primary value, which is mapped directly to
  50. // the field name - so, don't include it here among the subfields.
  51. if ($column_name != $this->primaryColumn) {
  52. $fields[$column_name] = empty($column_info['description']) ?
  53. $column_name : $column_info['description'];
  54. }
  55. }
  56. return $fields;
  57. }
  58. /**
  59. * Implements MigrateFieldHandler::prepare().
  60. */
  61. public function prepare($entity, array $field_info, array $instance, array $values) {
  62. // Get arguments, if any.
  63. $arguments = array();
  64. if (isset($values['arguments'])) {
  65. $arguments = array_filter($values['arguments']);
  66. unset($values['arguments']);
  67. }
  68. // Get language.
  69. $language = $this->getFieldLanguage($entity, $field_info, $arguments);
  70. // Get name-specific field settings.
  71. $components = array_filter($field_info['settings']['components']);
  72. // Setup the standard Field API array for saving.
  73. $delta = 0;
  74. foreach ($values as $value) {
  75. // Handle multivalue arguments (especially for subfields).
  76. $delta_arguments = array();
  77. foreach ($arguments as $name => $argument) {
  78. if (is_array($argument) && isset($argument[$delta])) {
  79. $delta_arguments[$name] = $argument[$delta];
  80. }
  81. else {
  82. $delta_arguments[$name] = $argument;
  83. }
  84. }
  85. $item = array($this->primaryColumn => $value) + array_intersect_key($delta_arguments, $field_info['columns']);
  86. $item = array_intersect_key($item, $components);
  87. if (array_filter($item)) {
  88. $return[$language][] = $item;
  89. }
  90. $delta++;
  91. }
  92. return isset($return[$language]) ? $return : NULL;
  93. }
  94. }