abstract class YamlFormUiElementFormBase in YAML Form 8
Provides a base class for form element forms.
The basic workflow for handling form elements.
- Read the element.
- Build element's properties form.
- Set the property values.
- Alter the element's properties form.
- Process the element's properties form.
- Validate the element's properties form.
- Submit the element's properties form.
- Get property values from the form state's values.
- Remove default properties from the element's properties.
- Update element properties.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\yamlform_ui\Form\YamlFormUiElementFormBase implements YamlFormUiElementFormInterface uses YamlFormDialogTrait
Expanded class hierarchy of YamlFormUiElementFormBase
File
- modules/
yamlform_ui/ src/ Form/ YamlFormUiElementFormBase.php, line 32
Namespace
Drupal\yamlform_ui\FormView source
abstract class YamlFormUiElementFormBase extends FormBase implements YamlFormUiElementFormInterface {
use YamlFormDialogTrait;
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* Form element manager.
*
* @var \Drupal\yamlform\YamlFormElementManagerInterface
*/
protected $elementManager;
/**
* Form element validator.
*
* @var \Drupal\yamlform\YamlFormEntityElementsValidator
*/
protected $elementsValidator;
/**
* The form.
*
* @var \Drupal\yamlform\YamlFormInterface
*/
protected $yamlform;
/**
* The form element.
*
* @var array
*/
protected $element = [];
/**
* The form element's original element type.
*
* @var string
*/
protected $originalType;
/**
* The action of the current form.
*
* @var string
*/
protected $action;
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'yamlform_ui_element_form';
}
/**
* Constructs a new YamlFormUiElementFormBase.
*
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer.
* @param \Drupal\yamlform\YamlFormElementManagerInterface $element_manager
* The form element manager.
* @param \Drupal\yamlform\YamlFormEntityElementsValidator $elements_validator
* Form element validator.
*/
public function __construct(RendererInterface $renderer, YamlFormElementManagerInterface $element_manager, YamlFormEntityElementsValidator $elements_validator) {
$this->renderer = $renderer;
$this->elementManager = $element_manager;
$this->elementsValidator = $elements_validator;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('renderer'), $container
->get('plugin.manager.yamlform.element'), $container
->get('yamlform.elements_validator'));
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, YamlFormInterface $yamlform = NULL, $key = NULL, $parent_key = '') {
$this->yamlform = $yamlform;
$yamlform_element = $this
->getYamlFormElement();
$form['properties'] = $yamlform_element
->buildConfigurationForm([], $form_state);
// Move messages to the top of the form.
if (isset($form['properties']['messages'])) {
$form['messages'] = $form['properties']['messages'];
$form['messages']['#weight'] = -100;
unset($form['properties']['messages']);
}
// Set parent key.
$form['parent_key'] = [
'#type' => 'value',
'#value' => $parent_key,
];
// Set element type.
$form['properties']['element']['type'] = [
'#type' => 'item',
'#title' => $this
->t('Type'),
'label' => [
'#markup' => $yamlform_element
->getPluginLabel(),
],
'#weight' => -100,
'#parents' => [
'type',
],
];
// Set change element type.
if ($key && $yamlform_element
->getRelatedTypes($this->element)) {
$route_parameters = [
'yamlform' => $yamlform
->id(),
'key' => $key,
];
if ($this->originalType) {
$original_yamlform_element = $this->elementManager
->createInstance($this->originalType);
$route_parameters = [
'yamlform' => $yamlform
->id(),
'key' => $key,
];
$form['properties']['element']['type']['cancel'] = [
'#type' => 'link',
'#title' => $this
->t('Cancel'),
'#url' => new Url('entity.yamlform_ui.element.edit_form', $route_parameters),
'#attributes' => YamlFormDialogHelper::getModalDialogAttributes(800, [
'button',
'button--small',
]),
];
$form['properties']['element']['type']['#description'] = '(' . $this
->t('Changing from %type', [
'%type' => $original_yamlform_element
->getPluginLabel(),
]) . ')';
}
else {
$form['properties']['element']['type']['change_type'] = [
'#type' => 'link',
'#title' => $this
->t('Change'),
'#url' => new Url('entity.yamlform_ui.change_element', $route_parameters),
'#attributes' => YamlFormDialogHelper::getModalDialogAttributes(800, [
'button',
'button--small',
]),
];
}
}
// Set element key reserved word warning message.
if (!$key) {
$reserved_keys = [
'form_build_id',
'form_token',
'form_id',
'data',
'op',
];
$reserved_keys = array_merge($reserved_keys, array_keys(\Drupal::service('entity_field.manager')
->getBaseFieldDefinitions('yamlform_submission')));
$form['#attached']['drupalSettings']['yamlform_ui']['reserved_keys'] = $reserved_keys;
$form['#attached']['library'][] = 'yamlform_ui/yamlform_ui.element';
$form['properties']['element']['key_warning'] = [
'#type' => 'yamlform_message',
'#message_type' => 'warning',
'#message_message' => $this
->t("Please avoid using the reserved word '@key' as the element's key."),
'#weight' => -99,
'#attributes' => [
'style' => 'display:none',
],
];
}
// Set element key.
$form['properties']['element']['key'] = [
'#type' => 'machine_name',
'#title' => $this
->t('Key'),
'#machine_name' => [
'label' => $this
->t('Key'),
'exists' => [
$this,
'exists',
],
'source' => [
'title',
],
],
'#required' => TRUE,
'#parents' => [
'key',
],
'#disabled' => $key ? TRUE : FALSE,
'#default_value' => $key,
'#weight' => -98,
];
// Remove the key's help text (aka description) once it has been set.
if ($key) {
$form['properties']['element']['key']['#description'] = NULL;
}
// Use title for key (machine_name).
if (isset($form['properties']['element']['title'])) {
$form['properties']['element']['key']['#machine_name']['source'] = [
'properties',
'element',
'title',
];
$form['properties']['element']['title']['#id'] = 'title';
}
// Set flex.
// Hide #flex property if parent element is not a 'yamlform_flexbox'.
if (isset($form['properties']['flex']) && !$this
->isParentElementFlexbox($key, $parent_key)) {
$form['properties']['flex']['#access'] = FALSE;
}
// Set actions.
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
'#button_type' => 'primary',
'#_validate_form' => TRUE,
];
$form = $this
->buildDialog($form, $form_state);
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// Only validate the submit button.
$button = $form_state
->getTriggeringElement();
if (empty($button['#_validate_form'])) {
return;
}
// The form element configuration is stored in the 'properties' key in
// the form, pass that through for validation.
$element_form_state = clone $form_state;
$element_form_state
->setValues($form_state
->getValue('properties'));
// Validate configuration form.
$yamlform_element = $this
->getYamlFormElement();
$yamlform_element
->validateConfigurationForm($form, $element_form_state);
// Get errors for element validation.
$element_errors = $element_form_state
->getErrors();
foreach ($element_errors as $element_error) {
$form_state
->setErrorByName(NULL, $element_error);
}
// Stop validation is the element properties has any errors.
if ($form_state
->hasAnyErrors()) {
return;
}
// Set element properties.
$properties = $yamlform_element
->getConfigurationFormProperties($form, $element_form_state);
$parent_key = $form_state
->getValue('parent_key');
$key = $form_state
->getValue('key');
if ($key) {
$this->yamlform
->setElementProperties($key, $properties, $parent_key);
// Validate elements.
if ($messages = $this->elementsValidator
->validate($this->yamlform)) {
$t_args = [
':href' => Url::fromRoute('entity.yamlform.source_form', [
'yamlform' => $this->yamlform
->id(),
])
->toString(),
];
$form_state
->setErrorByName('elements', $this
->t('There has been error validating the elements. You may need to edit the <a href=":href">YAML source</a> to resolve the issue.', $t_args));
foreach ($messages as $message) {
drupal_set_message($message, 'error');
}
}
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$yamlform_element = $this
->getYamlFormElement();
if ($response = $this
->validateDialog($form, $form_state)) {
return $response;
}
// The form element configuration is stored in the 'properties' key in
// the form, pass that through for submission.
$element_form_state = clone $form_state;
$element_form_state
->setValues($form_state
->getValue('properties'));
// Submit element configuration.
// Generally, elements will not be processing any submitted properties.
// It is possible that a custom element might need to call a third-party API
// to 'register' the element.
$yamlform_element
->submitConfigurationForm($form, $element_form_state);
// Save the form with its updated element.
$this->yamlform
->save();
// Display status message.
$properties = $form_state
->getValue('properties');
$t_args = [
'%title' => !empty($properties['title']) ? $properties['title'] : $form_state
->getValue('key'),
'@action' => $this->action,
];
drupal_set_message($this
->t('%title has been @action.', $t_args));
// Redirect.
return $this
->redirectForm($form, $form_state, $this->yamlform
->toUrl('edit-form'));
}
/**
* Determines if the form element key already exists.
*
* @param string $key
* The form element key.
*
* @return bool
* TRUE if the form element key, FALSE otherwise.
*/
public function exists($key) {
$elements = $this->yamlform
->getElementsInitializedAndFlattened();
return isset($elements[$key]) ? TRUE : FALSE;
}
/**
* {@inheritdoc}
*/
public function isNew() {
return $this instanceof YamlFormUiElementAddForm ? TRUE : FALSE;
}
/**
* {@inheritdoc}
*/
public function getYamlForm() {
return $this->yamlform;
}
/**
* {@inheritdoc}
*/
public function getYamlFormElement() {
return $this->elementManager
->getElementInstance($this->element);
}
/**
* Determine if the parent element is a 'yamlform_flexbox'.
*
* @param string|null $key
* The element's key. Only applicable for existing elements.
* @param string|null $parent_key
* The element's parent key. Only applicable for new elements.
* Parent key is set via query string parameter. (?parent={parent_key})
*
* @return bool
* TRUE if the parent element is a 'yamlform_flexbox'.
*/
protected function isParentElementFlexbox($key = NULL, $parent_key = NULL) {
$elements = $this->yamlform
->getElementsInitializedAndFlattened();
// Check the element #yamlform_parent_flexbox property.
if ($key && isset($elements[$key])) {
return $elements[$key]['#yamlform_parent_flexbox'];
}
// Check the parent element #type.
if ($parent_key && isset($elements[$parent_key]) && isset($elements[$parent_key]['#type'])) {
return $elements[$parent_key]['#type'] == 'yamlform_flexbox' ? TRUE : FALSE;
}
return FALSE;
}
}
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:: |
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. | |
YamlFormUiElementFormBase:: |
protected | property | The action of the current form. | |
YamlFormUiElementFormBase:: |
protected | property | The form element. | |
YamlFormUiElementFormBase:: |
protected | property | Form element manager. | |
YamlFormUiElementFormBase:: |
protected | property | Form element validator. | |
YamlFormUiElementFormBase:: |
protected | property | The form element's original element type. | |
YamlFormUiElementFormBase:: |
protected | property | The renderer. | |
YamlFormUiElementFormBase:: |
protected | property | The form. | |
YamlFormUiElementFormBase:: |
public | function |
Form constructor. Overrides FormInterface:: |
4 |
YamlFormUiElementFormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
YamlFormUiElementFormBase:: |
public | function | Determines if the form element key already exists. | 1 |
YamlFormUiElementFormBase:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
1 |
YamlFormUiElementFormBase:: |
public | function |
Return the form associated with this form. Overrides YamlFormUiElementFormInterface:: |
|
YamlFormUiElementFormBase:: |
public | function |
Return the form element associated with this form. Overrides YamlFormUiElementFormInterface:: |
|
YamlFormUiElementFormBase:: |
public | function |
Is new element. Overrides YamlFormUiElementFormInterface:: |
|
YamlFormUiElementFormBase:: |
protected | function | Determine if the parent element is a 'yamlform_flexbox'. | |
YamlFormUiElementFormBase:: |
public | function |
Form submission handler. Overrides FormInterface:: |
1 |
YamlFormUiElementFormBase:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
YamlFormUiElementFormBase:: |
public | function | Constructs a new YamlFormUiElementFormBase. |