-
Notifications
You must be signed in to change notification settings - Fork 11
/
paragraphs.admin.inc
977 lines (871 loc) · 32.5 KB
/
paragraphs.admin.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
<?php
/**
* @file
* Admin functions for the paragraphs module.
*/
/**
* Page callback to show the bundle overview page.
*
* @return null|string
* Rendered table of bundles.
*
* @throws Exception
*/
function paragraphs_admin_bundle_overview() {
$page = array();
$bundles = paragraphs_bundle_load();
$field_ui = module_exists('field_ui');
$header = array(
t('Label'),
t('Description'),
t('Operations'),
);
$rows = array();
$operations = array();
foreach ($bundles as $bundle) {
$type_url_str = strtr($bundle->bundle, array('_' => '-'));
$row = array(theme('label_machine_name__paragraphs', array(
'label' => $bundle->label,
'machine_name' => $bundle->bundle,
)));
$row['description'] = $bundle->description;
if ($field_ui) {
// Manage fields.
$operations['manage_fields'] = array(
'title' => t('Manage fields'),
'href' => 'admin/structure/paragraphs/' . $type_url_str . '/fields',
);
// Display fields.
$operations['manage_display'] = array(
'title' => t('Manage display'),
'href' => 'admin/structure/paragraphs/' . $type_url_str . '/display',
);
}
// Manage bundle.
$operations['edit_type'] = array(
'title' => t('Configure'),
'href' => 'admin/structure/paragraphs/' . $type_url_str . '/edit',
);
// Clone bundle.
$operations['clone_type'] = array(
'title' => t('clone'),
'href' => 'admin/structure/paragraphs/' . $type_url_str . '/clone',
);
// Manage permissions if module enabled.
if (module_exists('paragraphs_bundle_permissions')) {
$operations['permissions'] = array(
'title' => t('Permissions'),
'href' => paragraphs_bundle_permissions_url(),
'query' => array('search' => $bundle->name . ': '),
'attributes' => array('title' => t('Configure permissions')),
);
}
// Delete bundle.
$operations['delete_type'] = array(
'title' => t('Delete'),
'href' => 'admin/structure/paragraphs/' . $type_url_str . '/delete',
);
$row['operations'] = array(
'data' => array(
'#type' => 'dropbutton',
'#links' => $operations,
),
);
$rows[$bundle->bundle] = $row;
}
// Sort rows by bundle.
ksort($rows);
// Render paragraphs bundle table.
$page['paragraphs_bundle_table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => t('No paragraph types have been defined yet.'),
);
return $page;
}
/**
* Form to create or edit an paragraphs bundle.
*
* @param array $form
* The form structure array.
* @param array $form_state
* An associative array containing the current state of the form.
* @param object $bundle
* The bundle.
*
* @return array
* The form structure array.
*/
function paragraphs_admin_bundle_form(array $form, array &$form_state, $bundle = NULL) {
if (!isset($bundle) && !$bundle) {
// This is a new bundle.
$bundle = new stdClass();
$bundle->name = '';
$bundle->bundle = '';
$bundle->label = '';
$bundle->description = '';
$bundle->locked = 0;
$bundle->allow_unpublish = 1;
}
else {
if (!$bundle) {
backdrop_set_message(t('Could not load Paragraph type'), 'error');
backdrop_goto('admin/structure/paragraphs');
}
}
$form['#paragraphs_bundle'] = $bundle;
$form['name'] = array(
'#title' => t('Name'),
'#type' => 'textfield',
'#default_value' => $bundle->name,
'#description' => t('The human-readable name of this Paragraph type.'),
'#required' => TRUE,
'#size' => 30,
);
$form['bundle'] = array(
'#type' => 'machine_name',
'#default_value' => $bundle->bundle,
'#maxlength' => 32,
'#disabled' => $bundle->locked,
'#machine_name' => array(
'exists' => 'paragraphs_bundle_load',
),
'#description' => t('A unique machine-readable name for this Paragraph type. It must only contain lowercase letters, numbers, and underscores.'),
);
$form['label'] = array(
'#title' => t('Admin label'),
'#type' => 'textfield',
'#default_value' => $bundle->label,
'#description' => t('The label for this Paragraph type as it will appear to users on edit forms. Defaults to the name value if left empty.'),
'#size' => 30,
);
$form['description'] = array(
'#title' => t('Description'),
'#type' => 'textarea',
'#default_value' => $bundle->description,
'#description' => t('Describe this Paragraph type. The text will be displayed on the Paragraphs admin overview page.'),
);
$form['locked'] = array(
'#type' => 'value',
'#value' => $bundle->locked,
);
$form['allow_unpublish'] = array(
'#type' => 'checkbox',
'#title' => t('Allow unpublishing from the admin interface.'),
'#default_value' => (isset($bundle->allow_unpublish)) ? $bundle->allow_unpublish : 0,
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save Paragraph type'),
'#weight' => 40,
);
return $form;
}
/**
* Form validation handler for paragraphs_admin_bundle_form().
*
* @param array $form
* The form structure array.
* @param array $form_state
* An associative array containing the current state of the form.
*
* @see paragraphs_admin_bundle_form_submit()
*/
function paragraphs_admin_bundle_form_validate(array $form, array &$form_state) {
$bundle = new stdClass();
$bundle->name = trim($form_state['values']['name']);
if (!$form_state['values']['locked']) {
$bundle->bundle = trim($form_state['values']['bundle']);
// 'theme' conflicts with theme_node_form().
// '0' is invalid, since elsewhere we check it using empty().
if (in_array($bundle->bundle, array('0', 'theme'))) {
form_set_error('type', t("Invalid machine-readable name. Enter a name other than %invalid.", array('%invalid' => $bundle->bundle)));
}
}
}
/**
* Submit handler for paragraphs_admin_bundle_form().
*
* @param array $form
* The form structure array.
* @param array $form_state
* An associative array containing the current state of the form.
*
* @see paragraphs_admin_bundle_form()
*/
function paragraphs_admin_bundle_form_submit(array $form, array &$form_state) {
$bundle = new stdClass();
if (!$form_state['values']['locked']) {
$bundle->bundle = trim($form_state['values']['bundle']);
}
else {
$bundle->bundle = $form['#paragraphs_bundle']->bundle;
}
$bundle->locked = 1;
$bundle->name = trim($form_state['values']['name']);
$bundle->allow_unpublish = $form_state['values']['allow_unpublish'];
// Set bundle label equal to name if empty.
$bundle->label = empty($form_state['values']['label']) ? $bundle->name : trim($form_state['values']['label']);
$bundle->description = trim($form_state['values']['description']);
$variables = $form_state['values'];
// Remove everything that's been saved already - whatever's left is assumed
// to be a persistent variable.
foreach ($variables as $key => $value) {
if (isset($bundle->$key)) {
unset($variables[$key]);
}
}
unset($variables['form_token'], $variables['op'], $variables['submit'], $variables['delete'], $variables['reset'], $variables['form_id'], $variables['form_build_id']);
$status = paragraphs_bundle_save($bundle);
// Add permissions if paragraphs_bundle_permissions is enabled and paragraph
// is newly created.
if (module_exists('paragraphs_bundle_permissions') && $status == SAVED_NEW) {
// Define the permission name for viewing paragraph content.
$permission_name = 'view paragraph content ' . $bundle->bundle;
// Add the permission to the anonymous role.
user_role_change_permissions('anonymous', array($permission_name => TRUE));
// Add the permission to the authenticated role.
user_role_change_permissions('authenticated', array($permission_name => TRUE));
// Define all administrator permissions.
$admin_permissions = array('view', 'update', 'delete', 'create');
$permissions = array();
foreach ($admin_permissions as $permission) {
$permission_name = $permission . ' paragraph content ' . $bundle->bundle;
$permissions += array(
$permission_name => TRUE,
);
}
// Add the permissions to the administrator role.
$admin_role = config_get('system.core', 'user_admin_role');
user_role_change_permissions($admin_role, $permissions);
// Define a message informing the user who created the paragraph type that
// default permissions have been added.
$roles = user_roles();
$message = t("Default permissions have been added to the %anonymous and %authenticated roles ('View') and %admin role (all) for the %paragraph_type paragraph type. ",
array(
'%anonymous' => $roles['anonymous'],
'%authenticated' => $roles['authenticated'],
'%admin' => $roles[$admin_role],
'%paragraph_type' => $bundle->name,
)
);
// Define part of the message depending on whether the user has permission
// to administer permissions or not.
$conditional_message = paragraphs_bundle_permissions_conditional_message($bundle->name . ': ');
// Display the combined message.
backdrop_set_message($message . $conditional_message, 'info');
}
$t_args = array('%name' => $bundle->name);
if ($status == SAVED_UPDATED) {
backdrop_set_message(t('The Paragraph type %name has been updated.', $t_args));
}
elseif ($status == SAVED_NEW) {
backdrop_set_message(t('The Paragraph type %name has been added.', $t_args));
watchdog('paragraphs', 'Added Paragraph type %name.', $t_args, WATCHDOG_NOTICE, l(t('view'), 'admin/structure/paragraphs'));
}
$form_state['redirect'] = 'admin/structure/paragraphs';
}
/**
* Menu callback to delete a single paragraph bundle.
*
* @param array $form
* The form structure array.
* @param array $form_state
* An associative array containing the current state of the form.
* @param object $bundle
* The bundle.
*
* @ingroup forms
*/
function paragraphs_admin_bundle_delete_form(array $form, array &$form_state, $bundle) {
if (!$bundle) {
backdrop_set_message(t('Could not load Paragraph type'), 'error');
backdrop_goto('admin/structure/paragraphs');
}
$form['type'] = array(
'#type' => 'value',
'#value' => $bundle->bundle,
);
$form['name'] = array(
'#type' => 'value',
'#value' => $bundle->name,
);
$message = t('Are you sure you want to delete the Paragraph type %bundle?', array('%bundle' => $bundle->name));
$caption = '<p>' . t('This action cannot be undone. Content using this Paragraph type will be broken.') . '</p>';
return confirm_form($form, filter_xss_admin($message), 'admin/structure/paragraphs', filter_xss_admin($caption), t('Delete'));
}
/**
* Process and confirm paragraphs bundle deletion.
*
* @param array $form
* The form structure array.
* @param array $form_state
* An associative array containing the current state of the form.
*
* @see paragraphs_admin_bundle_delete_form()
*/
function paragraphs_admin_bundle_delete_form_submit(array $form, array &$form_state) {
paragraphs_bundle_delete($form_state['values']['type']);
$t_args = array('%name' => $form_state['values']['name']);
backdrop_set_message(t('The Paragraph type %name has been deleted.', $t_args));
watchdog('paragraphs', 'Deleted Paragraph type %name.', $t_args, WATCHDOG_NOTICE);
$form_state['redirect'] = 'admin/structure/paragraphs';
}
/**
* Separate paragraphs_item edit form.
*/
function paragraphs_modal_admin_edit($form, &$form_state, $paragraphs_item) {
if (!$paragraphs_item) {
backdrop_not_found();
}
$bundle = paragraphs_bundle_load($paragraphs_item->bundle);
backdrop_set_title(t('Edit !title paragraph', array('!title' => $bundle->name)));
$form['paragraphs_item'] = array(
'#type' => 'value',
'#value' => $paragraphs_item,
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#weight' => 10000,
'#value' => t('Save'),
'#ajax' => array(
'callback' => 'paragraphs_modal_admin_save_close',
'effect' => 'fade',
// 'wrapper' => $element['#wrapper_id'],
),
);
field_attach_form('paragraphs_item', $paragraphs_item, $form, $form_state);
// Get the top-level host entity.
$item = $paragraphs_item;
$host = NULL;
while (method_exists($item, 'hostEntity')) {
$host = $item->hostEntity();
$host_entity_type = $item->hostEntityType();
$host_entity_id = $item->hostEntityId();
$host_entity_bundle = $item->hostEntityBundle();
$item = $host;
}
// Only show revisioning options if our host is definitely revisioned.
$show_revision_options = FALSE;
$use_revisions = FALSE;
if ($host_entity_type == 'node') {
$show_revision_options = config_get('node.type.' . $host_entity_bundle, 'settings.revision_enabled');
$use_revisions = config_get('node.type.' . $host_entity_bundle, 'settings.revision_default');
}
if ($show_revision_options) {
$form['additional_settings'] = array(
'#type' => 'vertical_tabs',
'#weight' => 99,
);
$form['revision_information'] = array(
'#type' => 'fieldset',
'#title' => t('Revision information'),
'#collapsible' => TRUE,
// Collapsed by default when "Create new revision" is unchecked.
'#collapsed' => !$use_revisions,
'#group' => 'additional_settings',
'#attributes' => array(
'class' => array('node-form-revision-information'),
),
'#attached' => array(
'js' => array(backdrop_get_path('module', 'node') . '/js/node.js'),
),
'#weight' => 20,
'#access' => user_access('administer nodes'),
);
$form['revision_information']['revision'] = array(
'#type' => 'checkbox',
'#title' => t('Create new revision'),
'#default_value' => $use_revisions,
'#access' => user_access('administer nodes'),
);
}
return $form;
}
/**
* Validation function for entity form for validating the fields.
*/
function paragraphs_modal_admin_edit_validate($form, &$form_state) {
field_attach_form_validate('paragraphs_item', $form_state['values']['paragraphs_item'], $form, $form_state);
}
/**
* Submit function for edit entity form.
*/
function paragraphs_modal_admin_edit_submit($form, &$form_state) {
$paragraphs_item = $form_state['values']['paragraphs_item'];
field_attach_submit('paragraphs_item', $paragraphs_item, $form, $form_state);
// Save a new revision of our host entity?
$save_new_revision = !empty($form_state['values']['revision']);
if ($save_new_revision) {
$paragraphs_item->is_new_revision = TRUE;
}
// You get a new revision when you save a node, even if there are no changes.
// Ensure that we do not save the host node if $save_new_revision is FALSE.
$paragraphs_item->save(FALSE);
}
/**
* Save and close Ajax modal popup.
*/
function paragraphs_modal_admin_save_close($form, &$form_state) {
$paragraphs_item = $form_state['values']['paragraphs_item'];
$bundle = paragraphs_bundle_load($paragraphs_item->bundle);
// Close modal dialog upon submit.
$commands = array(
'#type' => 'ajax',
'#commands' => array(),
);
$commands['#commands'][] = ajax_command_close_modal_dialog();
// Refresh display of Paragraph that was edited.
if (is_object($form_state['build_info']['args'][0]) && !empty($form_state['build_info']['args'][0]->item_id)) {
$selector = '.paragraphs-item-' . $form_state['build_info']['args'][0]->item_id;
$revision_id = $form_state['build_info']['args'][0]->revision_id;
$fresh_paragraph = paragraphs_item_revision_load($revision_id)->view();
$new_element = '<div class=\'messages status paragraphs-modal-admin-message\'>' . t('Paragraph !type has been updated.', array('!type' => $bundle->name)) . '</div>';
$new_element .= backdrop_render($fresh_paragraph);
$commands['#commands'][] = array(
'command' => 'paragraphs_modal_admin_refresh',
'selector' => $selector,
'new_element' => $new_element,
);
$commands['#commands'][] = array(
'command' => 'paragraphs_modal_admin_message_hide',
);
}
return $commands;
}
/**
* Paragraphs sort dialog popup form.
*/
function paragraphs_modal_admin_sort_form($form, &$form_state, $paragraphs_item) {
if (!$paragraphs_item) {
backdrop_not_found();
}
$langcode = $paragraphs_item->langcode;
$host_entity = $paragraphs_item->hostEntity();
$field_name = $paragraphs_item->field_name;
$element = $paragraphs_item->fieldInfo();
$items = $host_entity->{$field_name};
// If the field can hold more than one item, display it as a draggable table.
if ($element['cardinality'] != 1 || count($form_state['build_info']['args']) > 1) {
$form['paragraphs_items']['#tree'] = TRUE;
$form['host_entity']['#tree'] = TRUE;
$table_id = backdrop_html_id($field_name . '_values');
$rows = array();
$form['host_entity'][] = array(
'entity_type' => array(
'#type' => 'hidden',
'#value' => $paragraphs_item->hostEntityType(),
),
'entity_id' => array(
'#type' => 'hidden',
'#value' => $paragraphs_item->hostEntityId(),
),
'revision_id' => array(
'#type' => 'hidden',
'#value' => $paragraphs_item->hostEntityRevisionId(),
),
'bundle' => array(
'#type' => 'hidden',
'#value' => $paragraphs_item->hostEntityBundle(),
),
'field_name' => array(
'#type' => 'hidden',
'#value' => $field_name,
),
);
foreach ($items[$langcode] as $delta => $item) {
$item['entity'] = paragraphs_item_load($item['value']);
$item_render_array = $item['entity']->view('paragraphs_editor_preview', $langcode);
$item_markup = backdrop_render($item_render_array);
if (empty($item_markup) || empty(trim(strip_tags($item_markup, '<img><a>')))) {
$bundle = paragraphs_bundle_load($item['entity']->bundle);
$item_markup = $bundle->name;
}
$form['paragraphs_items'][$item['value']] = array(
'preview' => array(
'#markup' => $item_markup,
),
'item_id' => array(
'#type' => 'hidden',
'#value' => $item['value'],
),
'weight' => array(
'#type' => 'weight',
'#title' => t('Weight'),
'#default_value' => $delta,
'#delta' => 10,
'#title_display' => 'invisible',
),
);
}
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save Order'),
'#ajax' => array(
'callback' => 'paragraphs_modal_admin_sort_form_close',
'effect' => 'fade',
),
);
}
return $form;
}
/**
* Paragraphs sort dialog popup form submit.
*/
function paragraphs_modal_admin_sort_form_submit($form, &$form_state) {
// Load host entity.
$entity_type = $form_state['values']['host_entity'][0]['entity_type'];
$entity_id = $form_state['values']['host_entity'][0]['entity_id'];
$field_name = $form_state['values']['host_entity'][0]['field_name'];
$entity = entity_load($entity_type, $entity_id);
$unsorted_items = $entity->{$field_name}[$entity->langcode];
unset($entity->{$field_name}[$entity->langcode]);
foreach ($unsorted_items as $key => $entity_item) {
$item_id = $entity_item['value'];
$unsorted_items[$key]['weight'] = $form_state['values']['paragraphs_items'][$item_id]['weight'];
}
backdrop_sort($unsorted_items, array('weight'));
$sorted_items = array_values($unsorted_items);
$entity->{$field_name}[$entity->langcode] = $sorted_items;
$entity->save();
}
/**
* Close Ajax modal popup.
*/
function paragraphs_modal_admin_sort_form_close($form, &$form_state) {
// Close modal dialog upon submit.
$commands = array(
'#type' => 'ajax',
'#commands' => array(),
);
$commands['#commands'][] = ajax_command_close_modal_dialog();
// Refresh display of field that was sorted.
if (!empty($form_state['values']['host_entity'][0]['entity_id']) && !empty($form_state['values']['host_entity'][0]['entity_type'])) {
$entity_id = $form_state['values']['host_entity'][0]['entity_id'];
$entity_type = $form_state['values']['host_entity'][0]['entity_type'];
$revision_id = $form_state['values']['host_entity'][0]['revision_id'];
$field_name = $form_state['values']['host_entity'][0]['field_name'];
$entity = entity_load($entity_type, $entity_id);
$fresh_field = field_view_field($entity_type, $entity, $field_name);
$new_element = '<div class=\'messages status paragraphs-modal-admin-message\'>' . t('Paragraphs have been reordered.') . '</div>';
$new_element .= backdrop_render($fresh_field);
$commands['#commands'][] = ajax_command_html('.paragraphs-items-' . strtr($field_name, '_', '-'), $new_element);
$commands['#commands'][] = array(
'command' => 'paragraphs_modal_admin_message_hide',
);
}
return $commands;
}
/**
* Paragraphs sort dialog popup form.
*/
function theme_paragraphs_modal_admin_sort_form($variables) {
$form = $variables['form'];
// Initialize the variable which will store our table rows.
$rows = array();
// Iterate over each element in our $form['example_items'] array.
foreach (element_children($form['paragraphs_items']) as $id) {
$form['paragraphs_items'][$id]['weight']['#attributes']['class'] = array('paragraphs-item-weight');
$rows[] = array(
'data' => array(
array(
'data' => '',
'class' => array('field-multiple-drag'),
),
backdrop_render($form['paragraphs_items'][$id]['preview']),
backdrop_render($form['paragraphs_items'][$id]['weight']),
),
'class' => array('draggable'),
);
}
$header = array(
array(
'data' => t('Preview'),
'colspan' => 2,
'class' => array('field-label'),
),
t('Weight'),
);
$table_id = backdrop_html_id('paragraph-items-sort');
$output = theme('table', array(
'header' => $header,
'rows' => $rows,
'attributes' => array(
'id' => $table_id,
),
));
$form['#children'] = $output;
$output .= backdrop_render_children($form);
backdrop_add_tabledrag($table_id, 'order', 'sibling', 'paragraphs-item-weight');
return $output;
}
/**
* Page callback: Form constructor for paragraph deletion confirmation form.
*
* @see paragraphs_menu()
* @see paragraphs_modal_admin_remove_confirm_submit()
*/
function paragraphs_modal_admin_remove_confirm($form, &$form_state, $paragraphs_item = NULL) {
if (empty($paragraphs_item) && !empty($form['paragraphs_item'])) {
$paragraphs_item = $form['paragraphs_item'];
}
$form['item_id'] = array(
'#type' => 'value',
'#value' => $paragraphs_item->item_id,
);
$bundle = paragraphs_bundle_load($paragraphs_item->bundle);
$form['description'] = array(
'#markup' => t('Are you sure you want to remove this !type? (This action cannot be undone.)', array('!type' => $bundle->name)),
);
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Remove'),
'#attributes' => array('class' => array('button-danger')),
'#ajax' => array(
'callback' => 'paragraphs_modal_admin_remove_confirm_submit',
'effect' => 'fade',
// 'wrapper' => $element['#wrapper_id'],
),
);
return $form;
}
/**
* Form submission handler for paragraphs_modal_admin_remove_confirm().
*
* @see paragraphs_modal_admin_remove_confirm()
*/
function paragraphs_modal_admin_remove_confirm_submit($form, &$form_state) {
// Close modal dialog upon submit.
$commands = array(
'#type' => 'ajax',
'#commands' => array(),
);
$commands['#commands'][] = ajax_command_close_modal_dialog();
// Delete paragraph item and refresh display.
if (is_object($form_state['build_info']['args'][0]) && !empty($form_state['build_info']['args'][0]->item_id)) {
$item_id = $form_state['build_info']['args'][0]->item_id;
$paragraph = paragraphs_item_load($item_id);
$paragraph->deleteRevision();
// Check entity_delete_multiple('paragraphs_item', array($item_id)).
$commands['#commands'][] = ajax_command_replace('.paragraphs-item-' . $item_id, '<div class=\'messages status paragraphs-modal-admin-message\'>Paragraph has been removed.</div>');
$commands['#commands'][] = array(
'command' => 'paragraphs_modal_admin_message_hide',
);
}
return $commands;
}
/**
* Page callback: Form constructor for modal Paragraph unpublishing.
*
* @see paragraphs_menu()
* @see paragraphs_modal_admin_unpublish_confirm_submit()
*/
function paragraphs_modal_admin_unpublish_confirm($form, &$form_state, $paragraphs_item = NULL) {
if (empty($paragraphs_item) && !empty($form['paragraphs_item'])) {
$paragraphs_item = $form['paragraphs_item'];
}
$form['item_id'] = array(
'#type' => 'value',
'#value' => $paragraphs_item->item_id,
);
$bundle = paragraphs_bundle_load($paragraphs_item->bundle);
if ($bundle->allow_unpublish) {
$form['description'] = array(
'#markup' => t('Are you sure you want to unpublish this !type? (You will need to edit the content from the administrative side if you wish to republish it in the future.)', array('!type' => $bundle->name)),
);
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Unpublish'),
'#ajax' => array(
'callback' => 'paragraphs_modal_admin_unpublish_confirm_submit',
'effect' => 'fade',
),
);
}
else {
$form['description'] = array(
'#markup' => t('Unpublishing is not configured for this !type. (You configure the Paragraphs type from the administrative side if you wish to allow unpublishing.)', array('!type' => $bundle->name)),
);
}
return $form;
}
/**
* Form submission handler for paragraphs_modal_admin_unpublish_confirm().
*
* @see paragraphs_modal_admin_unpublish_confirm()
*/
function paragraphs_modal_admin_unpublish_confirm_submit($form, &$form_state) {
// Close modal dialog upon submit.
$commands = array(
'#type' => 'ajax',
'#commands' => array(),
);
$commands['#commands'][] = ajax_command_close_modal_dialog();
// Unpublish paragraph item and refresh display.
if (is_object($form_state['build_info']['args'][0]) && !empty($form_state['build_info']['args'][0]->item_id)) {
$item_id = $form_state['build_info']['args'][0]->item_id;
$paragraph = paragraphs_item_load($item_id);
$paragraph->status = 0;
$paragraph->save();
$commands['#commands'][] = ajax_command_replace('.paragraphs-item-' . $item_id, '<div class=\'messages status paragraphs-modal-admin-message\'>Paragraph has been unpublished.</div>');
$commands['#commands'][] = array(
'command' => 'paragraphs_modal_admin_message_hide',
);
}
return $commands;
}
/**
* Separate paragraphs_item add form.
*
* Add a new paragraphs item above the submitted paragraphs item.
*/
function paragraphs_modal_admin_add_form($form, &$form_state, $mode, $paragraphs_bundle, $host_entity_type, $entity_id, $field_name) {
$new_paragraph_position = 0;
// If we start from an empty paragraph field.
if ($mode == 'scratch') {
// Load the host entity for the new paragraph.
$host_entity = entity_load($host_entity_type, array($entity_id));
$host_entity = reset($host_entity);
}
// Inserting a paragraph before or after an existing paragraph.
else {
// Load the existing paragraphs item.
$paragraphs_item = paragraphs_item_load_multiple(array($entity_id));
$paragraphs_item = reset($paragraphs_item);
$host_entity = $paragraphs_item->hostEntity();
$field_name = $paragraphs_item->field_name;
// If we are inserting the new paragraph after an existing one.
if ($mode == 'after' && $paragraphs_item) {
// Load the paragraphs field.
$paragraphs = field_get_items($host_entity_type, $host_entity, $paragraphs_item->field_name);
// Calculate the position of the new paragraph (delta).
foreach ($paragraphs as $key => $value) {
if ($value['value'] === $paragraphs_item->item_id) {
// After the existing one.
$new_paragraph_position = $key + 1;
break;
}
}
}
}
// Create the new paragraph item and set the host entity + position.
$new_paragraph = entity_create('paragraphs_item', array(
'bundle' => $paragraphs_bundle->bundle,
'field_name' => $field_name,
));
$new_paragraph->setHostEntity($host_entity_type, $host_entity, $host_entity->langcode, FALSE);
$new_paragraph->position = $new_paragraph_position;
// Nice title for the creation page.
backdrop_set_title(t('Create !bundle paragraph', array('!bundle' => $paragraphs_bundle->name)));
// Pass variables in form for usage in submit handler.
$form['paragraphs_bundle'] = array(
'#type' => 'value',
'#value' => $paragraphs_bundle,
);
$form['paragraphs_item'] = array(
'#type' => 'value',
'#value' => $new_paragraph,
);
$form['host_entity'] = array(
'#type' => 'value',
'#value' => $host_entity,
);
$form['host_entity_type'] = array(
'#type' => 'value',
'#value' => $host_entity_type,
);
$form['mode'] = array(
'#type' => 'value',
'#value' => $mode,
);
// Attach the form to create the new paragraphs item.
field_attach_form('paragraphs_item', $new_paragraph, $form, $form_state);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#weight' => 10000,
'#value' => t('Save'),
'#ajax' => array(
'callback' => 'paragraphs_modal_admin_add_form_close',
'effect' => 'fade',
),
);
return $form;
}
/**
* Submit function for add entity form.
*/
function paragraphs_modal_admin_add_form_submit($form, &$form_state) {
// Get data from form object.
$paragraphs_item = $form['paragraphs_item']['#value'];
$host_entity = $form['host_entity']['#value'];
$host_entity_type = $form['host_entity_type']['#value'];
$mode = $form['mode']['#value'];
// We have to perform a host save if we are adding the first paragraph in the paragraph field.
$skip_host_save = $mode == 'scratch' ? FALSE : TRUE;
// Set host entity for new paragraph.
$paragraphs_item->setHostEntity($host_entity_type, $host_entity, $host_entity->langcode, FALSE);
// Attach submit handler for paragraph items + save paragraph item.
field_attach_submit('paragraphs_item', $paragraphs_item, $form, $form_state);
$paragraphs_item->save($skip_host_save);
// If a paragraphs item is passed, we want to add the new paragraph after it.
if ($paragraphs_item && $mode != 'scratch') {
$new_paragraph = array(
array(
'value' => $paragraphs_item->item_id,
'revision_id' => $paragraphs_item->revision_id,
),
);
// Load the paragraphs field from the host entity and insert the new paragraph item in the correct position.
if ($paragraphs = field_get_items($host_entity_type, $host_entity, $paragraphs_item->field_name)) {
array_splice($paragraphs, $paragraphs_item->position, 0, $new_paragraph);
}
// Overwrite the paragraph field + save the host entity.
$field_name = $paragraphs_item->field_name;
$host_entity->{$field_name}[LANGUAGE_NONE] = $paragraphs;
$host_entity->save();
}
// Nice confirmation message.
$paragraphs_bundle = paragraphs_bundle_load($paragraphs_item->bundle);
backdrop_set_message(t('Paragraph !bundle has been saved.', array('!bundle' => $paragraphs_bundle->name)));
}
/**
* Close Ajax modal popup for adding a Paragraphs item.
*/
function paragraphs_modal_admin_add_form_close($form, &$form_state) {
// Close modal dialog upon submit.
$commands = array(
'#type' => 'ajax',
'#commands' => array(),
);
$commands['#commands'][] = ajax_command_close_modal_dialog();
$paragraphs_item = $form_state['values']['paragraphs_item'];
$host_entity_type = $form_state['values']['host_entity_type'];
$host_entity_id = $paragraphs_item->hostEntityId();
$field_name = $paragraphs_item->hostEntityBundle();
// Refresh display of field that was added.
if (!empty($host_entity_id) && !empty($host_entity_type)) {
$field_name = $paragraphs_item->field_name;
$host_entity = entity_load($host_entity_type, $host_entity_id);
$fresh_field = field_view_field($host_entity_type, $host_entity, $field_name);
$new_element = '<div class=\'messages status paragraphs-modal-admin-message\'>' . t('New paragraph has been added.') . '</div>';
$new_element .= backdrop_render($fresh_field);
$commands['#commands'][] = ajax_command_html('.paragraphs-items-' . strtr($field_name, '_', '-'), $new_element);
$commands['#commands'][] = array(
'command' => 'paragraphs_modal_admin_message_hide',
);
}
return $commands;
}