-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpatternbuilder.module
More file actions
2065 lines (1819 loc) · 63.3 KB
/
patternbuilder.module
File metadata and controls
2065 lines (1819 loc) · 63.3 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
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
* Module for loading and parsing json schema files.
*/
// Constants.
define('PATTERNBUILDER_PROPERTY_PREFIX_TUPLE_ITEM', 'pb_tuple_item');
define('PATTERNBUILDER_PROPERTY_DELIMITER', '::');
define('PATTERNBUILDER_PROPERTY_MAP_RENDERED_NAME', 'pb_rendered');
define('PATTERNBUILDER_PROPERTY_MAP_ITEM_DELIMITER', ',');
define('PATTERNBUILDER_PROPERTY_MAP_VALUE_DELIMITER', '=');
define('PATTERNBUILDER_PROPERTY_MAP_CHAIN_DELIMITER', ':');
/**
* Implements hook_hook_info().
*/
function patternbuilder_hook_info() {
$base_info = array('group' => 'patternbuilder');
return array(
'patternbuilder_pattern_types' => $base_info,
'patternbuilder_pattern_types_alter' => $base_info,
'patternbuilder_pattern_status_info' => $base_info,
'patternbuilder_pattern_status_info_alter' => $base_info,
);
}
/**
* Implements hook_menu().
*/
function patternbuilder_menu() {
$items = array();
// Admin config page.
$items['admin/config/content/patternbuilder'] = array(
'title' => 'Pattern Builder',
'description' => 'Settings for the patternbuilder module.',
'page callback' => 'drupal_get_form',
'page arguments' => array('patternbuilder_settings_form'),
'access arguments' => array('administer site configuration'),
'file' => 'patternbuilder.admin.inc',
'weight' => -100,
);
// Paragraph pattern info.
$items['admin/structure/paragraphs/%paragraphs_bundle/patternbuilder'] = array(
'title' => 'Pattern',
'page callback' => 'drupal_get_form',
'page arguments' => array('patternbuilder_admin_bundle_form', 3),
'access callback' => 'patternbuilder_admin_bundle_form_access',
'access arguments' => array(3),
'file' => 'patternbuilder.admin.inc',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'weight' => 10,
);
return $items;
}
/**
* Implements hook_menu_alter().
*/
function patternbuilder_menu_alter(&$items) {
// Override paragaphs bundle list to add "Pattern" link.
if (isset($items['admin/structure/paragraphs'])) {
$items['admin/structure/paragraphs']['page callback'] = 'patternbuilder_paragraphs_admin_bundle_overview';
$items['admin/structure/paragraphs']['file'] = 'patternbuilder.admin.inc';
$items['admin/structure/paragraphs']['file path'] = drupal_get_path('module', 'patternbuilder');
}
}
/**
* Access callback for patternbuilder_admin_bundle_form.
*
* @param object $bundle
* The paragraph bundle object.
*/
function patternbuilder_admin_bundle_form_access($bundle) {
return !empty($bundle->bundle) && user_access('administer paragraphs bundles') &&
patternbuilder_get_bundle_component($bundle->bundle);
}
/**
* Implements hook_libraries_info().
*/
function patternbuilder_libraries_info() {
$libraries = array();
$libraries['patternbuilder'] = array(
// Only used in administrative UI of Libraries API.
'title' => t('PatternBuilder'),
'vendor url' => 'https://github.com/PatternBuilder/pattern-builder-lib-php',
'download url' => 'https://github.com/PatternBuilder/pattern-builder-lib-php',
'path' => 'vendor',
'version' => 'v1.0.0',
'files' => array(
'php' => array(
'autoload.php',
),
),
);
return $libraries;
}
/**
* Implements hook_views_api().
*/
function patternbuilder_views_api() {
return array(
'api' => '3.0',
'path' => drupal_get_path('module', 'patternbuilder') . '/views',
);
}
/**
* Invalidate all patternbuilder cache, forcing a rebuild on the next grab.
*/
function patternbuilder_invalidate_cache() {
// Fully built schemas could reference a schema that has changed so all
// cached schema objects must be purged.
$controller = patternbuilder_get();
$controller->clearAllCache();
drupal_static_reset('patternbuilder_get_schemas');
cache_clear_all('patternbuilder_schema_paths', 'cache');
drupal_static_reset('patternbuilder_get_template_paths');
drupal_static_reset('patternbuilder_get_pattern_types');
cache_clear_all('patternbuilder_drupal:pattern_types', 'cache');
patternbuilder_pattern_statuses_reset();
drupal_static_reset('patternbuilder_components_load');
drupal_static_reset('patternbuilder_components_load__bundles');
drupal_static_reset('patternbuilder_get_resolver');
}
/**
* Returns the machine name for a schema name.
*
* @param string $name
* A schema machine name or file name.
*
* @return string
* Machine name.
*/
function patternbuilder_clean_schema_machine_name($name) {
if (strpos($name, '.') !== FALSE) {
$name = basename($name, '.json');
}
// Replace hyphen with underscore, @see paragraphs_bundle_load().
return strtr($name, array('-' => '_'));
}
/**
* Resolves a file path to be used for schemas or templates.
*
* @param string $path
* The file path.
*
* @return string
* The resolved file path.
*/
function patternbuilder_resolve_file_path($path) {
if (strpos($path, '/') === 0) {
// Set path as root file path.
$file_path = 'file://' . $path;
}
elseif (file_uri_scheme($path)) {
// Set path as URI.
$file_path = $path;
}
else {
// Assume relative to the Drupal root.
$file_path = 'file://' . DRUPAL_ROOT . '/' . trim($path, '/');
}
return $file_path;
}
/**
* Get a list of all schema files.
*
* @param bool $rebuild
* TRUE to rebuild the schema cache.
*
* @return array
* All available schema files, keyed by the schema name.
*/
function patternbuilder_get_schemas($rebuild = FALSE) {
$schemas = &drupal_static(__FUNCTION__);
// Cache check.
if (isset($schemas) && !$rebuild) {
if (!empty($schemas)) {
return $schemas;
}
elseif ($cached_schemas = cache_get('patternbuilder_schema_paths')) {
$schemas = $cached_schemas->data;
return $schemas;
}
}
// Build cache.
$schemas = array();
$schema_dirs = variable_get('patternbuilder_schema_dirs', array());
// Set to a consistent empty value.
if (!empty($schema_dirs)) {
// Scan directories.
foreach ($schema_dirs as $schema_dir) {
$files = file_scan_directory($schema_dir, '/\.json$/');
foreach ($files as $file) {
$machine_name = patternbuilder_clean_schema_machine_name($file->name);
if (!isset($schemas[$machine_name])) {
// Store resolved path.
$schemas[$machine_name] = patternbuilder_resolve_file_path($file->uri);
}
else {
// Log duplicates.
watchdog('patternbuilder', 'Duplicate schemas found for "@name" at "@path". The first schema registered will be used until this conflict has been resolved. Active schema: "@existing"', array(
'@name' => $machine_name,
'@path' => $file->uri,
'@existing' => $schemas[$machine_name],
), WATCHDOG_WARNING);
}
}
}
}
// Store both non-empty and empty so not constantly rebuild for empty.
cache_set('patternbuilder_schema_paths', $schemas);
return $schemas;
}
/**
* Add a schema directory.
*
* @param array|string $dirs
* An array of directories or a single directory.
*/
function patternbuilder_schema_dirs_add($dirs) {
$stored = variable_get('patternbuilder_schema_dirs', array());
if (!is_array($dirs)) {
$dirs = array($dirs);
}
$new_dirs = array_diff($dirs, $stored);
if ($new_dirs) {
$stored = array_merge($stored, $new_dirs);
variable_set('patternbuilder_schema_dirs', $stored);
patternbuilder_get_schemas(TRUE);
}
}
/**
* Remove a schema directory.
*
* @param array|string $dirs
* An array of directories or a single directory.
*/
function patternbuilder_schema_dirs_remove($dirs) {
$stored = variable_get('patternbuilder_schema_dirs', array());
if ($stored) {
if (!is_array($dirs)) {
$dirs = array($dirs);
}
$stored = array_diff($stored, $dirs);
variable_set('patternbuilder_schema_dirs', $stored);
patternbuilder_get_schemas(TRUE);
}
}
/**
* Get a list of all template paths.
*
* @param bool $rebuild
* TRUE to rebuild the cache.
*
* @return array
* All available template paths.
*/
function patternbuilder_get_template_paths($rebuild = FALSE) {
$cache = &drupal_static(__FUNCTION__);
if ($rebuild || !isset($cache)) {
$dirs = variable_get('patternbuilder_template_dirs', array());
if (!empty($dirs)) {
foreach ($dirs as &$dir) {
if (strpos($dir, '/') !== 0 && !file_uri_scheme($dir)) {
// Assume relative to the Drupal root.
$dir = DRUPAL_ROOT . '/' . trim($dir, '/');
}
}
unset($dir);
}
else {
$dirs = array();
}
$cache = $dirs;
}
return $cache;
}
/**
* Add a template directory.
*
* @param array|string $dirs
* An array of directories or a single directory.
*/
function patternbuilder_template_dirs_add($dirs) {
$stored = variable_get('patternbuilder_template_dirs', array());
if (!is_array($dirs)) {
$dirs = array($dirs);
}
$new_dirs = array_diff($dirs, $stored);
if ($new_dirs) {
$stored = array_merge($stored, $new_dirs);
variable_set('patternbuilder_template_dirs', $stored);
}
}
/**
* Remove a template directory.
*
* @param array|string $dirs
* An array of directories or a single directory.
*/
function patternbuilder_template_dirs_remove($dirs) {
$stored = variable_get('patternbuilder_template_dirs', array());
if ($stored) {
if (!is_array($dirs)) {
$dirs = array($dirs);
}
$stored = array_diff($stored, $dirs);
variable_set('patternbuilder_template_dirs', $stored);
}
}
/**
* Return an instance of the patternbuilder factory class.
*
* @param array $twig_arguments
* An array of twig environment variables.
*
* @return DrupalPatternBuilderSchema
* A drupal schema factory object.
*/
function patternbuilder_get($twig_arguments = array()) {
patternbuilder_init_libraries();
$template_paths = patternbuilder_get_template_paths();
$loader = new Twig_Loader_Filesystem($template_paths);
$twig = new Twig_Environment($loader, $twig_arguments);
$logger = new DrupalPatternBuilderLog();
$retriever = new JsonSchema\Uri\UriRetriever();
$resolver = new DrupalPatternBuilderRefResolver($retriever, 30);
$developer_mode = variable_get('patternbuilder_developer_mode_enabled', FALSE);
$configuration = new PatternBuilder\Configuration\Configuration($logger, $twig, $resolver, $developer_mode);
// TODO: schema object cache disabled until can effectively cache without
// object / TWIG corruption on loading multiple of the same schema type.
// See US83433, US70858.
$cache_enabled = FALSE;
return new DrupalPatternBuilderSchema($configuration, array(), $cache_enabled);
}
/**
* Return an instance of the JSON resolver class.
*
* @return DrupalPatternBuilderRefResolver
* A reference resolver.
*/
function patternbuilder_get_resolver() {
$resolver = &drupal_static(__FUNCTION__);
if (!isset($resolver)) {
patternbuilder_init_libraries();
$retriever = new JsonSchema\Uri\UriRetriever();
$resolver = new DrupalPatternBuilderRefResolver($retriever, 30);
}
return $resolver;
}
/**
* Resolve a JSON schema object.
*
* @param object $schema
* The schema object to resolve.
*
* @return object
* The resolved schema object.
* Note: The $schema object is altered by reference.
*/
function patternbuilder_resolve_schema(&$schema) {
if (isset($schema->{'$ref'})) {
if (preg_match('@\.json$@', $schema->{'$ref'})) {
$ref_name = basename($schema->{'$ref'}, '.json');
if ($ref_name) {
$schema_paths = patternbuilder_get_schemas();
if (isset($schema_paths[$ref_name])) {
$ref_original = $schema->{'$ref'};
$resolver = patternbuilder_get_resolver();
$resolver->resolve($schema, $schema_paths[$ref_name]);
// Cleanup $ref if not removed by resolver.
if (isset($schema->{'$ref'}) && $schema->{'$ref'} == $ref_original) {
unset($schema->{'$ref'});
}
}
}
}
}
return $schema;
}
/**
* Initializes twig and patternbuilder composer libraries.
*/
function patternbuilder_init_libraries() {
libraries_load('patternbuilder');
}
/**
* Returns the default pattern type used for un-claimed schemas.
*
* @return string
* The default pattern type.
*/
function patternbuilder_default_pattern_type() {
return variable_get('patternbuilder_default_pattern_type', 'pb_default');
}
/**
* Retrieve information for pattern types.
*
* @param string $pattern_type_name
* The pattern type machine name. If not provided, then all pattern types
* are returned.
* @param bool $rebuild
* TRUE to rebuild the cache.
*
* @return array
* An array of information for the given pattern type. If none provided, then
* all pattern types are returned keyed by the pattern type machine name.
*/
function patternbuilder_get_pattern_types($pattern_type_name = NULL, $rebuild = FALSE) {
$cache = &drupal_static(__FUNCTION__);
if (!isset($cache) || $rebuild) {
$cid = 'patternbuilder_drupal:pattern_types';
if (!$rebuild && ($stored_cache = cache_get($cid))) {
$cache = $stored_cache->data;
}
else {
$cache = array();
if ($rebuild) {
// Rebuild module hook cache.
module_implements('patternbuilder_pattern_types', FALSE, TRUE);
module_implements('patternbuilder_pattern_types_alter', FALSE, TRUE);
}
// Build pattern type cache.
$max_weight = 0;
foreach (module_implements('patternbuilder_pattern_types') as $module) {
foreach (module_invoke($module, 'patternbuilder_pattern_types') as $type_name => $pattern_type_info) {
$cache[$type_name] = $pattern_type_info;
$cache[$type_name]['name'] = $type_name;
$cache[$type_name]['module'] = $module;
// Set defaults.
$cache[$type_name] += array(
'weight' => 0,
'prefix' => 'pbi',
'resolve' => FALSE,
'field' => FALSE,
);
// Set default weight.
if (!isset($cache[$type_name]['weight'])) {
$cache[$type_name]['weight'] = 0;
}
elseif ($cache[$type_name]['weight'] > $max_weight) {
$max_weight = $cache[$type_name]['weight'];
}
// Field based schemas must be resolved.
if ($cache[$type_name]['field']) {
$cache[$type_name]['resolve'] = TRUE;
}
}
}
// Set default type if not defined.
$default_type = patternbuilder_default_pattern_type();
if ($default_type && !isset($cache[$default_type])) {
// This is weighted heavy to allow all other types to claim a schema
// during import.
$cache[$default_type] = array(
'name' => 'pb_default',
'label' => 'Default',
'module' => 'patternbuilder',
'prefix' => 'pbi',
'resolve' => FALSE,
'field' => FALSE,
'weight' => $max_weight + 100,
);
}
drupal_alter('patternbuilder_pattern_types', $cache);
uasort($cache, 'drupal_sort_weight');
cache_set($cid, $cache);
}
}
// Single type return.
if (isset($pattern_type_name)) {
return isset($cache[$pattern_type_name]) ? $cache[$pattern_type_name] : array();
}
// Return all types.
return $cache;
}
/**
* Returns a list of pattern type labels.
*
* @param string $name
* Optional parameter specifying the name of the pattern status whose title
* to return.
*
* @return array|string
* An array keyed by pattern type machine name with labels as values or a
* single label if the name is provided.
*/
function patternbuilder_pattern_types_labels($name = NULL) {
$labels = array();
$types = patternbuilder_get_pattern_types();
// Return a status title if specified and it exists.
if (!empty($name)) {
return isset($types[$name]['label']) ? $types[$name]['label'] : NULL;
}
// Return all.
if ($types) {
foreach ($types as $name => $info) {
$label = isset($info['label']) ? $info['label'] : $name;
$labels[$name] = $label;
}
}
return $labels;
}
/**
* Returns an option list suitable for an HTML element.
*/
function patternbuilder_pattern_types_options_list() {
$options = array();
$labels = patternbuilder_pattern_types_labels();
if ($labels) {
$options = array_map('check_plain', $labels);
}
return $options;
}
/**
* Returns a list of paragraph bundle labels.
*
* @return array
* An array keyed by bundle machine name with labels as values.
*/
function patternbuilder_paragraph_bundle_labels() {
$labels = array();
$bundles = paragraphs_bundle_load();
foreach ($bundles as $name => $bundle) {
$labels[$name] = !empty($bundle->name) ? $bundle->name : $name;
}
return $labels;
}
/**
* Loads all component IDs from memory.
*
* @return array
* All components by machine name => id
*/
function _patternbuilder_component_ids_load() {
return db_select('patternbuilder_components', 'c')
->fields('c', array('machine_name', 'id'))
->orderBy('c.id', 'ASC')
->execute()
->fetchAllKeyed();
}
/**
* Load stored pattern components.
*
* @param string|null $machine_name
* The component machine name or NULL to load all.
* @param bool $rebuild
* TRUE to rebuild the cache.
*
* @return array|object|null
* The component object if $machine_name is provided, else all components
* keyed by machine name.
*/
function patternbuilder_components_load($machine_name = NULL, $rebuild = FALSE) {
$cache = &drupal_static(__FUNCTION__);
$bundle_cache = &drupal_static(__FUNCTION__ . '__bundles');
if ($rebuild || !isset($cache)) {
$cache = array();
$bundle_cache = array();
$records = db_select('patternbuilder_components', 'c')
->fields('c')
->orderBy('c.id', 'ASC')
->execute()
->fetchAllAssoc('machine_name');
if ($records) {
// Set default status.
$default_status = patternbuilder_pattern_status_default();
$default_status_name = isset($default_status['name']) ? $default_status['name'] : NULL;
foreach ($records as $k => $record) {
if (empty($record->status)) {
$record->status = $default_status_name;
}
// Build component cache.
$cache[$k] = $record;
// Build bundle cache.
if (!empty($record->bundle_name) && !isset($bundle_cache[$record->bundle_name])) {
$bundle_cache[$record->bundle_name] = &$cache[$k];
}
}
}
}
if (!empty($machine_name)) {
return isset($cache[$machine_name]) ? $cache[$machine_name] : NULL;
}
return $cache;
}
/**
* Returns the pattern component for all bundles.
*
* @return array
* An array of pattern component objects keyed by bundle name.
*/
function patternbuilder_get_bundle_component_map() {
patternbuilder_components_load();
$bundle_components = drupal_static('patternbuilder_components_load__bundles', array());
return $bundle_components ? $bundle_components : array();
}
/**
* Returns the pattern component for a given bundle.
*
* @param string $bundle_name
* The bundle name.
*
* @return object|null
* The pattern component object corresponding to the bundle.
*/
function patternbuilder_get_bundle_component($bundle_name) {
$bundle_components = patternbuilder_get_bundle_component_map();
return isset($bundle_components[$bundle_name]) ? $bundle_components[$bundle_name] : NULL;
}
/**
* Returns a map of pattern type to bundles.
*
* @param string|null $pattern_type_name
* The component pattern type or NULL to load all pattern types.
* @param bool $rebuild
* TRUE to rebuild the cache.
*
* @return array
* An array with keys of pattern type and values of an array of bundle names
* keyed by machine name.
*/
function patternbuilder_components_get_pattern_type_bundles($pattern_type_name = NULL, $rebuild = FALSE) {
$return = array();
$components = patternbuilder_components_load(NULL, $rebuild);
if (!empty($pattern_type_name)) {
foreach ($components as $machine_name => $component) {
if (!empty($component->bundle_name) && !empty($component->pattern_type) && $component->pattern_type == $pattern_type_name) {
$return[$machine_name] = $component->bundle_name;
}
}
asort($return);
}
else {
foreach ($components as $machine_name => $component) {
if (!empty($component->bundle_name)) {
$pattern_type_key = !empty($component->pattern_type) ? $component->pattern_type : '';
$return[$pattern_type_key][$machine_name] = $component->bundle_name;
}
}
foreach ($return as $pattern_type_key => &$pattern_type_components) {
asort($pattern_type_components);
}
unset($pattern_type_components);
}
return $return;
}
/**
* Determine if the component is imported.
*
* @param object|string $component
* The component object or machine name.
*
* @return bool
* TRUE if the component is a resolved schema.
*/
function patternbuilder_component_is_imported($component) {
if (is_string($component)) {
$component = patternbuilder_components_load($component);
}
if (!empty($component->status)) {
$statuses = patternbuilder_pattern_statuses(array(
'import' => TRUE,
));
return isset($statuses[$component->status]);
}
return TRUE;
}
/**
* Determine if the component is creatable.
*
* @param object|string $component
* The component object or machine name.
*
* @return bool
* TRUE if the component is a resolved schema.
*/
function patternbuilder_component_is_creatable($component) {
if (is_string($component)) {
$component = patternbuilder_components_load($component);
}
if (!empty($component->status)) {
$statuses = patternbuilder_pattern_statuses(array(
'creatable' => TRUE,
));
return isset($statuses[$component->status]);
}
return TRUE;
}
/**
* Determine if the component is visible in the widget.
*
* @param object|string $component
* The component object or machine name.
*
* @return bool
* TRUE if the component is a resolved schema.
*/
function patternbuilder_component_is_widget_visible($component) {
if (is_string($component)) {
$component = patternbuilder_components_load($component);
}
if (!empty($component->status)) {
$statuses = patternbuilder_pattern_statuses(array(
'visible' => TRUE,
));
return isset($statuses[$component->status]);
}
return TRUE;
}
/**
* Determine if the component is resolved.
*
* @param object|string $component
* The component object or machine name.
*
* @return bool
* TRUE if the component is a resolved schema.
*/
function patternbuilder_component_is_resolved($component) {
if (is_string($component)) {
$component = patternbuilder_components_load($component);
}
if (isset($component->pattern_type) && $component->pattern_type) {
$type_info = patternbuilder_get_pattern_types($component->pattern_type);
return !empty($type_info['resolve']);
}
return FALSE;
}
/**
* Determine if the component is a field based schema.
*
* @param object|string $component
* The component object or machine name.
*
* @return bool
* TRUE if the component is a field.
*/
function patternbuilder_component_is_field($component) {
if (is_string($component)) {
$component = patternbuilder_components_load($component);
}
if (isset($component->pattern_type) && $component->pattern_type) {
$type_info = patternbuilder_get_pattern_types($component->pattern_type);
return !empty($type_info['field']);
}
return FALSE;
}
/**
* Returns the pattern component for a given field name.
*
* @param string $field_name
* The field name.
*
* @return object|null
* The pattern component object corresponding to the bundle.
*/
function patternbuilder_get_field_component($field_name) {
$components = patternbuilder_components_load();
foreach ($components as $machine_name => $component) {
if (!empty($component->field_name) && $component->field_name == $field_name) {
return $component;
}
}
}
/**
* Adds a unique machine name to the component table.
*
* @param array|object $component
* The component object or array with the following values:
* - machine_name: The machine name to create or update.
* - pattern_type: The pattern type machine name.
* - bundle_name: The paragraph bundle name.
* - field_name: The field name (if a non bundle component).
* - status: The pattern status name.
*
* @return object|null
* The fully loaded saved component object. NULL is returned if the component
* cannot be saved.
*
* @throws \Exception
* General error.
*/
function patternbuilder_component_save(&$component) {
$record = (array) $component;
if (empty($record['machine_name'])) {
return NULL;
}
$current_component = patternbuilder_components_load($record['machine_name']);
if (empty($record['pattern_type'])) {
$record['pattern_type'] = patternbuilder_default_pattern_type();
}
if (empty($record['status']) && empty($current_component)) {
$default_status = patternbuilder_pattern_status_default();
$default_status_key = isset($default_status['name']) ? $default_status['name'] : NULL;
$record['status'] = $default_status_key;
}
// Ensure only allowed fields to update.
$record = array_intersect_key($record, array(
'machine_name' => 1,
'pattern_type' => 1,
'status' => 1,
'bundle_name' => 1,
'field_name' => 1,
));
// Update record.
db_merge('patternbuilder_components')
->key(array('machine_name' => $record['machine_name']))
->fields($record)
->execute();
// Rebuild load cache and return the component.
$updated_component = patternbuilder_components_load($record['machine_name'], TRUE);
// Update referenced component.
if (is_object($component)) {
foreach ($updated_component as $property => $property_value) {
$component->{$property} = $property_value;
}
}
elseif (is_array($component)) {
foreach ($updated_component as $property => $property_value) {
$component[$property] = $property_value;
}
}
return $updated_component;
}
/**
* Returns an array of some or all of the order statuses keyed by name.
*
* @param array $conditions
* An array of conditions to filter the returned list by; for example, if you
* specify 'import' => TRUE in the array, then only statuses that can be
* imported will be returned.
* @param bool $rebuild
* TRUE to rebuild the cache.
*
* @return array
* The array of status info, keyed by status name.
*/
function patternbuilder_pattern_statuses($conditions = array(), $rebuild = FALSE) {
$statuses = &drupal_static(__FUNCTION__);
if (!isset($statuses) || $rebuild) {
$cid = 'patternbuilder_drupal:pattern_status_info';
if (!$rebuild && ($cache = cache_get($cid))) {
$statuses = $cache->data;
}
else {
// Build via module hooks.
if ($rebuild) {
// Rebuild module hook cache.
module_implements('patternbuilder_pattern_status_info', FALSE, TRUE);
module_implements('patternbuilder_pattern_status_info_alter', FALSE, TRUE);
}
// Process each module.
$statuses = array();
foreach (module_implements('patternbuilder_pattern_status_info') as $module) {
foreach (module_invoke($module, 'patternbuilder_pattern_status_info') as $name => $status) {
// Only allow valid status names.
if (drupal_strlen($name) <= 32) {
$statuses[$name] = $status;
$statuses[$name]['module'] = $module;
// Set defaults.
$statuses[$name] += array(
'name' => $name,
'label' => $name,
'weight' => 0,
'import' => TRUE,
'visible' => TRUE,
'creatable' => TRUE,
);
}
}
}
}
// Give other modules a chance to alter the order statuses.
drupal_alter('patternbuilder_pattern_status_info', $statuses);
uasort($statuses, 'drupal_sort_weight');
cache_set($cid, $statuses);
}
// Apply conditions to the returned statuses if specified.
if (!empty($conditions)) {
$matching_statuses = array();
foreach ($statuses as $name => $status) {
// Check the status against the conditions array to determine whether to
// add it to the return array or not.
$valid = TRUE;