-
Notifications
You must be signed in to change notification settings - Fork 2
/
luggage_aliases.module
176 lines (164 loc) · 5.51 KB
/
luggage_aliases.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
<?php
/**
* @file
* Code for the luggage_aliases feature.
*/
include_once 'luggage_aliases.features.inc';
const LUGGAGE_ALIASES_VOCAB_NAME = 'aliases';
/**
* Implements hook_FORM_ID_form_alter()
*/
function luggage_aliases_form_people_node_form_alter(&$form, &$form_state, $form_id) {
$form['#submit'][] = '_people_node_form_submit';
}
function _people_node_form_submit($form, &$form_state) {
$title = getTermName($form_state);
if(count($form_state['values']['field_people_aliases'][LANGUAGE_NONE]) == 0) {
// There is no alias assigned to this person
// Create alias
$t = taxonomy_get_term_by_name($title,LUGGAGE_ALIASES_VOCAB_NAME);
if(count($t) == 0) {
// There is no term in the vocabulary with the same name
// Create a new term
$form_state['values']['field_people_aliases'][LANGUAGE_NONE][] = (array)createTerm($title);
}
} else if($title !== trim($form['#node']->title)) {
// The person title has changed
// Rename alias
$t = taxonomy_get_term_by_name($form_state['values']['field_people_aliases'][LANGUAGE_NONE][0]['name'],LUGGAGE_ALIASES_VOCAB_NAME);
if(count($t) > 0) {
$t = array_slice($t,0,1)[0];
$t->name = $title;
taxonomy_term_save($t);
}
}
}
function getTermName($form_state) {
$title = $form_state['values']['field_people_title'][LANGUAGE_NONE][0]['value'] . ' ' . $form_state['values']['field_people_first_name'][LANGUAGE_NONE][0]['value'] . ' ' . $form_state['values']['field_people_middle_initial'][LANGUAGE_NONE][0]['value'] . ' ' . $form_state['values']['field_people_last_name'][LANGUAGE_NONE][0]['value'];
return trim($title);
}
function createTerm($name) {
$term = new stdClass();
$term->vid = get_vocab_id(LUGGAGE_ALIASES_VOCAB_NAME)->vid;
$term->name = $name;
$term->description = '';
taxonomy_term_save($term);
return $term;
}
/**
* @param $vocab_machine_name
* @return null or vocabulary object
*/
function get_vocab_id($vocab_machine_name) {
$vocabularies = taxonomy_get_vocabularies();
$vocab = Null;
foreach($vocabularies as $v) {
if($v->machine_name == $vocab_machine_name) {
$vocab = $v;
break;
}
}
return $vocab;
}
/**
* Implements hook_field_formatter_info()
*/
function luggage_aliases_field_formatter_info() {
$info = array(
'author_list' => array(
'label' => t('Author List'),
'field types' => array('taxonomy_term_reference'),
'description' => t('Displays Title as a link and URL as plain text.'),
),
);
return $info;
}
/**
* Implements hook_field_formatter_view()
*/
function luggage_aliases_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
switch ($display['type']) {
case 'author_list':
foreach ($items as $delta => $item) {
$query = db_select('taxonomy_index','t')->fields('t');
$query->condition('t.tid',$item['tid'],'=');
$nodes = db_select('node','n')->fields('n')->condition('type','people');
$d = $query->join($nodes,'n','t.nid = n.nid');
$data = $query->execute()->fetchCol();
if($data) {
$user = node_load($data[0]);
$element[$delta] = array(
'#theme' => 'luggage_aliases_default',
'#image_src' => image_style_url('thumbnail',$user->field_people_image[LANGUAGE_NONE][0]['uri']),
'#firstname' => $user->field_people_first_name[LANGUAGE_NONE][0]['value'],
'#lastname' => $user->field_people_last_name[LANGUAGE_NONE][0]['value'],
'#position' => $user->field_people_position[LANGUAGE_NONE][0]['value'],
'#url' => $user->nid,
'#bio' => (isset($user->field_people_bio[LANGUAGE_NONE][0]['value']))?substr($user->field_people_bio[LANGUAGE_NONE][0]['value'],0,300) . '...':'',
);
} else {
$term = taxonomy_term_load($item['tid']);
if($term) {
$element[$delta] = array(
'#theme' => 'luggage_aliases_default',
'#aliasName' => $term->name,
'#url' => '/authors/' . $term->tid,
);
}
}
}
drupal_add_css(drupal_get_path('module', 'luggage_aliases') . '/css/luggage_aliases.css');
break;
}
return $element;
}
/**
* Implements hook_theme()
*/
function luggage_aliases_theme() {
return array(
'luggage_aliases_default' => array(
'template' => 'luggage_aliases_default',
'variables' => array(
'image_src' => Null,
'firstname' => Null,
'lastname' => Null,
'position' => Null,
'url' => Null,
'bio' => Null,
'aliasName' => Null,
),
'path' => drupal_get_path('module','luggage_aliases') . '/templates'
),
'node__people' => array(
'render element' => 'content',
'base hook' => 'node',
'template' => 'node--people',
'path' => drupal_get_path('module','luggage_aliases') . '/templates'
)
);
}
/**
* Implements hook_field_widget_info().
*/
function luggage_aliases_field_widget_info() {
return array(
'author_widget' => array(
'label' => t('Author Widget'),
'field types' => array('taxonomy_term_reference'),
),
);
}
/**
* Implements hook_preprocess_HOOK
*/
function luggage_aliases_process_node(&$variables) {
if($variables['type'] == 'people') {
$variables['authNames'] = array();
foreach($variables['field_people_aliases'][LANGUAGE_NONE] as $name) {
array_push($variables['authNames'],$name['tid']);
}
$variables['authNames'] = implode('+',$variables['authNames']);
}
}