Projet

Général

Profil

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

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

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
/**
21
 * @todo document me properly
22
 */
23
function uuid_entity_uuid_get_child($plugin, $parent, $child) {
24
  $plugins = uuid_entity_uuid_get_children($plugin, $parent);
25
  return $plugins[$parent . ':' . $child];
26
}
27

    
28
/**
29
 * @todo document me properly
30
 */
31
function uuid_entity_uuid_get_children($original_plugin, $parent) {
32
  $entities = entity_get_info();
33
  $plugins = array();
34
  foreach ($entities as $entity_type => $entity) {
35
    $plugin = $original_plugin;
36
    $plugin['title'] = t('@entity: UUID', array('@entity' => $entity['label']));
37
    $plugin['keyword'] = $entity_type;
38
    $plugin['description'] = t('Creates @entity context from an UUID argument.', array('@entity' => $entity_type));
39
    $plugin['name'] = $parent . ':' . $entity_type;
40
    $plugin_id = $parent . ':' . $entity_type;
41
    drupal_alter('ctools_entity_context', $plugin, $entity, $plugin_id);
42
    $plugins[$plugin_id] = $plugin;
43
  }
44
  drupal_alter('ctools_entity_contexts', $plugins);
45
  return $plugins;
46
}
47

    
48
/**
49
 * Discover if this argument gives us the entity we crave.
50
 */
51
function uuid_entity_uuid_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.
55
  if ($empty) {
56
    return ctools_context_create_empty('entity:' . $entity_type);
57
  }
58

    
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);
62
  }
63

    
64
  if (!is_string($arg)) {
65
    return FALSE;
66
  }
67
  $entity_ids = entity_get_id_by_uuid($entity_type, array($arg));
68
  if (!isset($entity_ids[$arg])) {
69
    return FALSE;
70
  }
71
  $entity_id = $entity_ids[$arg];
72
  $entities = entity_load($entity_type, array($entity_id));
73
  if (!$entities) {
74
    return FALSE;
75
  }
76

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