Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / plugins / views_plugin_display_page.inc @ 5d12d676

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_plugin_display_page.
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
  /**
16
   * The page display has a path.
17
   */
18
  public function has_path() {
19
    return TRUE;
20
  }
21

    
22
  /**
23
   * {@inheritdoc}
24
   */
25
  public function uses_breadcrumb() {
26
    return TRUE;
27
  }
28

    
29
  /**
30
   * {@inheritdoc}
31
   */
32
  public function option_definition() {
33
    $options = parent::option_definition();
34

    
35
    $options['path'] = array('default' => '');
36
    $options['menu'] = 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' => variable_get('menu_default_node_menu', 'navigation')),
44
        'context' => array('default' => ''),
45
        'context_only_inline' => array('default' => FALSE),
46
       ),
47
    );
48
    $options['tab_options'] = array(
49
      'contains' => array(
50
        'type' => array('default' => 'none'),
51
        // Do not translate menu and title as menu system will.
52
        'title' => array('default' => '', 'translatable' => FALSE),
53
        'description' => array('default' => '', 'translatable' => FALSE),
54
        'weight' => array('default' => 0),
55
        'name' => array('default' => 'navigation'),
56
       ),
57
    );
58

    
59
    return $options;
60
  }
61

    
62
  /**
63
   * Add this display's path information to Drupal's menu system.
64
   */
65
  public function execute_hook_menu($callbacks) {
66
    $items = array();
67
    // Replace % with the link to our standard views argument loader
68
    // views_arg_load -- which lives in views.module
69

    
70
    $bits = explode('/', $this->get_option('path'));
71
    $page_arguments = array($this->view->name, $this->display->id);
72
    $this->view->init_handlers();
73
    $view_arguments = $this->view->argument;
74

    
75
    // Replace % with %views_arg for menu autoloading and add to the
76
    // page arguments so the argument actually comes through.
77
    foreach ($bits as $pos => $bit) {
78
      if ($bit == '%') {
79
        $argument = array_shift($view_arguments);
80
        if (!empty($argument->options['specify_validation']) && $argument->options['validate']['type'] != 'none') {
81
          $bits[$pos] = '%views_arg';
82
        }
83
        $page_arguments[] = $pos;
84
      }
85
    }
86

    
87
    $path = implode('/', $bits);
88

    
89
    $access_plugin = $this->get_plugin('access');
90
    if (!isset($access_plugin)) {
91
      $access_plugin = views_get_plugin('access', 'none');
92
    }
93

    
94
    // Get access callback might return an array of the callback + the dynamic
95
    // arguments.
96
    $access_plugin_callback = $access_plugin->get_access_callback();
97

    
98
    if (is_array($access_plugin_callback)) {
99
      $access_arguments = array();
100

    
101
      // Find the plugin arguments.
102
      $access_plugin_method = array_shift($access_plugin_callback);
103
      $access_plugin_arguments = array_shift($access_plugin_callback);
104
      if (!is_array($access_plugin_arguments)) {
105
        $access_plugin_arguments = array();
106
      }
107

    
108
      $access_arguments[0] = array($access_plugin_method, &$access_plugin_arguments);
109

    
110
      // Move the plugin arguments to the access arguments array.
111
      $i = 1;
112
      foreach ($access_plugin_arguments as $key => $value) {
113
        if (is_int($value)) {
114
          $access_arguments[$i] = $value;
115
          $access_plugin_arguments[$key] = $i;
116
          $i++;
117
        }
118
      }
119
    }
120
    else {
121
      $access_arguments = array($access_plugin_callback);
122
    }
123

    
124
    if ($path) {
125
      $items[$path] = array(
126
        // default views page entry
127
        'page callback' => 'views_page',
128
        'page arguments' => $page_arguments,
129
        // Default access check (per display)
130
        'access callback' => 'views_access',
131
        'access arguments' => $access_arguments,
132
        // Identify URL embedded arguments and correlate them to a handler
133
        'load arguments'  => array($this->view->name, $this->display->id, '%index'),
134
        // Make sure the menu router knows where views_page is.
135
        'module' => 'views',
136
      );
137
      $menu = $this->get_option('menu');
138
      if (empty($menu)) {
139
        $menu = array('type' => 'none');
140
      }
141
      // Set the title and description if we have one.
142
      if ($menu['type'] != 'none') {
143
        $items[$path]['title'] = $menu['title'];
144
        $items[$path]['description'] = $menu['description'];
145
      }
146

    
147
      if (isset($menu['weight'])) {
148
        $items[$path]['weight'] = intval($menu['weight']);
149
      }
150

    
151
      switch ($menu['type']) {
152
        case 'none':
153
        default:
154
          $items[$path]['type'] = MENU_CALLBACK;
155
          break;
156

    
157
        case 'normal':
158
          $items[$path]['type'] = MENU_NORMAL_ITEM;
159
          // Insert item into the proper menu
160
          $items[$path]['menu_name'] = $menu['name'];
161
          break;
162

    
163
        case 'tab':
164
          $items[$path]['type'] = MENU_LOCAL_TASK;
165
          break;
166

    
167
        case 'default tab':
168
          $items[$path]['type'] = MENU_DEFAULT_LOCAL_TASK;
169
          break;
170
      }
171

    
172
      // Add context for contextual links.
173
      // @see menu_contextual_links()
174
      if (!empty($menu['context'])) {
175
        $items[$path]['context'] = !empty($menu['context_only_inline']) ? MENU_CONTEXT_INLINE : (MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE);
176
      }
177

    
178
      // If this is a 'default' tab, check to see if we have to create teh
179
      // parent menu item.
180
      if ($menu['type'] == 'default tab') {
181
        $tab_options = $this->get_option('tab_options');
182
        if (!empty($tab_options['type']) && $tab_options['type'] != 'none') {
183
          $bits = explode('/', $path);
184
          // Remove the last piece.
185
          $bit = array_pop($bits);
186

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

    
212
              case 'tab':
213
                $items[$default_path]['type'] = MENU_LOCAL_TASK;
214
                break;
215
            }
216
            if (isset($tab_options['weight'])) {
217
              $items[$default_path]['weight'] = intval($tab_options['weight']);
218
            }
219
          }
220
        }
221
      }
222
    }
223

    
224
    return $items;
225
  }
226

    
227
  /**
228
   * The display page handler returns a normal view, but it also does
229
   * a drupal_set_title for the page, and does a views_set_page_view
230
   * on the view.
231
   */
232
  public function execute() {
233
    // Let the world know that this is the page view we're using.
234
    views_set_page_view($this->view);
235

    
236
    // Prior to this being called, the $view should already be set to this
237
    // display, and arguments should be set on the view.
238
    $this->view->build();
239
    if (!empty($this->view->build_info['fail'])) {
240
      return MENU_NOT_FOUND;
241
    }
242

    
243
    if (!empty($this->view->build_info['denied'])) {
244
      return MENU_ACCESS_DENIED;
245
    }
246

    
247
    $this->view->get_breadcrumb(TRUE);
248

    
249

    
250
    // And now render the view.
251
    $render = $this->view->render();
252

    
253
    // First execute the view so it's possible to get tokens for the title.
254
    // And the title, which is much easier.
255
    $title = $this->view->get_title();
256
    // Support the core method of using '<none>' to indicate nothing should be
257
    // assigned to the title, so only process the title value if it is not that
258
    // value.
259
    if ($title != '<none>') {
260
      drupal_set_title(filter_xss_admin($title), PASS_THROUGH);
261
    }
262
    return $render;
263
  }
264

    
265
  /**
266
   * Provide the summary for page options in the views UI.
267
   *
268
   * This output is returned as an array.
269
   */
270
  public function options_summary(&$categories, &$options) {
271
    // It is very important to call the parent function here.
272
    parent::options_summary($categories, $options);
273

    
274
    $categories['page'] = array(
275
      'title' => t('Page settings'),
276
      'column' => 'second',
277
      'build' => array(
278
        '#weight' => -10,
279
      ),
280
    );
281

    
282
    $path = strip_tags($this->get_option('path'));
283
    if (empty($path)) {
284
      $path = t('No path is set');
285
    }
286
    else {
287
      $path = '/' . $path;
288
    }
289

    
290
    $options['path'] = array(
291
      'category' => 'page',
292
      'title' => t('Path'),
293
      'value' => $path,
294
    );
295

    
296
    $menu = $this->get_option('menu');
297
    if (!is_array($menu)) {
298
      $menu = array('type' => 'none');
299
    }
300
    switch ($menu['type']) {
301
      case 'none':
302
      default:
303
        $menu_str = t('No menu');
304
        break;
305

    
306
      case 'normal':
307
        $menu_str = t('Normal: @title', array('@title' => $menu['title']));
308
        break;
309

    
310
      case 'tab':
311
      case 'default tab':
312
        $menu_str = t('Tab: @title', array('@title' => $menu['title']));
313
        break;
314
    }
315

    
316
    $options['menu'] = array(
317
      'category' => 'page',
318
      'title' => t('Menu'),
319
      'value' => views_ui_truncate($menu_str, 24),
320
    );
321

    
322
    // This adds a 'Settings' link to the style_options setting if the style
323
    // has options.
324
    if ($menu['type'] == 'default tab') {
325
      $options['menu']['setting'] = t('Parent menu item');
326
      $options['menu']['links']['tab_options'] = t('Change settings for the parent menu');
327
    }
328
  }
329

    
330
  /**
331
   * Provide the default form for setting options.
332
   */
333
  public function options_form(&$form, &$form_state) {
334
    // It is very important to call the parent function here.
335
    parent::options_form($form, $form_state);
336

    
337
    switch ($form_state['section']) {
338
      case 'path':
339
        $form['#title'] .= t('The menu path or URL of this view');
340
        $form['#help_topic'] = 'path';
341
        $form['path'] = array(
342
          '#type' => 'textfield',
343
          '#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".'),
344
          '#default_value' => $this->get_option('path'),
345
          '#field_prefix' => '<span dir="ltr">' . url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
346
          '#field_suffix' => '</span>&lrm;',
347
          '#attributes' => array('dir'=>'ltr'),
348
        );
349
        break;
350

    
351
      case 'menu':
352
        $form['#title'] .= t('Menu item entry');
353
        $form['#help_topic'] = 'menu';
354
        $form['menu'] = array(
355
          '#prefix' => '<div class="clearfix">',
356
          '#suffix' => '</div>',
357
          '#tree' => TRUE,
358
        );
359
        $menu = $this->get_option('menu');
360
        if (empty($menu)) {
361
          $menu = array('type' => 'none', 'title' => '', 'weight' => 0);
362
        }
363
        $form['menu']['type'] = array(
364
          '#prefix' => '<div class="views-left-30">',
365
          '#suffix' => '</div>',
366
          '#title' => t('Type'),
367
          '#type' => 'radios',
368
          '#options' => array(
369
            'none' => t('No menu entry'),
370
            'normal' => t('Normal menu entry'),
371
            'tab' => t('Menu tab'),
372
            'default tab' => t('Default menu tab')
373
          ),
374
          '#default_value' => $menu['type'],
375
        );
376
        $form['menu']['title'] = array(
377
          '#prefix' => '<div class="views-left-50">',
378
          '#title' => t('Title'),
379
          '#type' => 'textfield',
380
          '#default_value' => $menu['title'],
381
          '#description' => t('If set to normal or tab, enter the text to use for the menu item.'),
382
          '#dependency' => array('radio:menu[type]' => array('normal', 'tab', 'default tab')),
383
        );
384
        $form['menu']['description'] = array(
385
          '#title' => t('Description'),
386
          '#type' => 'textfield',
387
          '#default_value' => $menu['description'],
388
          '#description' => t("If set to normal or tab, enter the text to use for the menu item's description."),
389
          '#dependency' => array('radio:menu[type]' => array('normal', 'tab', 'default tab')),
390
        );
391

    
392
        // Only display the menu selector if menu module is enabled.
393
        if (module_exists('menu')) {
394
          $form['menu']['name'] = array(
395
            '#title' => t('Menu'),
396
            '#type' => 'select',
397
            '#options' => menu_get_menus(),
398
            '#default_value' => $menu['name'],
399
            '#description' => t('Insert item into an available menu.'),
400
            '#dependency' => array('radio:menu[type]' => array('normal', 'tab')),
401
          );
402
        }
403
        else {
404
          $form['menu']['name'] = array(
405
            '#type' => 'value',
406
            '#value' => $menu['name'],
407
          );
408
          $form['menu']['markup'] = array(
409
            '#markup' => t('Menu selection requires the activation of menu module.'),
410
          );
411
        }
412
        $form['menu']['weight'] = array(
413
          '#title' => t('Weight'),
414
          '#type' => 'textfield',
415
          '#default_value' => isset($menu['weight']) ? $menu['weight'] : 0,
416
          '#description' => t('The lower the weight the higher/further left it will appear.'),
417
          '#dependency' => array('radio:menu[type]' => array('normal', 'tab', 'default tab')),
418
        );
419
        $form['menu']['context'] = array(
420
          '#title' => t('Context'),
421
          '#type' => 'checkbox',
422
          '#default_value' => !empty($menu['context']),
423
          '#description' => t('Displays the link in contextual links'),
424
          '#dependency' => array('radio:menu[type]' => array('tab')),
425
        );
426
        $form['menu']['context_only_inline'] = array(
427
          '#title' => t('Hide menu tab'),
428
          '#suffix' => '</div>',
429
          '#type' => 'checkbox',
430
          '#default_value' => !empty($menu['context_only_inline']),
431
          '#description' => t('Only display menu item entry in contextual links. Menu tab should not be displayed.'),
432
          '#dependency' => array(
433
            'radio:menu[type]' => array('tab'),
434
            'edit-menu-context' => array(1),
435
          ),
436
          '#dependency_count' => 2,
437
        );
438
        break;
439

    
440
      case 'tab_options':
441
        $form['#title'] .= t('Default tab options');
442
        $tab_options = $this->get_option('tab_options');
443
        if (empty($tab_options)) {
444
          $tab_options = array('type' => 'none', 'title' => '', 'weight' => 0);
445
        }
446

    
447
        $form['tab_markup'] = array(
448
          '#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>',
449
        );
450

    
451
        $form['tab_options'] = array(
452
          '#prefix' => '<div class="clearfix">',
453
          '#suffix' => '</div>',
454
          '#tree' => TRUE,
455
        );
456
        $form['tab_options']['type'] = array(
457
          '#prefix' => '<div class="views-left-25">',
458
          '#suffix' => '</div>',
459
          '#title' => t('Parent menu item'),
460
          '#type' => 'radios',
461
          '#options' => array('none' => t('Already exists'), 'normal' => t('Normal menu item'), 'tab' => t('Menu tab')),
462
          '#default_value' => $tab_options['type'],
463
        );
464
        $form['tab_options']['title'] = array(
465
          '#prefix' => '<div class="views-left-75">',
466
          '#title' => t('Title'),
467
          '#type' => 'textfield',
468
          '#default_value' => $tab_options['title'],
469
          '#description' => t('If creating a parent menu item, enter the title of the item.'),
470
          '#dependency' => array('radio:tab_options[type]' => array('normal', 'tab')),
471
        );
472
        $form['tab_options']['description'] = array(
473
          '#title' => t('Description'),
474
          '#type' => 'textfield',
475
          '#default_value' => $tab_options['description'],
476
          '#description' => t('If creating a parent menu item, enter the description of the item.'),
477
          '#dependency' => array('radio:tab_options[type]' => array('normal', 'tab')),
478
        );
479
        // Only display the menu selector if menu module is enabled.
480
        if (module_exists('menu')) {
481
          $form['tab_options']['name'] = array(
482
            '#title' => t('Menu'),
483
            '#type' => 'select',
484
            '#options' => menu_get_menus(),
485
            '#default_value' => $tab_options['name'],
486
            '#description' => t('Insert item into an available menu.'),
487
            '#dependency' => array('radio:tab_options[type]' => array('normal')),
488
          );
489
        }
490
        else {
491
          $form['tab_options']['name'] = array(
492
            '#type' => 'value',
493
            '#value' => $tab_options['name'],
494
          );
495
          $form['tab_options']['markup'] = array(
496
            '#markup' => t('Menu selection requires the activation of menu module.'),
497
          );
498
        }
499
        $form['tab_options']['weight'] = array(
500
          '#suffix' => '</div>',
501
          '#title' => t('Tab weight'),
502
          '#type' => 'textfield',
503
          '#default_value' => $tab_options['weight'],
504
          '#size' => 5,
505
          '#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.'),
506
          '#dependency' => array('radio:tab_options[type]' => array('tab')),
507
        );
508
        break;
509
    }
510
  }
511

    
512
  /**
513
   * {@inheritdoc}
514
   */
515
  public function options_validate(&$form, &$form_state) {
516
    // It is very important to call the parent function here.
517
    parent::options_validate($form, $form_state);
518
    switch ($form_state['section']) {
519
      case 'path':
520
        if (strpos($form_state['values']['path'], '$arg') !== FALSE) {
521
          form_error($form['path'], t('"$arg" is no longer supported. Use % instead.'));
522
        }
523

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

    
528
        // Automatically remove '/' and trailing whitespace from path.
529
        $form_state['values']['path'] = trim($form_state['values']['path'], '/ ');
530
        break;
531

    
532
      case 'menu':
533
        $path = $this->get_option('path');
534
        if ($form_state['values']['menu']['type'] == 'normal' && strpos($path, '%') !== FALSE) {
535
          form_error($form['menu']['type'], t('Views cannot create normal menu items for paths with a % in them.'));
536
        }
537

    
538
        if ($form_state['values']['menu']['type'] == 'default tab' || $form_state['values']['menu']['type'] == 'tab') {
539
          $bits = explode('/', $path);
540
          $last = array_pop($bits);
541
          if ($last == '%') {
542
            form_error($form['menu']['type'], t('A display whose path ends with a % cannot be a tab.'));
543
          }
544
        }
545

    
546
        if ($form_state['values']['menu']['type'] != 'none' && empty($form_state['values']['menu']['title'])) {
547
          form_error($form['menu']['title'], t('Title is required for this menu type.'));
548
        }
549
        break;
550
    }
551
  }
552

    
553
  /**
554
   * {@inheritdoc}
555
   */
556
  public function options_submit(&$form, &$form_state) {
557
    // It is very important to call the parent function here.
558
    parent::options_submit($form, $form_state);
559
    switch ($form_state['section']) {
560
      case 'path':
561
        $this->set_option('path', $form_state['values']['path']);
562
        break;
563

    
564
      case 'menu':
565
        $this->set_option('menu', $form_state['values']['menu']);
566
        // Send ajax form to options page if we use it.
567
        if ($form_state['values']['menu']['type'] == 'default tab') {
568
          views_ui_add_form_to_stack('display', $this->view, $this->display->id, array('tab_options'));
569
        }
570
        break;
571

    
572
      case 'tab_options':
573
        $this->set_option('tab_options', $form_state['values']['tab_options']);
574
        break;
575
    }
576
  }
577

    
578
  /**
579
   * {@inheritdoc}
580
   */
581
  public function validate() {
582
    $errors = parent::validate();
583

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

    
589
    if ($menu['type'] == 'default tab') {
590
      $tab_options = $this->get_option('tab_options');
591
      if (!empty($tab_options['type']) && $tab_options['type'] != 'none' && empty($tab_options['title'])) {
592
        $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));
593
      }
594
    }
595

    
596
    return $errors;
597
  }
598

    
599
  /**
600
   * {@inheritdoc}
601
   */
602
  public function get_argument_text() {
603
    return array(
604
      'filter value not present' => t('When the filter value is <em>NOT</em> in the URL'),
605
      'filter value present' => t('When the filter value <em>IS</em> in the URL or a default is provided'),
606
      'description' => t('The contextual filter values is provided by the URL.'),
607
    );
608
  }
609

    
610
  /**
611
   * {@inheritdoc}
612
   */
613
  public function get_pager_text() {
614
    return array(
615
      'items per page title' => t('Items per page'),
616
      'items per page description' => t('The number of items to display per page. Enter 0 for no limit.'),
617
    );
618
  }
619

    
620
}