Projet

Général

Profil

Paste
Télécharger (2,24 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / uuid / plugins / arguments / entity_uuid.inc @ 3dfa8105

1
<?php
2

    
3
/**
4
 * @file
5
 * Plugin to provide an argument handler for all entity IDs.
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: UUID"),
14
  'description' => t('Creates an entity context from an entity UUID argument.'),
15
  'context' => 'uuid_entity_uuid_context',
16
  'get child' => 'uuid_entity_uuid_get_child',
17
  'get children' => 'uuid_entity_uuid_get_children',
18
);
19

    
20
function uuid_entity_uuid_get_child($plugin, $parent, $child) {
21
  $plugins = uuid_entity_uuid_get_children($plugin, $parent);
22
  return $plugins[$parent . ':' . $child];
23
}
24

    
25
function uuid_entity_uuid_get_children($original_plugin, $parent) {
26
  $entities = entity_get_info();
27
  $plugins = array();
28
  foreach ($entities as $entity_type => $entity) {
29
    $plugin = $original_plugin;
30
    $plugin['title'] = t('@entity: UUID', array('@entity' => $entity['label']));
31
    $plugin['keyword'] = $entity_type;
32
    $plugin['description'] = t('Creates @entity context from an UUID argument.', array('@entity' => $entity_type));
33
    $plugin['name'] = $parent . ':' . $entity_type;
34
    $plugin_id = $parent . ':' . $entity_type;
35
    drupal_alter('ctools_entity_context', $plugin, $entity, $plugin_id);
36
    $plugins[$plugin_id] = $plugin;
37
  }
38
  drupal_alter('ctools_entity_contexts', $plugins);
39
  return $plugins;
40
}
41

    
42
/**
43
 * Discover if this argument gives us the entity we crave.
44
 */
45
function uuid_entity_uuid_context($arg = NULL, $conf = NULL, $empty = FALSE) {
46
  $entity_type = explode(':', $conf['name']);
47
  $entity_type = $entity_type[1];
48
  // If unset it wants a generic, unfilled context.
49
  if ($empty) {
50
    return ctools_context_create_empty('entity:' . $entity_type);
51
  }
52

    
53
  // We can accept either an entity object or a pure id.
54
  if (is_object($arg)) {
55
    return ctools_context_create('entity:' . $entity_type, $arg);
56
  }
57

    
58
  if (!is_string($arg)) {
59
    return FALSE;
60
  }
61
  $entity_ids = entity_get_id_by_uuid($entity_type, array($arg));
62
  if (!isset($entity_ids[$arg])) {
63
    return FALSE;
64
  }
65
  $entity_id = $entity_ids[$arg];
66
  $entities = entity_load($entity_type, array($entity_id));
67
  if (!$entities) {
68
    return FALSE;
69
  }
70

    
71
  return ctools_context_create('entity:' . $entity_type, $entities[$entity_id]);
72
}