View source
<?php
namespace Drupal\hubspot_forms\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\filter\Entity\FilterFormat;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\editor\Ajax\EditorDialogSave;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\hubspot_forms\HubspotFormsCore;
class HubspotDialog extends FormBase implements ContainerInjectionInterface {
public function getFormId() {
return 'hubspot_forms_dialog';
}
public function buildForm(array $form, FormStateInterface $form_state, FilterFormat $filter_format = NULL) {
$user_input = $form_state
->getUserInput();
$input = isset($user_input['editor_object']) ? $user_input['editor_object'] : [];
$form['#tree'] = TRUE;
$form['#attached']['library'][] = 'editor/drupal.editor.dialog';
$form['#prefix'] = '<div id="hubspot-forms-dialog-form">';
$form['#suffix'] = '</div>';
$HubspotFormsCore = new HubspotFormsCore();
$form['formid'] = [
'#type' => 'select',
'#title' => $this
->t('Hubspot Form'),
'#description' => $this
->t('Please choose a form you would like to display.'),
'#options' => $HubspotFormsCore
->getFormIds(),
'#required' => TRUE,
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['save_modal'] = [
'#type' => 'submit',
'#value' => $this
->t('Insert'),
'#submit' => [],
'#ajax' => [
'callback' => '::submitForm',
'event' => 'click',
],
];
$form['attributes']['code'] = [
'#title' => $this
->t('Hubspot Forms'),
'#type' => 'textfield',
'#prefix' => '<div class="visually-hidden">',
'#suffix' => '</div>',
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$form_empty = TRUE;
$shortcode = '[hubspot-form:';
if ($form_state
->getValue('formid')) {
list($portal_id, $form_id) = explode("::", $form_state
->getValue('formid'));
$shortcode .= $form_id;
$form_empty = FALSE;
}
if (isset($portal_id)) {
$shortcode .= ' portal_id:' . $portal_id;
}
$shortcode .= ']';
if (!empty($shortcode) && !$form_empty) {
$form_state
->setValue([
'attributes',
'code',
], $shortcode);
}
if ($form_state
->getErrors()) {
unset($form['#prefix'], $form['#suffix']);
$form['status_messages'] = [
'#type' => 'status_messages',
'#weight' => -10,
];
$response
->addCommand(new HtmlCommand('#hubspot-forms-dialog-form', $form));
}
else {
$response
->addCommand(new EditorDialogSave($form_state
->getValues()));
$response
->addCommand(new CloseModalDialogCommand());
}
return $response;
}
}