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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

stiknoltz’s picture

Status: Active » Needs review
FileSize
6.29 KB

Here'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.

stiknoltz’s picture

Sorry, one more step was needed to ensure that when the view was actually loaded, the empty args were unset. Re-rolled again.

5kot’s picture

#2 works fine.

I had the same problem of the cleanup value not saving.

shevchess’s picture

The patch #2 worked for me too.

bkat’s picture

I wasn't getting the cleanup value to be saved, it turns out that the patch has

$form['view']['cleanup'] = array(
          '#type' => 'checkbox',
          '#title' => t('Remove empty arguments'),
          '#default' => $default,
          '#description' => '<p>' . t('Remove empty arguments after token replacement. This is useful if your view accepts optional arguments.') . '</p>',
        );

but it should be

$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>',
        );

It was using '#default' versus '#default_value'

bkat’s picture

Now 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

shevchess’s picture

Title: 'Remove empty arguments' checkbox doesn't save and doesn't work » Add "Remove empty arguments" checkbox when Views entity selection mode is used
FileSize
3.82 KB

Here'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.

Chris Matthews’s picture

Version: 7.x-1.1 » 7.x-1.x-dev