How th Add and/or Remove Fields Dynamically using Form API (FAPI) and Ajax on Drupal 8?
Example: Add/Remove 'test' Field depending on 'switch' field
1. on build()
$form['test'] = [
'#type' => 'container',
'#attributes' => ['id' => 'edit-fields-test'],
];
if ($form_state->getValue('switch') == 2) {
$form['test'][0] = $this->getReturnTripFields($form, $form_state);
$form['test'][0]['#prefix'] = '<div>';
$form['test'][0]['#suffix'] = '</div>';
}
$form['switch'] = [
'#type' => 'radios',
'#title' => 'Trip Type',
'#options' => [1 => 'NO', 2 => 'YES'],
'#ajax' => array(
'callback' => '::returnAjax',
'wrapper' => 'edit-fields-test',
'method' => 'replace',
),
];
2. AjaxMethode
function returnAjax($form, FormStateInterface $form_state) {
$response = new AjaxResponse();
if ($form_state->getValue('switch') == 2) {
$content = [ '#markup' => ' YES ', ];
}
else {
$content = [ '#markup' => ' ', ];
}
$response->addCommand(new HtmlCommand('#edit-fields-test', $content));
return $response;
}
Comments
followers canada
Web platform. Size. 80 MB (uncompressed Drupal 8 core) Type. Content management framework, content management system, blog software. by https://epicfollowers.ca/"
epicfollowers canada
Drupal is an open source website development platform. It simplifies the online management of content and users, and it runs millions of sites— ... by https://buyfollowerscanada.com
Visit Here
https://www.pinterest.com/pin/824158800551568295
https://www.pinterest.com/pin/824158800551568359
https://www.pinterest.com/pin/824158800551568454
followers canada
(to Drupal 8). As developers, we sometimes forget that we live in a tech bubble. In our highly technical world we assume things that not everyone ... by https://epicfollowers.ca/"
followers canada
Pantheon's workflow—combined with Multidev and Drupal 8's configuration management—is the modern way to develop. Just getting started? Rest easy—we've ... by https://epicfollowers.ca/
Add new comment