Problem/Motivation

Currently you can configure the output of a boolean field on a field settings level. For views itself though there are more use cases:

  • You want to output 0/1 for raw data output
  • For some nice listings, you might want to support some unicode chars
  • These are the default options + a fully customizable one.
    $default_formats = array(
    'yes-no' => array(t('Yes'), $this->t('No')),
    'true-false' => array(t('True'), $this->t('False')),
    'on-off' => array(t('On'), $this->t('Off')),
    'enabled-disabled' => array(t('Enabled'), $this->t('Disabled')),
    'boolean' => array(1, 0),
    'unicode-yes-no' => array('✔', '✖'),
    );

While we are converting base fields to use field formatters, this lack of support for boolean formatting causes problems in #2342045: Standard views base fields need to use same rendering as Field UI fields, for formatting, access checking, and translation consistency. Once #2342045: Standard views base fields need to use same rendering as Field UI fields, for formatting, access checking, and translation consistency gets in, not having all those options available on the boolean formatter would be a regression.

Proposed resolution

Extend the boolean field formatter with options to support these display formats. This will allow #2342045: Standard views base fields need to use same rendering as Field UI fields, for formatting, access checking, and translation consistency to not regress functionality for boolean fields.

Remaining tasks

Commit.

User interface changes

Boolean fields have more flexible display formats.

API changes

The boolean formatter schema is slightly expanded.

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Task, because its a part of not getting a regression, see #2342045-62: Standard views base fields need to use same rendering as Field UI fields, for formatting, access checking, and translation consistency
Issue priority Normal, because this issue just affects the rendering of booleans, but they ARE really common in admin UIs
Disruption Not disruptive, the existing boolean outputs will continue to work.

Issue fork drupal-2395613

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jhodgdon’s picture

Would it be a problem to add these options to the standard boolean field formatter, or to define additional Formatter classes for Boolean fields with these options available?

dawehner’s picture

FileSize
2.68 KB

Well ... the problem is more about knowing what kind of features we still want to support, and which are just too special.
So far this ports over the listed formatters, BUT in views you can even configure your own one.

jhodgdon’s picture

OK... I looked at what Views gives me now (without any patches) to configure the "Published" field in a node view, and as you say there is also the option "custom", which is basically to provide a value to display for true and a value to display for false. Is there a reason we can't add this option too? It doesn't seem all that complicated, and would require just two more settings strings for the labels.

dawehner’s picture

Status: Active » Needs review
Issue tags: +VDC
FileSize
7.76 KB
8.49 KB

Sure, let's do that.

While writing a test I realized that unicode (as supported by views) somehow doesn't work here.
There is no investigation yet, but just to say it, this patch will fail.

Status: Needs review » Needs work

The last submitted patch, 6: 2395613-6.patch, failed testing.

dawehner’s picture

Status: Needs work » Needs review
FileSize
647 bytes
8.49 KB

Ups, I simply mixed up true and false :)

dawehner’s picture

Wim Leers’s picture

Status: Needs review » Needs work

Ups, I simply mixed up true and false :)

:D


  1. +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/BooleanFormatter.php
    @@ -26,11 +27,98 @@ class BooleanFormatter extends FormatterBase {
    +    // Fallback to field settings by default.
    

    s/Fallback/Fall back/

  2. +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/BooleanFormatter.php
    @@ -26,11 +27,98 @@ class BooleanFormatter extends FormatterBase {
    +   *   A list of output formats. Each entry is keyed by the machine name of the
    +   *   format. The value is an array, of which the first item is the result for
    +   *   boolean TRUE, the second is for boolean FALSE.
    ...
    +      'custom' => [$this->t('Custom')],
    

    The array structure is not followed by this last one. I understand why; but wouldn't it be better to list an empty array in this case, and explicitly document it; referring to the associated settings?

  3. +++ b/core/modules/field/src/Tests/Boolean/BooleanFormatterTest.php
    @@ -0,0 +1,144 @@
    +namespace Drupal\field\Tests\Boolean;
    +use Drupal\Component\Utility\Unicode;
    

    I think we usually have a blank line between these?

  4. +++ b/core/modules/field/src/Tests/Boolean/BooleanFormatterTest.php
    @@ -0,0 +1,144 @@
    +    // Configure the theme system.
    +    $this->installConfig(['field']);
    +    $this->installEntitySchema('entity_test');
    

    Comment doesn't match what happens.

  5. +++ b/core/modules/field/src/Tests/Boolean/BooleanFormatterTest.php
    @@ -0,0 +1,144 @@
    \ No newline at end of file
    

    .

Probably also needs the beta evaluation in the IS.

dawehner’s picture

Issue summary: View changes
Status: Needs work » Needs review
FileSize
8.63 KB
2.88 KB

The array structure is not followed by this last one. I understand why; but wouldn't it be better to list an empty array in this case, and explicitly document it; referring to the associated settings?

Well yeah, I tried to make the code as simple as possible, maybe you like the slighly longer version better?

Comment doesn't match what happens.

Alright, dropped it.

Added the beta evalution.

Wim Leers’s picture

Ah, yes, now I understand; I didn't realize you were exploiting the behavior of implode(' /', $array) with $array a single-valued array.

+++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/BooleanFormatter.php
@@ -41,10 +41,11 @@ public static function defaultSettings() {
-   * @return array
+   * @return array|string
    *   A list of output formats. Each entry is keyed by the machine name of the
    *   format. The value is an array, of which the first item is the result for
-   *   boolean TRUE, the second is for boolean FALSE.
+   *   boolean TRUE, the second is for boolean FALSE. The value can be also an
+   *   array, but this is just the case for the custom format.
    */
   protected function getOutputFormats() {
     $formats = [
@@ -55,7 +56,7 @@ protected function getOutputFormats() {

@@ -55,7 +56,7 @@ protected function getOutputFormats() {
       'enabled-disabled' => [$this->t('Enabled'), $this->t('Disabled')],
       'boolean' => [1, 0],
       'unicode-yes-no' => ['✔', '✖'],
-      'custom' => [$this->t('Custom')],
+      'custom' => $this->t('Custom'),

I wonder if this is really better.

Since this is protected anyway; why not only list "actual" formats here, and just append the 'custom' one in ::settingsForm(). Then all data returned by ::getOutputFormats() would be of the same form!

Other than that, this looks ready to me :)

dawehner’s picture

Since this is protected anyway; why not only list "actual" formats here, and just append the 'custom' one in ::settingsForm(). Then all data returned by ::getOutputFormats() would be of the same form!

I kinda like to have a good place in the code to see all options though.

Wim Leers’s picture

Status: Needs review » Reviewed & tested by the community

Ok; that detail is not important enough to debate further. Let's do it.

dawehner’s picture

Thank you!

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs issue summary update

The beta evaluation lacks details about exactly what regression this is fixing.

dawehner’s picture

Issue summary: View changes
Wim Leers’s picture

Status: Needs work » Reviewed & tested by the community
jhodgdon’s picture

Status: Reviewed & tested by the community » Needs work

Um. The issue summary is making two references that should be to other issues but are pointing back to this issue... not sure which issue should be pointed to or I'd fix it (I keep getting confused about which issue is doing what lately on the Views stuff).

Gábor Hojtsy’s picture

Issue summary: View changes
Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs issue summary update +blocker

Updated issue summary with correct links, more details. This issue is currently rolled into the core critical #2342045: Standard views base fields need to use same rendering as Field UI fields, for formatting, access checking, and translation consistency, so marking it a blocker as well.

Gábor Hojtsy queued 11: 2395613-11.patch for re-testing.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

This is part of making views use field formatters and not regress. Thanks for adding the beta evaluation to the issue summary.

Committed d2ff08e and pushed to 8.0.x. Thanks!

  • alexpott committed d2ff08e on 8.0.x
    Issue #2395613 by dawehner: Make it possible to configure the output of...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

jhodgdon’s picture

Follow-up if anyone wants to take a look at this:
#2428087: Boolean field formatter - default choice appears in list twice

shivam_tiwari made their first commit to this issue’s fork.