-
Notifications
You must be signed in to change notification settings - Fork 0
/
link.migrate.inc
139 lines (126 loc) · 3.53 KB
/
link.migrate.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* @file
* Support for migrate module.
*
* With Migrate 2.4 or later, you can use the subfield syntax to set the title
* and attributes:
*
* @code
* $this->addFieldMapping('field_my_link', 'source_url');
* $this->addFieldMapping('field_my_link:title', 'source_title');
* $this->addFieldMapping('field_my_link:attributes', 'source_attributes');
*
* # With earlier versions of Migrate, you must pass an arguments array:
*
* $link_args = array(
* 'title' => array('source_field' => 'source_title'),
* 'attributes' => array('source_field' => 'source_attributes'),
* );
* $this->addFieldMapping('field_my_link', 'source_url')
* ->arguments($link_args);
* @endcode
*/
if (!class_exists('MigrateFieldHandler')) {
return;
}
/**
* Implements hook_migrate_api().
*/
function link_migrate_api() {
return array(
'api' => 2,
'field handlers' => array('MigrateLinkFieldHandler'),
);
}
// @codingStandardsIgnoreLine
class MigrateLinkFieldHandler extends MigrateFieldHandler {
/**
* Construct.
*/
public function __construct() {
$this->registerTypes(array('link_field'));
}
/**
* Arguments.
*/
public static function arguments($title = NULL, $attributes = NULL, $language = NULL) {
$arguments = array();
if (!is_null($title)) {
$arguments['title'] = $title;
}
if (!is_null($attributes)) {
$arguments['attributes'] = $attributes;
}
if (!is_null($language)) {
$arguments['language'] = $language;
}
return $arguments;
}
/**
* Implementation of MigrateFieldHandler::fields().
*
* @param array $type
* The field type.
* @param array $instance
* Instance info for the field.
* @param Migration $migration
* The migration context for the parent field. We can look at the mappings
* and determine which subfields are relevant.
*
* @return array
* Array with values.
*
* @codingStandardsIgnoreStart
*/
public function fields($type, $instance, $migration = NULL) {
// @codingStandardsIgnoreEnd
return array(
'title' => t('Subfield: The link title attribute'),
'attributes' => t('Subfield: The attributes for this link'),
'language' => t('Subfield: The language for the field'),
);
}
/**
* Prepare.
*/
public function prepare($entity, array $field_info, array $instance, array $values) {
if (isset($values['arguments'])) {
$arguments = $values['arguments'];
unset($values['arguments']);
}
else {
$arguments = array();
}
$language = $this->getFieldLanguage($entity, $field_info, $arguments);
$values = array_filter($values);
foreach ($values as $delta => $value) {
$item = array();
if (isset($arguments['title'])) {
if (!is_array($arguments['title'])) {
$item['title'] = $arguments['title'];
}
elseif (isset($arguments['title'][$delta])) {
$item['title'] = $arguments['title'][$delta];
}
}
if (isset($arguments['attributes'])) {
if (is_array($arguments['attributes']) && isset($arguments['attributes'][$delta])) {
$item['attributes'] = $arguments['attributes'][$delta];
}
else {
$item['attributes'] = $arguments['attributes'];
}
}
$item['url'] = $value;
if (is_array($language)) {
$current_language = $language[$delta];
}
else {
$current_language = $language;
}
$return[$current_language][$delta] = $item;
}
return isset($return) ? $return : NULL;
}
}