-
Notifications
You must be signed in to change notification settings - Fork 11
/
paragraphs.type_clone.inc
440 lines (405 loc) · 14.5 KB
/
paragraphs.type_clone.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<?php
/**
* @file
* Paragraphs Type cloning functions.
*/
/**
* Form constructor for the clone creation.
*
* @param array $form
* The form array.
* @param array $form_state
* Reference to the form state array.
* @param string $bundle
* The machine name of the source paragraph type.
*/
function paragraphs_admin_bundle_clone_create_form(array $form, array &$form_state, $bundle) {
// Load the paragraph type from machine name.
$paragraph_type = $bundle;
// Prepare the new paragraph type machine name prefix.
$paragraph_type_prefix = $paragraph_type->bundle . '_clone';
// Get the next occurrence count to use in the names.
$num = paragraphs_type_clone_get_next_type_number($paragraph_type_prefix);
// Source fieldset.
$form['fieldset_source'] = array(
'#type' => 'fieldset',
'#title' => t('Paragraphs type source'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
// Source paragraph type name field.
$form['fieldset_source']['source_name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#default_value' => $paragraph_type->name,
'#required' => TRUE,
'#attributes' => array(
'readonly' => 'readonly',
'class' => array('paragraphs-type-clone-readonly-field'),
),
);
// Source paragraph type machine name field.
$form['fieldset_source']['source_machine_name'] = array(
'#type' => 'textfield',
'#title' => t('Machine name'),
'#default_value' => $paragraph_type->bundle,
'#required' => TRUE,
'#attributes' => array(
'readonly' => 'readonly',
'class' => array('paragraphs-type-clone-readonly-field'),
),
);
// Source paragraph type label field.
$form['fieldset_source']['source_label'] = array(
'#type' => 'textfield',
'#title' => t('Label'),
'#default_value' => $paragraph_type->label,
'#required' => FALSE,
'#attributes' => array(
'readonly' => 'readonly',
'class' => array('paragraphs-type-clone-readonly-field'),
),
);
// Source paragraph type description field.
$form['fieldset_source']['source_description'] = array(
'#type' => 'textarea',
'#title' => t('Description'),
'#default_value' => $paragraph_type->description,
'#attributes' => array(
'readonly' => 'readonly',
'class' => array('paragraphs-type-clone-readonly-field'),
),
);
// Source paragraph type allow_unpublish field.
$form['fieldset_source']['source_allow_unpublish'] = array(
'#type' => 'checkbox',
'#title' => t('Allow unpublishing from the admin interface'),
'#default_value' => $paragraph_type->allow_unpublish,
'#disabled' => TRUE,
'#attributes' => array(
'class' => array('paragraphs-type-clone-readonly-field'),
),
);
// Target fieldset.
$form['fieldset_target'] = array(
'#type' => 'fieldset',
'#title' => t('Paragraphs type target'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
// Target paragraph type name field.
$default_value = 'Clone of ' . $paragraph_type->name . ' (' . $num . ')';
$form['fieldset_target']['target_name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#default_value' => $default_value,
'#token' => FALSE,
'#required' => TRUE,
);
// Target type machine name field.
$form['fieldset_target']['target_machine_name'] = array(
'#type' => 'textfield',
'#title' => t('Machine name'),
'#default_value' => $paragraph_type_prefix . '_' . $num,
'#required' => TRUE,
);
// Target paragraph type label field.
$default_value = 'Clone of ' . $paragraph_type->label . ' (' . $num . ')';
$form['fieldset_target']['target_label'] = array(
'#type' => 'textfield',
'#title' => t('Label'),
'#default_value' => $default_value,
'#required' => FALSE,
);
// Target paragraph type description field.
$form['fieldset_target']['target_description'] = array(
'#type' => 'textarea',
'#title' => t('Description'),
'#default_value' => $paragraph_type->description,
);
// Target paragraph type allow_unpublish field.
$form['fieldset_target']['target_allow_unpublish'] = array(
'#type' => 'checkbox',
'#title' => t('Allow unpublishing from the admin interface'),
'#default_value' => $paragraph_type->allow_unpublish,
);
// Submit button.
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Clone Paragraphs type'),
);
// Return the form.
return $form;
}
/**
* Generate a default item number for a new clone.
*
* This function will add the clone number to the machine name and name
* in the paragraph type creation form.
*
* The clone number is based on the number of existing clones
* for the paragraph type being cloned.
*
* @param string $paragraph_type_prefix
* The default prefix defined for the new paragraph type.
*/
function paragraphs_type_clone_get_next_type_number($paragraph_type_prefix) {
// Get paragraph types.
$paragraph_types = array_keys(paragraphs_bundle_load());
// Prepare the search pattern.
$pattern = '/^' . $paragraph_type_prefix . '/';
// Count existing prefix occurrences.
$occurrences = count(preg_grep($pattern, $paragraph_types));
// Return an incremented value.
return $occurrences + 1;
}
/**
* Form validation handler for the clone creation form.
*
* @param array $form
* The form array.
* @param array $form_state
* Reference to the form state array.
*/
function paragraphs_admin_bundle_clone_create_form_validate(array $form, array &$form_state) {
if ($form['#form_id'] == 'paragraphs_admin_bundle_clone_create_form') {
// Get paragraph types.
$paragraph_types = array_keys(paragraphs_bundle_load());
// If the machine name already exists.
if (in_array($form_state['values']['target_machine_name'], $paragraph_types)) {
form_set_error('target_machine_name', t('The paragraph type machine name provided already exists'));
}
// Add the custom form submit handler.
$form['#submit'][] = 'paragraphs_admin_bundle_clone_create_form_submit';
}
}
/**
* Form submit handler for the clone creation form.
*
* @param array $form
* The form array.
* @param array $form_state
* Reference to the form state array.
*/
function paragraphs_admin_bundle_clone_create_form_submit(array $form, array &$form_state) {
if ($form['#form_id'] == 'paragraphs_admin_bundle_clone_create_form') {
$values = $form_state['values'];
// Execute the function batch function.
$batch = paragraphs_type_clone_create($values);
if (empty($batch)) {
// If $batch is empty (i.e. no fields), skip batch set, set a message and
// end clone operation.
backdrop_set_message(
t('The "@source_name" paragraph type has been cloned successfully to "@target_name".',
array(
'@source_name' => $values['source_name'],
'@target_name' => $values['target_name'],
)
)
);
// Redirect to the paragraph types list.
$form_state['redirect'] = 'admin/structure/paragraphs';
}
else {
batch_set($batch);
}
}
}
/**
* Create a paragraph type clone.
*
* This function creates the new paragraph type and adds the cloned fields.
*
* @param array $values
* Form values coming from the paragraph type creation form.
*/
function paragraphs_type_clone_create(array $values) {
// Create the paragraph type object and populate from the form.
$bundle = new stdClass();
$bundle->bundle = $values['target_machine_name'];
$bundle->locked = 1;
$bundle->name = trim((string) $values['target_name']);
$bundle->allow_unpublish = (int) $values['target_allow_unpublish'];
$bundle->label = (empty($values['target_label'])) ? $bundle->name : trim($values['target_label']);
$bundle->description = trim((string) $values['target_description']);
// Save the paragraph type.
paragraphs_bundle_save($bundle);
// Prepare the operations array.
$operations = array();
// Get paragraph type base fields.
$base_fields = field_info_instances('paragraphs_item', $values['source_machine_name']);
// Check to see if fields exist before completing field actions.
if (!empty($base_fields)) {
// Get paragraph type extra fields.
$extra_fields = field_info_extra_fields('paragraphs_item', $values['source_machine_name'], 'form');
// Merge all fields.
$all_fields = array_merge($base_fields, $extra_fields);
// Create new fields.
foreach ($all_fields as $name => $instance) {
if (isset($name) && !empty($name) && isset($instance['entity_type']) && !empty($instance['entity_type'])) {
$operations[] = array(
'paragraphs_type_clone_set_target_field',
array($instance, $name, $values),
);
}
}
// Check if the Field Group module is installed.
if (module_exists('field_group')) {
// Get the groups from the form view and all display view modes.
$groups = field_group_info_groups('paragraphs_item', $values['source_machine_name'], NULL, TRUE);
// Loop through each display mode including form.
$view_modes = array_keys($groups);
foreach ($view_modes as $view_mode) {
// Loop through each group within the display mode.
foreach ($groups[$view_mode] as $group) {
// Set the bundle (paragraph type) to be the new type machine name.
$group->bundle = $values['target_machine_name'];
// Save the new group configuration.
field_group_group_save($group, $new = TRUE);
}
}
}
}
// Check if the Paragraphs Type Permissions module is enabled so we can clone
// permissions if they exist.
if (module_exists('paragraphs_bundle_permissions')) {
// Get all the roles defined in the site.
$roles = user_roles();
// Define the avaiable permission types for each paragraph type.
$permission_types = array('view', 'update', 'delete', 'create');
// Loop through each role on the site and get the existing permissions.
foreach ($roles as $role_name => $role_label) {
$role_permissions = user_role_permissions(array($role_name));
// Reset the list of target permissions for each role.
$target_permissions = array();
// Loop through the permission types to check if the source type has
// permissions for this action on this role.
foreach ($permission_types as $permission_type) {
// Define the source paragraph type permission to check for.
$permission_name = $permission_type . ' paragraph content ' . $values['source_machine_name'];
// Check if the role has this permission.
if (in_array($permission_name, $role_permissions)) {
// Add this to the list of target permissions to add for this role.
$target_permission_name = $permission_type . ' paragraph content ' . $bundle->bundle;
$target_permissions[$target_permission_name] = TRUE;
}
}
// Add all target_permissions to this role.
user_role_change_permissions($role_name, $target_permissions);
}
}
// Define batch operation if fields exist and therefore operations set.
if (!empty($operations) > 0) {
$batch = array(
'title' => t('Clone Paragraphs type'),
'operations' => $operations,
'file' => backdrop_get_path('module', 'paragraphs') . '/paragraphs.type_clone.inc',
'finished' => 'paragraphs_type_clone_finished',
'init_message' => t('Operation in progress...'),
'progress_message' => t('Task @current/@total'),
'error_message' => t('An error has occurred please try again.'),
);
return $batch;
}
else {
return FALSE;
}
}
/**
* Create a field.
*
* This function creates a field for a cloned paragraph type.
*
* @param object $instance
* The newly created paragraph type.
* @param string $field_name
* The name of the field in the source paragraph type.
* @param array $values
* Form values coming from the paragraph type creation form.
*/
function paragraphs_type_clone_set_target_field($instance, $field_name, array $values, &$context) {
// Progress context results.
$context['results'][] = $field_name;
// Pass the cloned paragraph type name to context.
$context['results']['source_name'] = $values['source_name'];
$context['results']['target_name'] = $values['target_name'];
// Update the field count.
if (!isset($context['results']['field_count'])) {
$context['results']['field_count'] = 0;
}
$context['results']['field_count']++;
// Progress context message.
$context['message'] = t('Cloning field @field',
array(
'@field' => $field_name,
)
);
// Verify the field does not already exist.
if (!_paragraphs_type_clone_exclude_field($field_name)) {
// If field doesn't exist, create it.
if (!field_info_field($field_name)) {
if ($field = field_info_field($field_name)) {
$field = field_create_field($field);
}
}
// Set the new bundle (paragraph type) name.
$instance['bundle'] = $values['target_machine_name'];
$instance['field_name'] = $field_name;
// Create an instance of the field and bind it to the bundle.
field_create_instance($instance);
}
}
/**
* Checks if a field should be excluded.
*
* @param string $field_name
* The field name.
*
* @return bool
* Number of requests.
*/
function _paragraphs_type_clone_exclude_field($field_name) {
$exclude = array('title', 'body', 'metatags', 'locations');
return in_array($field_name, $exclude);
}
/**
* Batch operation finished.
*
* This function displays a message after the batch operation ends.
*
* @param bool $success
* The batch status.
* @param array $results
* The batch results array.
* @param array $operations
* The batch operations array.
*/
function paragraphs_type_clone_finished($success, array $results, array $operations) {
// Test the results.
if ($success) {
backdrop_set_message(
t('The "@source_name" paragraph type and @field_count fields have been cloned successfully to "@target_name".',
array(
'@source_name' => $results['source_name'],
'@target_name' => $results['target_name'],
'@field_count' => $results['field_count'],
)
)
);
}
else {
$error_operation = reset($operations);
backdrop_set_message(
t('An error occurred while processing @operation with arguments : @args',
array(
'@operation' => $error_operation[0],
// phpcs:ignore Squiz.PHP.DiscouragedFunctions -- This is used widely in debugging.
'@args' => print_r($error_operation[0], TRUE),
)
)
);
}
// Redirect to the paragraph types list.
backdrop_goto('admin/structure/paragraphs');
}