link.migrate.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * @file
  4. * Support for migrate module.
  5. *
  6. * With Migrate 2.4 or later, you can use the subfield syntax to set the title
  7. * and attributes:
  8. *
  9. * @code
  10. * $this->addFieldMapping('field_my_link', 'source_url');
  11. * $this->addFieldMapping('field_my_link:title', 'source_title');
  12. * $this->addFieldMapping('field_my_link:attributes', 'source_attributes');
  13. * @endcode
  14. *
  15. * With earlier versions of Migrate, you must pass an arguments array:
  16. *
  17. * @code
  18. * $link_args = array(
  19. * 'title' => array('source_field' => 'source_title'),
  20. * 'attributes' => array('source_field' => 'source_attributes'),
  21. * );
  22. * $this->addFieldMapping('field_my_link', 'source_url')
  23. * ->arguments($link_args);
  24. * @endcode
  25. */
  26. /**
  27. * Implements hook_migrate_api().
  28. */
  29. function link_migrate_api() {
  30. return array(
  31. 'api' => 2,
  32. 'field handlers' => array('MigrateLinkFieldHandler'),
  33. );
  34. }
  35. class MigrateLinkFieldHandler extends MigrateFieldHandler {
  36. public function __construct() {
  37. $this->registerTypes(array('link_field'));
  38. }
  39. static function arguments($title = NULL, $attributes = NULL, $language = NULL) {
  40. $arguments = array();
  41. if (!is_null($title)) {
  42. $arguments['title'] = $title;
  43. }
  44. if (!is_null($attributes)) {
  45. $arguments['attributes'] = $attributes;
  46. }
  47. if (!is_null($language)) {
  48. $arguments['language'] = $language;
  49. }
  50. return $arguments;
  51. }
  52. /**
  53. * Implementation of MigrateFieldHandler::fields().
  54. *
  55. * @param $type
  56. * The field type.
  57. * @param $instance
  58. * Instance info for the field.
  59. * @param Migration $migration
  60. * The migration context for the parent field. We can look at the mappings
  61. * and determine which subfields are relevant.
  62. * @return array
  63. */
  64. public function fields($type, $instance, $migration = NULL) {
  65. return array(
  66. 'title' => t('Subfield: The link title attribute'),
  67. 'attributes' => t('Subfield: The attributes for this link'),
  68. 'language' => t('Subfield: The language for the field'),
  69. );
  70. }
  71. public function prepare($entity, array $field_info, array $instance, array $values) {
  72. if (isset($values['arguments'])) {
  73. $arguments = $values['arguments'];
  74. unset($values['arguments']);
  75. }
  76. else {
  77. $arguments = array();
  78. }
  79. $language = $this->getFieldLanguage($entity, $field_info, $arguments);
  80. $values = array_filter($values);
  81. foreach ($values as $delta => $value) {
  82. $item = array();
  83. if (isset($arguments['title'])) {
  84. if (!is_array($arguments['title'])) {
  85. $item['title'] = $arguments['title'];
  86. }
  87. elseif (isset($arguments['title'][$delta])) {
  88. $item['title'] = $arguments['title'][$delta];
  89. }
  90. }
  91. if (isset($arguments['attributes'])) {
  92. if (is_array($arguments['attributes']) && isset($arguments['attributes'][$delta])) {
  93. $item['attributes'] = $arguments['attributes'][$delta];
  94. }
  95. else {
  96. $item['attributes'] = $arguments['attributes'];
  97. }
  98. }
  99. $item['url'] = $value;
  100. $return[$language][$delta] = $item;
  101. }
  102. return isset($return) ? $return : NULL;
  103. }
  104. }