Projet

Général

Profil

Paste
Télécharger (5,14 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / plugins / arguments / entity_id.inc @ c304a780

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: ID"),
14
  'description' => t('Creates an entity context from an entity ID argument.'),
15
  'context' => 'ctools_argument_entity_id_context',
16
  'get child' => 'ctools_argument_entity_id_get_child',
17
  'get children' => 'ctools_argument_entity_id_get_children',
18
  'default' => array(
19
    'entity_id' => '',
20
  ),
21
  'placeholder form' => 'ctools_argument_entity_id_ctools_argument_placeholder',
22
);
23

    
24
function ctools_argument_entity_id_get_child($plugin, $parent, $child) {
25
  $plugins = ctools_argument_entity_id_get_children($plugin, $parent);
26
  return $plugins[$parent . ':' . $child];
27
}
28

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

    
44
  return $plugins;
45
}
46

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

    
58
  // We can accept either an entity object or a pure id.
59
  if (is_object($arg)) {
60
    return ctools_context_create('entity:' . $entity_type, $arg);
61
  }
62

    
63
  // Trim spaces and other garbage.
64
  $arg = trim($arg);
65

    
66
  if (!is_numeric($arg)) {
67
    $preg_matches = array();
68
    $match = preg_match('/\[id: (\d+)\]/', $arg, $preg_matches);
69
    if (!$match) {
70
      $match = preg_match('/^id: (\d+)/', $arg, $preg_matches);
71
    }
72

    
73
    if ($match) {
74
      $id = $preg_matches[1];
75
    }
76
    if (isset($id) && is_numeric($id)) {
77
      return ctools_context_create('entity:' . $entity_type, $id);
78
    }
79
    return FALSE;
80
  }
81

    
82
  $entities = entity_load($entity_type, array($arg));
83
  if (empty($entities)) {
84
    return FALSE;
85
  }
86

    
87
  return ctools_context_create('entity:' . $entity_type, reset($entities));
88
}
89

    
90
function ctools_argument_entity_id_settings_form(&$form, &$form_state, $conf) {
91
  $plugin = &$form_state['plugin'];
92

    
93
  $form['settings']['entity'] = array(
94
    '#title' => t('Enter the title or ID of a @entity entity', array('@entity' => $plugin['keyword'])),
95
    '#type' => 'textfield',
96
    '#maxlength' => 512,
97
    '#autocomplete_path' => 'ctools/autocomplete/' . $plugin['keyword'],
98
    '#weight' => -10,
99
  );
100

    
101
  if (!empty($conf['entity_id'])) {
102
    $info = entity_load($plugin['keyword'], array($conf['entity_id']));
103
    $info = $info[$conf['entity_id']];
104
    if ($info) {
105
      $entity = entity_get_info($plugin['keyword']);
106
      $uri = entity_uri($plugin['keyword'], $info);
107
      if (is_array($uri) && $entity['entity keys']['label']) {
108
        $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));
109
      }
110
      elseif (is_array($uri)) {
111
        $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));
112
      }
113
      elseif ($entity['entity keys']['label']) {
114
        $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));
115
      }
116
      else {
117
        $link = t("[%type id %id]", array('%type' => $plugin['keyword'], '%id' => $conf['entity_id']));
118
      }
119
      $form['settings']['entity']['#description'] = t('Currently set to !link', array('!link' => $link));
120
    }
121
  }
122

    
123
  $form['settings']['entity_id'] = array(
124
    '#type' => 'value',
125
    '#value' => isset($conf['entity_id']) ? $conf['entity_id'] : '',
126
  );
127

    
128
  $form['settings']['entity_type'] = array(
129
    '#type' => 'value',
130
    '#value' => $plugin['keyword'],
131
  );
132

    
133
  return $form;
134
}
135

    
136
function ctools_argument_entity_id_ctools_argument_placeholder($conf) {
137
  $conf = array(
138
    '#title' => t('Enter the title or ID of a @entity entity', array('@entity' => $conf['keyword'])),
139
    '#type' => 'textfield',
140
    '#maxlength' => 512,
141
    '#autocomplete_path' => 'ctools/autocomplete/' . $conf['keyword'],
142
    '#weight' => -10,
143
  );
144

    
145
  return $conf;
146
}