Projet

Général

Profil

Paste
Télécharger (22,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / plugins / views_plugin_display_page.inc @ 4003efde

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 (!$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
        case 'local action':
171
          $items[$path]['type'] = MENU_LOCAL_ACTION;
172
          break;
173
      }
174

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

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

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

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

    
227
    return $items;
228
  }
229

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

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

    
246
    if (!empty($this->view->build_info['denied'])) {
247
      return MENU_ACCESS_DENIED;
248
    }
249

    
250
    $this->view->get_breadcrumb(TRUE);
251

    
252

    
253
    // And now render the view.
254
    $render = $this->view->render();
255

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

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

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

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

    
293
    $options['path'] = array(
294
      'category' => 'page',
295
      'title' => t('Path'),
296
      'value' => $path,
297
    );
298

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

    
309
      case 'normal':
310
        $menu_str = t('Normal: @title', array('@title' => $menu['title']));
311
        break;
312

    
313
      case 'tab':
314
      case 'default tab':
315
        $menu_str = t('Tab: @title', array('@title' => $menu['title']));
316
        break;
317
      case 'local action':
318
        $menu_str = t('Local action: @title', array('@title' => $menu['title']));
319
    }
320

    
321
    $options['menu'] = array(
322
      'category' => 'page',
323
      'title' => t('Menu'),
324
      'value' => views_ui_truncate($menu_str, 24),
325
    );
326

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

    
335
  /**
336
   * Provide the default form for setting options.
337
   */
338
  public function options_form(&$form, &$form_state) {
339
    // It is very important to call the parent function here.
340
    parent::options_form($form, $form_state);
341

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

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

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

    
448
      case 'tab_options':
449
        $form['#title'] .= t('Default tab options');
450
        $tab_options = $this->get_option('tab_options');
451
        if (empty($tab_options)) {
452
          $tab_options = array('type' => 'none', 'title' => '', 'weight' => 0);
453
        }
454

    
455
        $form['tab_markup'] = array(
456
          '#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>',
457
        );
458

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

    
520
  /**
521
   * {@inheritdoc}
522
   */
523
  public function options_validate(&$form, &$form_state) {
524
    // It is very important to call the parent function here.
525
    parent::options_validate($form, $form_state);
526
    switch ($form_state['section']) {
527
      case 'path':
528
        if (strpos($form_state['values']['path'], '$arg') !== FALSE) {
529
          form_error($form['path'], t('"$arg" is no longer supported. Use % instead.'));
530
        }
531

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

    
536
        // Automatically remove '/' and trailing whitespace from path.
537
        $form_state['values']['path'] = trim($form_state['values']['path'], '/ ');
538
        break;
539

    
540
      case 'menu':
541
        $path = $this->get_option('path');
542
        if ($form_state['values']['menu']['type'] == 'normal' && strpos($path, '%') !== FALSE) {
543
          form_error($form['menu']['type'], t('Views cannot create normal menu items for paths with a % in them.'));
544
        }
545

    
546
        if ($form_state['values']['menu']['type'] == 'default tab' || $form_state['values']['menu']['type'] == 'tab') {
547
          $bits = explode('/', $path);
548
          $last = array_pop($bits);
549
          if ($last == '%') {
550
            form_error($form['menu']['type'], t('A display whose path ends with a % cannot be a tab.'));
551
          }
552
        }
553

    
554
        if ($form_state['values']['menu']['type'] != 'none' && empty($form_state['values']['menu']['title'])) {
555
          form_error($form['menu']['title'], t('Title is required for this menu type.'));
556
        }
557
        break;
558
    }
559
  }
560

    
561
  /**
562
   * {@inheritdoc}
563
   */
564
  public function options_submit(&$form, &$form_state) {
565
    // It is very important to call the parent function here.
566
    parent::options_submit($form, $form_state);
567
    switch ($form_state['section']) {
568
      case 'path':
569
        $this->set_option('path', $form_state['values']['path']);
570
        break;
571

    
572
      case 'menu':
573
        $this->set_option('menu', $form_state['values']['menu']);
574
        // Send ajax form to options page if we use it.
575
        if ($form_state['values']['menu']['type'] == 'default tab') {
576
          views_ui_add_form_to_stack('display', $this->view, $this->display->id, array('tab_options'));
577
        }
578
        break;
579

    
580
      case 'tab_options':
581
        $this->set_option('tab_options', $form_state['values']['tab_options']);
582
        break;
583
    }
584
  }
585

    
586
  /**
587
   * {@inheritdoc}
588
   */
589
  public function validate() {
590
    $errors = parent::validate();
591

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

    
597
    if ($menu['type'] == 'default tab') {
598
      $tab_options = $this->get_option('tab_options');
599
      if (!empty($tab_options['type']) && $tab_options['type'] != 'none' && empty($tab_options['title'])) {
600
        $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));
601
      }
602
    }
603

    
604
    return $errors;
605
  }
606

    
607
  /**
608
   * {@inheritdoc}
609
   */
610
  public function get_argument_text() {
611
    return array(
612
      'filter value not present' => t('When the filter value is <em>NOT</em> in the URL'),
613
      'filter value present' => t('When the filter value <em>IS</em> in the URL or a default is provided'),
614
      'description' => t('The contextual filter values is provided by the URL.'),
615
    );
616
  }
617

    
618
  /**
619
   * {@inheritdoc}
620
   */
621
  public function get_pager_text() {
622
    return array(
623
      'items per page title' => t('Items per page'),
624
      'items per page description' => t('The number of items to display per page. Enter 0 for no limit.'),
625
    );
626
  }
627

    
628
}