Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I implemented #2010898 patch #126, which works fine when there is the same number of arguments sent as required. If the number of arguments sent are less than the required, it doesn't work.
With my limited knowledge of Drupal programing, I looked into entityreference/plugins/selection/EntityReference_SelectionHandler_Views.class.php code, and found one issue - '#default' instead of '#default_value':
if (module_exists('token')) {
$default = !empty($view_settings['cleanup']) ? $view_settings['cleanup'] : 0;
$form['view']['cleanup'] = array(
'#type' => 'checkbox',
'#title' => t('Remove empty arguments'),
'#default_value' => $default,
'#description' => '<p>' . t('Remove empty arguments after token replacement. This is useful if your view accepts optional arguments.') . '</p>',
);
I also changed the FALSE in to places to TRUE and got cleanup to always be TRUE, and work.
A temporary fix:
function validateReferencableEntities(array $ids) {
$display_name = $this->field['settings']['handler_settings']['view']['display_name'];
if (isset($this->field['settings']['handler_settings']['view']['cleanup'])) {
$cleanup = $this->field['settings']['handler_settings']['view']['cleanup'];
}
else {
$cleanup = TRUE;
}
public function entityFieldQueryAlter(SelectQueryInterface $query) {
}
/**
* Handles arguments for views.
*
* Replaces tokens using token_replace().
*
* @param array $args
* Usually $this->field['settings']['handler_settings']['view']['args'].
*
* @param $cleanup
* $this->field['settings']['handler_settings']['view']['cleanup'].
*
* @return array
* The arguments to be send to the View.
*/
protected function handleArgs($args, $cleanup = TRUE) {
// Parameters for token_replace().
I wish I know what to fix here.
I wish I know what to fix here.
Comment | File | Size | Author |
---|---|---|---|
#7 | remove-empty-arguments-2498755-7.patch | 3.82 KB | shevchess |
| |||
#2 | remove-empty-arguments-2498755-2.patch | 6.52 KB | stiknoltz |
#1 | remove-empty-arguments-2498755-1.patch | 6.29 KB | stiknoltz |
Comments
Comment #1
stiknoltz CreditAttribution: stiknoltz as a volunteer commentedHere's a re-roll of #126 with a fix for the unsaved cleanup setting. You were halfway there w/ the 'default_value' issue. When the value of the field was re-assigned on line 272 in the validation function, it was missing the cleanup value.
Comment #2
stiknoltz CreditAttribution: stiknoltz as a volunteer commentedSorry, one more step was needed to ensure that when the view was actually loaded, the empty args were unset. Re-rolled again.
Comment #3
5kot CreditAttribution: 5kot as a volunteer commented#2 works fine.
I had the same problem of the cleanup value not saving.
Comment #4
shevchess CreditAttribution: shevchess commentedThe patch #2 worked for me too.
Comment #5
bkat CreditAttribution: bkat commentedI wasn't getting the cleanup value to be saved, it turns out that the patch has
but it should be
It was using '#default' versus '#default_value'
Comment #6
bkat CreditAttribution: bkat commentedNow that I have cleanup empty values working, its not behaving like I was hoping. I have a entity reference field that is being used in a couple of different types:
1. Registration: I need [registration:entity-id] used as the view argument
2. Game: I need [multifield:field-team:field-league:nid] use as the view argument
When I set cleanup empty fields, it does leave one argument but it could be index 0 or 1. When its index 1, my view doesn't work.
I can make it behave the way I want if EntityReference_SelectionHandler_Views::handleArgs returns array_values($args).
But if the intent is to really leave holes in the args list, then I would like extend the argument syntax for entity_reference so that it accepts
arg1a|arg1b|arg1c, arg2, arg3, etc
That is each argument is comprised of a set of possible arguments and handleArgs will return the first one that returns a non-empty value. If no option returns a non-empty value, it will return the last value (just in case an empty string is pertinent)
So in my case I would set my views args to be
[multifield:field-team:field-league:nid]|[registration:entity-id]
If I'm editing a registration, the first token will not return a value so it will try the 2nd one and use that.
If this is worthwhile to others, I will gladly whip it up and create a patch
Comment #7
shevchess CreditAttribution: shevchess commentedHere's a re-roll of #2 against current 7.x-1.x (works fine with 7.x-1.5). Since the issue https://www.drupal.org/node/2010898 is fixed and committed now, the new patch was simplified significantly. It adds "Remove empty arguments" option.
The solution in #2 in the issue thread https://www.drupal.org/project/entityreference/issues/2830703 doesn't work for me.
Comment #8
Chris Matthews CreditAttribution: Chris Matthews as a volunteer commented