youtube.migrate.inc
2.48 KB
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
<?php
/**
* @file
* YouTube Field support for use with the migrate module.
*/
/**
* Implements hook_migrate_api().
*/
function youtube_migrate_api() {
$api = array(
'api' => 2,
'field handlers' => array('MigrateYoutubeFieldHandler'),
);
return $api;
}
class MigrateYoutubeFieldHandler extends MigrateFieldHandler {
/**
* Declares the type(s) of fields used.
*/
public function __construct() {
$this->registerTypes(array('youtube'));
}
/**
* Arguments for a YouTube field migration.
*
* @param string $input
* The URL of the YouTube video. If a value is not supplied, this will be
* constructed from the $video_id.
*
* @return array
* An array of the defined variables in this scope.
*/
static function arguments($input = NULL) {
return get_defined_vars();
}
/**
* Implementation of MigrateFieldHandler::fields().
*
* @param $type
* The field type.
* @param $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
*/
public function fields($type, $instance, $migration = NULL) {
return array(
'input' => t('Subfield: The full YouTube video URL'),
);
}
/**
* Converts incoming data to the proper format for YouTube fields.
*
* @param object $entity
* The destination entity which will hold the field arrays.
* @param array $field_info
* Metadata for the YouTube field being populated.
* @param array $instance
* Metadata for this instance of the YouTube field being populated.
* @param array $values
* Array of YouTube values to be fielded.
*
* @return array|null
* An array of YouTube fields.
*/
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();
$video_id = youtube_get_video_id($value);
if (!empty($video_id)) {
$item['input'] = $value;
$item['video_id'] = $video_id;
}
$return[$language][$delta] = $item;
}
return isset($return) ? $return : NULL;
}
}