link.migrate.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. *
  14. * # With earlier versions of Migrate, you must pass an arguments array:
  15. *
  16. * $link_args = array(
  17. * 'title' => array('source_field' => 'source_title'),
  18. * 'attributes' => array('source_field' => 'source_attributes'),
  19. * );
  20. * $this->addFieldMapping('field_my_link', 'source_url')
  21. * ->arguments($link_args);
  22. * @endcode
  23. */
  24. if (!class_exists('MigrateFieldHandler')) {
  25. return;
  26. }
  27. /**
  28. * Implements hook_migrate_api().
  29. */
  30. function link_migrate_api() {
  31. return array(
  32. 'api' => 2,
  33. 'field handlers' => array('MigrateLinkFieldHandler'),
  34. );
  35. }
  36. // @codingStandardsIgnoreLine
  37. class MigrateLinkFieldHandler extends MigrateFieldHandler {
  38. /**
  39. * Construct.
  40. */
  41. public function __construct() {
  42. $this->registerTypes(array('link_field'));
  43. }
  44. /**
  45. * Arguments.
  46. */
  47. public static function arguments($title = NULL, $attributes = NULL, $language = NULL) {
  48. $arguments = array();
  49. if (!is_null($title)) {
  50. $arguments['title'] = $title;
  51. }
  52. if (!is_null($attributes)) {
  53. $arguments['attributes'] = $attributes;
  54. }
  55. if (!is_null($language)) {
  56. $arguments['language'] = $language;
  57. }
  58. return $arguments;
  59. }
  60. /**
  61. * Implementation of MigrateFieldHandler::fields().
  62. *
  63. * @param array $type
  64. * The field type.
  65. * @param array $instance
  66. * Instance info for the field.
  67. * @param Migration $migration
  68. * The migration context for the parent field. We can look at the mappings
  69. * and determine which subfields are relevant.
  70. *
  71. * @return array
  72. * Array with values.
  73. *
  74. * @codingStandardsIgnoreStart
  75. */
  76. public function fields($type, $instance, $migration = NULL) {
  77. // @codingStandardsIgnoreEnd
  78. return array(
  79. 'title' => t('Subfield: The link title attribute'),
  80. 'attributes' => t('Subfield: The attributes for this link'),
  81. 'language' => t('Subfield: The language for the field'),
  82. );
  83. }
  84. /**
  85. * Prepare.
  86. */
  87. public function prepare($entity, array $field_info, array $instance, array $values) {
  88. if (isset($values['arguments'])) {
  89. $arguments = $values['arguments'];
  90. unset($values['arguments']);
  91. }
  92. else {
  93. $arguments = array();
  94. }
  95. $language = $this->getFieldLanguage($entity, $field_info, $arguments);
  96. // URLs are numeric array elements, keyed by delta. As those elements don't
  97. // exist for empty URLs they are allowed and it's up to the developer what
  98. // data to migrate, add elements for those URLs up to the maximum delta
  99. // found.
  100. //
  101. // First of all, determine the maximum delta from non-empty URLs.
  102. $max_delta = max(array_merge(array(0), array_keys($values)));
  103. // Next, determine the max delta from arguments.
  104. foreach ($arguments as $key => $value) {
  105. if (is_array($value)) {
  106. $max_delta = max($max_delta, max(array_keys($value)));
  107. }
  108. }
  109. // Finally, add missing elements for empty URLs.
  110. for ($delta = 0; $delta <= $max_delta; $delta++) {
  111. if (!isset($values[$delta])) {
  112. $values[$delta] = '';
  113. }
  114. }
  115. foreach ($values as $delta => $value) {
  116. $item = array();
  117. if (isset($arguments['title'])) {
  118. if (!is_array($arguments['title'])) {
  119. $item['title'] = $arguments['title'];
  120. }
  121. elseif (isset($arguments['title'][$delta])) {
  122. $item['title'] = $arguments['title'][$delta];
  123. }
  124. }
  125. if (isset($arguments['attributes'])) {
  126. if (is_array($arguments['attributes']) && isset($arguments['attributes'][$delta])) {
  127. $item['attributes'] = $arguments['attributes'][$delta];
  128. }
  129. else {
  130. $item['attributes'] = $arguments['attributes'];
  131. }
  132. }
  133. $item['url'] = $value;
  134. if (is_array($language)) {
  135. $current_language = $language[$delta];
  136. }
  137. else {
  138. $current_language = $language;
  139. }
  140. $return[$current_language][$delta] = $item;
  141. }
  142. return isset($return) ? $return : NULL;
  143. }
  144. }