-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautotag.module
More file actions
209 lines (185 loc) · 6.08 KB
/
Copy pathautotag.module
File metadata and controls
209 lines (185 loc) · 6.08 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* @file
* Contains autotag.module..
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_help().
*/
function autotag_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the autotag module.
case 'help.page.autotag':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Implements the Tags API to help link entities to terms already present on a site.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_form_alter().
*/
function autotag_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form_id == 'node_type_add_form' || $form_id == 'node_type_edit_form') {
$entityManager = Drupal::service('entity.manager');
$source_fields = array();
$destination_fields = array();
$form['autotag'] = array(
'#type' => 'details',
'#group' => 'additional_settings',
'#title' => t('Autotag'),
);
$type = $form['type']['#default_value'];
$fields = $entityManager->getFieldDefinitions('node', $type);
foreach ($fields as $field) {
if ($field->getType() == 'text_with_summary' || $field->getType() == 'text_long') {
$source_fields[$field->getName()] = $field->getLabel();
}
if ($field->getType() == 'entity_reference' && $field->getSettings()['target_type'] == 'taxonomy_term') {
$destination_fields[$field->getName()] = $field->getLabel();
}
}
if ($type) {
$default_source = \Drupal::config('autotag.type.' . $type)->get('source');
$default_destination = \Drupal::config('autotag.type.' . $type)->get('destination');
}
$form['autotag']['autotag_source_field'] = array(
'#type' => 'checkboxes',
'#title' => t('Source fields to get text:'),
'#default_value' => $default_source,
'#options' => $source_fields,
'#access' => TRUE,
);
$form['autotag']['autotag_destination_field'] = array(
'#type' => 'select',
'#title' => t('Destination fields to get text:'),
'#default_value' => $default_destination,
'#options' => $destination_fields,
'#access' => TRUE,
);
$form['autotag']['autotag_rebuild_on_submit'] = array(
'#type' => 'checkboxes',
'#title' => t('Assign tags on save.'),
'#options' => array('rebuild' => 'Rebuild on this form submission'),
'#access' => TRUE,
);
array_unshift($form['actions']['submit']['#submit'], 'autotag_form_submit_save_config');
$form['actions']['submit']['#submit'][] = 'autotag_form_submit_autotag_nodes';
}
}
/**
* Save config on form submit.
*/
function autotag_form_submit_save_config($form, FormStateInterface $form_state) {
$type = $form_state->getValue('type');
$source = $form_state->getValue('autotag_source_field');
$destination = $form_state->getValue('autotag_destination_field');
\Drupal::configFactory()->getEditable('autotag.type.' . $type)
->set('source', $source)
->save();
\Drupal::configFactory()->getEditable('autotag.type.' . $type)
->set('destination', $destination)
->save();
}
/**
* Autotag all nodes of certain content type on configuration submit.
*
* @param array $form
* The sended form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The sended form_state.
*/
function autotag_form_submit_autotag_nodes($form, FormStateInterface $form_state) {
$type = $form_state->getValue('type');
if ($form_state->getValue('autotag_rebuild_on_submit')['rebuild'] === 'rebuild') {
$batch = autotag_get_batch_tag_all_nodes($type);
batch_set($batch);
}
}
/**
* Implements hook_node_presave().
*/
function autotag_node_presave(EntityInterface $node) {
autotag_tag_node($node);
}
/**
* Asign tags to node depending to bundle configuration.
*
* @param EntityInterface $node
* The node to autotag.
*/
function autotag_tag_node(EntityInterface &$node) {
$text_array = array();
$type = $node->bundle();
$source = \Drupal::config('autotag.type.' . $type)->get('source');
$text = '';
$destination = \Drupal::config('autotag.type.' . $type)->get('destination');
$vocabularies = $node->getFieldDefinition($destination)->getItemDefinition()->getSettings()['handler_settings']['target_bundles'];
foreach ($source as $field) {
if ($field) {
$text_array[] = $node->get($field)->getValue();
}
}
foreach ($text_array as $item) {
if (isset($item[0]['summary'])) {
$text .= strtolower($item[0]['summary']);
}
if (isset($item[0]['value'])) {
$text .= strtolower($item[0]['value']);
}
}
foreach ($vocabularies as $vocabulary) {
$tree = \Drupal::getContainer()->get('entity.manager')->getStorage('taxonomy_term')->loadTree($vocabulary);
$current_terms = explode(', ', $node->get($destination)->getString());
foreach ($tree as $term) {
$found = strpos($text, strtolower($term->name));
if ($found !== FALSE && !in_array($term->tid, $current_terms)) {
$value = array('target_id' => $term->tid);
$node->{$destination}->appendItem($value);
}
}
}
}
/**
* Return the batch to autotag all nodes of certaing bundle.
*
* @param string $bundle
* The bundle to retag.
*
* @return array
* The batch to execute.
*/
function autotag_get_batch_tag_all_nodes($bundle) {
$nids = \Drupal::entityQuery('node')
->condition('type', $bundle)
->execute();
$batch = array(
'title' => t('Autotagging all nodes of type %type', array('%type' => $bundle)),
'operations' => array(),
);
foreach ($nids as $nid) {
$batch['operations'][] = array('autotag_load_and_retag_node', array($nid));
}
return $batch;
}
/**
* Loads a node and autotags it.
*
* @param int $nid
* The id of the node to load.
* @param array $context
* Batch API parameter.
*/
function autotag_load_and_retag_node($nid, &$context) {
$node = \Drupal::entityTypeManager()
->getStorage('node')
->load($nid);
autotag_tag_node($node);
$node->save();
$context['results'][] = 1;
$context['message'] = 'Node tagged';
}