6 * Plugin to provide an argument handler for all entity ids.
10 * Plugins are described by creating a $plugin array which will be used
11 * by the system that includes this file.
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',
22 'placeholder form' => 'ctools_argument_entity_id_ctools_argument_placeholder',
25 function ctools_argument_entity_id_get_child($plugin, $parent, $child) {
26 $plugins = ctools_argument_entity_id_get_children($plugin, $parent);
27 return $plugins[$parent . ':' . $child];
30 function ctools_argument_entity_id_get_children($original_plugin, $parent) {
31 $entities = entity_get_info();
33 foreach ($entities as $entity_type => $entity) {
34 $plugin = $original_plugin;
35 $plugin['title'] = t('@entity: ID', array('@entity' => $entity['label']));
36 $plugin['keyword'] = $entity_type;
37 $plugin['description'] = t('Creates @entity context from an ID argument.', array('@entity' => $entity_type));
38 $plugin['name'] = $parent . ':' . $entity_type;
39 $plugin_id = $parent . ':' . $entity_type;
40 drupal_alter('ctools_entity_context', $plugin, $entity, $plugin_id);
41 $plugins[$plugin_id] = $plugin;
43 drupal_alter('ctools_entity_contexts', $plugins);
49 * Discover if this argument gives us the entity we crave.
51 function ctools_argument_entity_id_context($arg = NULL, $conf = NULL, $empty = FALSE) {
52 $entity_type = explode(':', $conf['name']);
53 $entity_type = $entity_type[1];
54 // If unset it wants a generic, unfilled context.
56 return ctools_context_create_empty('entity:' . $entity_type);
59 // We can accept either an entity object or a pure id.
60 if (is_object($arg)) {
61 return ctools_context_create('entity:' . $entity_type, $arg);
64 // Trim spaces and other garbage.
67 if (!is_numeric($arg)) {
68 $preg_matches = array();
69 $match = preg_match('/\[id: (\d+)\]/', $arg, $preg_matches);
71 $match = preg_match('/^id: (\d+)/', $arg, $preg_matches);
75 $id = $preg_matches[1];
77 if (isset($id) && is_numeric($id)) {
78 return ctools_context_create('entity:' . $entity_type, $id);
83 $entities = entity_load($entity_type, array($arg));
84 if (empty($entities)) {
88 return ctools_context_create('entity:' . $entity_type, reset($entities));
91 function ctools_argument_entity_id_settings_form(&$form, &$form_state, $conf) {
92 $plugin = &$form_state['plugin'];
94 $form['settings']['entity'] = array(
95 '#title' => t('Enter the title or ID of a @entity entity', array('@entity' => $plugin['keyword'])),
96 '#type' => 'textfield',
98 '#autocomplete_path' => 'ctools/autocomplete/' . $plugin['keyword'],
102 if (!empty($conf['entity_id'])) {
103 $info = entity_load($plugin['keyword'], array($conf['entity_id']));
104 $info = $info[$conf['entity_id']];
106 $entity = entity_get_info($plugin['keyword']);
107 $uri = entity_uri($plugin['keyword'], $info);
108 if (is_array($uri) && $entity['entity keys']['label']) {
109 $link = l(t("'%title' [%type id %id]", array('%title' => $info->{$entity['entity keys']['label']}, '%type' => $plugin['keyword'], '%id' => $conf['entity_id'])), $uri['path'], array('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => TRUE));
111 elseif (is_array($uri)) {
112 $link = l(t("[%type id %id]", array('%type' => $plugin['keyword'], '%id' => $conf['entity_id'])), $uri['path'], array('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => TRUE));
114 elseif ($entity['entity keys']['label']) {
115 $link = l(t("'%title' [%type id %id]", array('%title' => $info->{$entity['entity keys']['label']}, '%type' => $plugin['keyword'], '%id' => $conf['entity_id'])), file_create_url($uri), array('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => TRUE));
118 $link = t("[%type id %id]", array('%type' => $plugin['keyword'], '%id' => $conf['entity_id']));
120 $form['settings']['entity']['#description'] = t('Currently set to !link', array('!link' => $link));
124 $form['settings']['entity_id'] = array(
126 '#value' => isset($conf['entity_id']) ? $conf['entity_id'] : '',
129 $form['settings']['entity_type'] = array(
131 '#value' => $plugin['keyword'],
137 function ctools_argument_entity_id_ctools_argument_placeholder($conf) {
139 '#title' => t('Enter the title or ID of a @entity entity', array('@entity' => $conf['keyword'])),
140 '#type' => 'textfield',
142 '#autocomplete_path' => 'ctools/autocomplete/' . $conf['keyword'],