-
Notifications
You must be signed in to change notification settings - Fork 0
/
luggage_courses.module
139 lines (128 loc) · 3.75 KB
/
luggage_courses.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
<?php
/**
* @file
* Code for the luggage_courses feature.
*/
include_once 'luggage_courses.features.inc';
/**
* Token key used for courses.
*/
define('LUGGAGE_COURSES_TOKEN_TYPE', 'luggage_courses');
/**
* Implements hook_update_projects_alter().
*/
function luggage_courses_update_projects_alter(&$projects) {
// Hide a site-specific module from the list.
unset($projects['luggage_courses']);
}
/**
* Implements hook_token_info()
*/
function luggage_courses_token_info() {
return array(
'types' => array(
LUGGAGE_COURSES_TOKEN_TYPE => array(
'name' => t('Course tokens'),
'description' => t('Tokens for course content.'),
),
),
'tokens' => array(
LUGGAGE_COURSES_TOKEN_TYPE => array(
'section_course_title' => array(
'name' => t('Section\'s course title'),
'description' => t('Renders the title of the course that a section is attached to'),
),
),
),
);
}
/**
* Implements hook_tokens().
*/
function luggage_courses_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
if ($type == LUGGAGE_COURSES_TOKEN_TYPE) {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'section_course_title':
$node = $data['node'];
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'course')
->fieldCondition('field_course_section', 'target_id', $node->nid);
$result = $query->execute();
if (!empty($result)) {
$nid = key($result['node']);
$node = node_load($nid);
$replacements[$original] = check_plain($node->title);
}
break;
}
}
}
return $replacements;
}
/**
* Implements hook_ds_fields_info().
*/
function luggage_courses_ds_fields_info($entity_type) {
$fields = array();
if ($entity_type == 'node') {
$fields['related_courses'] = array(
'title' => t('Related courses'),
'field_type' => DS_FIELD_TYPE_FUNCTION,
'function' => '_luggage_courses_related_courses',
'file' => drupal_get_path('module', 'luggage_courses') . '/luggage_courses.ds.field.inc',
);
$fields['section_title'] = array(
'title' => t('Section title'),
'description' => t('Renders section title and wraps it in a link if URL field is set.'),
'field_type' => DS_FIELD_TYPE_FUNCTION,
'function' => '_luggage_courses_section_title',
'file' => drupal_get_path('module', 'luggage_courses') . '/luggage_courses.ds.field.inc',
);
return array($entity_type => $fields);
}
return;
}
/**
* Implements hook_node_insert().
*
* @param object $node
*/
function luggage_courses_node_insert($node) {
if ($node->type == 'course') {
_luggage_courses_set_title($node);
}
}
/**
* Implements hook_node_update().
*
* @param object $node
*/
function luggage_courses_node_update($node) {
if ($node->type == 'course') {
_luggage_courses_set_title($node);
}
}
/**
* Callback to update course section title.
*
* Needed when a section is added to a course not saved yet or when
* a course changes its title.
*
* @param object $node
*/
function _luggage_courses_set_title($node) {
$ewrapper = entity_metadata_wrapper('node', $node);
foreach ($ewrapper->field_course_section->getIterator() as $delta => $item_wrapper) {
$course_section_node = $item_wrapper->value();
if (auto_entitylabel_is_needed($course_section_node, 'node', TRUE)) {
auto_entitylabel_set_title($course_section_node, 'node');
// only save if the title has actually changed.
if (!empty($course_section_node->auto_entitylabel_changed)) {
entity_save('node', $course_section_node);
}
}
}
}