-
Notifications
You must be signed in to change notification settings - Fork 0
/
metatag-vbo-integration-1624266.patch
152 lines (151 loc) · 4.91 KB
/
metatag-vbo-integration-1624266.patch
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
diff --git a/metatag.module b/metatag.module
index a44128327f..fc264a5f0e 100644
--- a/metatag.module
+++ b/metatag.module
@@ -247,6 +247,147 @@ function metatag_flush_caches() {
return array('cache_metatag');
}
+/**
+ * Implements hook_action_info().
+ * Provides integration with Views bulk operations.
+ */
+function metatag_action_info() {
+ return array(
+ 'metatag_modify_metatags_action' => array(
+ 'type' => 'entity',
+ 'label' => t('Modify entity metatags'),
+ 'configurable' => FALSE,
+ 'vbo_configurable' => TRUE,
+ 'behavior' => array('changes_property'),
+ 'triggers' => array('any'),
+ 'permissions' => array('edit meta tags'),
+ ),
+ );
+}
+
+/**
+ * Updates entity metatags with values from the action form.
+ *
+ * @param object $entity
+ * The entity housing the metatags to modify.
+ * @param array $context
+ * Contextual information passed from the View bulk operation configuration
+ * form. The updated metatag values for the entity are stored in
+ * $context['updates'].
+ */
+function metatag_modify_metatags_action($entity, $context) {
+ if (empty($entity)) {
+ drupal_set_message(t("Error while trying to update an entity's metatags."),
+ 'warning', FALSE);
+ return;
+ }
+
+ $updates = $context['updates'];
+ $language = $entity->language;
+
+ // Reset metatags to the entity default configs.
+ if ($context['reset_default']) {
+ $instance = $context['entity_type'] . ':' . $entity->type;
+ $entity_defaults = metatag_config_load_with_defaults($instance);
+
+ // Clean up empty values.
+ foreach ($entity_defaults as $name => $tag) {
+ if (empty($tag['value'])) {
+ unset($entity_defaults[$name]);
+ }
+ }
+
+ $entity->metatags[$language] = $entity_defaults;
+ }
+ // Otherwise, we're updating existing values. Ensure that the entity has any
+ // metatags already set. If so then merge the updates to avoid overwriting
+ // existing values that may exist as an array. E.g. robots.
+ elseif (!empty($entity->metatags) && !empty($entity->metatags[$language])) {
+ foreach ($updates as $tag => $value_array) {
+ if (is_array($updates[$tag]['value']) && !empty($entity->metatags[$language][$tag]['value'])) {
+ $entity->metatags[$language][$tag]['value'] = array_merge($entity->metatags[$language][$tag]['value'],
+ array_filter($updates[$tag]['value']));
+ }
+ elseif (!empty($updates[$tag]['value'])) {
+ $entity->metatags[$language][$tag]['value'] = $updates[$tag]['value'];
+ }
+ }
+ }
+ // Otherwise just set the net new tags.
+ else {
+ $entity->metatags[$language] = $updates;
+ }
+
+ entity_save($context['entity_type'], $entity);
+}
+
+/**
+ * The Views bulk operation configuration form for modifying metatags.
+ *
+ * @param array $context
+ * Contextual information passed from the View bulk operation configuration
+ * form.
+ *
+ * @return array
+ * A form API compatible array.
+ */
+function metatag_modify_metatags_action_form($context) {
+ $form = array(
+ '#entity_type' => $context['entity_type'],
+ );
+
+ // There can be multiple instances being edited here. So the 2nd param passed
+ // here is as generic as possible.
+ metatag_metatags_form($form, 'global');
+
+ // Force the metatags tab to be fully visible and save a click from the user.
+ $form['metatags']['#collapsed'] = FALSE;
+ $form['metatags']['#collapsible'] = FALSE;
+
+ // If we're reseting to the entity default, then we don't need to show the
+ // form fields.
+ $form['metatags']['#states'] = array(
+ 'visible' => array(
+ ':input[name="reset_default"]' => array('checked' => FALSE),
+ ),
+ );
+
+ // Add an option to reset selected entities to the default configuration.
+ $form['reset_default'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Reset to metatag defaults'),
+ '#default_value' => FALSE,
+ '#description' => t('Check to <strong>fully reset all metatags</strong> on
+ the entities being modified to their <a href="@settings">default
+ configuration</a>.', array(
+ '@settings' => url('admin/config/search/metatags'),
+ )),
+ );
+
+ return $form;
+}
+
+/**
+ * Submit handler for metatag_modify_metatags_action_form(). Filters out
+ * the user entered values from the defaults and returns the updated values to
+ * the $context array.
+ *
+ * @return array
+ * The updated metatag values that is ultimately keyed at $context['updates'].
+ */
+function metatag_modify_metatags_action_submit($form, &$form_state) {
+ $updates = $form_state['values']['metatags'][LANGUAGE_NONE];
+ $defaults = metatag_config_load_with_defaults($form['#entity_type']);
+
+ // Filter out the true updates.
+ metatag_filter_values_from_defaults($updates, $defaults);
+
+ return array(
+ 'updates' => $updates,
+ 'reset_default' => $form_state['values']['reset_default'],
+ );
+}
+
/**
* Load a metatag configuration record with all the defaults merged in.
*