5 * Definition of views_plugin_style_mapping.
9 * Allows fields to be mapped to specific use cases.
11 abstract class views_plugin_style_mapping extends views_plugin_style {
14 * Builds the list of field mappings.
17 * An associative array, keyed by the field name, containing the following
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.
27 abstract protected function define_mapping();
30 * Overrides views_plugin_style::option_definition().
32 function option_definition() {
33 $options = parent::option_definition();
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,
41 if (!empty($value['#toggle'])) {
42 $options['mapping']['contains']["toggle_$key"] = array(
53 * Overrides views_plugin_style::options_form().
55 function options_form(&$form, &$form_state) {
56 parent::options_form($form, $form_state);
59 $mapping = $this->define_mapping();
61 // Restrict the list of defaults to the mapping, in case they have changed.
62 $options = array_intersect_key($this->options['mapping'], $mapping);
64 // Get the labels of the fields added to this display.
65 $field_labels = $this->display->handler->get_field_labels();
67 // Provide some default values.
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 -'));
82 $field_options += $field_labels;
84 // Optionally filter the available fields.
85 if (isset($mapping[$key]['#filter'])) {
86 $this->view->init_handlers();
87 $this::$mapping[$key]['#filter']($field_options);
88 unset($mapping[$key]['#filter']);
91 // These values must always be set.
93 '#options' => $field_options,
94 '#default_value' => $options[$key],
97 // Optionally allow the select to be toggleable.
98 if (!empty($mapping[$key]['#toggle'])) {
99 $form['mapping']["toggle_$key"] = array(
100 '#type' => 'checkbox',
101 '#title' => t('Use a custom %field_name', array('%field_name' => strtolower($mapping[$key]['#title']))),
102 '#default_value' => $this->options['mapping']["toggle_$key"],
104 $overrides['#states']['visible'][':input[name="style_options[mapping][' . "toggle_$key" . ']"]'] = array('checked' => TRUE);
107 $form['mapping'][$key] = $overrides + $mapping[$key] + $defaults;
112 * Overrides views_plugin_style::render().
114 * 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(),