Skip to content

Add a method to check for presence of multiple values in SelectableOptionsArray #2164

@SkyLundy

Description

@SkyLundy

It would be very useful to have a method that checks for multiple values in a SelectableOptionsArray object. This would make working with Checkboxes fields a little less verbose.

Example

<?php 

// Assuming a Checkboxes field with options for 'foo', 'bar', 'baz', 'fizz', 'buzz'
// We want to check if 'foo', 'bar', 'baz' are checked

// Currently
if (
  $checkboxes->hasValue('foo') &&
  $checkboxes->hasValue('bar') &&
  $checkboxes->hasValue('baz')
) {
  // Do something
}

// With a hasValues method
if ($checkboxes->hasValues(['foo', 'bar', 'baz'])) {
  // Do something
}

Use case

There are instances where 2 or more checked values represent a unique state or value not otherwise defined in the field configuration. In some cases explicitly defining multiple state or value combinations in the field isn't feasible.

A very simple example is a Checkboxes field describing a meeting location with options for:

  • In Person
  • Online
  • To Be Announced

For the purposes of rendering the template, each of these is a unique value:

  • In Person
  • Online
  • In Person and Online
  • To Be Announced
  • In Person and To Be Announced (address not yet known)
  • Online and To Be Announced (URL not yet known)
  • In Person, Online, and To Be Announced (address/URL not yet known)

Implementation

Method would mirror the return value style of SelectableOptionsArray::hasValues()

A value for true returns a new SelectableOptionsArray object containing all found values
A value for false returns boolean false

I've written a method that is implemented like this that I can open a PR for if it would be helpful.

Thanks for considering!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions