I have a configuration form for a Condition plugin I want to have ajax in. When selecting from this field, I want to get a list of displays for that View:
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$views = $this->entityTypeManager->getStorage('view')->loadMultiple();
$options = [];
/** @var \Drupal\views\Views $view */
foreach ($views as $view) {
$options[$view->id()] = $view->label();
}
$form['#prefix'] = '<div id="views-settings">';
$form['#suffix'] = '</div>';
$view_id = $form_state->getValue('view_id');
if (empty($view_id)) {
$view_id = $form_state->getUserInput()["conditions"]["sfp_condition_view_not_empty"]["view_id"] ?? NULL;
}
if (empty($view_id)) {
$view_id = $this->configuration['view_id'] ?? NULL;
}
$displays = isset($view_id) ? $this->getViewsDisplays($view_id) : [];
$form['view_id'] = [
'#type' => 'select',
'#title' => $this->t('View'),
'#required' => TRUE,
'#options' => $options,
'#default_value' => $view_id ? $options[$view_id] : '',
'#ajax' => [
'wrapper' => 'views-settings',
'callback' => '::updateViewsDisplay',
'event' => 'change',
],
];
if (!empty($displays)) {
$form['view_display'] = [
'#type' => 'select',
'#title' => $this->t('View display'),
'#required' => TRUE,
'#options' => $displays,
'#default_value' => $displays[$this->configuration['view_display']] ?? $displays['default'],
];
}
return $form;
}
/**
* Trigger form rebuild.
*
* @param array $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @return array
*/
public function updateViewsDisplay(array $form, FormStateInterface $form_state) {
return $form;
}
Xdebug hits the method fine, but I get this AJAX error:
"The specified #ajax callback is empty or not callable."
and nothing happens after that.
I checked elsewhere where I have done this and the only difference I can see is the ones that worked are regular Drupal Form API forms, and this is a plugin form passed from buildConfigurationForm. Outside of that, I am not seeing the issue.