Révision c22e192e
Ajouté par Assos Assos il y a plus de 9 ans
drupal7/sites/all/modules/ctools/includes/content.menu.inc | ||
---|---|---|
23 | 23 |
/** |
24 | 24 |
* Helper function for autocompletion of entity titles. |
25 | 25 |
*/ |
26 |
function ctools_content_autocomplete_entity($type, $string = '') { |
|
26 |
function ctools_content_autocomplete_entity($entity_type, $string = '') {
|
|
27 | 27 |
if ($string != '') { |
28 |
global $user; |
|
29 |
$entity_info = entity_get_info($type); |
|
30 |
if ($type == 'node') { |
|
31 |
$entity_info['entity keys']['bundle field'] = 'type'; |
|
28 |
$entity_info = entity_get_info($entity_type); |
|
29 |
|
|
30 |
if (!module_exists('entity')) { |
|
31 |
module_load_include('inc', 'ctools', 'includes/entity-access'); |
|
32 |
_ctools_entity_access($entity_info, $entity_type); |
|
32 | 33 |
} |
33 | 34 |
|
34 | 35 |
// We must query all ids, because if every one of the 10 don't have access |
... | ... | |
42 | 43 |
// If an ID match was found, use that ID rather than the whole string. |
43 | 44 |
if ($match) { |
44 | 45 |
$entity_id = $preg_matches[1]; |
45 |
$entity = entity_load($type, array($entity_id)); |
|
46 |
|
|
47 |
// Format results in an array so later we could add attributes to the |
|
48 |
// autocomplete text that is returned. |
|
49 |
$results = array($entity_id => array( |
|
50 |
'label' => $entity[$entity_id]->$entity_info['entity keys']['label'], |
|
51 |
)); |
|
46 |
$results = _ctools_getReferencableEntities($entity_type, $entity_info, $entity_id, '=', 1); |
|
52 | 47 |
} |
53 | 48 |
else { |
54 |
$results = _ctools_getReferencableEntities($type, $entity_info, $string, 'LIKE', 10); |
|
55 |
} |
|
56 |
foreach($results as $entity_id => $result) { |
|
57 |
if (!$entity_info['entity keys']['label']) { |
|
58 |
$matches["[id: $entity_id]"] = '<span class="autocomplete_title">' . $entity_id . '</span>'; |
|
59 |
} |
|
60 |
else { |
|
61 |
$matches[$result['label'] . " [id: $entity_id]"] = '<span class="autocomplete_title">' . check_plain($result['label']) . '</span>'; |
|
62 |
$matches[$result['label'] . " [id: $entity_id]"] .= isset($result['bundle field']) ? ' <span class="autocomplete_bundle">(' . check_plain($result['bundle field']) . ')</span>' : ''; |
|
49 |
// We cannot find results if the entity doesn't have a label to search. |
|
50 |
if (!isset($entity_info['entity keys']['label'])) { |
|
51 |
drupal_json_output(array("[id: NULL]" => '<span class="autocomplete_title">' . t('Entity Type !entity_type does not support autocomplete search.', array('!entity_type' => $entity_type)) . '</span>')); |
|
52 |
return; |
|
63 | 53 |
} |
54 |
$results = _ctools_getReferencableEntities($entity_type, $entity_info, $string, 'LIKE', 10); |
|
55 |
} |
|
56 |
foreach ($results as $entity_id => $result) { |
|
57 |
$matches[$result['label'] . " [id: $entity_id]"] = '<span class="autocomplete_title">' . check_plain($result['label']) . '</span>'; |
|
58 |
$matches[$result['label'] . " [id: $entity_id]"] .= isset($result['bundle']) ? ' <span class="autocomplete_bundle">(' . check_plain($result['bundle']) . ')</span>' : ''; |
|
64 | 59 |
} |
65 | 60 |
|
66 | 61 |
drupal_json_output($matches); |
... | ... | |
73 | 68 |
*/ |
74 | 69 |
function _ctools_buildQuery($entity_type, $entity_info, $match = NULL, $match_operator = 'CONTAINS') { |
75 | 70 |
$base_table = $entity_info['base table']; |
76 |
$query = db_select($base_table) |
|
77 |
->fields($base_table, array($entity_info['entity keys']['id'])); |
|
71 |
$label_key = $entity_info['entity keys']['label']; |
|
72 |
$query = db_select($base_table) |
|
73 |
->fields($base_table, array($entity_info['entity keys']['id'])); |
|
78 | 74 |
|
79 |
if (isset($match)) { |
|
80 |
if (isset($entity_info['entity keys']['label'])) { |
|
81 |
$query->condition($base_table .'.'. $entity_info['entity keys']['label'], '%' . $match . '%' , $match_operator); |
|
82 |
} |
|
75 |
if (isset($match)) { |
|
76 |
if (isset($label_key)) { |
|
77 |
$query->condition($base_table . '.' . $label_key, '%' . $match . '%', $match_operator); |
|
83 | 78 |
} |
84 |
|
|
85 |
// Add a label to the query, if the label exists |
|
86 |
if (isset($entity_info['entity keys']['label'])) { |
|
87 |
$query->fields($base_table, array($entity_info['entity keys']['label'])); |
|
79 |
// This should never happen, but double check just in case. |
|
80 |
else { |
|
81 |
return array(); |
|
88 | 82 |
} |
89 |
|
|
90 |
// Add bundle field to the query, if it exists. |
|
91 |
if (isset($entity_info['entity keys']['bundle field'])) { |
|
92 |
$query->fields($base_table, array($entity_info['entity keys']['bundle field'])); |
|
83 |
} |
|
84 |
// Add a generic entity access tag to the query. |
|
85 |
$query->addTag('ctools'); |
|
86 |
|
|
87 |
// We have to perform two checks. First check is a query alter (with tags) |
|
88 |
// in an attempt to only return results that have access. However, this is |
|
89 |
// not full-proof since entities many not implement hook_access query tag. |
|
90 |
// This is why we have a second check after entity load, before we display |
|
91 |
// the label of an entity. |
|
92 |
if ($entity_type == 'comment') { |
|
93 |
// Adding the 'comment_access' tag is sadly insufficient for comments: core |
|
94 |
// requires us to also know about the concept of 'published' and |
|
95 |
// 'unpublished'. |
|
96 |
if (!user_access('administer comments')) { |
|
97 |
$query->condition('comment.status', COMMENT_PUBLISHED); |
|
93 | 98 |
} |
94 | 99 |
|
95 |
// Add a generic entity access tag to the query. |
|
96 |
$query->addTag('ctools'); |
|
97 |
|
|
98 |
if($entity_type == 'comment') { |
|
99 |
// Adding the 'comment_access' tag is sadly insufficient for comments: core |
|
100 |
// requires us to also know about the concept of 'published' and |
|
101 |
// 'unpublished'. |
|
102 |
if (!user_access('administer comments')) { |
|
103 |
$query->condition('comment.status', COMMENT_PUBLISHED); |
|
104 |
} |
|
105 |
// Join to a node if the user does not have node access bypass permissions |
|
106 |
// to obey node published permissions |
|
107 |
if (!user_access('bypass node access') && !count(module_implements('node_grants'))) { |
|
108 |
$node_alias = $query->innerJoin('node', 'n', '%alias.nid = comment.nid'); |
|
109 |
$query->condition($node_alias . '.status', NODE_PUBLISHED); |
|
110 |
} |
|
111 |
$query->addTag('node_access'); |
|
112 |
} |
|
113 |
else { |
|
114 |
$query->addTag($entity_type . '_access'); |
|
100 |
// Join to a node if the user does not have node access bypass permissions |
|
101 |
// to obey node published permissions |
|
102 |
if (!user_access('bypass node access')) { |
|
103 |
$node_alias = $query->innerJoin('node', 'n', '%alias.nid = comment.nid'); |
|
104 |
$query->condition($node_alias . '.status', NODE_PUBLISHED); |
|
115 | 105 |
} |
106 |
$query->addTag('node_access'); |
|
107 |
} |
|
108 |
else { |
|
109 |
$query->addTag($entity_type . '_access'); |
|
110 |
} |
|
116 | 111 |
|
117 |
// Add the sort option.
|
|
118 |
if(isset($entity_info['entity keys']['label'])) {
|
|
119 |
$query->orderBy($base_table .'.'. $entity_info['entity keys']['label'], 'ASC');
|
|
120 |
}
|
|
112 |
// Add the sort option. |
|
113 |
if (isset($label_key)) {
|
|
114 |
$query->orderBy($base_table . '.' . $label_key, 'ASC');
|
|
115 |
} |
|
121 | 116 |
|
122 |
return $query;
|
|
117 |
return $query; |
|
123 | 118 |
} |
124 | 119 |
|
125 | 120 |
/** |
... | ... | |
127 | 122 |
* Entity Reference module. |
128 | 123 |
*/ |
129 | 124 |
function _ctools_getReferencableEntities($entity_type, $entity_info, $match = NULL, $match_operator = 'LIKE', $limit = 0) { |
125 |
global $user; |
|
126 |
$account = $user; |
|
130 | 127 |
$options = array(); |
131 |
|
|
132 |
$query = _ctools_buildQuery($entity_type, $entity_info, $match, $match_operator); |
|
133 |
if ($limit > 0) { |
|
134 |
$query->range(0, $limit); |
|
128 |
// We're an entity ID, return the id |
|
129 |
if (is_numeric($match) && $match_operator == '=') { |
|
130 |
if ($entity = array_shift(entity_load($entity_type, array($match)))) { |
|
131 |
if (isset($entity_info['access callback']) && function_exists($entity_info['access callback'])) { |
|
132 |
if ($entity_info['access callback']('view', $entity, $account, $entity_type)) { |
|
133 |
$label = entity_label($entity_type, $entity); |
|
134 |
return array( |
|
135 |
$match => array( |
|
136 |
'label' => !empty($label) ? $label : $entity->{$entity_info['entity keys']['id']}, |
|
137 |
'bundle' => !empty($entity_info['entity keys']['bundle']) ? check_plain($entity->{$entity_info['entity keys']['bundle']}) : NULL, |
|
138 |
), |
|
139 |
); |
|
140 |
} |
|
141 |
} |
|
142 |
} |
|
143 |
// If you don't have access, or an access callback or a valid entity, just |
|
144 |
// Return back the Entity ID. |
|
145 |
return array( |
|
146 |
$match => array( |
|
147 |
'label' => $match, |
|
148 |
'bundle' => NULL, |
|
149 |
), |
|
150 |
); |
|
135 | 151 |
} |
136 | 152 |
|
137 |
$results = $query->execute(); |
|
153 |
// We have matches, build a query to fetch the result. |
|
154 |
if ($query = _ctools_buildQuery($entity_type, $entity_info, $match, $match_operator)) { |
|
155 |
if ($limit > 0) { |
|
156 |
$query->range(0, $limit); |
|
157 |
} |
|
138 | 158 |
|
139 |
if (!empty($results)) { |
|
140 |
foreach ($results as $record) { |
|
141 |
$options[$record->{$entity_info['entity keys']['id']}] = array( |
|
142 |
'label' => isset($entity_info['entity keys']['label']) ? check_plain($record->{$entity_info['entity keys']['label']}) : $record->{$entity_info['entity keys']['id']}, |
|
143 |
'bundle field' => isset($entity_info['entity keys']['bundle field']) ? check_plain($record->{$entity_info['entity keys']['bundle field']}) : '', |
|
144 |
); |
|
159 |
$results = $query->execute(); |
|
160 |
|
|
161 |
if (!empty($results)) { |
|
162 |
foreach ($results as $record) { |
|
163 |
$entities = entity_load($entity_type, array($record->{$entity_info['entity keys']['id']})); |
|
164 |
$entity = array_shift($entities); |
|
165 |
if (isset($entity_info['access callback']) && function_exists($entity_info['access callback'])) { |
|
166 |
if ($entity_info['access callback']('view', $entity, $account, $entity_type)) { |
|
167 |
$label = entity_label($entity_type, $entity); |
|
168 |
$options[$record->{$entity_info['entity keys']['id']}] = array( |
|
169 |
'label' => !empty($label) ? $label : $entity->{$entity_info['entity keys']['id']}, |
|
170 |
'bundle' => !empty($entity_info['entity keys']['bundle']) ? check_plain($entity->{$entity_info['entity keys']['bundle']}) : NULL, |
|
171 |
); |
|
172 |
} |
|
173 |
} |
|
174 |
} |
|
145 | 175 |
} |
176 |
return $options; |
|
146 | 177 |
} |
147 |
|
|
148 |
return $options; |
|
149 |
} |
|
178 |
return array(); |
|
179 |
} |
Formats disponibles : Unified diff
Weekly update of contrib modules