forked from NCIOCPL/cgov-digital-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgov-export-nav.php
184 lines (158 loc) · 4.39 KB
/
cgov-export-nav.php
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* @file
* Script to export cgov_site_section tree.
*/
use Drupal\taxonomy\TermInterface;
use Drupal\taxonomy\TermStorageInterface;
/** @var \Drupal\Core\Entity\EntityTermManagerInterface */
$entity_type_manager = \Drupal::service('entity_type.manager');
/** @var \Drupal\taxonomy\TermStorageInterface */
$term_storage = $entity_type_manager->getStorage('taxonomy_term');
$terms = $term_storage->loadByProperties([
'vid' => 'cgov_site_sections',
'parent' => 0,
]);
foreach ($terms as $term) {
// @todo Open a file for writing.
export_term($term, $term_storage);
}
/**
* Exports a term, and it's children as a yaml_content file.
*
* @param \Drupal\taxonomy\TermInterface $term
* The term to export.
* @param \Drupal\taxonomy\TermStorageInterface $term_storage
* The term storage.
*/
function export_term(TermInterface $term, TermStorageInterface $term_storage) {
$serializer = \Drupal::service('serialization.yaml');
$file = fopen(
'/tmp/term-yaml' . $term->id() . '.yml',
'w'
);
foreach (get_term_objects($term, $term_storage, NULL) as $yaml_content) {
fwrite(
$file,
$serializer->encode([$yaml_content])
);
}
fclose($file);
}
/**
* Gets terms and their children as yaml_content for a term.
*
* This is a generator that will recurse and return child terms. This is done
* this way because yaml_content needs terms to always have a parent.
*
* @param \Drupal\taxonomy\TermInterface $term
* The term to export.
* @param \Drupal\taxonomy\TermStorageInterface $term_storage
* The term storage.
* @param \Drupal\taxonomy\TermInterface|null $parent_term
* The parent term if it exists.
*/
function get_term_objects(
TermInterface $term,
TermStorageInterface $term_storage,
?TermInterface $parent_term
) {
$content = [
"tid" => $term->id(),
"entity" => "taxonomy_term",
"vid" => "cgov_site_sections",
"langcode" => $term->language()->getId(),
"name" => $term->getName(),
"weight" => $term->getWeight(),
];
$string_fields = [
"field_navigation_label", "field_pretty_url", "field_levels_to_display",
"field_channel", "field_content_group",
];
foreach ($string_fields as $field_name) {
$value_obj = get_string_field($term, $field_name);
if ($value_obj !== NULL) {
$content[$field_name] = $value_obj;
}
}
$bool_fields = [
"field_breadcrumb_root", "field_section_nav_root", "field_main_nav_root",
];
foreach ($bool_fields as $field_name) {
$value_obj = get_boolean_field($term, $field_name);
if ($value_obj !== NULL) {
$content[$field_name] = $value_obj;
}
}
$multi_list_fields = ["field_navigation_display_options"];
foreach ($multi_list_fields as $field_name) {
$value_obj = get_multi_list_field($term, $field_name);
if ($value_obj !== NULL) {
$content[$field_name] = $value_obj;
}
}
if ($parent_term !== NULL) {
$content["parent"] = ["target_id" => $parent_term->id()];
}
yield $content;
// Fetch children for the term.
$children = $term_storage->loadChildren($term->id(), 'cgov_site_section');
foreach ($children as $child) {
foreach (get_term_objects($child, $term_storage, $term) as $yaml_content) {
yield $yaml_content;
}
}
}
/**
* Gets a multiple value list field.
*
* @param \Drupal\taxonomy\TermInterface $term
* The term.
* @param string $field_name
* The name of the field.
*/
function get_multi_list_field(TermInterface $term, $field_name) {
$field_list = $term->get($field_name);
$values = [];
foreach ($field_list as $field) {
$value = $field->value;
if ($value !== NULL) {
$values[] = [
"value" => $value,
];
}
}
return count($values) > 0 ? $values : NULL;
}
/**
* Gets a single value string field.
*
* @param \Drupal\taxonomy\TermInterface $term
* The term.
* @param string $field_name
* The name of the field.
*/
function get_string_field(TermInterface $term, $field_name) {
$value = $term->get($field_name)->value;
if ($value !== NULL) {
return [
"value" => $value,
];
}
}
/**
* Gets a single value boolean field.
*
* @param \Drupal\taxonomy\TermInterface $term
* The term.
* @param string $field_name
* The name of the field.
*/
function get_boolean_field(TermInterface $term, $field_name) {
$value = $term->get($field_name)->value;
if ($value !== NULL) {
return [
"value" => $value ? TRUE : FALSE,
];
}
}