Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / plugins / views_plugin_display_page.inc @ 651307cd

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains the page display plugin.
6
 */
7

    
8
/**
9
 * The plugin that handles a full page.
10
 *
11
 * @ingroup views_display_plugins
12
 */
13
class views_plugin_display_page extends views_plugin_display {
14
  /**
15
   * The page display has a path.
16
   */
17
  function has_path() { return TRUE; }
18
  function uses_breadcrumb() { return TRUE; }
19

    
20
  function option_definition() {
21
    $options = parent::option_definition();
22

    
23
    $options['path'] = array('default' => '');
24
    $options['menu'] = array(
25
      'contains' => array(
26
        'type' => array('default' => 'none'),
27
        // Do not translate menu and title as menu system will.
28
        'title' => array('default' => '', 'translatable' => FALSE),
29
        'description' => array('default' => '', 'translatable' => FALSE),
30
        'weight' => array('default' => 0),
31
        'name' => array('default' => variable_get('menu_default_node_menu', 'navigation')),
32
        'context' => array('default' => ''),
33
        'context_only_inline' => array('default' => FALSE),
34
       ),
35
    );
36
    $options['tab_options'] = array(
37
      'contains' => array(
38
        'type' => array('default' => 'none'),
39
        // Do not translate menu and title as menu system will.
40
        'title' => array('default' => '', 'translatable' => FALSE),
41
        'description' => array('default' => '', 'translatable' => FALSE),
42
        'weight' => array('default' => 0),
43
        'name' => array('default' => 'navigation'),
44
       ),
45
    );
46

    
47
    return $options;
48
  }
49

    
50
  /**
51
   * Add this display's path information to Drupal's menu system.
52
   */
53
  function execute_hook_menu($callbacks) {
54
    $items = array();
55
    // Replace % with the link to our standard views argument loader
56
    // views_arg_load -- which lives in views.module
57

    
58
    $bits = explode('/', $this->get_option('path'));
59
    $page_arguments = array($this->view->name, $this->display->id);
60
    $this->view->init_handlers();
61
    $view_arguments = $this->view->argument;
62

    
63
    // Replace % with %views_arg for menu autoloading and add to the
64
    // page arguments so the argument actually comes through.
65
    foreach ($bits as $pos => $bit) {
66
      if ($bit == '%') {
67
        $argument = array_shift($view_arguments);
68
        if (!empty($argument->options['specify_validation']) && $argument->options['validate']['type'] != 'none') {
69
          $bits[$pos] = '%views_arg';
70
        }
71
        $page_arguments[] = $pos;
72
      }
73
    }
74

    
75
    $path = implode('/', $bits);
76

    
77
    $access_plugin = $this->get_plugin('access');
78
    if (!isset($access_plugin)) {
79
      $access_plugin = views_get_plugin('access', 'none');
80
    }
81

    
82
    // Get access callback might return an array of the callback + the dynamic arguments.
83
    $access_plugin_callback = $access_plugin->get_access_callback();
84

    
85
    if (is_array($access_plugin_callback)) {
86
      $access_arguments = array();
87

    
88
      // Find the plugin arguments.
89
      $access_plugin_method = array_shift($access_plugin_callback);
90
      $access_plugin_arguments = array_shift($access_plugin_callback);
91
      if (!is_array($access_plugin_arguments)) {
92
        $access_plugin_arguments = array();
93
      }
94

    
95
      $access_arguments[0] = array($access_plugin_method, &$access_plugin_arguments);
96

    
97
      // Move the plugin arguments to the access arguments array.
98
      $i = 1;
99
      foreach ($access_plugin_arguments as $key => $value) {
100
        if (is_int($value)) {
101
          $access_arguments[$i] = $value;
102
          $access_plugin_arguments[$key] = $i;
103
          $i++;
104
        }
105
      }
106
    }
107
    else {
108
      $access_arguments = array($access_plugin_callback);
109
    }
110

    
111
    if ($path) {
112
      $items[$path] = array(
113
        // default views page entry
114
        'page callback' => 'views_page',
115
        'page arguments' => $page_arguments,
116
        // Default access check (per display)
117
        'access callback' => 'views_access',
118
        'access arguments' => $access_arguments,
119
        // Identify URL embedded arguments and correlate them to a handler
120
        'load arguments'  => array($this->view->name, $this->display->id, '%index'),
121
        // Make sure the menu router knows where views_page is.
122
        'module' => 'views',
123
      );
124
      $menu = $this->get_option('menu');
125
      if (empty($menu)) {
126
        $menu = array('type' => 'none');
127
      }
128
      // Set the title and description if we have one.
129
      if ($menu['type'] != 'none') {
130
        $items[$path]['title'] = $menu['title'];
131
        $items[$path]['description'] = $menu['description'];
132
      }
133

    
134
      if (isset($menu['weight'])) {
135
        $items[$path]['weight'] = intval($menu['weight']);
136
      }
137

    
138
      switch ($menu['type']) {
139
        case 'none':
140
        default:
141
          $items[$path]['type'] = MENU_CALLBACK;
142
          break;
143
        case 'normal':
144
          $items[$path]['type'] = MENU_NORMAL_ITEM;
145
          // Insert item into the proper menu
146
          $items[$path]['menu_name'] = $menu['name'];
147
          break;
148
        case 'tab':
149
          $items[$path]['type'] = MENU_LOCAL_TASK;
150
          break;
151
        case 'default tab':
152
          $items[$path]['type'] = MENU_DEFAULT_LOCAL_TASK;
153
          break;
154
      }
155

    
156
      // Add context for contextual links.
157
      // @see menu_contextual_links()
158
      if (!empty($menu['context'])) {
159
        $items[$path]['context'] = !empty($menu['context_only_inline']) ? MENU_CONTEXT_INLINE : (MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE);
160
      }
161

    
162
      // If this is a 'default' tab, check to see if we have to create teh
163
      // parent menu item.
164
      if ($menu['type'] == 'default tab') {
165
        $tab_options = $this->get_option('tab_options');
166
        if (!empty($tab_options['type']) && $tab_options['type'] != 'none') {
167
          $bits = explode('/', $path);
168
          // Remove the last piece.
169
          $bit = array_pop($bits);
170

    
171
          // we can't do this if they tried to make the last path bit variable.
172
          // @todo: We can validate this.
173
          if ($bit != '%views_arg' && !empty($bits)) {
174
            $default_path = implode('/', $bits);
175
            $items[$default_path] = array(
176
              // default views page entry
177
              'page callback' => 'views_page',
178
              'page arguments' => $page_arguments,
179
              // Default access check (per display)
180
              'access callback' => 'views_access',
181
              'access arguments' => $access_arguments,
182
              // Identify URL embedded arguments and correlate them to a handler
183
              'load arguments'  => array($this->view->name, $this->display->id, '%index'),
184
              'title' => $tab_options['title'],
185
              'description' => $tab_options['description'],
186
              'menu_name' => $tab_options['name'],
187
              // Make sure the menu router knows where views_page is.
188
              'module' => 'views',
189
            );
190
            switch ($tab_options['type']) {
191
              default:
192
              case 'normal':
193
                $items[$default_path]['type'] = MENU_NORMAL_ITEM;
194
                break;
195
              case 'tab':
196
                $items[$default_path]['type'] = MENU_LOCAL_TASK;
197
                break;
198
            }
199
            if (isset($tab_options['weight'])) {
200
              $items[$default_path]['weight'] = intval($tab_options['weight']);
201
            }
202
          }
203
        }
204
      }
205
    }
206

    
207
    return $items;
208
  }
209

    
210
  /**
211
   * The display page handler returns a normal view, but it also does
212
   * a drupal_set_title for the page, and does a views_set_page_view
213
   * on the view.
214
   */
215
  function execute() {
216
    // Let the world know that this is the page view we're using.
217
    views_set_page_view($this->view);
218

    
219
    // Prior to this being called, the $view should already be set to this
220
    // display, and arguments should be set on the view.
221
    $this->view->build();
222
    if (!empty($this->view->build_info['fail'])) {
223
      return MENU_NOT_FOUND;
224
    }
225

    
226
    if (!empty($this->view->build_info['denied'])) {
227
      return MENU_ACCESS_DENIED;
228
    }
229

    
230
    $this->view->get_breadcrumb(TRUE);
231

    
232

    
233
    // And now render the view.
234
    $render = $this->view->render();
235

    
236
    // First execute the view so it's possible to get tokens for the title.
237
    // And the title, which is much easier.
238
    $title = $this->view->get_title();
239
    // Support the core method of using '<none>' to indicate nothing should be
240
    // assigned to the title, so only process the title value if it is not that
241
    // value.
242
    if ($title != '<none>') {
243
      drupal_set_title(filter_xss_admin($title), PASS_THROUGH);
244
    }
245
    return $render;
246
  }
247

    
248
  /**
249
   * Provide the summary for page options in the views UI.
250
   *
251
   * This output is returned as an array.
252
   */
253
  function options_summary(&$categories, &$options) {
254
    // It is very important to call the parent function here:
255
    parent::options_summary($categories, $options);
256

    
257
    $categories['page'] = array(
258
      'title' => t('Page settings'),
259
      'column' => 'second',
260
      'build' => array(
261
        '#weight' => -10,
262
      ),
263
    );
264

    
265
    $path = strip_tags($this->get_option('path'));
266
    if (empty($path)) {
267
      $path = t('No path is set');
268
    }
269
    else {
270
      $path = '/' . $path;
271
    }
272

    
273
    $options['path'] = array(
274
      'category' => 'page',
275
      'title' => t('Path'),
276
      'value' => $path,
277
    );
278

    
279
    $menu = $this->get_option('menu');
280
    if (!is_array($menu)) {
281
      $menu = array('type' => 'none');
282
    }
283
    switch($menu['type']) {
284
      case 'none':
285
      default:
286
        $menu_str = t('No menu');
287
        break;
288
      case 'normal':
289
        $menu_str = t('Normal: @title', array('@title' => $menu['title']));
290
        break;
291
      case 'tab':
292
      case 'default tab':
293
        $menu_str = t('Tab: @title', array('@title' => $menu['title']));
294
        break;
295
    }
296

    
297
    $options['menu'] = array(
298
      'category' => 'page',
299
      'title' => t('Menu'),
300
      'value' => views_ui_truncate($menu_str, 24),
301
    );
302

    
303
    // This adds a 'Settings' link to the style_options setting if the style has options.
304
    if ($menu['type'] == 'default tab') {
305
      $options['menu']['setting'] = t('Parent menu item');
306
      $options['menu']['links']['tab_options'] = t('Change settings for the parent menu');
307
    }
308
  }
309

    
310
  /**
311
   * Provide the default form for setting options.
312
   */
313
  function options_form(&$form, &$form_state) {
314
    // It is very important to call the parent function here:
315
    parent::options_form($form, $form_state);
316

    
317
    switch ($form_state['section']) {
318
      case 'path':
319
        $form['#title'] .= t('The menu path or URL of this view');
320
        $form['#help_topic'] = 'path';
321
        $form['path'] = array(
322
          '#type' => 'textfield',
323
          '#description' => t('This view will be displayed by visiting this path on your site. You may use "%" in your URL to represent values that will be used for contextual filters: For example, "node/%/feed".'),
324
          '#default_value' => $this->get_option('path'),
325
	  '#field_prefix' => '<span dir="ltr">' . url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
326
	  '#field_suffix' => '</span>&lrm;',
327
	  '#attributes' => array('dir'=>'ltr'),
328
        );
329
        break;
330
      case 'menu':
331
        $form['#title'] .= t('Menu item entry');
332
        $form['#help_topic'] = 'menu';
333
        $form['menu'] = array(
334
          '#prefix' => '<div class="clearfix">',
335
          '#suffix' => '</div>',
336
          '#tree' => TRUE,
337
        );
338
        $menu = $this->get_option('menu');
339
        if (empty($menu)) {
340
          $menu = array('type' => 'none', 'title' => '', 'weight' => 0);
341
        }
342
        $form['menu']['type'] = array(
343
          '#prefix' => '<div class="views-left-30">',
344
          '#suffix' => '</div>',
345
          '#title' => t('Type'),
346
          '#type' => 'radios',
347
          '#options' => array(
348
            'none' => t('No menu entry'),
349
            'normal' => t('Normal menu entry'),
350
            'tab' => t('Menu tab'),
351
            'default tab' => t('Default menu tab')
352
          ),
353
          '#default_value' => $menu['type'],
354
        );
355
        $form['menu']['title'] = array(
356
          '#prefix' => '<div class="views-left-50">',
357
          '#title' => t('Title'),
358
          '#type' => 'textfield',
359
          '#default_value' => $menu['title'],
360
          '#description' => t('If set to normal or tab, enter the text to use for the menu item.'),
361
          '#dependency' => array('radio:menu[type]' => array('normal', 'tab', 'default tab')),
362
        );
363
        $form['menu']['description'] = array(
364
          '#title' => t('Description'),
365
          '#type' => 'textfield',
366
          '#default_value' => $menu['description'],
367
          '#description' => t("If set to normal or tab, enter the text to use for the menu item's description."),
368
          '#dependency' => array('radio:menu[type]' => array('normal', 'tab', 'default tab')),
369
        );
370

    
371
        // Only display the menu selector if menu module is enabled.
372
        if (module_exists('menu')) {
373
          $form['menu']['name'] = array(
374
            '#title' => t('Menu'),
375
            '#type' => 'select',
376
            '#options' => menu_get_menus(),
377
            '#default_value' => $menu['name'],
378
            '#description' => t('Insert item into an available menu.'),
379
            '#dependency' => array('radio:menu[type]' => array('normal', 'tab')),
380
          );
381
        }
382
        else {
383
          $form['menu']['name'] = array(
384
            '#type' => 'value',
385
            '#value' => $menu['name'],
386
          );
387
          $form['menu']['markup'] = array(
388
            '#markup' => t('Menu selection requires the activation of menu module.'),
389
          );
390
        }
391
        $form['menu']['weight'] = array(
392
          '#title' => t('Weight'),
393
          '#type' => 'textfield',
394
          '#default_value' => isset($menu['weight']) ? $menu['weight'] : 0,
395
          '#description' => t('The lower the weight the higher/further left it will appear.'),
396
          '#dependency' => array('radio:menu[type]' => array('normal', 'tab', 'default tab')),
397
        );
398
        $form['menu']['context'] = array(
399
          '#title' => t('Context'),
400
          '#type' => 'checkbox',
401
          '#default_value' => !empty($menu['context']),
402
          '#description' => t('Displays the link in contextual links'),
403
          '#dependency' => array('radio:menu[type]' => array('tab')),
404
        );
405
        $form['menu']['context_only_inline'] = array(
406
          '#title' => t('Hide menu tab'),
407
          '#suffix' => '</div>',
408
          '#type' => 'checkbox',
409
          '#default_value' => !empty($menu['context_only_inline']),
410
          '#description' => t('Only display menu item entry in contextual links. Menu tab should not be displayed.'),
411
          '#dependency' => array(
412
            'radio:menu[type]' => array('tab'),
413
            'edit-menu-context' => array(1),
414
          ),
415
          '#dependency_count' => 2,
416
        );
417
        break;
418
      case 'tab_options':
419
        $form['#title'] .= t('Default tab options');
420
        $tab_options = $this->get_option('tab_options');
421
        if (empty($tab_options)) {
422
          $tab_options = array('type' => 'none', 'title' => '', 'weight' => 0);
423
        }
424

    
425
        $form['tab_markup'] = array(
426
          '#markup' => '<div class="form-item description">' . t('When providing a menu item as a tab, Drupal needs to know what the parent menu item of that tab will be. Sometimes the parent will already exist, but other times you will need to have one created. The path of a parent item will always be the same path with the last part left off. i.e, if the path to this view is <em>foo/bar/baz</em>, the parent path would be <em>foo/bar</em>.') . '</div>',
427
        );
428

    
429
        $form['tab_options'] = array(
430
          '#prefix' => '<div class="clearfix">',
431
          '#suffix' => '</div>',
432
          '#tree' => TRUE,
433
        );
434
        $form['tab_options']['type'] = array(
435
          '#prefix' => '<div class="views-left-25">',
436
          '#suffix' => '</div>',
437
          '#title' => t('Parent menu item'),
438
          '#type' => 'radios',
439
          '#options' => array('none' => t('Already exists'), 'normal' => t('Normal menu item'), 'tab' => t('Menu tab')),
440
          '#default_value' => $tab_options['type'],
441
        );
442
        $form['tab_options']['title'] = array(
443
          '#prefix' => '<div class="views-left-75">',
444
          '#title' => t('Title'),
445
          '#type' => 'textfield',
446
          '#default_value' => $tab_options['title'],
447
          '#description' => t('If creating a parent menu item, enter the title of the item.'),
448
          '#dependency' => array('radio:tab_options[type]' => array('normal', 'tab')),
449
        );
450
        $form['tab_options']['description'] = array(
451
          '#title' => t('Description'),
452
          '#type' => 'textfield',
453
          '#default_value' => $tab_options['description'],
454
          '#description' => t('If creating a parent menu item, enter the description of the item.'),
455
          '#dependency' => array('radio:tab_options[type]' => array('normal', 'tab')),
456
        );
457
        // Only display the menu selector if menu module is enabled.
458
        if (module_exists('menu')) {
459
          $form['tab_options']['name'] = array(
460
            '#title' => t('Menu'),
461
            '#type' => 'select',
462
            '#options' => menu_get_menus(),
463
            '#default_value' => $tab_options['name'],
464
            '#description' => t('Insert item into an available menu.'),
465
            '#dependency' => array('radio:tab_options[type]' => array('normal')),
466
          );
467
        }
468
        else {
469
          $form['tab_options']['name'] = array(
470
            '#type' => 'value',
471
            '#value' => $tab_options['name'],
472
          );
473
          $form['tab_options']['markup'] = array(
474
            '#markup' => t('Menu selection requires the activation of menu module.'),
475
          );
476
        }
477
        $form['tab_options']['weight'] = array(
478
          '#suffix' => '</div>',
479
          '#title' => t('Tab weight'),
480
          '#type' => 'textfield',
481
          '#default_value' => $tab_options['weight'],
482
          '#size' => 5,
483
          '#description' => t('If the parent menu item is a tab, enter the weight of the tab. The lower the number, the more to the left it will be.'),
484
          '#dependency' => array('radio:tab_options[type]' => array('tab')),
485
        );
486
        break;
487
    }
488
  }
489

    
490
  function options_validate(&$form, &$form_state) {
491
    // It is very important to call the parent function here:
492
    parent::options_validate($form, $form_state);
493
    switch ($form_state['section']) {
494
      case 'path':
495
        if (strpos($form_state['values']['path'], '$arg') !== FALSE) {
496
          form_error($form['path'], t('"$arg" is no longer supported. Use % instead.'));
497
        }
498

    
499
        if (strpos($form_state['values']['path'], '%') === 0) {
500
          form_error($form['path'], t('"%" may not be used for the first segment of a path.'));
501
        }
502

    
503
        // automatically remove '/' and trailing whitespace from path.
504
        $form_state['values']['path'] = trim($form_state['values']['path'], '/ ');
505
        break;
506
      case 'menu':
507
        $path = $this->get_option('path');
508
        if ($form_state['values']['menu']['type'] == 'normal' && strpos($path, '%') !== FALSE) {
509
          form_error($form['menu']['type'], t('Views cannot create normal menu items for paths with a % in them.'));
510
        }
511

    
512
        if ($form_state['values']['menu']['type'] == 'default tab' || $form_state['values']['menu']['type'] == 'tab') {
513
          $bits = explode('/', $path);
514
          $last = array_pop($bits);
515
          if ($last == '%') {
516
            form_error($form['menu']['type'], t('A display whose path ends with a % cannot be a tab.'));
517
          }
518
        }
519

    
520
        if ($form_state['values']['menu']['type'] != 'none' && empty($form_state['values']['menu']['title'])) {
521
          form_error($form['menu']['title'], t('Title is required for this menu type.'));
522
        }
523
        break;
524
    }
525
  }
526

    
527
  function options_submit(&$form, &$form_state) {
528
    // It is very important to call the parent function here:
529
    parent::options_submit($form, $form_state);
530
    switch ($form_state['section']) {
531
      case 'path':
532
        $this->set_option('path', $form_state['values']['path']);
533
        break;
534
      case 'menu':
535
        $this->set_option('menu', $form_state['values']['menu']);
536
        // send ajax form to options page if we use it.
537
        if ($form_state['values']['menu']['type'] == 'default tab') {
538
          views_ui_add_form_to_stack('display', $this->view, $this->display->id, array('tab_options'));
539
        }
540
        break;
541
      case 'tab_options':
542
        $this->set_option('tab_options', $form_state['values']['tab_options']);
543
        break;
544
    }
545
  }
546

    
547
  function validate() {
548
    $errors = parent::validate();
549

    
550
    $menu = $this->get_option('menu');
551
    if (!empty($menu['type']) && $menu['type'] != 'none' && empty($menu['title'])) {
552
      $errors[] = t('Display @display is set to use a menu but the menu link text is not set.', array('@display' => $this->display->display_title));
553
    }
554

    
555
    if ($menu['type'] == 'default tab') {
556
      $tab_options = $this->get_option('tab_options');
557
      if (!empty($tab_options['type']) && $tab_options['type'] != 'none' && empty($tab_options['title'])) {
558
        $errors[] = t('Display @display is set to use a parent menu but the parent menu link text is not set.', array('@display' => $this->display->display_title));
559
      }
560
    }
561

    
562
    return $errors;
563
  }
564

    
565
  function get_argument_text() {
566
    return array(
567
      'filter value not present' => t('When the filter value is <em>NOT</em> in the URL'),
568
      'filter value present' => t('When the filter value <em>IS</em> in the URL or a default is provided'),
569
      'description' => t('The contextual filter values is provided by the URL.'),
570
    );
571
  }
572

    
573
  function get_pager_text() {
574
    return array(
575
      'items per page title' => t('Items per page'),
576
      'items per page description' => t('The number of items to display per page. Enter 0 for no limit.')
577
    );
578
  }
579
}