This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
forked from drupalprojects/metatag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metatag.migrate.inc
124 lines (112 loc) · 2.78 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
<?php
/**
* @file
* Metatag support for Migrate.
*/
/**
* 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 {
public function __construct() {
$entity_types = array();
foreach (entity_get_info() as $entity_type => $entity_info) {
if (isset($entity_info['metatags']) && $entity_info['metatags']) {
$entity_types[] = $entity_type;
}
}
$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)) {
$entity->metatags[$value['name']]['value'] = $entity->$metatag_field;
unset($entity->$metatag_field);
}
}
}
}