-
Notifications
You must be signed in to change notification settings - Fork 11
/
efq_example.module
211 lines (199 loc) · 5.84 KB
/
efq_example.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
<?php
/**
* @file
* Code for the efq_example module
*/
/**
* Implements hook_menu().
*/
function efq_example_menu() {
$items['efq'] = array(
'title' => 'EntityFieldQuery example: recently published content',
'page callback' => 'efq_example_listing',
'access arguments' => array('access content'),
);
return $items;
}
/**
* Implements hook_block_info().
*/
function efq_example_block_info() {
$blocks['content_by_state'] = array(
'info' => t('Other Content In This State'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function efq_example_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'content_by_state':
$block = efq_example_content_by_state();
break;
}
return $block;
}
/**
* Implements hook_node_info().
*/
function efq_example_node_info() {
$items = array(
'efq_article' => array(
'name' => t('EFQ Article'),
'base' => 'node_content',
'description' => t('Article node type for EFQ example.'),
'has_title' => '1',
'title_label' => t('Title'),
'help' => '',
),
'efq_page' => array(
'name' => t('EFQ Page'),
'base' => 'node_content',
'description' => t('Page type for the EFQ example module.'),
'has_title' => '1',
'title_label' => t('Title'),
'help' => '',
),
'efq_photo' => array(
'name' => t('EFQ Photo'),
'base' => 'node_content',
'description' => t('Photo node type for the EFQ example module.'),
'has_title' => '1',
'title_label' => t('Title'),
'help' => '',
),
);
return $items;
}
/**
* [efq_example_listing description]
*
* @param string $state
* state code
* @param array $node_types
* node types to filter
*
* @return array
* markup array
* @todo default : 'efq_article', 'efq_page', 'efq_photo' - coder doesnt like it
*/
function efq_example_listing($state = NULL, $node_types = array()) {
// Instantiate the query using our extended query object.
$query = new NodeEntityFieldQuery();
$query
->entityCondition('bundle', $node_types);
// If $state is defined, add that fieldCondition to the query.
if (!empty($state)) {
$query->fieldCondition('field_us_state', 'value', $state);
}
$result = $query->execute();
$output = array();
// Return the nodes as teasers.
if (!empty($result['node'])) {
$output['nodes'] = node_view_multiple(node_load_multiple(array_keys($result['node'])), 'teaser');
$output['pager']['#markup'] = theme('pager', $query->pager);
}
else {
$output['status']['#markup'] = t('No results were returned.');
}
return $output;
}
/**
* Produces content for a block, based on the state of the host node.
*/
function efq_example_content_by_state() {
$block = array();
// If we don't have a node to draw from, in our current setup,
// we won't have a state and can't continue.
if ($node = menu_get_object()) {
// Get the state value.
$field_name = "field_us_state";
if (!empty($node->$field_name)) {
$items = field_get_items('node', $node, $field_name);
$state = $items[0]['value'];
}
// Only continue if we have a state value.
if ($state) {
// Instantiate the query.
$query = new NodeEntityFieldQuery();
// Limit the query to our established node types.
$node_types = array('efq_article', 'efq_page', 'efq_photo');
$query->entityCondition('bundle', $node_types);
// Add the state value.
$query->fieldCondition($field_name, 'value', $state);
// Prevent the current node from displaying in the block.
$query->propertyCondition('nid', $node->nid, '<>');
// Add a small range.
$query->range(0, 5);
// Remove the current node from the query.
$query->excludeNode();
// Execute the query.
$result = $query->execute();
$output = array();
$block['subject'] = t('Other Content for @state', array('@state' => $state));
if (!empty($result['node'])) {
// Return the nodes as teasers.
$nodes = node_view_multiple(node_load_multiple(array_keys($result['node'])), 'teaser');
$block['content']['nodes'] = $nodes;
// Let's include links to the content listing for convenience.
$node_types = array(
'efq_article' => 'Articles',
'efq_page' => 'Pages',
'efq_photo' => 'Photos',
);
$links = array(
l(t('All Content for @state',
array('@state' => $state)),
"efq/$state"));
foreach ($node_types as $node_type => $node_type_name) {
$links[] = array(
l(t('All @node_type_name for @state',
array(
'@node_type_name' => $node_type_name,
'@state' => $state)),
"efq/$state/${node_type}"));
}
$item_list = array(
'#items' => $links,
'#type' => 'ul',
'#theme' => 'item_list',
);
$block['content']['links'] = $item_list;
}
else {
$block['content']['status']['#markup'] = t('No results.');
}
}
}
return $block;
}
class NodeEntityFieldQuery extends EntityFieldQuery {
/**
* Define some defaults for the class.
*/
public function __construct() {
// Now we don't need to define these over and over anymore.
$this
->entityCondition('entity_type', 'node')
->propertyCondition('status', 1)
->propertyOrderBy('created', 'DESC');
$this->pager();
}
/**
* If we're on a node, and if the entity_type is node,
* exclude the local node from the query
*/
public function excludeNode($nid = '') {
if ($nid = '') {
$object = menu_get_object();
$nid = $object->nid;
}
if (!empty($nid) && $this->entityConditions['entity_type']['value'] === 'node') {
$this->propertyCondition('nid', $nid, '<>');
}
return $this;
}
}