Skip to content
Snippets Groups Projects
Commit 7bd9556a authored by Harumi Jang's avatar Harumi Jang
Browse files

Add ajax callback

parent 9f77f167
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,8 @@
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\SortArray;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
......@@ -14,6 +16,7 @@
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TempStore\PrivateTempStore;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field_ui\Controller\FieldConfigAddController;
use Drupal\field_ui\FieldUI;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -177,6 +180,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $entity_t
'class' => 'add-field-container',
],
];
$field_type_options_radios = [];
foreach ($field_type_options as $id => $field_type) {
/** @var \Drupal\Core\Field\FieldTypeCategoryInterface $category_info */
......@@ -345,12 +349,19 @@ public function buildForm(array $form, FormStateInterface $form_state, $entity_t
'#type' => 'submit',
'#value' => $this->t('Continue'),
'#button_type' => 'primary',
'#attributes' => [
'class' => ['use-ajax'],
],
'#ajax' => [
'callback' => [$this, 'ajaxSubmitForm'],
],
];
$form['#attached']['library'] = [
'field_ui/drupal.field_ui',
'field_ui/drupal.field_ui.manage_fields',
'core/drupal.ajax',
'core/drupal.dialog.ajax',
];
return $form;
}
......@@ -403,6 +414,25 @@ protected function validateAddNew(array $form, FormStateInterface $form_state) {
}
/**
* Callback function to open field instance form in a modal.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function ajaxSubmitForm(array &$form, FormStateInterface $form_state) {
$field_name = $form_state->getValue('field_name');
$field_config_controller = new FieldConfigAddController($this->tempStore);
$instance_form = $field_config_controller->fieldConfigAddConfigureForm($this->entityTypeId, $field_name);
$response = new AjaxResponse();
$response->addCommand(new OpenModalDialogCommand($this->t('Configure field'), $instance_form, [
'width' => '85vw',
]));
return $response;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment