How can I give Ajax access to currently set fields on my form? I'm working on a block form that needs to update certain elements in the form based on what the user selects with certain elements. I've defined the internal class method and it's getting called correctly; however, it can't access the values stored in the form from the $form_state.

For example, when I call $form_state-&gt;getValue('filter_field'), it returns an empty value, even though it has been set on the page. I suspect that this may be due to the fact that I'm setting the callback like so: 'callback' =&gt; [get_class($this), 'updateWidgetAjax'], and in the final comment in the API, it says to do it like this: 'callback' =&gt; '::updateWidgetAjax'. However, when I try this, I get the The specified #ajax callback is empty or not callable. error. I tried following the tutorial on the API page as well as here, yet neither of them worked properly, spouting out the same error as before. I also went through this checklist, but most of the items don't apply.

Additionally, and this is just something annoying but related, if I don't mark updateWidgetAjax as static I get the following message in my log: Deprecated function: call_user_func_array() expects parameter 1 to be a valid callback, non-static method Drupal\global_filter\Plugin\Block\FilterBlock::updateWidgetAjax() should not be called statically, which I suspect will be resolved with fixing the above issue.

In short, how do I get the form values in my Ajax callback?



The element calling the Ajax:

$form['filter_field'] = [
  '#type' =&gt; 'select',
  '#name' =&gt; 'filter_field',
  '#title' =&gt; t('Select field to get the filter value from.'),
  '#options' =&gt; self::getAvailableFields(),
  '#empty_option' =&gt; t('Select a field'),
  '#ajax' =&gt; [
    'callback' =&gt; [get_class($this), 'updateWidgetAjax'],
    'event' =&gt; 'change',
    'wrapper' =&gt; 'default-value-ajax-wrapper',
    //The wrapper is elsewhere on the form
  ]
  '#required' =&gt; TRUE,
];


The Ajax callback function:

public function updateWidgetAjax(array &amp;$form, FormStateInterface $form_state) {
  $widget = '';
  $multichoice = FALSE;

  switch ($form_state-&gt;getValue('widget_type')) {
    //code that sets $widget
  }
  //Return a render array
  $render = [
    '#type' =&gt; $widget,
    '#name' =&gt; 'default_value',
    '#title' =&gt; t('Default value'),
    '#description' =&gt; t('Select the default value for this filter. Leave blank for none.'),
    '#options' =&gt; self::getOptions($widget, $form_state-&gt;getValue('filter_field')),
    '#required' =&gt; TRUE,
  ];
  return $render;
}


EDIT: I forgot to mention that the form is defined with a blockForm, since I'm trying to create new blocks with this. I'm following the Drupal documentation found here. I think this might be a big part of the issue.
I have tried this on my local it works . So you can achieve the form values by using this . Please try this 

    $form['filter_field'] = [
      '#title' =&gt; $this-&gt;t('Filter'),
      '#type' =&gt; 'select',
      '#options' =&gt; ['drupal7' =&gt; 'Drupal7', 'drupal8' =&gt; 'Drupal8'],
      '#empty_option' =&gt; $this-&gt;t('-select'),
      '#ajax' =&gt; [
        'callback' =&gt; '::updateWidgetAjax',
        'wrapper' =&gt; 'default-value-ajax-wrapper',
      ],
    ];
    $form['widget_type'] = [
      '#title' =&gt; $this-&gt;t('Widget Type'),
      '#type' =&gt; 'select',
      '#options' =&gt; ['pdf' =&gt; 'Pdf', 'ppt' =&gt; 'PPT'],
      '#empty_option' =&gt; $this-&gt;t('-select'),
    ];

    $form['wrapper'] = [
      '#type' =&gt; 'container',
      '#attributes' =&gt; ['id' =&gt; 'default-value-ajax-wrapper'],
    ];

    return $form;


//Ajax callback 
  public function updateWidgetAjax(array &amp;$form, FormStateInterface $form_state) {
   $widget = $form_state-&gt;getValue('widget_type');
    print_r($widget); //Print the value of the field
  }