-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathawesomerelationship.module
256 lines (228 loc) · 8.32 KB
/
awesomerelationship.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
<?php
// $Id$
/**
* Implementation of hook_entity_info().
*/
function awesomerelationship_entity_info() {
$return = array(
'relationship' => array(
'label' => t('Relationship'),
'base table' => 'relationship',
'fieldable' => FALSE,
'controller class' => 'EntityRelationshipController',
'entity keys' => array(
'id' => 'relationship_id',
'bundle' => 'predicate',
),
'bundle keys' => array(
'bundle' => 'predicate',
),
'bundles' => array(
// describe Drupal's built in relationships
'creator' => array(
'label' => t('Author'),
),
),
'view modes' => array(),
),
);
return $return;
}
function awesomerelationship_entity_info_alter(&$entity_info) {
$entity_info['node']['relationships']['user'] = array(
// a handler -- this can be reusable or specific to the entity type
// it will do things like list 'user' entities related to one or more nodes
'handler' => 'AwesomerelationshipEntityRelation',
);
}
function awesomerelationship_field_info_alter(&$field_info) {
$field_info['file']['relationships']['files'] = array(
'handler' => 'AwesomerelationshipFieldRelation',
);
}
/**
* Controller class for entity relationships.
*
* This extends the DrupalDefaultEntityController class. The buildQuery method
* is overriden to add the self join and to exclude rows where the left and
* right entities are identical.
*/
class EntityRelationshipController extends DrupalDefaultEntityController {
protected function buildQuery($ids, $conditions = array(), $revision_id = FALSE) {
$query = parent::buildQuery($ids, $conditions, $revision_id);
_awesomerationships_query_helper($query);
return $query;
}
}
function _awesomerationships_query_helper($query) {
// Add the machine name field from the {taxonomy_vocabulary} table.
$query->innerJoin('relationship_data', 'l', 'base.relationship_id = l.relationship_id');
$query->innerJoin('relationship_data', 'r', 'base.relationship_id = r.relationship_id AND NOT (l.entity_type = r.entity_type AND l.entity_id = r.entity_id)');
$query->addField('base', 'relationship_id');
$query->addField('base', 'predicate');
$query->addField('l', 'entity_type', 'left_entity_type');
$query->addField('l', 'entity_id', 'left_entity_id');
$query->addField('r', 'entity_type', 'right_entity_type');
$query->addField('r', 'entity_id', 'right_entity_id');
}
/**
* Interface for relationship handlers.
*/
interface AwesomeRelationship {
// bangpound
public function getRelated($entity, $type);
// becw
function init($left, $right); // sets types
function set_left($entity_ids = array()); // sets left objects
function set_right($entity_ids = array()); // sets right objects
function get_left(); // returns left
function get_right(); // returns right
}
/**
* Handler class for entity relationships.
*/
class AwesomeRelationshipHandler implements AwesomeRelationship {
function __construct() {
}
/**
* Entity is a fully loaded entity (node, user, term, etc.)
* Type is the predicate.
*/
public function getRelated($entity, $type) {
return NULL;
}
function init($left, $right) {
}
function set_left($entity_ids = array()) {
}
function set_right($entity_ids = array()) {
}
function get_left() {
}
function get_right() {
}
}
function awesomerelationship_field_info() {
return array(
'awesomerelationship' => array(
'label' => t('Awesome Relationship'),
'description' => t('This field stores relationships between entities.'),
'settings' => array('allowed_values' => '', 'allowed_values_function' => ''),
'default_widget' => 'awesomerelationship_default',
'default_formatter' => 'awesomerelationship_default',
),
);
}
function awesomerelationship_field_is_empty() {
return FALSE;
}
function awesomerelationship_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
list($entity_id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
$insert1 = db_insert('relationship')->fields(array('predicate'));
$insert2 = db_insert('relationship_data')->fields(array('relationship_id', 'entity_type', 'entity_id'));
foreach ($items as $item) {
$relationship_id = $insert1->values(array($field['field_name']))->execute();
$insert2->values(array($relationship_id, $item['entity_type'], $item['entity_id']));
$insert2->values(array($relationship_id, $entity_type, $entity_id));
}
$insert2->execute();
$items = array();
}
function awesomerelationship_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
awesomerelationship_field_delete($entity_type, $entity, $field, $instance, $langcode, $items);
awesomerelationship_field_insert($entity_type, $entity, $field, $instance, $langcode, $items);
}
function awesomerelationship_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) {
list($entity_id) = entity_extract_ids($entity_type, $entity);
$result = db_query('SELECT relationship_id FROM {relationship_data} WHERE entity_type = :entity_type AND entity_id = :entity_id', array(':entity_type' => $entity_type, ':entity_id' => $entity_id));
foreach ($result as $row) {
db_delete('relationship')->condition('relationship_id', $row->relationship_id)->execute();
db_delete('relationship_data')->condition('relationship_id', $row->relationship_id)->execute();
}
}
function awesomerelationship_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
foreach ($entities as $entity) {
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
$entity_ids[] = $id;
}
$query = db_select('relationship', 'base')
->condition('l.entity_type', $entity_type)
->condition('l.entity_id', $entity_ids);
_awesomerationships_query_helper($query);
foreach ($query->execute() as $item) {
$items[$item->left_entity_id][] = array(
'relationship_id' => $item->relationship_id,
'predicate' => $item->predicate,
'entity_id' => $item->right_entity_id,
'entity_type' => $item->right_entity_type,
);
}
}
function awesomerelationship_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
$entities_to_load = array();
foreach ($items as $key => $item) {
foreach ($item as $delta => $value) {
$entities_to_load[$value['entity_type']][] = $value['entity_id'];
$lookup[$value['entity_type']][$value['entity_id']][] = array($key, $delta);
}
}
foreach ($entities_to_load as $entity_type => $ids) {
$entities = entity_load($entity_type, $ids);
foreach ($entities as $entity_id => $entity) {
foreach ($lookup[$entity_type][$entity_id] as $data) {
$items[$data[0]][$data[1]]['entity'] = $entity;
}
}
}
}
function awesomerelationship_field_widget_info() {
return array(
'awesomerelationship_default' => array(
'label' => t('Awesomerelationship chooser'),
'field types' => array('awesomerelationship'),
),
);
}
/**
* Implements hook_field_widget_form().
*/
function awesomerelationship_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$element['#type'] = 'fieldset';
$element['entity_type'] = array(
'#type' => 'select',
'#title' => t('Entity type'),
'#options' => drupal_map_assoc(array_keys(entity_get_info())),
'#default_value' => isset($items[$delta]) ? $items[$delta]['entity_type'] : '',
);
$element['entity_id'] = array(
'#title' => t('Entity ID'),
'#type' => 'textfield',
'#default_value' => isset($items[$delta]) ? $items[$delta]['entity_id'] : '',
);
return $element;
}
/**
* Implements hook_field_formatter_info().
*/
function awesomerelationship_field_formatter_info() {
return array(
'awesomerelationship_default' => array(
'label' => t('Default'),
'field types' => array('awesomerelationship'),
),
);
}
/**
* Implements hook_field_formatter_view().
*/
function awesomerelationship_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$list = array();
foreach ($items as $item) {
$uri = entity_uri($item['entity_type'], $item['entity']);
$list[] = l($item['entity_type'], $uri['path'], $uri['options']);
}
return array(
'#theme' => 'item_list',
'#items' => $list,
);
}