From 8787384e111a45f971e11a4dee5b6fdcb95b2f8f Mon Sep 17 00:00:00 2001 From: catch Date: Wed, 6 Feb 2013 16:15:31 +0000 Subject: [PATCH] Issue #1869124 by Les Lim, damiankloip: Added Customizable 'true'/'false' Views output for booleans. --- .../views/Plugin/views/field/Boolean.php | 30 ++++- .../views/Tests/Handler/FieldBooleanTest.php | 1 - .../views/Tests/UI/CustomBooleanTest.php | 114 ++++++++++++++++++ 3 files changed, 142 insertions(+), 3 deletions(-) create mode 100644 core/modules/views/lib/Drupal/views/Tests/UI/CustomBooleanTest.php diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/field/Boolean.php b/core/modules/views/lib/Drupal/views/Plugin/views/field/Boolean.php index c4f876de49a..906265a07ef 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/field/Boolean.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/field/Boolean.php @@ -36,6 +36,8 @@ class Boolean extends FieldPluginBase { protected function defineOptions() { $options = parent::defineOptions(); $options['type'] = array('default' => 'yes-no'); + $options['type_custom_true'] = array('default' => '', 'translatable' => TRUE); + $options['type_custom_false'] = array('default' => '', 'translatable' => TRUE); $options['not'] = array('definition bool' => 'reverse'); return $options; @@ -56,7 +58,8 @@ class Boolean extends FieldPluginBase { 'unicode-yes-no' => array('✔', '✖'), ); $output_formats = isset($this->definition['output formats']) ? $this->definition['output formats'] : array(); - $this->formats = array_merge($default_formats, $output_formats); + $custom_format = array('custom' => array(t('Custom'))); + $this->formats = array_merge($default_formats, $output_formats, $custom_format); } public function buildOptionsForm(&$form, &$form_state) { @@ -70,6 +73,26 @@ class Boolean extends FieldPluginBase { '#options' => $options, '#default_value' => $this->options['type'], ); + $form['type_custom_true'] = array( + '#type' => 'textfield', + '#title' => t('Custom output for TRUE'), + '#default_value' => $this->options['type_custom_true'], + '#states' => array( + 'visible' => array( + 'select[name="options[type]"]' => array('value' => 'custom'), + ), + ), + ); + $form['type_custom_false'] = array( + '#type' => 'textfield', + '#title' => t('Custom output for FALSE'), + '#default_value' => $this->options['type_custom_false'], + '#states' => array( + 'visible' => array( + 'select[name="options[type]"]' => array('value' => 'custom'), + ), + ), + ); $form['not'] = array( '#type' => 'checkbox', '#title' => t('Reverse'), @@ -85,7 +108,10 @@ class Boolean extends FieldPluginBase { $value = !$value; } - if (isset($this->formats[$this->options['type']])) { + if ($this->options['type'] == 'custom') { + return $value ? filter_xss_admin($this->options['type_custom_true']) : filter_xss_admin($this->options['type_custom_false']); + } + elseif (isset($this->formats[$this->options['type']])) { return $value ? $this->formats[$this->options['type']][0] : $this->formats[$this->options['type']][1]; } else { diff --git a/core/modules/views/lib/Drupal/views/Tests/Handler/FieldBooleanTest.php b/core/modules/views/lib/Drupal/views/Tests/Handler/FieldBooleanTest.php index 61b2a6d4378..417e0b58128 100644 --- a/core/modules/views/lib/Drupal/views/Tests/Handler/FieldBooleanTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/Handler/FieldBooleanTest.php @@ -84,7 +84,6 @@ class FieldBooleanTest extends ViewUnitTestBase { $view->field['age']->options['type'] = 'test'; $this->assertEqual(t('Test-False'), $view->field['age']->advanced_render($view->result[0])); $this->assertEqual(t('Test-True'), $view->field['age']->advanced_render($view->result[1])); - } } diff --git a/core/modules/views/lib/Drupal/views/Tests/UI/CustomBooleanTest.php b/core/modules/views/lib/Drupal/views/Tests/UI/CustomBooleanTest.php new file mode 100644 index 00000000000..0b2ff19b101 --- /dev/null +++ b/core/modules/views/lib/Drupal/views/Tests/UI/CustomBooleanTest.php @@ -0,0 +1,114 @@ + 'Boolean custom options: UI', + 'description' => 'Tests the UI and functionality for the Custom boolean field handler options', + 'group' => 'Views UI', + ); + } + + /** + * \Drupal\views\Tests\ViewTestBase::viewsData(). + */ + public function viewsData() { + $data = parent::viewsData(); + $data['views_test_data']['age']['field']['id'] = 'boolean'; + return $data; + } + + /** + * Overrides \Drupal\views\Tests\ViewTestBase::dataSet(). + */ + public function dataSet() { + $data = parent::dataSet(); + $data[0]['age'] = 0; + $data[3]['age'] = 0; + return $data; + } + + /** + * Tests the setting and output of custom labels for boolean values. + */ + public function testCustomOption() { + // Add the boolean field handler to the test view. + $view = views_get_view('test_view'); + $view->setDisplay(); + + $view->displayHandlers->get('default')->overrideOption('fields', array( + 'age' => array( + 'id' => 'age', + 'table' => 'views_test_data', + 'field' => 'age', + 'relationship' => 'none', + ), + )); + + $this->executeView($view); + + $custom_true = 'Yay'; + $custom_false = 'Nay'; + + // Set up some custom value mappings for different types. + $custom_values = array( + 'plain' => array( + 'true' => $custom_true, + 'false' => $custom_false, + 'test' => 'assertTrue', + ), + 'allowed tag' => array( + 'true' => '

' . $custom_true . '

', + 'false' => '

' . $custom_false . '

', + 'test' => 'assertTrue', + ), + 'disallowed tag' => array( + 'true' => '', + 'false' => '', + 'test' => 'assertFalse', + ), + ); + + // Run the same tests on each type. + foreach ($custom_values as $type => $values) { + $options = array( + 'options[type]' => 'custom', + 'options[type_custom_true]' => $values['true'], + 'options[type_custom_false]' => $values['false'], + ); + $this->drupalPost('admin/structure/views/nojs/config-item/test_view/default/field/age', $options, 'Apply'); + + // Save the view. + $this->drupalPost('admin/structure/views/view/test_view', array(), 'Save'); + + $view = views_get_view('test_view'); + $output = $view->preview(); + + $replacements = array('%type' => $type); + $this->{$values['test']}(strpos($output, $values['true']), format_string('Expected custom boolean TRUE value in output for %type.', $replacements)); + $this->{$values['test']}(strpos($output, $values['false']), format_string('Expected custom boolean FALSE value in output for %type', $replacements)); + } + } + +} +