0

I have a code which works for using the title of a field as exposed filter select but it works only for the first time it is used afterwards (and because of ajax) it goes back to the state of field text instead of select.

Here is my code:

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

  $view = $form_state->getStorage('view');
  if (($view['view']->id() == 'paragraph_view_on_node')) {

    $node = \Drupal::routeMatch()->getParameter('node');
    $options = [];
    if ($node){
      if($node->getType() == 'library') {
        $result = $node->get('field_pdfs')->referencedEntities();
        $result[""]=t("");
        if (!empty($result)) {
          foreach ($result as $paragraph) {
            if ($paragraph instanceof Paragraph) {
              $value = $paragraph->field_pdf_title->value;
              $options[$value] = $value;
            }
          }
        }
      }
      unset ($form['field_pdf_title_value']);

      $form['field_pdf_title_value']['#type'] = 'select';
      $form['field_pdf_title_value']['#multiple'] = FALSE;
      $form['field_pdf_title_value']['#multiple'] = t('Title');
      $form['field_pdf_title_value']['#options'] = $options;
      unset($form['field_pdf_title_value']['#size']);
    }
  }
}

Your Answer

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

Browse other questions tagged or ask your own question.