View source
<?php
namespace Drupal\yamlform\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\yamlform\YamlFormDialogTrait;
use Drupal\yamlform\YamlFormHandlerInterface;
use Drupal\yamlform\YamlFormInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
abstract class YamlFormHandlerFormBase extends FormBase {
use YamlFormDialogTrait;
protected $yamlform;
protected $yamlformHandler;
public function getFormId() {
return 'yamlform_handler_form';
}
public function buildForm(array $form, FormStateInterface $form_state, YamlFormInterface $yamlform = NULL, $yamlform_handler = NULL) {
$this->yamlform = $yamlform;
try {
$this->yamlformHandler = $this
->prepareYamlFormHandler($yamlform_handler);
} catch (PluginNotFoundException $e) {
throw new NotFoundHttpException("Invalid handler id: '{$yamlform_handler}'.");
}
if (!$this->yamlformHandler
->getHandlerId()) {
$plugin_id = $this->yamlformHandler
->getPluginId();
$cardinality = $this->yamlformHandler
->cardinality();
$number_of_instances = $yamlform
->getHandlers($plugin_id)
->count();
if ($cardinality !== YamlFormHandlerInterface::CARDINALITY_UNLIMITED && $cardinality <= $number_of_instances) {
$t_args = [
'@number' => $cardinality,
'@instances' => $this
->formatPlural($cardinality, $this
->t('instance is'), $this
->t('instances are')),
];
throw new NotFoundHttpException($this
->t('Only @number @instance permitted', $t_args));
}
}
$request = $this
->getRequest();
$form['description'] = [
'#markup' => $this->yamlformHandler
->description(),
'#prefix' => '<p>',
'#suffix' => '</p>',
];
$form['id'] = [
'#type' => 'value',
'#value' => $this->yamlformHandler
->getPluginId(),
];
$form['status'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable the %name handler.', [
'%name' => $this->yamlformHandler
->label(),
]),
'#default_value' => $this->yamlformHandler
->isEnabled(),
'#disabled' => $this->yamlformHandler
->getPluginId() == 'broken',
];
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Title'),
'#maxlength' => 255,
'#default_value' => $this->yamlformHandler
->label(),
'#required' => TRUE,
'#attributes' => [
'autofocus' => 'autofocus',
],
];
$form['handler_id'] = [
'#type' => 'machine_name',
'#maxlength' => 64,
'#description' => $this
->t('A unique name for this handler instance. Must be alpha-numeric and underscore separated.'),
'#default_value' => $this->yamlformHandler
->getHandlerId() ?: $this
->getUniqueMachineName($this->yamlformHandler),
'#required' => TRUE,
'#disabled' => $this->yamlformHandler
->getHandlerId() ? TRUE : FALSE,
'#machine_name' => [
'exists' => [
$this,
'exists',
],
],
];
$form['settings'] = $this->yamlformHandler
->buildConfigurationForm([], $form_state);
if (isset($form['settings']['#attributes']['novalidate'])) {
$form['#attributes']['novalidate'] = 'novalidate';
}
$form['settings']['#tree'] = TRUE;
$form['weight'] = [
'#type' => 'hidden',
'#value' => $request->query
->has('weight') ? (int) $request->query
->get('weight') : $this->yamlformHandler
->getWeight(),
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
'#button_type' => 'primary',
];
$form = $this
->buildDialog($form, $form_state);
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$settings = $form_state
->getValue('settings') ?: [];
$handler_state = (new FormState())
->setValues($settings);
$this->yamlformHandler
->validateConfigurationForm($form, $handler_state);
$this
->processHandlerFormErrors($handler_state, $form_state);
$form_state
->setValue('settings', $handler_state
->getValues());
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($response = $this
->validateDialog($form, $form_state)) {
return $response;
}
$form_state
->cleanValues();
$handler_data = (new FormState())
->setValues($form_state
->getValue('settings'));
$this->yamlformHandler
->submitConfigurationForm($form, $handler_data);
$form_state
->setValue('settings', $handler_data
->getValues());
$is_new = $this->yamlformHandler
->getHandlerId() ? FALSE : TRUE;
$this->yamlformHandler
->setHandlerId($form_state
->getValue('handler_id'));
$this->yamlformHandler
->setLabel($form_state
->getValue('label'));
$this->yamlformHandler
->setStatus($form_state
->getValue('status'));
$this->yamlformHandler
->setWeight($form_state
->getValue('weight'));
if ($is_new) {
$this->yamlform
->addYamlFormHandler($this->yamlformHandler
->getConfiguration());
}
$this->yamlform
->save();
drupal_set_message($this
->t('The form handler was successfully applied.'));
return $this
->redirectForm($form, $form_state, $this->yamlform
->toUrl('handlers-form'));
}
public function getUniqueMachineName(YamlFormHandlerInterface $handler) {
$suggestion = $handler
->getPluginId();
$count = 1;
$machine_default = $suggestion;
$instance_ids = $this->yamlform
->getHandlers()
->getInstanceIds();
while (isset($instance_ids[$machine_default])) {
$machine_default = $suggestion . '_' . $count++;
}
return $machine_default != $handler
->getPluginId() ? $machine_default : '';
}
public function exists($handler_id) {
$instance_ids = $this->yamlform
->getHandlers()
->getInstanceIds();
return isset($instance_ids[$handler_id]) ? TRUE : FALSE;
}
protected function processHandlerFormErrors(FormStateInterface $handler_state, FormStateInterface &$form_state) {
foreach ($handler_state
->getErrors() as $name => $message) {
$form_state
->setErrorByName($name, $message);
}
}
}