From my experience, I believe Drupal8 treat Form API in BlockBase differently from FormBase Class, the below block doesn't seem to work. Do I have to do anything else to make Ajax Form work in Block? Please suggest. Maybe I need to #attach something? or do I have to do it non Drupal way by directly injecting jQuery into the code?
class SearchBlock extends BlockBase
{
/**
* {@inheritdoc}
*/
public function build()
{
$build = array();
$build['form'] = array(
'#e' => 'form',
'#method' => 'get'
);
$build['form']['q'] = array(
'#e' => 'textfield',
'#size' => 50,
'#name' => 'q',
'#ajax' => array(
'callback' => '::ajaxSearchSuggest',
'wrapper' => 'search-suggest',
'event' => 'keypress'
),
'#default_value' => $_GET['q'],
'#suffix' => format_string('<div style="search-suggest"></div>'),
);
$build['form']['submit'] = array(
'#e' => 'button',
'#attributes' => array('onclick' => 'return false;'),
'#ajax' => array(
'callback' => '::ajaxSearchSuggest',
'wrapper' => 'search-suggest',
'event' => 'click'
)
);
return $build;
}
public function ajaxSearchSuggest($form, FormStateInterface $form_state)
{
return array('#markup' => 'TEST');
}
}