1

I have a view which has a filter by users that created de nodes. This filter is exposed, in order to select an user and filter the results by this user. I would like to change the default autocomplete filter by a select list of user, and in this list I want to show only some users.
I followed this tutorial but without success. Although I change "uid" form field, It does anything. How could I do it? Thanks

UPDATE: my code.

function my_module_form_views_exposed_form_alter(&$form, FormStateInterface $form_state, $form_id) {


$pos = stripos($form['#id'], 'views-exposed-form-my-form');
if ($pos !== false) {

    $opcusuarios = array();
    $query = \Drupal::entityQuery('user');
    ... some conditions ...
    $entity_ids = $query->execute();              
    $usuarios = \Drupal::entityTypeManager()->getStorage('user')->loadMultiple($entity_ids);
    foreach($usuarios as $user) {
        $opcusuarios[$user->id()] = $user->getAccountName();
    }

    // Change the input
    $form['uid']['#type'] = 'select';
    $form['uid']['#multiple'] = FALSE;

    // Add the $options from above to our select list
    $form['uid']['#options'] = $opcusuarios;
    unset($form['uid']['#size']);
}

}

4
  • Did you clear the caches?
    – user72672
    Oct 20, 2018 at 7:09
  • Yes, I did it, and I checked it adding drupal logger messages.
    – briast
    Oct 20, 2018 at 7:42
  • You also changed the places in the code were there stood Artist to your node type?
    – user72672
    Oct 20, 2018 at 7:47
  • Yes. My $formf field is uid, as I have checked dumping $form variable. I have updated my question with the code I use (following the tutorial)
    – briast
    Oct 20, 2018 at 8:58

1 Answer 1

2

It won't work because validateExposed function from core/modules/user/src/Plugin/views/filter/Name.php expect the values to be something like :

array:1 [▼
   0 => array:1 [▼
     "target_id" => "1"
   ]
 ]

So if you just modified the autocomplete to select you will send the value of uid like 1 for example:

Suggested solution

  • Add Validation function before validateExposed and refactor the value of input uid.
    Here is code snippet:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function my_module_form_views_exposed_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  // If not the view we are looking, move on
  if (stripos($form['#id'], 'views-exposed-form-my-form')) {
    return FALSE;
  }
  //- query
  $query = \Drupal::entityQuery('user');
  /**
   * @TODO: Add your conditions.
   */
  $options = [];
  //- get user ids
  $entity_ids = $query->execute();
  //- Get Users List
  $users_list = \Drupal::entityTypeManager()
    ->getStorage('user')
    ->loadMultiple($entity_ids);
 //- Prepare the list options.
  foreach ($users_list as $user) {
    $options[$user->id()] = $user->getAccountName();
  }
  if (isset($form['uid'])) {
    //- alter the Uid field.
    $form['uid'] = [
      '#type'         => 'select',
      '#options'      => $options,
      '#empty_option' => t('-- Select User --'),
      '#multiple'     => FALSE,
    ];
    unset($form['uid']['#size']);
    //- Add Validate in the beginning To be before validateExposed
    array_unshift($form['#validate'], 'validateUserExposedFilter');
  }
}

/** 
 * @param $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 */
function validateUserExposedFilter(&$form, FormStateInterface $form_state) {
//- Get the input value
  $input = $form_state->getValue('uid');
  //- Prepare value to be like the expected
  if ($input) {
    $value = [
      0 => [
        "target_id" => $input,
      ],
    ];
    //- Set the new value
    $form['uid']['#value'] = $value;
    //- Update the form state
    $form_state->setValue('uid', $value);
  }
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.