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