Skip to content

Add displayValues option to respect property order#2393

Open
jimsafley wants to merge 2 commits intodevelopfrom
display-values-respect-property-order-option
Open

Add displayValues option to respect property order#2393
jimsafley wants to merge 2 commits intodevelopfrom
display-values-respect-property-order-option

Conversation

@jimsafley
Copy link
Member

See #2391. Adds "propertiesRespectOrder" option to AbstractResourceEntityRepresentation::displayValues(). Setting it to true changes default behavior to respect the order of properties passed in the "properties" option. The default behavior is to maintain the original property order, irrespective of the passed order. For example, this:

$item->displayValues(['properties' => ['dcterms:subject', 'dcterms:description', 'dcterms:title']]);

will, in most cases, output "Title, Description, Subject" because that's the order defined on the resource itself. Whereas, this:

$item->displayValues(['properties' => ['dcterms:subject', 'dcterms:description', 'dcterms:title'], 'propertiesRespectOrder' => true])

will output "Subject, Description, Title" because "propertiesRespectOrder" is true, and that's the order passed in the "properties" option.

@jimsafley jimsafley requested a review from zerocrates December 13, 2025 21:53
@zerocrates
Copy link
Member

zerocrates commented Dec 24, 2025

This won't work properly if properties contains terms that the resource has no values for: you'll get an entry for the "missing" terms in the final result when you shouldn't (despite its name, array_replace will introduce new keys in the result that aren't in the first arg).

It's possible to fix this with more array_ calls... one option that comes to mind would be doing another array_intersect_keys with the original $values passed second to drop out anything in we don't actually have a value for... but we may just want to swap this out with a foreach:

$result = [];
foreach ($options['properties'] as $term) {
    if (isset($values[$term])) {
        $result[$term] = $values[$term];
    }
}
$values = $result;

@jimsafley
Copy link
Member Author

I feel that propertiesRespectOrder is an awkward option name. It implies that the default behavior is somehow not respecting an order. Maybe a better option is originalOrder with the default set to true? That way the default behavior remains the same, and originalOrder=false respects the order passed in properties.

@zerocrates
Copy link
Member

I'd want properties first in any option as this is something that only works with the properties option. propertiesOrdered or whatever if we want to shorten it, i'm fine with. The reverses, like propertiesOriginalOrder also fine, but also kind of long.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants