Projet

Général

Profil

Paste
Télécharger (3,85 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_plugin_style_mapping.
6
 */
7

    
8
/**
9
 * Allows fields to be mapped to specific use cases.
10
 */
11
abstract class views_plugin_style_mapping extends views_plugin_style {
12

    
13
  /**
14
   * Builds the list of field mappings.
15
   *
16
   * @return array
17
   *   An associative array, keyed by the field name, containing the following
18
   *   key-value pairs:
19
   *   - #title: The human-readable label for this field.
20
   *   - #default_value: The default value for this field. If not provided, an
21
   *     empty string will be used.
22
   *   - #description: A description of this field.
23
   *   - #required: Whether this field is required.
24
   *   - #filter: (optional) A method on the plugin to filter field options.
25
   *   - #toggle: (optional) If this select should be toggled by a checkbox.
26
   */
27
  abstract protected function define_mapping();
28

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

    
35
    // Parse the mapping and add a default for each.
36
    foreach ($this->define_mapping() as $key => $value) {
37
      $default = !empty($value['#multiple']) ? array() : '';
38
      $options['mapping']['contains'][$key] = array(
39
        'default' => isset($value['#default_value']) ? $value['#default_value'] : $default,
40
      );
41
      if (!empty($value['#toggle'])) {
42
        $options['mapping']['contains']["toggle_$key"] = array(
43
          'default' => FALSE,
44
          'bool' => TRUE,
45
        );
46
      }
47
    }
48

    
49
    return $options;
50
  }
51

    
52
  /**
53
   * {@inheritdoc}
54
   */
55
  public function options_form(&$form, &$form_state) {
56
    parent::options_form($form, $form_state);
57

    
58
    // Get the mapping.
59
    $mapping = $this->define_mapping();
60

    
61
    // Restrict the list of defaults to the mapping, in case they have changed.
62
    $options = array_intersect_key($this->options['mapping'], $mapping);
63

    
64
    // Get the labels of the fields added to this display.
65
    $field_labels = $this->display->handler->get_field_labels();
66

    
67
    // Provide some default values.
68
    $defaults = array(
69
      '#type' => 'select',
70
      '#required' => FALSE,
71
      '#multiple' => FALSE,
72
    );
73

    
74
    // For each mapping, add a select element to the form.
75
    foreach ($options as $key => $value) {
76
      // If the field is optional, add a 'None' value to the top of the options.
77
      $field_options = array();
78
      $required = !empty($mapping[$key]['#required']);
79
      if (!$required && empty($mapping[$key]['#multiple'])) {
80
        $field_options = array('' => t('- None -'));
81
      }
82
      $field_options += $field_labels;
83

    
84
      // Optionally filter the available fields.
85
      if (isset($mapping[$key]['#filter'])) {
86
        $this->view->init_handlers();
87
        $filter = $mapping[$key]['#filter'];
88
        $this::$filter($field_options);
89
        unset($mapping[$key]['#filter']);
90
      }
91

    
92
      // These values must always be set.
93
      $overrides = array(
94
        '#options' => $field_options,
95
        '#default_value' => $options[$key],
96
      );
97

    
98
      // Optionally allow the select to be toggleable.
99
      if (!empty($mapping[$key]['#toggle'])) {
100
        $form['mapping']["toggle_$key"] = array(
101
          '#type' => 'checkbox',
102
          '#title' => t('Use a custom %field_name', array('%field_name' => strtolower($mapping[$key]['#title']))),
103
          '#default_value' => $this->options['mapping']["toggle_$key"],
104
        );
105
        $overrides['#states']['visible'][':input[name="style_options[mapping][' . "toggle_$key" . ']"]'] = array('checked' => TRUE);
106
      }
107

    
108
      $form['mapping'][$key] = $overrides + $mapping[$key] + $defaults;
109
    }
110
  }
111

    
112
  /**
113
   * {@inheritdoc}
114
   */
115
  public function render() {
116
    // Provides the mapping definition as an available variable.
117
    return theme($this->theme_functions(), array(
118
      'view' => $this->view,
119
      'options' => $this->options,
120
      'rows' => $this->view->result,
121
      'mapping' => $this->define_mapping(),
122
    ));
123
  }
124

    
125
}