Problem/Motivation

Similar to #3511069: Allow the ProjectBrowser render element to customize the available filtering options , we want the ProjectBrowser render element to be able to control which sort options are exposed.

Proposed resolution

Introduce a #sort_options property to the render element. If provided, it should be an associative array like the one returned from ProjectBrowserSourceInterface::getSortOptions():

'#sort_options' => [
  'a_z' => $this->t('Alphabetical'),
  'z_a' => $this->t('Reverse alphabetical'),
];

Every key in the array needs to also be defined by getSortOptions() (using array_diff_key()), or we throw an exception. If it passes that bit of validation, then this is provided to the Project Browser instance settings' sorts property. This allows render element calling code to override the names of the sorts, but not their identifiers.

Meanwhile, on the frontend, we don't need to change much; all we have to do is hide the "Sort by" field if there are fewer than 2 sort options.

This should also be configurable for the Project Browser block -- for now, in a simple way, similar to the filters, where we just allow site admins to choose which sorts to show (as a set of checkboxes), but not influence their names or ordering.

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:

Comments

phenaproxima created an issue. See original summary.

phenaproxima’s picture

Status: Active » Needs work
phenaproxima’s picture

Status: Needs work » Needs review
penyaskito’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new88.81 KB
new40.87 KB

Have zero comments on the code. Looks perfect.
Test coverage it's great too.

TBH I don't feel like the block should be able to allow to override the sort labels. Nice that the API provides this though.

A screenshot of 2 PB blocks, one with only ony sorting enabled (so no sort exposed), another one with only 2 sort options enabled:

Screenshot with 2 project browser blocks

A screenshot of the validation error after selecting as default a sorting that is not enabled:

Validation error after selecting as default a sorting that is not enabled

The errored field is not highlighted, but has the "has-error" class. So this is a problem with off-canvas styling, not the field error handling here.

tim.plunkett’s picture

Saving credit

tim.plunkett’s picture

Status: Reviewed & tested by the community » Fixed

Merged !776 🎉

  • tim.plunkett committed 04b4ae6b on 2.0.x
    Issue #3511246 by tim.plunkett: Follow-up for last-minute test change.
    

Status: Fixed » Closed (fixed)

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