In this snippet, located under src/Form/DocumentCreationForm.php
, I create an AJAX callback that adds more section
elements to the form when the add section
button is clicked.
Now I'm trying to read those values in submitForm
or validateForm
.
I read somewhere (sorry, I can't remember where) that it was impossible to submit AJAX-added form elements via submit Form, so I'm thinking I might have to intercept the submit button, add a Controller and submit the form to the controller via javascript when the submit button is pushed.
Does anyone know of a better way?
<?php
namespace Drupal\mc_documents\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\cloud\CloudAdapter;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\AppendCommand;
use Drupal\Core\Url;
class DocumentCreationForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId(){
return 'mc_documents_DocumentCreationForm';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $param1=NULL){
// $form['attached']['library'][] = 'mc_documents/mc_documents';
// ... populate the form here. see https://api.drupal.org/api/drupal/elements for options
$form_state->set('num_sections', 0);
$form['document_name'] = [
'#weight' => 0,
'#title' => t('Document Name'),
'#type' => 'textfield',
'#attributes' => [
'id' => 'document_name'
]
];
$cloud = new CloudAdapter();
$document_types = $cloud->query(
"select * from MC_Selection_V where Category = 'MC_Documents_Document_Type';",
[]
);
$formatted_document_types = [];
$len = count($document_types);
for($i = 0; $i < $len; $i++){
$formatted_document_types[$document_types[$i]['Value']] = $document_types[$i]['Caption'];
}
$form['document_type'] = [
'#weight' => 1,
'#title' => t('Document Type'),
'#type' => 'select',
'#options' => $formatted_document_types
];
$form['sections'] = [
'#weight' => 2,
'#type' => 'container',
'#title' => t('Sections'),
'#attributes' => [
'id' => ['sections']
]
];
$form['add_section'] = [
'#weight' => 3,
'#value' => t('Add Section'),
'#type' => 'button',
'#ajax' => [
'callback' => '::addSection',
'event' => 'click',
'wrapper' => 'sections'
],
];
$form['submit'] = [
'#weight' => 4,
'#type' => 'submit',
'#value' => t('Submit'),
'#attributes' => [
// 'onclick' => 'function(){ submit_document(); return false; }'
],
];
return $form;
}
public function addSection(array &$form, FormStateInterface $form_state){
$form_state->setValue('num_sections', $form_state->getValue('num_sections') + 1);
$cloud = new CloudAdapter();
$field_types = $cloud->query(
"select * from MC_Selection_V where Category = 'MC_Documents_Field_Type';",
[]
);
$formatted_field_types = [];
$len = count($field_types);
for($i = 0; $i < $len; $i++){
$field_type = $field_types[$i];
$formatted_field_types[$field_type['Value']] = $field_type['Caption'];
}
$elems = [
[
'#type' => 'html_tag',
'#tag' => 'hr'
],
[
'#type' => 'select',
'#title' => t('Field Type'),
'#options' => $formatted_field_types,
'#attributes' => [
'class' => ['field-type']
]
],
[
'#type' => 'textarea',
'#title' => t('Section'),
'#placeholder' => 'Enter a placeholder here...',
'#attributes' => [
'class' => 'section-markup'
],
'#value' => ''
]
];
$renderer = \Drupal::service('renderer');
$response = new AjaxResponse();
$response->addCommand(new AppendCommand('#sections', $renderer->render($elems)));
return $response;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state){
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state){
drupal_set_message($form_state->getValues('sections'));
// insert document
// insert document field
// insert document field data
}
}
?>