1 |
85ad3d82
|
Assos Assos
|
<?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 |
|
|
* Overrides views_plugin_style::option_definition().
|
31 |
|
|
*/
|
32 |
|
|
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 |
|
|
* Overrides views_plugin_style::options_form().
|
54 |
|
|
*/
|
55 |
|
|
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 |
b08fce64
|
Assos Assos
|
$filter = $mapping[$key]['#filter'];
|
88 |
|
|
$this::$filter($field_options);
|
89 |
85ad3d82
|
Assos Assos
|
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 |
|
|
* Overrides views_plugin_style::render().
|
114 |
|
|
*
|
115 |
|
|
* Provides the mapping definition as an available variable.
|
116 |
|
|
*/
|
117 |
|
|
function render() {
|
118 |
|
|
return theme($this->theme_functions(), array(
|
119 |
|
|
'view' => $this->view,
|
120 |
|
|
'options' => $this->options,
|
121 |
|
|
'rows' => $this->view->result,
|
122 |
|
|
'mapping' => $this->define_mapping(),
|
123 |
|
|
));
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
} |