comparison sites/all/modules/ctools/plugins/arguments/entity_id.inc @ 0:ff03f76ab3fe

initial version
author danieleb <danielebarchiesi@me.com>
date Wed, 21 Aug 2013 18:51:11 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:ff03f76ab3fe
1 <?php
2
3 /**
4 * @file
5 *
6 * Plugin to provide an argument handler for all entity ids
7 */
8
9 /**
10 * Plugins are described by creating a $plugin array which will be used
11 * by the system that includes this file.
12 */
13 $plugin = array(
14 'title' => t("Entity: ID"),
15 'description' => t('Creates an entity context from an entity ID argument.'),
16 'context' => 'ctools_argument_entity_id_context',
17 'get child' => 'ctools_argument_entity_id_get_child',
18 'get children' => 'ctools_argument_entity_id_get_children',
19 );
20
21 function ctools_argument_entity_id_get_child($plugin, $parent, $child) {
22 $plugins = ctools_argument_entity_id_get_children($plugin, $parent);
23 return $plugins[$parent . ':' . $child];
24 }
25
26 function ctools_argument_entity_id_get_children($original_plugin, $parent) {
27 $entities = entity_get_info();
28 $plugins = array();
29 foreach ($entities as $entity_type => $entity) {
30 $plugin = $original_plugin;
31 $plugin['title'] = t('@entity: ID', array('@entity' => $entity['label']));
32 $plugin['keyword'] = $entity_type;
33 $plugin['description'] = t('Creates @entity context from an ID argument.', array('@entity' => $entity_type));
34 $plugin['name'] = $parent . ':' . $entity_type;
35 $plugin_id = $parent . ':' . $entity_type;
36 drupal_alter('ctools_entity_context', $plugin, $entity, $plugin_id);
37 $plugins[$plugin_id] = $plugin;
38 }
39 drupal_alter('ctools_entity_contexts', $plugins);
40 return $plugins;
41 }
42
43 /**
44 * Discover if this argument gives us the entity we crave.
45 */
46 function ctools_argument_entity_id_context($arg = NULL, $conf = NULL, $empty = FALSE) {
47 $entity_type = explode(':', $conf['name']);
48 $entity_type = $entity_type[1];
49 // If unset it wants a generic, unfilled context.
50 if ($empty) {
51 return ctools_context_create_empty('entity:' . $entity_type);
52 }
53
54 // We can accept either an entity object or a pure id.
55 if (is_object($arg)) {
56 return ctools_context_create('entity:' . $entity_type, $arg);
57 }
58
59 if (!is_numeric($arg)) {
60 return FALSE;
61 }
62
63 $entity = entity_load($entity_type, array($arg));
64 if (!$entity) {
65 return FALSE;
66 }
67
68 return ctools_context_create('entity:' . $entity_type, $entity[$arg]);
69 }
70