-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbiblio.module
1040 lines (930 loc) · 27.8 KB
/
biblio.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
* Maintains bibliographic lists.
*/
/**
* Implements hook_hook_info().
*/
function biblio_hook_info() {
$hooks = array();
$hooks['biblio_fields_info'] = array(
'group' => 'biblio',
);
$hooks['biblio_types_info'] = array(
'group' => 'biblio',
);
return $hooks;
}
/**
* Implements hook_permission().
*
* Since we are limiting the ability to create new nodes to certain users,
* we need to define what those permissions are here. We also define a permission
* to allow users to edit the nodes they created.
*/
function biblio_permission() {
return array(
'administer biblio' => array(
'title' => t('Administer Biblio'),
'description' => t('Allows full control (create, update, delete) of all Biblio nodes.'),
),
'access biblio content' => array(
'title' => t('Access Biblio content'),
'description' => t('Allows the user to view Biblio nodes.'),
),
'create biblio content' => array(
'title' => t('Create Biblio'),
'description' => t('Allows the user to create new Biblios.'),
),
'edit biblio content' => array(
'title' => t('Edit Biblio'),
'description' => t('Allows the user to edit any biblio.'),
),
'edit own biblio content' => array(
'title' => t('Edit own Biblio'),
'description' => t('Allows the user to edit his own biblio.'),
),
'delete biblio content' => array(
'title' => t('Delete Biblio'),
'description' => t('Allows the user to delete any biblio.'),
),
'delete own biblio content' => array(
'title' => t('Delete own Biblio'),
'description' => t('Allows the user to delete his own biblio.'),
),
'edit by all biblio authors' => array(
'title' => t('Edit by all Biblio authors'),
'description' => t('Allows any/all of the authors associated with a biblio to edit the biblio. This requires the Drupal User ID be mapped to a Biblio author ID.'),
),
'edit biblio authors' => array(
'title' => t('Edit Biblio authors'),
'description' => t('Allows the user to edit author information.'),
),
'manage biblio structure' => array(
'title' => t('Manage Biblio structure'),
'description' => t('This determines if the user will be able to modify field and display settings for biblio and contributor entities (admin/structure/biblio).'),
),
'manage biblio content' => array(
'title' => t('Manage Biblio content'),
'description' => t('This determines if the user will be able to access the managment interface for biblios and contributors (admin/content/biblio).'),
),
'import from file' => array(
'title' => t('Import from file'),
'description' => t('Allows the user to import bibliographic data from files such as BibTex, RIS, EndNote.'),
),
'show export links' => array(
'title' => t('Show export links'),
'description' => t('Allows users to see links which allow export of bibliographic data for an individual entry or the entire result set.'),
),
// Contributor permissions.
'create biblio contributor' => array(
'title' => t('Create Biblio contributor'),
'description' => t('Grant to the user the permission to create a biblio contributor via the UI.'),
),
'edit biblio contributor' => array(
'title' => t('Edit Biblio contributor'),
'description' => t('Grant to the user the permission to update a biblio contributor via the UI.'),
),
'delete biblio contributor' => array(
'title' => t('Delete Biblio contributor'),
'description' => t('Grant to the user the permission to delete a biblio contributor via the UI.'),
),
'view biblio contributor' => array(
'title' => t('View Biblio contributor'),
'description' => t('Grant to the user the permission to view a biblio contributor via the UI.'),
),
);
}
/**
* Implements hook_ctools_plugin_api().
*/
function biblio_ctools_plugin_api($module, $api) {
if ($module == 'biblio' && $api == 'biblio_style') {
return array('version' => 1);
}
}
/**
* Implements hook_ctools_plugin_type().
*/
function biblio_ctools_plugin_type() {
$plugins['biblio_style'] = array(
'classes' => array('class'),
'process' => 'biblio_plugin_process',
);
return $plugins;
}
/**
* Add defaults values to the notifier plugins.
*
* - 'description': The description of the plugin.
*/
function biblio_plugin_process(&$plugin, $info) {
$plugin += array(
'description' => '',
'options' => array(),
'assets' => array(
'js' => array(),
'css' => array(),
),
);
}
/**
* Implements hook_ctools_plugin_directory().
*/
function biblio_ctools_plugin_directory($module, $plugin) {
if ($module == 'biblio') {
return 'plugins/' . $plugin;
}
}
/**
* Get a loaded class from a style plugin.
*
* @todo: Add static cache.
*
* @param $style_name
* The style plugin name.
*
* @return BiblioStyleInterface
* The Biblio style object.
*/
function biblio_get_class_from_style($style_name) {
ctools_include('plugins');
$plugin = biblio_get_biblio_style($style_name);
$class = ctools_plugin_load_class('biblio', 'biblio_style', $style_name, 'class');
return new $class($plugin);
}
/**
* Helper function to include CTools plugins and get a notifier plguin.
*
* @param $plugin_name
* The plugin that should be loaded.
*/
function biblio_get_biblio_style($style_name) {
ctools_include('plugins');
return ctools_get_plugins('biblio', 'biblio_style', $style_name);
}
/**
* Helper function to return only exportable Biblio style.
*
* @param $style_name
* The plugin that should be loaded.
* @param $export_format_name
* (Optional) Export format name.
*
* @return array
* Biblio style plugin.
*/
function biblio_get_exportable_biblio_style($style_name, $export_format_name = NULL) {
$style = biblio_get_biblio_style($style_name);
if (empty($style['export'])) {
// No export information - not a exportable plugin.
return;
}
if ($export_format_name == NULL) {
return $style;
}
if (empty($style['export'][$export_format_name])) {
return;
}
return $style;
}
/**
* Helper function to return only exportable biblio styles.
*/
function biblio_get_exportable_biblio_styles() {
ctools_include('plugins');
$exportable_plugins = array();
foreach (biblio_get_biblio_styles() as $plugin_name => $style) {
if (!empty($style['export'])) {
$exportable_plugins[$plugin_name] = $style;
}
}
return $exportable_plugins;
}
/**
* Helper function to include CTools plugins and get all notifier plugins.
*/
function biblio_get_biblio_styles() {
ctools_include('plugins');
return ctools_get_plugins('biblio', 'biblio_style');
}
/**
* Helper function to return all notifiers as options for a select list.
*
* @return array
* An array of biblio types for use in a field option list
*/
function biblio_get_notifiers_as_options() {
$options = array();
foreach (biblio_get_biblio_styles() as $style_name => $style) {
$options[$style_name] = check_plain($style['title']);
}
return $options;
}
/**
* Implements hook_views_api().
*/
function biblio_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'biblio') . '/includes/views',
);
}
/**
* Implements hook_entity_info().
*
* Inform the Drupal and the Field API about entity types.
* Uses the contrib Entity API module to create entities
*/
function biblio_entity_info() {
$return['biblio'] = array(
'label' => t('Biblio'),
'entity class' => 'Biblio',
'controller class' => 'EntityAPIController',
'base table' => 'biblio',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'bid',
'bundle' => 'type',
'label' => 'title',
),
'bundle keys' => array(
'bundle' => 'type',
),
'load hook' => 'biblio_load',
'view modes' => array(
'full' => array(
'label' => t('Full content'),
'custom settings' => FALSE,
),
'teaser' => array(
'label' => t('Teaser'),
'custom settings' => TRUE,
),
),
// Entity API label callback that takes a look at our entity class method defaultLabel()
'label callback' => 'entity_class_label',
// This is also a standard Entity API callback for uri.
// It executes our entity defaultUri() method
'uri callback' => 'entity_class_uri',
'module' => 'biblio',
'access callback' => 'biblio_access',
'views controller class' => 'BiblioViewsController',
'metadata controller class' => 'BiblioMetadataController',
);
// @todo: Biblio 1.x had a biblio type called "Biblio" if type was 0.
$return['biblio']['bundles']['biblio'] = array('label' => 'Biblio');
if (db_table_exists('biblio_type') && $bundles = biblio_types()) {
foreach ($bundles as $bundle) {
$return['biblio']['bundles'][$bundle->type] = array('label' => $bundle->name);
}
}
$return['biblio_contributor'] = array(
'label' => t('Contributor'),
'entity class' => 'BiblioContributor',
'controller class' => 'EntityAPIController',
'base table' => 'biblio_contributor',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'cid',
'label' => 'name',
),
'bundles' => array(
'contributor' => array(
'label' => t('Contributor'),
),
),
'load hook' => 'biblio_contributor_load',
'view modes' => array(
'full' => array(
'label' => t('Full'),
'custom settings' => FALSE,
),
),
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'module' => 'biblio',
'access callback' => 'biblio_contributor_access',
'inline entity form' => array(
'controller' => 'BiblioContributorInlineEntityFormController',
),
);
return $return;
}
/**
* Implements hook_field_extra_fields().
*/
function biblio_field_extra_fields() {
$info = entity_get_info('biblio');
if (empty($info['bundles'])) {
return array();
}
$return = array();
foreach (array_keys($info['bundles']) as $bundle) {
$return['biblio'][$bundle] = array(
'display' => array(
'biblio_get_text' => array(
'label' => t('Rendered Biblio'),
'description' => t('Display the rendered text of the biblio.'),
'weight' => 0,
),
),
);
}
return $return;
}
/**
* Get the bundles of the biblio.
*
* @param string $type
* Optional; A specific type name.
* @return array
* List of biblio types.
*/
function biblio_types($type = NULL) {
$results = db_select('biblio_type')
->fields('biblio_type')
->execute()
->fetchAllAssoc('type');
if (!empty($type)) {
return isset($results[$type]) ? $results[$type] : array();
}
return $results;
}
/**
* Save biblio type.
*
* @todo: When changing the machine name create a batch api that will update
* the biblio entries to the new bundle machine name.
*
* @param object $biblio_type
* The values of a biblio type.
*/
function biblio_type_save($biblio_type) {
if (biblio_types($biblio_type->type)) {
db_update('biblio_type')
->fields(array(
'name' => $biblio_type->name,
'description' => $biblio_type->description,
)
)
->condition('type', $biblio_type->type)
->execute();
}
else {
db_insert('biblio_type')
->fields((array)$biblio_type)
->execute();
// Attach the contributors field collection to the new biblio bundle.
biblio_create_field('contributor_field_collection', 'biblio', $biblio_type->type);
biblio_create_field('biblio_contributor_role', 'field_collection_item', 'contributor_field_collection');
biblio_create_field('biblio_contributor', 'field_collection_item', 'contributor_field_collection');
}
}
/**
* Delete a biblio bundle.
*
* @param string $type
* The value of a biblio type.
*/
function biblio_type_delete($type) {
$query = new entityFieldQuery();
$number = $query
->entityCondition('entity_type', 'biblio')
->propertyCondition('type', $type)
->count()
->execute();
if ($number) {
throw new Exception(t("You cannot delete the bundle @type because there are biblio entries of this bundle."));
}
db_delete('biblio_type')
->condition('type', $type)
->execute();
}
/**
* Access function for the biblio entity.
*
* @param $op
* The type of the operation.
* @param Biblio $entity
* The entity object.
* @param $account
* The user object.
* @param $entity_type
* The entity type.
*
* @return bool
* Return TRUE/FALSE if the user has the privilege for this action.
*/
function biblio_access($op, Biblio $entity = NULL, $account = NULL, $entity_type = NULL) {
global $user;
$account = $account ? $account : user_load($user->uid);
if (user_access('administer biblio', $account)) {
return TRUE;
}
$strings = array(
'create' => 'create biblio content',
'update' => 'edit biblio content',
'delete' => 'delete biblio content',
'view' => 'access biblio content',
);
$string = $strings[$op];
// When checking the access on the biblio entity we need to check if the user
// can delete/update entries which created by himself/other users.
if (is_object($entity) && in_array($op, array('update', 'delete'))) {
if (!user_access($string, $account) && $entity->uid == $account->uid) {
// The user can't delete/update any publication. We need to check if the
// user can update/delete his own biblio entries.
$string = $op == 'delete' ? 'delete own biblio content' : 'edit own biblio content';
}
}
return user_access($string, $account);
}
/**
* Access function for the biblio contributor entity.
*
* @param $op
* The type of the operation.
* @param BiblioContributor $entity
* The entity object.
* @param $account
* The user object.
* @param $entity_type
* The entity type.
*
* @return bool
* Return TRUE/FALSE if the user has the privilege for this action.
*/
function biblio_contributor_access($op, BiblioContributor $entity = NULL, $account = NULL, $entity_type = NULL) {
global $user;
$account = $account ? $account : user_load($user->uid);
if (user_access('administer biblio', $account)) {
return TRUE;
}
$strings = array(
'create' => 'create biblio contributor',
'update' => 'edit biblio contributor',
'delete' => 'delete biblio contributor',
'view' => 'view biblio contributor',
);
return user_access($strings[$op], $account);
}
/**
* Implements hook_biblio_insert().
*
* Save the path of the Biblio entry.
*/
function biblio_biblio_insert($biblio) {
if (!module_exists('path')) {
return;
}
if (isset($biblio->path)) {
$path = $biblio->path;
$path['alias'] = trim($path['alias']);
// Only save a non-empty alias.
if (!empty($path['alias'])) {
// Ensure fields for programmatic executions.
$langcode = entity_language('biblio', $biblio);
$path['source'] = 'biblio/' . $biblio->bid;
$path['language'] = isset($langcode) ? $langcode : LANGUAGE_NONE;
path_save($path);
}
}
if (!module_exists('pathauto')) {
return;
}
biblio_update_alias($biblio);
}
/**
* Implements hook_biblio_update().
*
* Update the path of the Biblio entry.
*/
function biblio_biblio_update($biblio) {
if (!module_exists('path')) {
return;
}
if (isset($biblio->path)) {
$path = $biblio->path;
$path['alias'] = trim($path['alias']);
// Delete old alias if user erased it.
if (!empty($path['pid']) && empty($path['alias'])) {
path_delete($path['pid']);
}
biblio_biblio_insert($biblio);
}
if (!module_exists('pathauto')) {
return;
}
biblio_update_alias($biblio);
}
/**
* Implements hook_biblio_delete().
*/
function biblio_biblio_delete($biblio) {
if (!module_exists('path')) {
return;
}
$conditions = array('source' => 'biblio/' . $biblio->bid);
path_delete($conditions);
if (!module_exists('pathauto')) {
return;
}
pathauto_entity_path_delete_all('biblio', $biblio, "node/{$biblio->bid}");
}
/**
* Update the URL aliases for an individual biblio.
*
* @param $node
* A node object.
* @param $op
* Operation being performed on the node ('insert', 'update' or 'bulkupdate').
* NULL by default.
*/
function biblio_update_alias(Biblio $biblio, $op = NULL) {
// Skip processing if the node has no pattern.
if (!pathauto_pattern_load_by_entity('biblio')) {
return;
}
if (!$op) {
// When the operation is not provided we need to set the value by the is_new
// property.
$op = $biblio->bid ? 'update' : 'insert';
}
module_load_include('inc', 'pathauto');
$uri = entity_uri('biblio', $biblio);
pathauto_create_alias('biblio', $op, $uri['path'], array('biblio' => $biblio));
}
/**
* Implements hook_biblio_load().
*/
function biblio_biblio_load($biblios) {
if (!module_exists('path')) {
return;
}
foreach ($biblios as $biblio) {
$conditions = array('source' => 'biblio/' . $biblio->bid);
$biblio->path = path_load($conditions);
}
}
/**
* Create a Biblio entity object.
*
* @param $type
* The publication type of the Biblio to be created (bundle).
* @param array $values
* An associative array of any additional values to be set when creating this
* entity. These values will be carried throughout the biblio object's life.
* Example: $values['title'] => 'new publication';
*
* @return Biblio
* The Biblio object, with default values.
*/
function biblio_create($type, $values = array()) {
if (empty($account)) {
global $user;
$account = clone $user;
}
$values['type'] = $type;
$values['uid'] = $account->uid;
$values += array(
'created' => REQUEST_TIME,
'changed' => REQUEST_TIME,
);
$values['type'] = $type;
return entity_create('biblio', $values);
}
/**
* Load a biblio object from the database.
*
* @param string $bid
* The biblio ID.
*
* @return object
* A fully-populated biblio object.
*/
function biblio_load($bid) {
return entity_load_single('biblio', $bid);
}
/**
* Load biblio entities from the database.
*
* This function should be used whenever you need to load more than one biblio
* from the database. biblios are loaded into memory and will not require
* database access if loaded again during the same page request.
*
* @see entity_load()
*
* @param array $bids
* An array of biblio IDs.
* @param bool $reset
* Whether to reset the internal entity_load cache.
*
* @return
* An array of biblio objects indexed by bid.
*/
function biblio_load_multiple($bids = array(), $reset = FALSE) {
return entity_load('biblio', $bids, array(), $reset);
}
/**
* Create a contributor entity object.
*
* @param string $name
* The name of the contributor. If given, this function will parse out the
* author name and automatically fill in any associated fields (first name,
* last name, initials, etc.) and the type
* @param array $values
*
* @return BiblioContributor
* The contributor entity object.
*/
function biblio_contributor_create($values = array()) {
global $language;
$values['type'] = 'biblio_contributor';
$values += array(
'language' => $language->language,
'created' => REQUEST_TIME,
'changed' => REQUEST_TIME,
'prefix' => '',
'lastname' => '',
'initials' => '',
'suffix' => '',
'firstname' => '',
);
return entity_create('biblio_contributor', $values);
}
/**
* Load a contributor.
*/
function biblio_contributor_load($cid) {
return entity_load_single('biblio_contributor', $cid);
}
/**
* Load multiple contributors based on certain conditions.
*/
function biblio_contributor_load_multiple($cids = array()) {
return entity_load('biblio_contributor', $cids);
}
/**
* Attach fields to bundles.
*
* @param $bundles
* Array with the bundles to process, or empty array to use all Biblio
* bundles.
*/
function biblio_create_fields_by_bundle($bundles = array(), $clear_cache = TRUE) {
$entity_info = entity_get_info('biblio');
$bundles = $bundles ? $bundles : array_keys($entity_info['bundles']);
$fields_info = biblio_fields_info();
foreach ($bundles as $bundle) {
foreach ($fields_info as $field_name => $field_info) {
if (in_array($field_name, array('biblio_contributor_role', 'biblio_contributor'))) {
continue;
}
if (!empty($field_info['bundles_info']) && isset($field_info['bundles_info'][$bundle])) {
// Allow per bundle settings to override the instance's default settings.
$field_info['instance'] = drupal_array_merge_deep($field_info['instance'], $field_info['bundles_info'][$bundle]);
biblio_create_field($field_name, 'biblio', $bundle, $field_info, $clear_cache);
}
elseif (!isset($field_info['bundles_info'])) {
biblio_create_field($field_name, 'biblio', $bundle, $field_info, $clear_cache);
}
}
}
}
/**
* Create a biblio field in a bundle.
*
* @param string $field_name
* The field name.
* @param string $entity_type
* The entity type.
* @param string $bundle
* The bundle name.
* @param array $biblio_field
* Optional; Array with field definitions, to allow easier overriding by the
* caller. If empty, function will get the field info by calling
* biblio_fields_info() with the field name.
*/
function biblio_create_field($field_name, $entity_type, $bundle, $biblio_field = array(), $clear_cache = TRUE) {
if (!$biblio_field && !$biblio_field = biblio_fields_info($field_name)) {
return;
}
$field = field_info_field($field_name);
// Allow overriding the field name.
$biblio_field['field']['field_name'] = $field_name;
if (empty($field)) {
field_create_field($biblio_field['field']);
}
$instance = field_info_instance($entity_type, $field_name, $bundle);
if (empty($instance)) {
$instance = $biblio_field['instance'];
$instance += array(
'field_name' => $field_name,
'bundle' => $bundle,
'entity_type' => $entity_type,
);
field_create_instance($instance);
if ($clear_cache) {
field_cache_clear();
entity_property_info_cache_clear();
}
}
}
/**
* Get the field info.
*
* @param string $field_name
* The field name.
*
* @return array
* An array with the field and instance definitions, or FALSE if not found.
*/
function biblio_fields_info($field_name = NULL) {
$return = &drupal_static(__FUNCTION__, array());
if (empty($return)) {
foreach (module_implements('biblio_fields_info') as $module) {
if ($fields = module_invoke($module, 'biblio_fields_info')) {
foreach ($fields as $key => $field) {
// Add default values.
$field += array(
'entity type' => array(),
'multiple' => FALSE,
'description' => '',
);
// Add the module information.
$return[$key] = array_merge($field, array('module' => $module));
}
}
}
// Allow other modules to alter the field info.
drupal_alter('biblio_fields_info', $return);
}
if (!empty($field_name)) {
return !empty($return[$field_name]) ? $return[$field_name] : FALSE;
}
return $return;
}
/**
* Create new Biblio bundles.
*
* @param Array $types
* List of specific types to create.
*
* @todo: Make the bundles configurable?
*/
function biblio_add_publication_types($types = array()) {
foreach (biblio_get_types_info() as $type => $info) {
if ($types && !in_array($type, $types)) {
// The type was not specified in the list of types to enable. Skipping.
continue;
}
$name = $info['name'];
$row = new stdClass();
$row->type = strtolower(str_replace(' ', '_', $name));
$row->name = $name;
$row->description = $info['description'];
biblio_type_save($row);
}
}
/**
* Return default Biblio types definitions.
*
* @param $type
* Optional; The definition of the given type. If empty, all types will be
* returned.
*/
function biblio_get_types_info($type = NULL) {
$return = &drupal_static(__FUNCTION__, array());
if (empty($return)) {
foreach (module_implements('biblio_types_info') as $module) {
if ($types = module_invoke($module, 'biblio_types_info')) {
foreach ($types as $key => $value) {
// Add default values.
$value += array(
'description' => '',
'style_info' => array(),
);
// Add the module information.
$return[$key] = array_merge($value, array('module' => $module));
}
}
}
// Allow other modules to alter the field info.
drupal_alter('biblio_types_info', $return);
}
if (!empty($type)) {
return !empty($return[$type]) ? $return[$type] : FALSE;
}
return $return;
}
/**
* Implements hook_field_access().
*/
function biblio_field_access($op, $field, $entity_type, $entity, $account) {
if ($field['field_name'] == 'biblio_first_letter') {
return FALSE;
}
}
/**
* Implements hook_migrate_api().
*/
function biblio_migrate_api() {
$migrations = $groups = array();
if (biblio_check_biblio_migration_needed()) {
$group_name = 'biblio_3';
$groups = array(
$group_name => array(
'title' => t('Biblio 3.x'),
),
);
$migrations = array (
'BiblioMigrateContributorCollections' => array(
'group_name' => $group_name,
'class_name' => 'BiblioMigrateContributorCollections',
),
'BiblioMigrateTypes' => array(
'group_name' => $group_name,
'class_name' => 'BiblioMigrateTypes',
),
);
// Handle per bundle migration.
$entity_info = entity_get_info('biblio');
foreach (array_keys($entity_info['bundles']) as $bundle_name) {
$machine_name = 'BiblioMigrateEntries_' . $bundle_name;
$migration = array();
$migration['class_name'] = 'BiblioMigrateEntries';
$migration['bundle_name'] = $bundle_name;
$migration['group_name'] = $group_name;
$migrations[$machine_name] = $migration;
}
}
$api = array(
'api' => 2,
'groups' => $groups,
'migrations' => $migrations,
);
return $api;
}
/**
* Check whether Biblio migration from 1.x to 3.x is needed.
*
* @return bool
* TRUE if Biblio 1.x migration needs to be registered.
*/
function biblio_check_biblio_migration_needed() {
if (!module_exists('migrate')) {
// Migrate module is not a dependency, so we need to verify it is installed.
return;
}
if (!module_exists('migrate_extras')) {
return;
}
if (!db_table_exists('_biblio_1x')) {
// Migrate is relevant only if there is old Biblio 1.x DB table to work on.
return;
}
if (!class_exists('MigrateDefaultFieldHandler')) {
// We need to make sure we've got at least the Migrate 2.6 beta - identify
// it by the new MigrateDefaultFieldHandler class.
return;
}
return TRUE;
}
/**
* Implements hook_theme().
*/
function biblio_theme() {