abstract class YamlFormHandlerFormBase in YAML Form 8
Provides a base form for form handlers.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\yamlform\Form\YamlFormHandlerFormBase uses YamlFormDialogTrait
Expanded class hierarchy of YamlFormHandlerFormBase
File
- src/
Form/ YamlFormHandlerFormBase.php, line 17
Namespace
Drupal\yamlform\FormView source
abstract class YamlFormHandlerFormBase extends FormBase {
use YamlFormDialogTrait;
/**
* The form.
*
* @var \Drupal\yamlform\YamlFormInterface
*/
protected $yamlform;
/**
* The form handler.
*
* @var \Drupal\yamlform\YamlFormHandlerInterface
*/
protected $yamlformHandler;
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'yamlform_handler_form';
}
/**
* {@inheritdoc}
*
* @param \Drupal\yamlform\YamlFormInterface $yamlform
* The form.
* @param string $yamlform_handler
* The form handler ID.
*
* @return array
* The form structure.
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
* Throws not found exception if the number of handler instances for this
* form exceeds the handler's cardinality.
*/
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}'.");
}
// Limit the number of plugin instanced allowed.
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(),
// Disable broken plugins.
'#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);
// Get $form['settings']['#attributes']['novalidate'] and apply it to the
// $form.
// This allows handlers with hide/show logic to skip HTML5 validation.
// @see http://stackoverflow.com/questions/22148080/an-invalid-form-control-with-name-is-not-focusable
if (isset($form['settings']['#attributes']['novalidate'])) {
$form['#attributes']['novalidate'] = 'novalidate';
}
$form['settings']['#tree'] = TRUE;
// Check the URL for a weight, then the form handler,
// otherwise use default.
$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;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// The form handler configuration is stored in the 'settings' key in
// the form, pass that through for validation.
$settings = $form_state
->getValue('settings') ?: [];
$handler_state = (new FormState())
->setValues($settings);
$this->yamlformHandler
->validateConfigurationForm($form, $handler_state);
// Process handler state form errors.
$this
->processHandlerFormErrors($handler_state, $form_state);
// Update the original form values.
$form_state
->setValue('settings', $handler_state
->getValues());
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($response = $this
->validateDialog($form, $form_state)) {
return $response;
}
$form_state
->cleanValues();
// The form handler configuration is stored in the 'settings' key in
// the form, pass that through for submission.
$handler_data = (new FormState())
->setValues($form_state
->getValue('settings'));
$this->yamlformHandler
->submitConfigurationForm($form, $handler_data);
// Update the original form values.
$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();
// Display status message.
drupal_set_message($this
->t('The form handler was successfully applied.'));
// Redirect.
return $this
->redirectForm($form, $form_state, $this->yamlform
->toUrl('handlers-form'));
}
/**
* Generates a unique machine name for a form handler instance.
*
* @param \Drupal\yamlform\YamlFormHandlerInterface $handler
* The form handler.
*
* @return string
* Returns the unique name.
*/
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++;
}
// Only return a suggestion if it is not the default plugin id.
return $machine_default != $handler
->getPluginId() ? $machine_default : '';
}
/**
* Determines if the form handler ID already exists.
*
* @param string $handler_id
* The form handler ID.
*
* @return bool
* TRUE if the form handler ID exists, FALSE otherwise.
*/
public function exists($handler_id) {
$instance_ids = $this->yamlform
->getHandlers()
->getInstanceIds();
return isset($instance_ids[$handler_id]) ? TRUE : FALSE;
}
/**
* Process handler form errors in form.
*
* @param FormStateInterface $handler_state
* The form handler form state.
* @param FormStateInterface &$form_state
* The form state.
*/
protected function processHandlerFormErrors(FormStateInterface $handler_state, FormStateInterface &$form_state) {
foreach ($handler_state
->getErrors() as $name => $message) {
$form_state
->setErrorByName($name, $message);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
87 |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. | |
YamlFormDialogTrait:: |
protected | function | Add modal dialog support to a form. | |
YamlFormDialogTrait:: |
protected | function | Is the current request for an AJAX modal dialog. | |
YamlFormDialogTrait:: |
protected | function | Handler dialog redirect after form is submitted. | |
YamlFormDialogTrait:: |
protected | function | Display validation error messages in modal dialog. | |
YamlFormHandlerFormBase:: |
protected | property | The form. | |
YamlFormHandlerFormBase:: |
protected | property | The form handler. | |
YamlFormHandlerFormBase:: |
public | function |
Overrides FormInterface:: |
2 |
YamlFormHandlerFormBase:: |
public | function | Determines if the form handler ID already exists. | |
YamlFormHandlerFormBase:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
YamlFormHandlerFormBase:: |
public | function | Generates a unique machine name for a form handler instance. | |
YamlFormHandlerFormBase:: |
protected | function | Process handler form errors in form. | |
YamlFormHandlerFormBase:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
YamlFormHandlerFormBase:: |
public | function |
Form validation handler. Overrides FormBase:: |