Created
September 20, 2016 05:56
-
-
Save anonymous/cb00a9b6c26429cb3c0146c3731fffe4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\example\Form; | |
use Drupal\Core\Form\FormBase; | |
use Drupal\Core\Form\FormStateInterface; | |
use Drupal\Core\Ajax\AjaxResponse; | |
use Drupal\Core\Ajax\HtmlCommand; | |
/** | |
* Class ExampleForm. | |
* | |
* @package Drupal\example\Form | |
*/ | |
class ExampleForm extends FormBase { | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getFormId() { | |
return 'example_form'; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildForm(array $form, FormStateInterface $form_state) { | |
$form['group1'] = [ | |
'#type' => 'container', | |
]; | |
$form['group2'] = [ | |
'#type' => 'container', | |
'#access' => TRUE, | |
'#prefix' => '<div id="test">', | |
'#suffix' => '</div>', | |
]; | |
$form['group2']['test'] = [ | |
'#type' => 'submit', | |
'#value' => t('Enable'), | |
'#ajax' => [ | |
'callback' => '::testCallback', | |
'wrapper' => 'test', | |
], | |
]; | |
$form['group2']['example'] = [ | |
'#type' => 'textfield', | |
'#access' => TRUE, | |
'#required' => TRUE, | |
]; | |
return $form; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function validateForm(array &$form, FormStateInterface $form_state) { | |
parent::validateForm($form, $form_state); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function submitForm(array &$form, FormStateInterface $form_state) { | |
// Display result. | |
foreach ($form_state->getValues() as $key => $value) { | |
drupal_set_message($key . ': ' . $value); | |
} | |
} | |
public function testCallback(array &$form, FormStateInterface $form_state) { | |
$form['group2']['example']['#access'] = TRUE; | |
unset($form['group2']['test']); | |
$form['group2']['form'] = [ | |
'#type' => 'container', | |
'#attributes' => [ | |
'id' => 'foo', | |
] | |
]; | |
$form['group2']['form']['yolo'] = [ | |
'#type' => 'textfield', | |
'#required' => TRUE, | |
'#description' => 'haha', | |
]; | |
// Does not work if defined here | |
$form['group2']['submitter'] = [ | |
'#type' => 'button', | |
'#value' => t('Enable me'), | |
'#attributes' => [ | |
'style' => 'display: none', | |
], | |
'#ajax' => [ | |
'wrapper' => 'test', | |
'effect' => 'fade', | |
'callback' => '::doSomething', | |
], | |
]; | |
return $form['group2']; | |
} | |
public function doSomething(array &$form, FormStateInterface $form_state) { | |
unset($form['group2']['submitter']); | |
unset($form['group2']['test']); | |
$form['group2']['blaba'] = [ | |
'#type' => 'html_tag', | |
'#tag' => 'p', | |
'#value' => 'yo', | |
]; | |
return $form['group2']; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment