Skip to content

Instantly share code, notes, and snippets.

@mattschaff
Created April 3, 2019 14:14
Show Gist options
  • Save mattschaff/3bb5b471b437a14c58ee57df7d0b08aa to your computer and use it in GitHub Desktop.
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
<?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;
}
}
@Prosolmir
Copy link

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.

checkboxes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment