Created
April 3, 2019 14:14
-
-
Save mattschaff/3bb5b471b437a14c58ee57df7d0b08aa to your computer and use it in GitHub Desktop.
Drupal 8: Set default value of checkbox field in views exposed form with AJAX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Drupal\Core\Form\FormStateInterface; | |
/** | |
* Implements hook_form_alter() | |
*/ | |
function my_module_form_alter(&$form, FormStateInterface $form_state, $form_id) { | |
switch ($form_id) { | |
case 'views_exposed_form': | |
// set a checkbox to true in the view exposed form, controlling for ajax | |
$path = \Drupal::service('path.current')->getPath(); | |
if ((!isset($_POST['_drupal_ajax']) || empty($path)) || | |
$_POST['view_path'] != '/views/ajax' | |
) { | |
$form['field_with_checkbox_value']['#value'] = 1; | |
} | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi. Could you give me a hint, please? I have a list of checkboxes and I need the program to set some of them as selected. I managed to add values to individual fields programmatically, but I can't manage the list of checkboxes. Sorry about my English. Thanks.