Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
"Add more" AJAX button in Drupal 8.
{
"name": "drupal/test1",
"type": "drupal-module",
"description": "Test 01",
"keywords": ["Drupal"],
"license": "GPL-2.0+",
"homepage": "https://www.drupal.org/project/test1",
"minimum-stability": "dev",
"support": {
"issues": "https://www.drupal.org/project/issues/test1",
"source": "http://cgit.drupalcode.org/test1"
},
"require": { }
}
<?php
namespace Drupal\test1\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Class DefaultForm.
*
* @package Drupal\test1\Form
*/
class DefaultForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'default_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
if (!$form_state->get('number_of_fields')) {
$form_state->set('number_of_fields', 1);
}
$form['#tree'] = TRUE;
$form['container'] = [
'#type' => 'container',
'#id' => 'replace-me',
];
for ($i = 0; $i < $form_state->get('number_of_fields'); $i++) {
$form['container'][$i] = [
'my_text' => [
'#type' => 'textfield',
'#title' => $this->t('Any text'),
],
];
}
$form['add'] = [
'#type' => 'submit',
'#value' => t('Add another'),
'#submit' => [[$this, 'addTextFieldCallback']],
'#ajax' => [
'callback' => [$this, 'ajaxReplaceMeCallback'],
'wrapper' => 'replace-me',
],
];
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Submit'),
];
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) {
// Do something with the text fields.
drupal_set_message("Submitted");
}
public function addTextFieldCallback(array &$form, FormStateInterface $form_state) {
$form_state->set('number_of_fields', $form_state->get('number_of_fields') + 1);
$form_state->setRebuild();
}
public function ajaxReplaceMeCallback(array &$form, FormStateInterface $form_state) {
return $form['container'];
}
}
<?php
namespace Drupal\test1\Tests;
use Drupal\Core\Url;
use Drupal\simpletest\WebTestBase;
/**
* Simple test to ensure that main page loads with module enabled.
*
* @group test1
*/
class LoadTest extends WebTestBase{
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['test1'];
/**
* A user with permission to administer site configuration.
*
* @var \Drupal\user\UserInterface
*/
protected $user;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->user = $this->drupalCreateUser(['administer site configuration']);
$this->drupalLogin($this->user);
}
/**
* Tests that the home page loads with a 200 response.
*/
public function testLoad() {
$this->drupalGet(Url::fromRoute('<front>'));
$this->assertResponse(200);
}
}
<!-- Add you custom twig html here -->
name: test1
type: module
description: Test 01
core: 8.x
package: Custom
<?php
/**
* @file
* Contains test1.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function test1_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the test1 module.
case 'help.page.test1':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Test 01') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function test1_theme() {
return [
'test1' => [
'template' => 'test1',
'render element' => 'children',
],
];
}
test1.default_form:
path: '/test1/form/default'
defaults:
_form: '\Drupal\test1\Form\DefaultForm'
_title: 'DefaultForm'
requirements:
_access: 'TRUE'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.