diff --git a/plugins/views_plugin_style_mapping.inc b/plugins/views_plugin_style_mapping.inc index c8f3037..7b2e2ef 100644 --- a/plugins/views_plugin_style_mapping.inc +++ b/plugins/views_plugin_style_mapping.inc @@ -16,12 +16,13 @@ class views_plugin_style_mapping extends views_plugin_style { * @return array * An associative array, keyed by the field name, containing the following * key-value pairs: - * - label: The human-readable label for this field. - * - default: The default value for this field. If not provided, an empty - * string will be used. - * - description: (optional) A description of this field. - * - optional: Whether this field is optional. If not provided, the field - * will be required. + * - #title: The human-readable label for this field. + * - #default_value: The default value for this field. If not provided, an + * empty string will be used. + * - #description: A description of this field. + * - #required: Whether this field is required. + * - #filter: (optional) A method on the plugin to filter field options. + * - #toggle: (optional) If this select should be toggled by a checkbox. */ function define_mapping() { return array(); @@ -35,9 +36,16 @@ function option_definition() { // Parse the mapping and add a default for each. foreach ($this->define_mapping() as $key => $value) { - $options['views_field_mappings']['contains'][$key] = array( - 'default' => isset($value['default']) ? $value['default'] : '', + $default = !empty($value['#multiple']) ? array() : ''; + $options['views_mapping']['contains'][$key] = array( + 'default' => isset($value['#default_value']) ? $value['#default_value'] : $default, ); + if (!empty($value['#toggle'])) { + $options['views_mapping']['contains']["toggle_$key"] = array( + 'default' => FALSE, + 'bool' => TRUE, + ); + } } return $options; @@ -53,32 +61,50 @@ function options_form(&$form, &$form_state) { $mapping = $this->define_mapping(); // Restrict the list of defaults to the mapping, in case they have changed. - $defaults = array_intersect_key($this->options['views_field_mappings'], $mapping); + $options = array_intersect_key($this->options['views_mapping'], $mapping); // Get the labels of the fields added to this display. $field_labels = $this->display->handler->get_field_labels(); + // Provide some default values. + $defaults = array( + '#type' => 'select', + '#required' => FALSE, + '#multiple' => FALSE, + ); + // For each mapping, add a select element to the form. - foreach($defaults as $key => $value) { + foreach ($options as $key => $value) { // If the field is optional, add a 'None' value to the top of the options. $field_options = array(); - if ($optional = !empty($mapping[$key]['optional'])) { + $required = !empty($mapping[$key]['required']); + if (!$required) { $field_options = array('' => t('- None -')); } $field_options += $field_labels; - $form['views_field_mappings'][$key] = array( - '#type' => 'select', + // Optionally filter the available fields. + if (isset($mapping[$key]['#filter'])) { + $this::$mapping[$key]['#filter']($field_options); + unset($mapping[$key]['#filter']); + } + + // These values must always be set. + $overrides = array( '#options' => $field_options, - '#title' => $mapping[$key]['label'], - '#default_value' => $defaults[$key], - '#required' => !$optional, + '#default_value' => $options[$key], ); - // Add a description if one is provided. - if (!empty($mapping[$key]['description'])) { - $form['views_field_mappings'][$key]['#description'] = $mapping[$key]['description']; + // Optionally allow the select to be toggleable. + if (!empty($mapping[$key]['#toggle'])) { + $form['views_mapping']["toggle_$key"] = array( + '#type' => 'checkbox', + '#title' => t('Use a custom %field_name', array('%field_name' => strtolower($mapping[$key]['#title']))), + ); + $overrides['#states']['visible'][':input[name="style_options[views_mappings][' . "toggle_$key" . ']"]'] = array('checked' => TRUE); } + + $form['views_mapping'][$key] = $overrides + $mapping[$key] + $defaults; } }