views_plugin_style_mapping.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. * {@inheritdoc}
  27. */
  28. public 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. * {@inheritdoc}
  47. */
  48. public 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. $filter = $mapping[$key]['#filter'];
  75. $this::$filter($field_options);
  76. unset($mapping[$key]['#filter']);
  77. }
  78. // These values must always be set.
  79. $overrides = array(
  80. '#options' => $field_options,
  81. '#default_value' => $options[$key],
  82. );
  83. // Optionally allow the select to be toggleable.
  84. if (!empty($mapping[$key]['#toggle'])) {
  85. $form['mapping']["toggle_$key"] = array(
  86. '#type' => 'checkbox',
  87. '#title' => t('Use a custom %field_name', array('%field_name' => strtolower($mapping[$key]['#title']))),
  88. '#default_value' => $this->options['mapping']["toggle_$key"],
  89. );
  90. $overrides['#states']['visible'][':input[name="style_options[mapping][' . "toggle_$key" . ']"]'] = array('checked' => TRUE);
  91. }
  92. $form['mapping'][$key] = $overrides + $mapping[$key] + $defaults;
  93. }
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function render() {
  99. // Provides the mapping definition as an available variable.
  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. }