Description

When I first look at a view with an on/off filter, the checkboxes on the field list should properly reflect what fields start on or off.

Desired behavior

If I have uid, user name, and email visible and all of those fields are included in the on/off handler, when I first hit the view, uid, user name, and email should start checked instead of unchecked.

Current behavior

All field checkboxes start unchecked, regardless of whether the field is visible or not.

Comments

franksj created an issue. See original summary.

andileco’s picture

Status: Active » Needs review
StatusFileSize
new843 bytes

Here's a stab, which will also work with https://www.drupal.org/project/views_fields_on_off/issues/3048250 (although we may want to update this after that one goes in so as to have a default state radios (I'm thinking maybe a "All" option selected)).

andileco’s picture

StatusFileSize
new1.18 KB

Last one wasn't working in some cases after submitting. I think this one should work.

franksj’s picture

Setting ['#attributes']['checked'] isn't a totally valid way to do this. I know that ['#default_value'] doesn't work as expected for an exposed form, but here's a case where ['#attributes']['checked'] doesn't work:
Create a view on Article with the following fields:
Content: Title
Content: Authored by (Authored by)
Content: Authored on (Authored on) [hidden]
Content: Body (Body)
Content: Content type (Content type)
Content: ID (ID) [hidden]
Content: Tags (Tags)
Global: On/Off Form (On/Off Form)

The On/Off Form only allows selection of visible fields (and I'm not sure when it started doing that - I thought that previously, it allowed selection of fields whether they were visible or not, which I think it should do), so leave them all visible. Select all of the fields in the On/Off form, then edit "Authored on" to Excluded.

Now, when you go to the view, all of the fields are checked, even though "Authored on" isn't visible. We need to be able to set the checked fields individually. I started working on this and found that ['#default_value'] didn't work as expected in the exposed form.

For instance, if you create a standard form and display it, this behaves correctly:

$options = [
      'one' => 'One',
      'two' => 'Two',
      'three' => 'Three',
    ];
    $defaultValues = ['two'];
    $form['checkboxes'] = [
      '#type' => 'checkboxes',
      '#title' => $this->t('Test'),
      '#description' => t('Select the fields you want to display.'),
      '#options' => $options,
      '#default_value' => $defaultValues,
    ];

If you paste that into the exposed form, the default value for a checkboxes element is ignored (note that #default_value works for other controls on an exposed form, and that it works for checkboxes on another form, but for checkboxes on an exposed form, it doesn't work). If we can't use the default value or attributes for the whole checkboxes element, we need some other way to check them individually. I don't have that other way yet :)

franksj’s picture

So we don't lose this experiment for testing:

    $options = [
      'one' => 'One',
      'two' => 'Two',
      'three' => 'Three',
    ];
    $defaultValues = ['two'];
    $form['checkboxes'] = [
      '#type' => 'checkboxes',
      '#title' => $this->t('Test'),
      '#description' => t('Select the fields you want to display.'),
      '#options' => $options,
      '#default_value' => $defaultValues,
    ];

    $form['select'] = [
      '#type' => 'select',
      '#options' => $options,
      '#title' => $this->t('Order3', [], ['context' => 'Sort order']),
      '#default_value' => ['three'],
    ];

If we add the code above to a regular form, then the "Two" checkbox will be selected by default, and the "Three" option will be selected by default.
If we add this code to the exposed form, then the checkboxes are all un-checked and the "Three" option will be selected by default.

franksj’s picture

Status: Needs review » Needs work
ranavaibhav’s picture

+1 still in scope?

andileco’s picture

Yes, still in scope, and the patch should work if you need a quick fix.

maxilein’s picture

  • andileco committed ad3e4a4 on 8.x-1.x
    Issue #3042469 by andileco, franksj, ranavaibhav, maxilein: Initial view...

  • andileco committed 6e3a584 on 8.x-1.x
    Issue #3042469 by andileco, franksj, ranavaibhav, maxilein: Initial view...
andileco’s picture

Status: Needs work » Fixed

This commit adds a checkbox: "Enable fields by default." - if this is checked, the checkboxes and multi-select fields will be shown with all the items selected (I skipped this for radios and single-select because it wouldn't make sense to show them as selected when more fields are showing than can be selected for these select types).

Status: Fixed » Closed (fixed)

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