-
Notifications
You must be signed in to change notification settings - Fork 8
/
metatag.migrate.inc
130 lines (116 loc) · 2.91 KB
/
metatag.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
<?php
/**
* @file
* Metatag support for Migrate.
*/
if (!class_exists('MigrateDestinationHandler')) {
return;
}
/**
* Basic usage of the Migrate integration.
*
* This example assumes the custom module's name is "example_migrate".
*
* example_migrate.inc:
*
* class MetatagTestMigration extends DynamicMigration {
*
* public function __construct() {
* parent::__construct();
*
* $this->description = t('Migrate test.');
*
* $this->map = new MigrateSQLMap(
* $this->machineName,
* array(
* 'id' => array(
* 'type' => 'varchar',
* 'not null' => TRUE,
* 'length' => 254,
* 'description' => 'ID of record.',
* ),
* ),
* MigrateDestinationNode::getKeySchema()
* );
*
* $this->source = new MigrateSourceCSV(
* drupal_get_path('module', 'example_migrate') . '/sample.csv',
* array(),
* array('header_rows' => TRUE)
* );
*
* $this->destination = new MigrateDestinationNode('article');
*
* $this->addFieldMapping('metatag_description', 'description');
* $this->addFieldMapping('metatag_keywords', 'keywords');
* }
* }
*
* example_migrate.migrate.inc:
*
* /**
* * Implements hook_migrate_api().
* * /
* function example_migrate_migrate_api() {
* $api = array(
* 'api' => 2,
* 'migrations' => array(
* 'MetatagTest' => array('class_name' => 'MetatagTestMigration'),
* ),
* );
*
* return $api;
* }
*/
/**
* Implements hook_migrate_api().
*/
function metatag_migrate_api() {
$api = array(
'api' => 2,
'destination handlers' => array(
'MigrateMetatagHandler',
),
);
return $api;
}
/**
* Metatag destination handler.
*/
class MigrateMetatagHandler extends MigrateDestinationHandler {
/**
* Identify a list of supported entity types.
*/
public function __construct() {
$entity_types = metatag_entity_supports_metatags();
$entity_types = array_filter($entity_types);
$entity_types = array_keys($entity_types);
$this->registerTypes($entity_types);
}
/**
* Implements MigrateDestinationHandler::fields().
*/
public function fields() {
$fields = array();
$elements = metatag_get_info();
foreach ($elements['tags'] as $value) {
$metatag_field = 'metatag_' . $value['name'];
$fields[$metatag_field] = $value['description'];
}
return $fields;
}
/**
* Implements MigrateDestinationHandler::prepare().
*/
public function prepare($entity, stdClass $row) {
$elements = metatag_get_info();
foreach ($elements['tags'] as $value) {
$metatag_field = 'metatag_' . $value['name'];
if (isset($entity->$metatag_field)) {
$language = isset($entity->language) ? $entity->language : LANGUAGE_NONE;
$entity->metatags[$language][$value['name']]['value'] = $entity->$metatag_field;
unset($entity->$metatag_field);
}
}
}
}