0

I have the following D8 form

  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['bulk_perfplans_roles'] = array(
      '#type'          => 'checkboxes',
      '#title'         => $this->t('Generate plans for which roles?'),
      '#options'       => user_role_names(),
    );
    $form['bulk_add'] = array(
      '#type' => 'submit',
      '#value' => $this->t('Create new Performance Plans'),
    );

    return $form;
  }

I'm trying to pass the information into an entityQuery as follows...

  public function submitForm(array &$form, FormStateInterface $form_state) {
    $selectedRole = $form_state->get('bulk_perfplans_roles');
    $uids = \Drupal::entityQuery('user')
      ->condition('roles', $selectedRole)
      ->sort('created', 'ASC')
      ->execute();

I'm trying to make it so it conditionally alters the query based on the user selected roles from $form_state. It's currently not working, and I'm not sure where to go next. Any thoughts?

2

Does $selectesRole have the expected value? I'm pretty sure you want to use $form_state->getValue('bulk_perfplans_roles') instead. Details here.

Also is it a single value field? You may need to pass an additional IN parameter to your field condition and do some additional prep if the result from getValue is an array.

Also make sure you're passing the machine name of the role and not the label.

1

As @sonfd said you submit function will be like:

public function submitForm(array &$form, FormStateInterface $form_state) {
    $selectedRole = $form_state->getValue('bulk_perfplans_roles');
    $uids = \Drupal::entityQuery('user')
      ->condition('roles', $selectedRole,'IN')
      ->sort('created', 'ASC')
      ->execute();
}

After submitting your roles look like:

enter image description here

And UID's will be like:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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