-
Notifications
You must be signed in to change notification settings - Fork 11
/
paragraphs.module
2049 lines (1852 loc) · 69.2 KB
/
paragraphs.module
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
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Paragraphs hooks and common functions.
*
* Paragraphs allows you to embed multiple entity bundles in field.
*/
define('PARAGRAPHS_RECURSION_LIMIT', 20);
define('PARAGRAPHS_DEFAULT_TITLE', 'Paragraph');
define('PARAGRAPHS_DEFAULT_TITLE_MULTIPLE', 'Paragraphs');
define('PARAGRAPHS_DEFAULT_EDIT_MODE', 'open');
define('PARAGRAPHS_DEFAULT_EDIT_MODE_OVERRIDE', 1);
define('PARAGRAPHS_DEFAULT_ADD_MODE', 'select');
define('PARAGRAPHS_DEFAULT_MODAL_ADMIN', FALSE);
// Modules should return this value from hook_paragraphs_item_access() to allow
// access to a paragraphs item.
define('PARAGRAPHS_ITEM_ACCESS_ALLOW', 'allow');
// Modules should return this value from hook_paragraphs_item_access() to deny
// access to a paragraphs item.
define('PARAGRAPHS_ITEM_ACCESS_DENY', 'deny');
// Modules should return this value from hook_paragraphs_item_access() to not
// affect paragraphs item access.
define('PARAGRAPHS_ITEM_ACCESS_IGNORE', NULL);
// Separate some Field API parts in different files.
require_once dirname(__FILE__) . '/paragraphs.field_formatter.inc';
require_once dirname(__FILE__) . '/paragraphs.field_widget.inc';
require_once dirname(__FILE__) . '/paragraphs.node_clone.inc';
/**
* Loads a paragraphs item.
*
* @param int $item_id
* The paragraphs item ID.
* @param bool $reset
* Should we reset the entity cache?
*
* @return ParagraphsItemEntity
* The paragraphs item entity or FALSE.
*/
function paragraphs_item_load($item_id, $reset = FALSE) {
$result = paragraphs_item_load_multiple(array($item_id), array(), $reset);
return $result ? reset($result) : FALSE;
}
/**
* Loads a paragraphs revision.
*
* @param int $revision_id
* The paragraphs revision ID.
*
* @return ParagraphsItemEntity
* The paragraphs item entity or FALSE.
*/
function paragraphs_item_revision_load($revision_id) {
return entity_plus_revision_load('paragraphs_item', $revision_id);
}
/**
* Loads paragraphs items.
*
* @param array $ids
* An array of paragraphs item IDs or FALSE to load all.
* @param array|bool $conditions
* Should we reset the entity cache?
* @param bool $reset
* Should we reset the entity cache?
*
* @return ParagraphsItemEntity[]
* An array of paragraphs item entities.
*/
function paragraphs_item_load_multiple(array $ids = array(), $conditions = array(), $reset = FALSE) {
return entity_load_multiple('paragraphs_item', $ids, $conditions, $reset);
}
/**
* Implements hook_config_info().
*/
function paragraphs_config_info() {
return array(
'paragraphs.type' => array(
'name_key' => 'bundle',
'label_key' => 'name',
'group' => t('Paragraph types'),
),
);
}
/**
* Implements hook_entity_info().
*/
function paragraphs_entity_info() {
$return['paragraphs_item'] = array(
'label' => t('Paragraphs item'),
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'entity class' => 'ParagraphsItemEntity',
'controller class' => 'ParagraphsItemEntityController',
'base table' => 'paragraphs_item',
'revision table' => 'paragraphs_item_revision',
'fieldable' => TRUE,
// For integration with Redirect module.
// @see http://drupal.org/node/1263884
'redirect' => FALSE,
'entity keys' => array(
'id' => 'item_id',
'label' => 'label',
'revision' => 'revision_id',
'bundle' => 'bundle',
'field_name' => 'field_name',
'language' => 'langcode',
),
'module' => 'paragraphs',
'view modes' => array(
'full' => array(
'label' => t('Full content'),
'custom settings' => FALSE,
),
'paragraphs_editor_preview' => array(
'label' => t('Paragraphs Editor Preview'),
'custom settings' => TRUE,
),
),
'bundle keys' => array(
'bundle' => 'bundle',
),
'access callback' => 'paragraphs_item_access',
'metadata controller class' => 'ParagraphsItemMetadataController',
);
$bundles = paragraphs_bundle_load();
// Add info about the bundles. We do not use field_info_fields() but directly
// use field_read_fields() as field_info_fields() requires built entity info
// to work.
foreach ($bundles as $machine_name => $bundle) {
$return['paragraphs_item']['bundles'][$bundle->bundle] = array(
'label' => $bundle->name,
'admin' => array(
'path' => 'admin/structure/paragraphs/%paragraphs_bundle',
'real path' => 'admin/structure/paragraphs/' . strtr($machine_name, array('_' => '-')),
'bundle argument' => 3,
'access arguments' => array('administer paragraphs bundles'),
),
);
}
$return['paragraphs_item']['field cache'] = FALSE;
$return['paragraphs_item']['entity cache'] = TRUE;
return $return;
}
/**
* Access check for Paragraphs items.
*
* Most of the time the access callback is on the host entity.
* In some cases you want specific access checks on paragraphs.
* You can do this by implementing hook_paragraphs_item_access().
*
* @return bool
* Whether the user has access to a Paragraphs item.
*/
function paragraphs_item_access($op, $entity, $account = NULL) {
// If no user object is supplied, the access check is for the current user.
if (empty($account)) {
$account = $GLOBALS['user'];
}
$permissions = &backdrop_static(__FUNCTION__, array());
// If the $op was not one of the supported ones, we return access denied.
if (!in_array($op, array('view', 'update', 'delete', 'create'), TRUE)) {
return FALSE;
}
// If $op view and Paragraphs item not published, we return access denied.
if ($op == 'view' && !$entity->status) {
return FALSE;
}
// When we have no entity, create a generic cid.
if (empty($entity) || !is_object($entity)) {
$cid = 'all_entities:' . $op;
}
// When OP is create, or the entity is new, the bundle is the cache key.
elseif ($op == 'create' || (isset($entity->is_new) && $entity->is_new)) {
$cid = $entity->bundle;
}
// Else our cid is entity specific.
else {
$cid = $entity->item_id . '_' . $entity->revision_id;
}
// If we've already checked access for this bundle, user and op, return from
// cache. Otherwise, we are optimistic and consider that the user can
// view / update / delete or create a paragraph.
if (isset($permissions[$account->uid][$cid][$op])) {
return $permissions[$account->uid][$cid][$op];
}
// We grant access to the paragraph item if both of these conditions are met:
// - No modules say to deny access.
// - At least one module says to grant access.
// If no module specified either allow or deny, we always allow.
$access = module_invoke_all('paragraphs_item_access', $entity, $op, $account);
if (in_array(PARAGRAPHS_ITEM_ACCESS_DENY, $access, TRUE)) {
$user_access_permission = FALSE;
}
elseif (in_array(PARAGRAPHS_ITEM_ACCESS_ALLOW, $access, TRUE)) {
$user_access_permission = TRUE;
}
else {
// Deny access by default.
$user_access_permission = FALSE;
}
// Store the result of the permission in our matrix.
$permissions[$account->uid][$cid][$op] = $user_access_permission;
return $permissions[$account->uid][$cid][$op];
}
/**
* Implements hook_paragraphs_item_access().
*/
function paragraphs_paragraphs_item_access(ParagraphsItemEntity $entity, $op, $account) {
$permissions = &backdrop_static(__FUNCTION__, array());
$parent_permissions = &backdrop_static(__FUNCTION__ . '_parents', array());
if (!in_array($op, array('view', 'update', 'delete', 'create'), TRUE)) {
// If there was no bundle to check against, or the $op was not one of the
// supported ones, we return access denied.
return PARAGRAPHS_ITEM_ACCESS_IGNORE;
}
$check_parent_op = $op;
// Update/Delete/Create access requires update access on the parent.
if (in_array($op, array('update', 'delete', 'create'), TRUE)) {
$check_parent_op = 'update';
}
// If $op view and Paragraphs item not published, we return access denied.
if ($op == 'view' && !$entity->status) {
return FALSE;
}
// When we have no entity, create a generic cid.
if (empty($entity)) {
$cid = 'all_entities:' . $op;
}
// When OP is create, or the entity is new, the bundle is the cache key.
elseif ($op == 'create' || (isset($entity->is_new) && $entity->is_new)) {
$cid = $entity->bundle;
}
// Else our cid is entity specific.
else {
$cid = $entity->item_id . '_' . $entity->revision_id;
}
// Check if we cached permission check.
if (isset($permissions[$account->uid][$cid][$op])) {
return $permissions[$account->uid][$cid][$op];
}
if (empty($entity)) {
// Ignore access when we don't have a host entity.
$permissions[$account->uid][$cid][$op] = PARAGRAPHS_ITEM_ACCESS_IGNORE;
}
elseif ($host_entity = $entity->hostEntity()) {
$host_entity_info = entity_get_info($entity->hostEntityType());
$host_id_key = $host_entity_info['entity keys']['id'];
$parent_cid = $entity->hostEntityType() . '_' . implode('_', entity_extract_ids($entity->hostEntityType(), $host_entity));
// Check if we have an ID key set. If not then the parent entity is new and
// we check for create access.
if (!isset($host_entity->{$host_id_key}) || empty($host_entity->{$host_id_key})) {
$check_parent_op = 'create';
$host_entity->is_new = TRUE;
}
if (isset($parent_permissions[$account->uid][$parent_cid][$check_parent_op])) {
return $parent_permissions[$account->uid][$parent_cid][$check_parent_op];
}
// We need to use node_access over node's entity_access permission check
// because entity_access will fallback to checking revision permissions when
// trying to update paragraph items in a node revision.
if ($entity->hostEntityType() == 'node') {
// We assume that any node without a nid is being created and check
// permissions against the node bundle.
if (!isset($host_entity->nid) && node_access($check_parent_op, $entity->hostEntityBundle())) {
$permissions[$account->uid][$cid][$op] = PARAGRAPHS_ITEM_ACCESS_ALLOW;
}
elseif (isset($host_entity->nid) && node_access($check_parent_op, $host_entity)) {
$permissions[$account->uid][$cid][$op] = PARAGRAPHS_ITEM_ACCESS_ALLOW;
}
else {
$permissions[$account->uid][$cid][$op] = PARAGRAPHS_ITEM_ACCESS_DENY;
}
}
else {
if (entity_access($check_parent_op, $entity->hostEntityType(), $host_entity)) {
$permissions[$account->uid][$cid][$op] = PARAGRAPHS_ITEM_ACCESS_ALLOW;
$parent_permissions[$account->uid][$parent_cid][$check_parent_op] = PARAGRAPHS_ITEM_ACCESS_ALLOW;
}
else {
// Deny access as parent entity access failed.
$permissions[$account->uid][$cid][$op] = PARAGRAPHS_ITEM_ACCESS_DENY;
$parent_permissions[$account->uid][$parent_cid][$check_parent_op] = PARAGRAPHS_ITEM_ACCESS_DENY;
}
}
}
else {
// Ignore access when we don't have a host entity.
$permissions[$account->uid][$cid][$op] = PARAGRAPHS_ITEM_ACCESS_IGNORE;
}
return $permissions[$account->uid][$cid][$op];
}
/**
* Implements hook_permission().
*/
function paragraphs_permission() {
$perms = array(
'administer paragraphs bundles' => array(
'title' => t('Administer Paragraph types'),
'description' => t('Is able to administer Paragraph types for the Paragraphs module'),
),
);
return $perms;
}
/**
* Implements hook_menu().
*/
function paragraphs_menu() {
$items = array();
$items['admin/structure/paragraphs'] = array(
'title' => 'Paragraphs types',
'description' => 'Manage Paragraph types',
'page callback' => 'paragraphs_admin_bundle_overview',
'access arguments' => array('administer paragraphs bundles'),
'file' => 'paragraphs.admin.inc',
);
$items['admin/structure/paragraphs/list'] = array(
'title' => 'List Paragraphs types',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/structure/paragraphs/add'] = array(
'title' => 'Add Paragraphs type',
'page callback' => 'backdrop_get_form',
'page arguments' => array('paragraphs_admin_bundle_form'),
'access arguments' => array('administer paragraphs bundles'),
'type' => MENU_LOCAL_ACTION,
'file' => 'paragraphs.admin.inc',
);
$items['admin/structure/paragraphs/%paragraphs_bundle'] = array(
'title' => 'Edit Paragraphs type',
'title callback' => 'paragraphs_bundle_title_callback',
'title arguments' => array(3),
'page callback' => 'backdrop_get_form',
'page arguments' => array('paragraphs_admin_bundle_form', 3),
'access arguments' => array('administer paragraphs bundles'),
'file' => 'paragraphs.admin.inc',
);
$items['admin/structure/paragraphs/%paragraphs_bundle/configure'] = array(
'title' => 'Configure',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/structure/paragraphs/%paragraphs_bundle/clone'] = array(
'title' => 'Clone Paragraphs type',
'access arguments' => array('administer paragraphs bundles'),
'page callback' => 'backdrop_get_form',
'page arguments' => array('paragraphs_admin_bundle_clone_create_form', 3),
'file' => 'paragraphs.type_clone.inc',
'type' => MENU_VISIBLE_IN_BREADCRUMB,
);
$items['admin/structure/paragraphs/%paragraphs_bundle/delete'] = array(
'title' => 'Delete Paragraphs type',
'page callback' => 'backdrop_get_form',
'page arguments' => array('paragraphs_admin_bundle_delete_form', 3),
'access arguments' => array('administer paragraphs bundles'),
'type' => MENU_VISIBLE_IN_BREADCRUMB,
'file' => 'paragraphs.admin.inc',
);
$items['paragraphs/%paragraphs_item/edit'] = array(
'title' => 'Edit',
'type' => MENU_LOCAL_ACTION,
'page callback' => 'backdrop_get_form',
'page arguments' => array('paragraphs_modal_admin_edit', 1),
'access callback' => 'paragraphs_modal_admin_access',
'access arguments' => array(1),
'file' => 'paragraphs.admin.inc',
);
$items['paragraphs/%paragraphs_item/remove'] = array(
'title' => 'Remove',
'type' => MENU_LOCAL_ACTION,
'page callback' => 'backdrop_get_form',
'page arguments' => array('paragraphs_modal_admin_remove_confirm', 1),
'access callback' => 'paragraphs_modal_admin_access',
'access arguments' => array(1),
'file' => 'paragraphs.admin.inc',
);
$items['paragraphs/%paragraphs_item/unpublish'] = array(
'title' => 'Unpublish',
'type' => MENU_LOCAL_ACTION,
'page callback' => 'backdrop_get_form',
'page arguments' => array('paragraphs_modal_admin_unpublish_confirm', 1),
'access callback' => 'paragraphs_modal_admin_access',
'access arguments' => array(1),
'file' => 'paragraphs.admin.inc',
);
$items['paragraphs/%paragraphs_item/sort'] = array(
'title' => 'Sort',
'type' => MENU_LOCAL_ACTION,
'page callback' => 'backdrop_get_form',
'page arguments' => array('paragraphs_modal_admin_sort_form', 1),
'access callback' => 'paragraphs_modal_admin_access',
'access arguments' => array(1),
'file' => 'paragraphs.admin.inc',
);
$items['paragraphs/add/%/%paragraphs_bundle/%/%/%'] = array(
'title' => 'Add paragraph',
'title callback' => 'paragraphs_add_title',
'title arguments' => array(3),
'type' => MENU_LOCAL_ACTION,
'page callback' => 'backdrop_get_form',
'page arguments' => array('paragraphs_modal_admin_add_form', 2, 3, 4, 5, 6),
'access callback' => 'paragraphs_separate_create_access',
'access arguments' => array(2, 3, 4, 5, 6),
'file' => 'paragraphs.admin.inc',
);
return $items;
}
/**
* Title callback for separate add page.
*/
function paragraphs_add_title($paragraphs_bundle) {
return t('Add !bundle', array('!bundle' => $paragraphs_bundle->name));
}
/**
* Implements hook_admin_bar_map().
*/
function paragraphs_admin_bar_map() {
if (!user_access('administer paragraphs bundles')) {
return;
}
$types = paragraphs_bundle_load();
$type_keys = array();
foreach ($types as $key => $type) {
$type_keys[] = str_replace('_', '-', $key);
}
$map = array(
'admin/structure/paragraphs/%paragraphs_bundle' => array(
'parent' => 'admin/structure/paragraphs',
'arguments' => array(
array('%paragraphs_bundle' => $type_keys),
),
),
'admin/structure/paragraphs/%paragraphs_bundle/edit' => array(
'parent' => 'admin/structure/paragraphs/%paragraphs_bundle',
'arguments' => array(
array('%paragraphs_bundle' => $type_keys),
),
),
);
return $map;
}
/**
* Implements hook_field_info().
*/
function paragraphs_field_info() {
$info = array();
$info['paragraphs'] = array(
'label' => t('Paragraphs'),
'description' => t('Paragraphs field using the Paragraph types.'),
'instance_settings' => array(
'title' => PARAGRAPHS_DEFAULT_TITLE,
'title_multiple' => PARAGRAPHS_DEFAULT_TITLE_MULTIPLE,
'allowed_bundles' => array(),
'bundle_weights' => array(),
),
'default_widget' => 'paragraphs_embed',
'default_formatter' => 'paragraphs_view',
'settings' => array(),
'property_type' => 'paragraphs_item',
'property_callbacks' => array('paragraphs_entity_metadata_property_callback'),
);
return $info;
}
/**
* Implements hook_icon_info().
*/
function paragraphs_icon_info() {
$icons = array(
'paragraphs-add' => array(),
'paragraphs-edit' => array(),
'paragraphs-remove' => array(),
'paragraphs-unpublish' => array(),
'paragraphs-sort' => array(),
'paragraph' => array(),
);
return $icons;
}
/**
* Implements hook_library_info().
*/
function paragraphs_library_info() {
$libraries = array();
$module_path = backdrop_get_path('module', 'paragraphs');
$info = system_get_info('module', 'paragraphs');
$libraries['paragraphs_modal_admin'] = array(
'title' => 'Paragraphs Modal Admin',
'version' => $info['version'],
'js' => array(
$module_path . '/js/paragraphs_modal_admin.js' => array(
'type' => 'file',
'scope' => 'footer',
),
),
'css' => array(
$module_path . '/css/paragraphs_modal_admin.css' => array(),
),
'icons' => array(
'paragraphs-add',
'paragraphs-edit',
'paragraphs-remove',
'paragraphs-unpublish',
'paragraphs-sort',
'paragraph',
),
'dependencies' => array(
array('system', 'backdrop.dialog.ajax'),
),
);
return $libraries;
}
/**
* Implements hook_form_field_ui_field_edit_form_alter().
*/
function paragraphs_form_field_ui_field_edit_form_alter(&$form, $form_state) {
if ($form['#field']['type'] == 'paragraphs') {
$form['#theme'] = array('paragraphs_bundle_settings_form');
array_unshift($form['#submit'], 'paragraphs_bundle_settings_form_submit');
}
elseif ($form['#instance']['entity_type'] == 'paragraphs_item') {
$form['actions']['delete']['#submit'][] = 'paragraphs_field_edit_form_submit';
}
}
/**
* Submit callback for paragraphs field settings form.
*
* @see paragraphs_form_field_ui_field_edit_form_alter
*/
function paragraphs_field_edit_form_submit($form, &$form_state) {
$instance = $form['#instance'];
$destination = backdrop_get_destination();
$form_state['redirect'] = array('admin/structure/paragraphs/' . $instance['bundle'] . '/fields/' . $instance['field_name'] . '/delete', array('query' => $destination));
}
/**
* Submit callback for paragraphs bundle settings form.
*
* @see paragraphs_form_field_ui_field_edit_form_alter
*/
function paragraphs_bundle_settings_form_submit($form, &$form_state) {
$bundle_settings = array();
$bundle_weights = array();
if (isset($form_state['values']['instance']['settings']['allowed_bundles_table'])) {
$bundle_settings_table = $form_state['values']['instance']['settings']['allowed_bundles_table'];
backdrop_sort($bundle_settings_table, array('#weight'));
foreach ($bundle_settings_table as $machine_name => $value) {
$bundle_settings[$machine_name] = (($value['enabled'] === 1) ? $machine_name : -1);
$bundle_weights[$machine_name] = $value['weight'];
}
}
$form_state['values']['instance']['settings']['allowed_bundles'] = $bundle_settings;
$form_state['values']['instance']['settings']['bundle_weights'] = $bundle_weights;
unset($form_state['values']['instance']['settings']['allowed_bundles_table']);
}
/**
* Implements hook_field_instance_settings_form().
*/
function paragraphs_field_instance_settings_form($field, $instance) {
$settings = $instance['settings'];
$bundles = array();
$_bundles = paragraphs_bundle_load();
$form_delta = count($_bundles) * 2;
$max_weight = 0;
$weights = array();
foreach ($_bundles as $machine_name => $bundle) {
$bundles[$machine_name] = $bundle->name;
if (isset($settings['bundle_weights'][$machine_name])) {
$weights[$machine_name] = $settings['bundle_weights'][$machine_name];
if ($settings['bundle_weights'][$machine_name] > $max_weight) {
$max_weight = $settings['bundle_weights'][$machine_name];
}
}
}
$max_weight++;
$element['allowed_bundles_table'] = array(
'#tree' => TRUE,
'#prefix' => '<label>' . t('Allowed Paragraph types') . '</label>',
'#suffix' => '<div class="description">' . t('If no Paragraph type is selected, all the types will be available.') . '</div>',
);
$weight = 1;
foreach ($_bundles as $machine_name => $bundle) {
$enabled = FALSE;
if (isset($settings['allowed_bundles'][$machine_name]) && $settings['allowed_bundles'][$machine_name] === $machine_name) {
$enabled = TRUE;
}
$element['allowed_bundles_table'][$machine_name] = array(
'enabled' => array(
'#type' => 'checkbox',
'#title' => check_plain($bundle->name) . ' <em>(' . check_plain($bundle->label) . ')</em>',
'#title_display' => 'after',
'#default_value' => $enabled,
),
'weight' => array(
'#type' => 'weight',
'#title' => t('Weight'),
'#default_value' => (isset($weights[$machine_name]) ? $weights[$machine_name] : $weight + $max_weight),
'#delta' => $form_delta,
'#title_display' => 'invisible',
),
);
$element['allowed_bundles_table'][$machine_name]['#weight'] = $element['allowed_bundles_table'][$machine_name]['weight']['#default_value'];
$weight++;
}
$element['title'] = array(
'#type' => 'textfield',
'#title' => t('Item Title'),
'#description' => t('Label to appear as title on the button as "Add new [title]", this label is translatable'),
'#default_value' => isset($settings['title']) ? $settings['title'] : PARAGRAPHS_DEFAULT_TITLE,
'#required' => TRUE,
);
$element['title_multiple'] = array(
'#type' => 'textfield',
'#title' => t('Plural Item Title'),
'#description' => t('Title in its plural form.'),
'#default_value' => isset($settings['title_multiple']) ? $settings['title_multiple'] : PARAGRAPHS_DEFAULT_TITLE_MULTIPLE,
'#required' => TRUE,
);
$element['default_edit_mode'] = array(
'#type' => 'select',
'#title' => t('Default edit mode'),
'#description' => t('The default edit mode the paragraph item is in. Preview will render the paragraph in the preview view mode.'),
'#options' => array(
'open' => t('Open'),
'closed' => t('Closed'),
'preview' => t('Preview'),
),
'#default_value' => isset($settings['default_edit_mode']) ? $settings['default_edit_mode'] : PARAGRAPHS_DEFAULT_EDIT_MODE,
'#required' => TRUE,
);
$element['default_edit_mode_override'] = array(
'#type' => 'number',
'#title' => t('Edit mode override'),
'#title_display' => 'invisible',
'#field_prefix' => t('Force items to display as "Open" when there are less then'),
'#field_suffix' => t('items.'),
'#default_value' => (isset($settings['default_edit_mode_override']) && !empty($settings['default_edit_mode_override'])) ? $settings['default_edit_mode_override'] : PARAGRAPHS_DEFAULT_EDIT_MODE_OVERRIDE,
'#states' => array(
// Hide the settings when the the default edit mode is set to "Open.".
'invisible' => array(
':input[name="instance[settings][default_edit_mode]"]' => array('value' => 'open'),
),
),
'#size' => 2,
'#maxlength' => 2,
// Specify width, since size property isn't supported on number fields.
'#attributes' => array('style' => 'width:3em'),
'#min' => 1,
'#max' => 99,
);
$element['add_mode'] = array(
'#type' => 'select',
'#title' => t('Add mode'),
'#description' => t('The way to add new paragraphs.'),
'#options' => array(
'select' => t('Select List'),
'button' => t('Buttons'),
),
'#default_value' => isset($settings['add_mode']) ? $settings['add_mode'] : PARAGRAPHS_DEFAULT_ADD_MODE,
'#required' => TRUE,
);
$element['modal_admin'] = array(
'#type' => 'checkbox',
'#title' => t('Enable modal administration (experimental)'),
'#description' => t('Enables a dropbutton per paragraph for performing administrative tasks in a modal dialog on the front end.'),
'#default_value' => isset($settings['modal_admin']) ? $settings['modal_admin'] : PARAGRAPHS_DEFAULT_MODAL_ADMIN,
);
if (!count($bundles)) {
$link = l(t('click here to add one'), 'admin/structure/paragraphs/add', array('query' => backdrop_get_destination()));
$element['allowed_bundles_explain'] = array(
'#type' => 'markup',
'#markup' => t('You did not add any Paragraph types yet, !clickhere.', array('!clickhere' => $link)),
);
}
$element['fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Default value'),
'#collapsible' => FALSE,
// As field_ui_default_value_widget() does, we change the #parents so that
// the value below is writing to $instance in the right location.
'#parents' => array('instance'),
);
// Be sure to set the default value to NULL, for example to repair old fields
// that still have one.
$element['fieldset']['default_value'] = array(
'#type' => 'value',
'#value' => NULL,
);
$element['fieldset']['content'] = array(
'#pre' => '<p>',
'#markup' => t('To specify a default value, configure it via the regular default value setting of each field that is part of the Paragraph type. To do so, go to the <a href="!url">Manage fields</a> screen of the Paragraph type.', array('!url' => url('admin/structure/paragraphs'))),
'#suffix' => '</p>',
);
return $element;
}
/**
* Theme function for paragraphs bundle settings form.
*
* @see paragraphs_form_field_ui_field_edit_form_alter
*/
function theme_paragraphs_bundle_settings_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['instance']['settings']['allowed_bundles_table'], TRUE) as $id) {
// Before we add our 'weight' column to the row, we need to give the
// element a custom class so that it can be identified in the
// backdrop_add_tabledrag call.
//
// This could also have been done during the form declaration by adding
// '#attributes' => array('class' => 'example-item-weight'),
// directy to the 'weight' element in tabledrag_example_simple_form().
$form['instance']['settings']['allowed_bundles_table'][$id]['weight']['#attributes']['class'] = array('paragraphs-bundle-item-weight');
// We are now ready to add each element of our $form data to the $rows
// array, so that they end up as individual table cells when rendered
// in the final table. We run each element through the backdrop_render()
// function to generate the final html markup for that element.
$rows[] = array(
'data' => array(
// Add our 'enabled' column.
backdrop_render($form['instance']['settings']['allowed_bundles_table'][$id]['enabled']),
// Add our 'weight' column.
backdrop_render($form['instance']['settings']['allowed_bundles_table'][$id]['weight']),
),
// To support the tabledrag behaviour, we need to assign each row of the
// table a class attribute of 'draggable'. This will add the 'draggable'
// class to the <tr> element for that row when the final table is
// rendered.
'class' => array('draggable'),
);
}
// We now define the table header values. Ensure that the 'header' count
// matches the final column count for your table.
$header = array(t('Paragraph Type'), t('Weight'));
// We also need to pass the backdrop_add_tabledrag() function an id which will
// be used to identify the <table> element containing our tabledrag form.
// Because an element's 'id' should be unique on a page, make sure the value
// you select is NOT the same as the form ID used in your form declaration.
$table_id = backdrop_html_id('paragraphs-bundle-table');
// We can render our tabledrag table for output.
$output = theme('table', array(
'header' => $header,
'rows' => $rows,
'attributes' => array('id' => $table_id),
));
$form['instance']['settings']['allowed_bundles_table']['#markup'] = $output;
// And then render any remaining form elements (such as our submit button).
$output = backdrop_render_children($form);
// We now call the backdrop_add_tabledrag() function in order to add the
// tabledrag.js goodness onto our page.
//
// For a basic sortable table, we need to pass it:
// - the $table_id of our <table> element,
// - the $action to be performed on our form items ('order'),
// - a string describing where $action should be applied ('siblings'),
// - and the class of the element containing our 'weight' element.
backdrop_add_tabledrag($table_id, 'order', 'sibling', 'paragraphs-bundle-item-weight');
return $output;
}
/**
* Implements hook_field_settings_form().
*/
function paragraphs_field_settings_form($field, $instance) {
$form = array();
return $form;
}
/**
* Implements hook_field_presave().
*
* Support saving paragraph items in @code $item['entity'] @endcode. This
* may be used to seamlessly create paragraph items during host-entity
* creation or to save changes to the host entity and its collections at once.
*/
function paragraphs_field_presave($host_entity_type, $host_entity, $field, $instance, $langcode, &$items) {
$top_host = $host_entity;
while (method_exists($top_host, 'hostEntity')) {
$top_host = $top_host->hostEntity();
}
// Prevent workbench moderation from deleting paragraphs on node_save()
// during workbench_moderation_store(), when $host_entity->revision == 0.
if (!empty($top_host->workbench_moderation['updating_live_revision'])) {
return;
}
foreach ($items as $key => &$item) {
// In case the entity has been changed / created, save it and set the id.
// If the host entity creates a new revision, save new item-revisions as
// well.
$entity = FALSE;
if (isset($item['entity'])) {
$entity = paragraphs_field_get_entity($item);
}
elseif (isset($item['revision_id'])) {
$entity = paragraphs_item_revision_load($item['revision_id']);
}
if ($entity) {
$entity->setHostEntity($host_entity_type, $host_entity, $langcode, FALSE);
// If the host entity supports revisions and is saved as new revision, do
// the same for the item.
if (!empty($host_entity->revision)) {
$entity->revision = TRUE;
$is_default = entity_plus_revision_is_default($host_entity_type, $host_entity);
// If an entity type does not support saving non-default entities,
// assume it will be saved as default.
if (!isset($is_default) || $is_default) {
$entity->default_revision = TRUE;
$entity->archived = FALSE;
}
}
if (isset($entity->removed) && $entity->removed) {
unset($items[$key]);
}
else {
// Update status from form item.
if (isset($item['status'])) {
$entity->status = $item['status'];
}
$entity->save(TRUE);
$item = array(
'value' => $entity->item_id,
'revision_id' => $entity->revision_id,
);
}
}
}
}
/**
* Implements hook_field_update().
*
* Care about removed paragraph items.
*/
function paragraphs_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
// Prevent workbench moderation from deleting paragraphs on node_save() during
// workbench_moderation_store(), when $host_entity->revision == 0.
if (!empty($entity->workbench_moderation['updating_live_revision'])) {
return;
}
// Prevent State Flow Entity from deleting paragraphs on node_save()
// when adding new paragraphs item and a published revision of the node exist.
// @see state_flow_entity_exit().
if (!empty($entity->state_flow) && isset($entity->revision) && $entity->revision === FALSE) {
return;
}
$items_original = !empty($entity->original->{$field['field_name']}[$langcode]) ? $entity->original->{$field['field_name']}[$langcode] : array();
$original_by_id = array_flip(paragraphs_field_item_to_ids($items_original));
foreach ($items as $item) {
unset($original_by_id[$item['value']]);
}
// If there are removed items, care about deleting the item entities.
if ($original_by_id) {
$ids = array_flip($original_by_id);
// If we are creating a new revision, the old-items should be kept but get
// marked as archived now.
if (!empty($entity->revision)) {
db_update('paragraphs_item')
->fields(array('archived' => 1))
->condition('item_id', $ids, 'IN')
->execute();
}
else {
// Delete unused paragraph items now.
foreach (paragraphs_item_load_multiple($ids) as $item) {
$item->setHostEntity($entity_type, $entity, $langcode, FALSE);
$item->deleteRevision(TRUE);
}
}
list($entity_id,,) = entity_extract_ids($entity_type, $entity);
entity_get_controller($entity_type)->resetCache(array($entity_id));
}
}
/**
* Implements hook_field_delete().
*/
function paragraphs_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) {
if ($field['type'] == 'paragraphs') {
// Also delete all embedded entities.
if ($ids = paragraphs_field_item_to_ids($items)) {
// We filter out entities that are still being referenced by other
// host-entities. This should never be the case, but it might happen e.g.
// when modules clone a node without knowing about paragraphs.
$entity_info = entity_get_info($entity_type);
$entity_id_name = $entity_info['entity keys']['id'];
$field_column = key($field['columns']);
// Extra check to make sure our field exists.
if (is_scalar($field)) {
$field_definition = field_info_field($field['field_name']);
if (!empty($field_definition)) {
foreach ($ids as $id_key => $id) {
$query = new EntityFieldQuery();
$entities = $query
->fieldCondition($field['field_name'], $field_column, $id)
->execute();
unset($entities[$entity_type][$entity->$entity_id_name]);
if (!empty($entities[$entity_type])) {
// Filter this $id out.
unset($ids[$id_key]);
}
}
}
}