views_plugin_style_mapping.inc

Definition of views_plugin_style_mapping.

File

plugins/views_plugin_style_mapping.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_plugin_style_mapping.
  5. */
  6. /**
  7. * Allows fields to be mapped to specific use cases.
  8. */
  9. abstract class views_plugin_style_mapping extends views_plugin_style {
  10. /**
  11. * Builds the list of field mappings.
  12. *
  13. * @return array
  14. * An associative array, keyed by the field name, containing the following
  15. * key-value pairs:
  16. * - #title: The human-readable label for this field.
  17. * - #default_value: The default value for this field. If not provided, an
  18. * empty string will be used.
  19. * - #description: A description of this field.
  20. * - #required: Whether this field is required.
  21. * - #filter: (optional) A method on the plugin to filter field options.
  22. * - #toggle: (optional) If this select should be toggled by a checkbox.
  23. */
  24. abstract protected function define_mapping();
  25. /**
  26. * Overrides views_plugin_style::option_definition().
  27. */
  28. function option_definition() {
  29. $options = parent::option_definition();
  30. // Parse the mapping and add a default for each.
  31. foreach ($this->define_mapping() as $key => $value) {
  32. $default = !empty($value['#multiple']) ? array() : '';
  33. $options['mapping']['contains'][$key] = array(
  34. 'default' => isset($value['#default_value']) ? $value['#default_value'] : $default,
  35. );
  36. if (!empty($value['#toggle'])) {
  37. $options['mapping']['contains']["toggle_$key"] = array(
  38. 'default' => FALSE,
  39. 'bool' => TRUE,
  40. );
  41. }
  42. }
  43. return $options;
  44. }
  45. /**
  46. * Overrides views_plugin_style::options_form().
  47. */
  48. function options_form(&$form, &$form_state) {
  49. parent::options_form($form, $form_state);
  50. // Get the mapping.
  51. $mapping = $this->define_mapping();
  52. // Restrict the list of defaults to the mapping, in case they have changed.
  53. $options = array_intersect_key($this->options['mapping'], $mapping);
  54. // Get the labels of the fields added to this display.
  55. $field_labels = $this->display->handler->get_field_labels();
  56. // Provide some default values.
  57. $defaults = array(
  58. '#type' => 'select',
  59. '#required' => FALSE,
  60. '#multiple' => FALSE,
  61. );
  62. // For each mapping, add a select element to the form.
  63. foreach ($options as $key => $value) {
  64. // If the field is optional, add a 'None' value to the top of the options.
  65. $field_options = array();
  66. $required = !empty($mapping[$key]['#required']);
  67. if (!$required && empty($mapping[$key]['#multiple'])) {
  68. $field_options = array('' => t('- None -'));
  69. }
  70. $field_options += $field_labels;
  71. // Optionally filter the available fields.
  72. if (isset($mapping[$key]['#filter'])) {
  73. $this->view->init_handlers();
  74. $this::$mapping[$key]['#filter']($field_options);
  75. unset($mapping[$key]['#filter']);
  76. }
  77. // These values must always be set.
  78. $overrides = array(
  79. '#options' => $field_options,
  80. '#default_value' => $options[$key],
  81. );
  82. // Optionally allow the select to be toggleable.
  83. if (!empty($mapping[$key]['#toggle'])) {
  84. $form['mapping']["toggle_$key"] = array(
  85. '#type' => 'checkbox',
  86. '#title' => t('Use a custom %field_name', array('%field_name' => strtolower($mapping[$key]['#title']))),
  87. '#default_value' => $this->options['mapping']["toggle_$key"],
  88. );
  89. $overrides['#states']['visible'][':input[name="style_options[mapping][' . "toggle_$key" . ']"]'] = array('checked' => TRUE);
  90. }
  91. $form['mapping'][$key] = $overrides + $mapping[$key] + $defaults;
  92. }
  93. }
  94. /**
  95. * Overrides views_plugin_style::render().
  96. *
  97. * Provides the mapping definition as an available variable.
  98. */
  99. function render() {
  100. return theme($this->theme_functions(), array(
  101. 'view' => $this->view,
  102. 'options' => $this->options,
  103. 'rows' => $this->view->result,
  104. 'mapping' => $this->define_mapping(),
  105. ));
  106. }
  107. }