1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Plugin to provide an relationship handler for entities from query string.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Plugins are described by creating a $plugin array which will be used
|
10
|
* by the system that includes this file.
|
11
|
*/
|
12
|
$plugin = array(
|
13
|
'title' => t('Entity from query string'),
|
14
|
'keyword' => 'query_string_entity',
|
15
|
'description' => t('Entity from query string.'),
|
16
|
'required context' => new ctools_context_required(t('Query string'), 'query_string'),
|
17
|
'context' => 'ctools_entity_from_query_string_context',
|
18
|
'edit form' => 'ctools_entity_from_query_string_settings_form',
|
19
|
);
|
20
|
|
21
|
/**
|
22
|
* Return a new context based on an existing context.
|
23
|
*/
|
24
|
function ctools_entity_from_query_string_context($context, $conf) {
|
25
|
$entity_type = $conf['entity_type'];
|
26
|
|
27
|
// If unset it wants a generic, unfilled context, which is just NULL.
|
28
|
if (empty($context->data) || !isset($context->data) || !is_numeric($context->data)) {
|
29
|
return ctools_context_create_empty('entity:' . $entity_type, NULL);
|
30
|
}
|
31
|
|
32
|
if (!empty($context->data) && is_numeric($context->data)) {
|
33
|
// Load the entity from the query string value.
|
34
|
$entity_id = $context->data;
|
35
|
$entity = entity_load_single($entity_type, $entity_id);
|
36
|
|
37
|
// Send it to ctools.
|
38
|
return ctools_context_create('entity:' . $entity_type, $entity);
|
39
|
}
|
40
|
}
|
41
|
|
42
|
/**
|
43
|
* Settings form for the relationship.
|
44
|
*/
|
45
|
function ctools_entity_from_query_string_settings_form($form, &$form_state) {
|
46
|
|
47
|
// Get all avalible enity types.
|
48
|
foreach (entity_get_info() as $key => $value) {
|
49
|
$entity_types[$key] = $value['label'];
|
50
|
}
|
51
|
|
52
|
$form['entity_type'] = array(
|
53
|
'#title' => t('Entity type'),
|
54
|
'#description' => t('Choose entity type to load from query value'),
|
55
|
'#type' => 'select',
|
56
|
'#options' => $entity_types,
|
57
|
);
|
58
|
if (isset($form_state['conf']['entity_type'])) {
|
59
|
$form['entity_type']['#default_value'] = $form_state['conf']['entity_type'];
|
60
|
}
|
61
|
|
62
|
return $form;
|
63
|
}
|
64
|
|
65
|
/**
|
66
|
* Submit handler; settings form for the context.
|
67
|
*/
|
68
|
function ctools_entity_from_query_string_settings_form_submit($form, &$form_state) {
|
69
|
$form_state['conf']['entity_type'] = $form_state['values']['entity_type'];
|
70
|
}
|