Projet

Général

Profil

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

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

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
  'default' => array(
20
    'entity_id' => ''
21
  ),
22
  'placeholder form' => 'ctools_argument_entity_id_ctools_argument_placeholder',
23
);
24

    
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];
28
}
29

    
30
function ctools_argument_entity_id_get_children($original_plugin, $parent) {
31
  $entities = entity_get_info();
32
  $plugins = array();
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;
42
  }
43
  drupal_alter('ctools_entity_contexts', $plugins);
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
  if (!is_numeric($arg)) {
64
    $preg_matches = array();
65
    $match        = preg_match('/\[id: (\d+)\]/', $arg, $preg_matches);
66
    if (!$match) {
67
      $match = preg_match('/^id: (\d+)/', $arg, $preg_matches);
68
    }
69

    
70
    if ($match) {
71
      $id = $preg_matches[1];
72
    }
73
    if (is_numeric($id)) {
74
      return ctools_context_create('entity:' . $entity_type, $id);
75
    }
76
    return FALSE;
77
  }
78

    
79
  $entity = entity_load($entity_type, array($arg));
80
  if (!$entity) {
81
    return FALSE;
82
  }
83

    
84
  return ctools_context_create('entity:' . $entity_type, $entity[$arg]);
85
}
86

    
87
function ctools_argument_entity_id_settings_form(&$form, &$form_state, $conf) {
88
  $plugin = &$form_state['plugin'];
89

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

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

    
120
  $form['settings']['entity_id'] = array(
121
    '#type' => 'value',
122
    '#value' => $conf['entity_id'],
123
  );
124

    
125
  $form['settings']['entity_type'] = array(
126
    '#type' => 'value',
127
    '#value' => $plugin['keyword'],
128
  );
129

    
130
  return $form;
131
}
132

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