Projet

Général

Profil

Paste
Télécharger (4,69 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / views_content / plugins / contexts / view.inc @ c304a780

1
<?php
2

    
3
/**
4
 * @file
5
 * Plugin to provide a node context. A node context is a node wrapped in a
6
 * context object that can be utilized by anything that accepts contexts.
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("View"),
15
  'description' => t('Loads a view result into a context that can then be displayed across a panel or turned into other contexts.'),
16
  'context' => 'views_content_context_view_create',
17

    
18
  'edit form' => 'views_content_context_view_settings_form',
19
  'edit form validate' => 'views_content_context_view_settings_form_validate',
20
  'edit form submit' => 'views_content_context_view_settings_form_submit',
21

    
22
  'defaults' => array('view' => ''),
23

    
24
  'keyword' => 'view',
25
  'context name' => 'view',
26

    
27
  'get child' => 'views_content_context_view_get_child',
28
  'get children' => 'views_content_context_view_get_children',
29
);
30

    
31
function views_content_context_view_get_child($plugin, $parent, $child) {
32
  list($name, $id) = explode('-', $child, 2);
33
  $view = views_get_view($name);
34
  if (!$view) {
35
    return;
36
  }
37

    
38
  $view->set_display($id);
39
  if ($view->current_display != $id) {
40
    return;
41
  }
42

    
43
  $info = _views_content_get_context_from_display($view, $id, $parent, FALSE);
44
  if ($info) {
45
    return $info;
46
  }
47
  return;
48
}
49

    
50
function views_content_context_view_get_children($plugin, $parent) {
51
  $types = array(
52
    'view' => $plugin,
53
  );
54

    
55
  // We're keeping the 'view' context around for legacy reasons but
56
  // we want to disable the UI so you can't add it that way anymore.
57
  $types['view']['no ui'] = TRUE;
58

    
59
  $views = views_get_applicable_views('returns context');
60
  foreach ($views as $data) {
61
    list($view, $id) = $data;
62
    $info = _views_content_get_context_from_display($view, $id, $parent, FALSE);
63
    if ($info) {
64
      $info['no required context ui'] = TRUE;
65
      $types[$info['name']] = $info;
66
    }
67
  }
68

    
69
  return $types;
70
}
71

    
72
function views_content_context_view_create($empty, $data = NULL, $conf = FALSE, $plugin = array()) {
73
  $context = new ctools_context('view');
74
  $context->plugin = 'view';
75

    
76
  if ($empty) {
77
    return $context;
78
  }
79

    
80
  if ($conf) {
81
    if (is_array($data) && !empty($data['view'])) {
82
      // This code is left in for backward compatibility. Will not be used
83
      // with child plugins.
84
      list($name, $display_id) = explode(':', $data['view'], 2);
85
      $data = views_get_view($name);
86
      if ($data) {
87
        $data->set_display($display_id);
88
      }
89
    }
90
    elseif (!empty($plugin['view name'])) {
91
      $data = views_get_view($plugin['view name']);
92
      $data->set_display($plugin['view display id']);
93
    }
94
  }
95

    
96
  if (is_object($data) && $data->current_display != 'default') {
97
    // We don't store the loaded view as we don't want the view object
98
    // cached. However, in order to extract it you can use:
99
    // @code
100
    // $output = views_content_context_get_output($context);
101
    // $view = $output['view'];
102
    // @endcode
103
    $context->data = array(
104
      'name' => $data->name,
105
      'display' => $data->current_display,
106
      'args' => $data->args,
107
    );
108

    
109
    // At runtime, this can get populated. Once it is populated this
110
    // object should not be cached.
111
    $context->view     = NULL;
112
    $context->title    = $data->get_title();
113
    $context->argument = $data->name . ':' . $data->current_display;
114

    
115
    $context->restrictions['base'] = array($data->base_table);
116

    
117
    return $context;
118
  }
119
}
120

    
121
function views_content_context_view_settings_form($form, &$form_state) {
122
  $conf = $form_state['conf'];
123
  $views = views_get_applicable_views('returns context');
124
  foreach ($views as $data) {
125
    list($view, $id) = $data;
126
    $title = views_content_get_display_title($view, $id, 'admin_title');
127
    $options[$view->name . ':' . $id] = $title;
128
  }
129

    
130
  if (!empty($options)) {
131
    natcasesort($options);
132
    $form['view'] = array(
133
      '#type' => 'select',
134
      '#options' => $options,
135
      '#title' => t('View'),
136
    );
137
  }
138
  else {
139
    $form['view'] = array(
140
      '#value' => '<p>' . t('There are currently no views with Context displays enabled. You should go to the view administration and add a Context display to use a view as a context.') . '</p>',
141
    );
142
  }
143

    
144
  return $form;
145
}
146

    
147
/**
148
 * Validate a node.
149
 */
150
function views_content_context_view_settings_form_validate($form, &$form_state) {
151
  if (empty($form_state['values']['view'])) {
152
    form_error($form['view'], t('You must select a view.'));
153
  }
154
}
155

    
156
/**
157
 * Provide a list of ways that this context can be converted to a string.
158
 */
159
function views_content_context_view_convert_list() {
160
  $list = array();
161

    
162
  return $list;
163
}
164

    
165
/**
166
 * Convert a context into a string.
167
 */
168
function views_content_context_view_convert($context, $type) {
169
  switch ($type) {
170
  }
171
}