-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgnl_schema.module
391 lines (352 loc) · 12.3 KB
/
gnl_schema.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
<?php
/**
* @file
* Code for the gnl_schema feature.
*/
include_once 'gnl_schema.features.inc';
/**
* Implements hook_entity_info_alter().
*
* Add view modes.
*/
function gnl_schema_entity_info_alter(&$entity_info) {
$entity_info['node']['view modes']['grantor_only'] = array(
'label' => t('Grantor only'),
'custom settings' => FALSE,
);
$entity_info['node']['view modes']['grantee_only'] = array(
'label' => t('Grantee only'),
'custom settings' => FALSE,
);
}
/**
* Implements hook_preprocess_node().
*
* Add template hints for the added view modes
*/
function gnl_schema_preprocess_node(&$vars) {
if($vars['view_mode'] == 'grantor_only') {
$vars['theme_hook_suggestions'][] = 'node__' . $vars['type'] . '__grantor_only';
}
if($vars['view_mode'] == 'grantee_only') {
$vars['theme_hook_suggestions'][] = 'node__' . $vars['type'] . '__grantee_only';
}
}
/**
* Implements hook_form_alter().
*
* Dupe some fields from the previously-added grant.
function gnl_schema_form_alter(&$form, $form_state, $form_id) {
if (arg(1) == 'add' && $form_id == 'grant_node_form') {
$q = new EntityFieldQuery();
$q->entityCondition('entity_type', 'node')
->propertyCondition('type', 'grant')
->range(0, 1)
->propertyOrderBy('changed', 'DESC');
$res = $q->execute();
$node = node_load(current(array_keys($res['node'])));
$mru = entity_metadata_wrapper('node', $node);
$mru_dates = $mru->field_year->value();
$form['field_year'][LANGUAGE_NONE][0]['#default_value']['value'] = $mru_dates['value'];
$form['field_year'][LANGUAGE_NONE][0]['#default_value']['value2'] = $mru_dates['value2'];
$form['field_source'][LANGUAGE_NONE][0]['value']['#default_value'] = $mru->field_source->value();
// kludge-ville USA: https://drupal.org/node/1514444
$form['field_funder'][LANGUAGE_NONE][0]['target_id']['#default_value'] = $mru->field_funder->value()->title . " (" . $mru->field_funder->value()->nid . ")";
}
}
*/
/**
* Implements hook_init().
*
* Allow people to log in at /user,
* API users to access the API,
* logged-in users to do anything,
* Drush to do anything,
* and cli server to do anything.
*/
function gnl_schema_init() {
if (user_is_anonymous()
&& arg(0) !== 'user'
&& arg(0) !== 'api'
&& arg(0) !== 'flags'
&& arg(0) !== 'grantscsv'
&& arg(0) !== 'organizationscsv'
&& !function_exists('drush_main')
&& !drupal_is_cli()) {
drupal_goto('http://detroitledger.org');
}
}
function gnl_schema_get_organization($node) {
$organization_id = $node->field_organization['und'][0]['target_id'];
return entity_load_single('node', $organization_id);
}
function gnl_schema_get_person($node) {
$person_id = $node->field_person['und'][0]['target_id'];
return entity_load_single('node', $person_id);
}
/**
* Return a reasonable default title for a board term.
*/
function gnl_schema_board_term_title($node) {
$person = gnl_schema_get_person($node);
$organization = gnl_schema_get_organization($node);
if ($person && $organization) {
return $organization->title . " / " . $person->title;
}
return "Board membership";
}
function gnl_schema_node_presave_board_term($node) {
if (!isset($node->title) || $node->title == '') {
$node->title = gnl_schema_board_term_title($node);
}
}
function gnl_schema_node_presave($node) {
if ($node->type == 'board_term') {
gnl_schema_node_presave_board_term($node);
}
}
function gnl_schema_form_board_term_node_form_alter(&$form, &$form_state, $form_id) {
// Hide the ``title`` field and make it non-required. Its value will
// be populated by ``gnl_schema_node_presave()`` above.
$form['title']['#access'] = FALSE;
$form['title']['#required'] = FALSE;
}
/**
* Alter the IEF reference form to provide a better UI/UX.
*
* DOES NOT implement hook_form_alter() as that hook never gets called for
* this section of the form tree. However, it's intent is the same as a
* hook_form_alter implementation.
*
*/
function gnl_schema_alter_inline_entity_form_reference_form(&$form) {
if ($form['#parents'][0] == 'field_person') {
// By default, for node entities, the term "Node" is used instead of the
// bundle name. We want to use something more specific.
//
// @hack: Hard code the label name. It would be more elegant to use the
// human-readable bundle name, but that's not easily accessable from this
// context.
$type = 'Person';
$form['#title'] = t('Add existing @type', array('@type' => $type));
$form['entity_id']['#title'] = $type;
$form['actions']['ief_reference_save']['#value'] = t('Add @type', array('@type' => $type));
// Hide the "Cancel" button
$form['actions']['ief_reference_cancel']['#access'] = FALSE;
}
}
/**
* Alter the IEF add form to provide a better UI/UX.
*
* DOES NOT implement hook_form_alter() as that hook never gets called for
* this section of the form tree. However, it's intent is the same as a
* hook_form_alter implementation.
*
*/
function gnl_schema_alter_inline_entity_form_add_form(&$form) {
global $user;
if ($form['#parents'][0] == 'field_person') {
// By default, for node entities, the term "Node" is used instead of the
// bundle name. We want to use something more specific.
//
// @hack: Hard code the label name. It would be more elegant to use the
// human-readable bundle name, but that's not easily accessable from this
// context.
$type = 'Person';
$form['#title'] = t('Add new @type', array('@type' => $type));
$form['actions']['ief_add_save']['#value'] = t('Create @type', array('@type' => $type));
}
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*
* Always show the search form.
*/
function gnl_schema_field_widget_inline_entity_form_form_alter(&$element, &$form_state, $context) {
if ($element['#field_name'] == 'field_person') {
$ief_id = $element['#ief_id'];
// @hack: Hard code the label name. It would be more elegant to use the
// human-readable bundle name, but that's not easily accessable from this
// context.
$type = 'Person';
// No referenced entities are listed and no form is open
if (empty($form_state['inline_entity_form'][$ief_id]['entities']) &&
empty($form_state['inline_entity_form'][$ief_id]['form'])) {
$instance = $form_state['inline_entity_form'][$ief_id]['instance'];
$settings = inline_entity_form_settings($context['field'], $instance);
$controller = inline_entity_form_get_controller($instance);
// Build a parents array for this element's values in the form.
$parents = array_merge($element['#field_parents'], array($element['#field_name'], $element['#language']));
// Get the langcode of the parent entity.
$parent_langcode = entity_language($element['#entity_type'], $element['#entity']);
// Show the form for adding an existin element (reference form)
$form_state['inline_entity_form'][$ief_id]['form'] = 'ief_add_existing';
$element['form'] = array(
'#type' => 'fieldset',
'#attributes' => array('class' => array('ief-form', 'ief-form-bottom')),
// Identifies the IEF widget to which the form belongs.
'#ief_id' => $ief_id,
// Used by Field API and controller methods to find the relevant
// values in $form_state.
'#parents' => array_merge($parents, array('form')),
// Pass the current entity type.
'#entity_type' => $settings['entity_type'],
// Pass the langcode of the parent entity,
'#parent_language' => $parent_langcode,
);
$element['form'] += inline_entity_form_reference_form($controller, $element['form'], $form_state);
gnl_schema_alter_inline_entity_form_reference_form($element['form']);
// Remove the "Add existing" button
unset($element['actions']['ief_add_existing']);
// Replace "node" with "Person" in the "Add new ..." button
$element['actions']['ief_add']['#value'] = t('Add new @type', array('@type' => $type));
}
else if ($form_state['inline_entity_form'][$ief_id]['form'] == 'add') {
// Add form is shown
gnl_schema_alter_inline_entity_form_add_form($element['form']);
}
}
}
// See http://www.thedavidmeister.info/post/exporting-and-maintaining-custom-date-formats-drupal-7
// for the date format hack backstory.
/**
* Purge date formats cache.
*
* Call via hook_flush_caches() or something similar.
*/
function _gnl_schema_purge_date_formats() {
$formats = system_get_date_formats('gnl_date_type_human_short');
variable_set('date_format_' . 'gnl_date_type_human_short', 'F j, Y');
// Date formats without a dfid are being read directly from code. We can use
// the existence of this value to detect "ghost" formats that are not managed
// in this file.
if (!empty($format['dfid'])) {
system_date_format_delete($format['dfid']);
}
}
/**
* Implements hook_date_formats().
*/
function gnl_schema_date_formats() {
$formats = array();
$formats[] = array(
'type' => 'gnl_date_type_human_short',
'format' => 'F j, Y',
'locales' => array(),
);
return $formats;
}
/**
* Implements hook_date_format_types().
*/
function gnl_schema_date_format_types() {
$types = array();
$types['gnl_date_type_human_short'] = t('Human date (F j, Y)');
return $types;
}
function gnl_schema_flush_caches() {
_gnl_schema_purge_date_formats();
}
/**
* Returns true if management links be shown for this node type and view.
*/
function gnl_schema_show_management_links($node, $view_mode) {
$visibility_conditions = array(
'org' => array(
'view_modes' => array('full', 'teaser',),
'permission' => 'create board_term content',
),
'board_term' => array(
'view_modes' => array('full', 'teaser',),
'permission' => 'edit own board_term content',
),
);
if (!array_key_exists($node->type, $visibility_conditions)) {
return FALSE;
}
if (!user_permission($visibility_conditions[$node->type]['permission'])) {
return FALSE;
}
return in_array($view_mode, $visibility_conditions[$node->type]['view_modes']);
}
function gnl_schema_management_links($node) {
if ($node->type == 'org') {
return array(
array(
'title' => 'create board term',
'href' => 'node/add/board-term',
'query' => array(
'field_org' => $node->nid,
'destination' => current_path(),
),
),
);
}
else if ($node->type == 'board_term') {
return array(
array(
'title' => 'edit',
'href' => 'node/' . $node->nid . '/edit',
'query' => array(
'destination' => current_path(),
),
),
array(
'title' => 'delete',
'href' => 'node/' . $node->nid . '/delete',
'query' => array(
'destination' => current_path(),
),
),
);
}
else {
return array();
}
}
/**
* Implements hook_node_view().
*
* Add management links to org type.
*/
function gnl_schema_node_view($node, $view_mode, $langcode) {
// only show management links during certain view modes
if (gnl_schema_show_management_links($node, $view_mode)) {
// remove "read more"
unset($node->content['links']['node']);
// @hack: Even though the view mode is checked in
// gnl_schema_show_management_links, we check again here.
// This is because the links should show up in the full view
// of an org node, but not in the links region. Instead,
// we render them below the list of board members
if ($node->type == 'org' && $view_mode != 'full') {
$node->content['links']['add_board_term'] = array(
'#links' => gnl_schema_management_links($node),
);
}
else if ($node->type == 'board_term') {
$node->content['links']['manage_board_term'] = array(
'#links' => gnl_schema_management_links($node),
);
}
}
}
/**
* Implements hook_views_data_alter().
*
* For tables with an EIN relationship defined, also join to the org entity.
*/
function gnl_schema_views_data_alter(&$data) {
foreach ($data as &$view) {
if (isset($view['table']['join']['field_data_field_ein'])) {
$field = $view['table']['join']['field_data_field_ein']['field'];
$view['table']['join']['node'] = array(
'left_table' => 'field_data_field_ein',
'left_field' => 'field_ein_value',
'field' => $field,
);
}
}
}
function gnl_schema_views_pre_execute(&$view) {
}