link.migrate.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. $values = array_filter($values);
  97. foreach ($values as $delta => $value) {
  98. $item = array();
  99. if (isset($arguments['title'])) {
  100. if (!is_array($arguments['title'])) {
  101. $item['title'] = $arguments['title'];
  102. }
  103. elseif (isset($arguments['title'][$delta])) {
  104. $item['title'] = $arguments['title'][$delta];
  105. }
  106. }
  107. if (isset($arguments['attributes'])) {
  108. if (is_array($arguments['attributes']) && isset($arguments['attributes'][$delta])) {
  109. $item['attributes'] = $arguments['attributes'][$delta];
  110. }
  111. else {
  112. $item['attributes'] = $arguments['attributes'];
  113. }
  114. }
  115. $item['url'] = $value;
  116. if (is_array($language)) {
  117. $current_language = $language[$delta];
  118. }
  119. else {
  120. $current_language = $language;
  121. }
  122. $return[$current_language][$delta] = $item;
  123. }
  124. return isset($return) ? $return : NULL;
  125. }
  126. }