class WebformEntityElementsForm in Webform 6.x
Same name and namespace in other branches
- 8.5 src/WebformEntityElementsForm.php \Drupal\webform\WebformEntityElementsForm
Webform manage elements YAML source form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\Core\Entity\BundleEntityFormBase
- class \Drupal\webform\WebformEntityElementsForm uses WebformDialogFormTrait
- class \Drupal\Core\Entity\BundleEntityFormBase
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of WebformEntityElementsForm
File
- src/
WebformEntityElementsForm.php, line 15
Namespace
Drupal\webformView source
class WebformEntityElementsForm extends BundleEntityFormBase {
use WebformDialogFormTrait;
/**
* The element info manager.
*
* @var \Drupal\Core\Render\ElementInfoManagerInterface
*/
protected $elementInfo;
/**
* The webform element manager.
*
* @var \Drupal\webform\Plugin\WebformElementManagerInterface
*/
protected $elementManager;
/**
* The webform element validator.
*
* @var \Drupal\webform\WebformEntityElementsValidatorInterface
*/
protected $elementsValidator;
/**
* The webform token manager.
*
* @var \Drupal\webform\WebformTokenManagerInterface
*/
protected $tokenManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->elementInfo = $container
->get('plugin.manager.element_info');
$instance->elementManager = $container
->get('plugin.manager.webform.element');
$instance->elementsValidator = $container
->get('webform.elements_validator');
$instance->tokenManager = $container
->get('webform.token_manager');
return $instance;
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
return $this
->buildDialogForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
/** @var \Drupal\webform\WebformInterface $webform */
$webform = $this
->getEntity();
$t_args = [
':form_api_href' => 'https://www.drupal.org/node/37775',
':render_api_href' => 'https://www.drupal.org/developing/api/8/render',
':yaml_href' => 'https://en.wikipedia.org/wiki/YAML',
];
$form['elements'] = [
'#type' => 'webform_codemirror',
'#mode' => 'yaml',
'#title' => $this
->t('Elements (YAML)'),
'#description' => $this
->t('Enter a <a href=":form_api_href">Form API (FAPI)</a> and/or a <a href=":render_api_href">Render Array</a> as <a href=":yaml_href">YAML</a>.', $t_args) . '<br /><br />' . '<em>' . $this
->t('Please note that comments are not supported and will be removed.') . '</em>',
'#default_value' => $this
->getElementsWithoutWebformTypePrefix($webform
->get('elements')),
'#required' => TRUE,
'#element_validate' => [
'::validateElementsYaml',
],
'#attributes' => [
'style' => 'min-height: 300px',
],
];
$form['token_tree_link'] = $this->tokenManager
->buildTreeElement();
$this->tokenManager
->elementValidate($form);
return parent::form($form, $form_state);
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
unset($actions['delete']);
return $actions;
}
/**
* Element validate callback: Add 'webform_' #type prefix to elements.
*/
public function validateElementsYaml(array &$element, FormStateInterface $form_state) {
if ($form_state
->getErrors()) {
return;
}
$elements = $form_state
->getValue('elements');
$elements = $this
->getElementsWithWebformTypePrefix($elements);
$form_state
->setValueForElement($element, $elements);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
if ($form_state
->hasAnyErrors()) {
return;
}
/** @var \Drupal\webform\WebformInterface $webform */
$webform = $this
->getEntity();
$elements = $webform
->getElementsDecoded();
$this
->addWebformTypePrefixRecursive($elements);
$webform
->setElements($elements);
// Validate elements YAML.
if ($messages = $this->elementsValidator
->validate($webform)) {
$form_state
->setErrorByName('elements');
foreach ($messages as $message) {
$this
->messenger()
->addError($message);
}
}
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
/** @var \Drupal\webform\WebformInterface $webform */
$webform = $this
->getEntity();
$webform
->save();
$context = [
'@label' => $webform
->label(),
'link' => $webform
->toLink($this
->t('Edit'), 'edit-form')
->toString(),
];
$t_args = [
'%label' => $webform
->label(),
];
$this
->logger('webform')
->notice('Webform @label elements saved.', $context);
$this
->messenger()
->addStatus($this
->t('Webform %label elements saved.', $t_args));
}
/****************************************************************************/
// Webform type prefix add and remove methods.
/****************************************************************************/
/**
* Get elements without 'webform_' #type prefix.
*
* @return string
* Elements (YAML) without 'webform_' #type prefix.
*/
protected function getElementsWithoutWebformTypePrefix($value) {
$elements = WebformYaml::decode($value);
if (!is_array($elements)) {
return $value;
}
$this
->removeWebformTypePrefixRecursive($elements);
return WebformYaml::encode($elements);
}
/**
* Remove 'webform_' prefix from #type.
*
* @param array $element
* A form element.
*/
protected function removeWebformTypePrefixRecursive(array &$element) {
if (isset($element['#type']) && strpos($element['#type'], 'webform_') === 0 && $this->elementManager
->hasDefinition($element['#type'])) {
$type = str_replace('webform_', '', $element['#type']);
if (!$this->elementInfo
->hasDefinition($type) && !$this->elementManager
->hasDefinition($type)) {
$element['#type'] = $type;
}
}
foreach (Element::children($element) as $key) {
if (is_array($element[$key])) {
$this
->removeWebformTypePrefixRecursive($element[$key]);
}
}
}
/**
* Get elements with 'webform_' #type prefix.
*
* @return string
* Elements (YAML) with 'webform_' #type prefix.
*/
protected function getElementsWithWebformTypePrefix($value) {
$elements = WebformYaml::decode($value);
if (!is_array($elements)) {
return $value;
}
$this
->addWebformTypePrefixRecursive($elements);
return WebformYaml::encode($elements);
}
/**
* Remove 'webform_' prefix from #type.
*
* @param array $element
* A form element.
*/
protected function addWebformTypePrefixRecursive(array &$element) {
if (isset($element['#type']) && !$this->elementInfo
->hasDefinition($element['#type'])) {
$type = 'webform_' . $element['#type'];
if ($this->elementManager
->hasDefinition($type)) {
$element['#type'] = $type;
}
}
foreach (Element::children($element) as $key) {
if (is_array($element[$key])) {
$this
->addWebformTypePrefixRecursive($element[$key]);
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BundleEntityFormBase:: |
protected | function | Protects the bundle entity's ID property's form element against changes. | |
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityForm:: |
protected | property | The entity being used by this form. | 11 |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
3 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties. | 9 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
6 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
3 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
12 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides FormInterface:: |
20 |
FormBase:: |
protected | property | The config factory. | 3 |
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. | 3 |
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. | |
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. | |
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. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
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. | 4 |
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. | |
WebformAjaxFormTrait:: |
protected | function | Queue announcement with Ajax response. | |
WebformAjaxFormTrait:: |
protected | function | Add Ajax support to a form. | |
WebformAjaxFormTrait:: |
protected | function | Create an AjaxResponse or WebformAjaxResponse object. | |
WebformAjaxFormTrait:: |
protected | function | Get announcements. | |
WebformAjaxFormTrait:: |
protected | function | Get default ajax callback settings. | 1 |
WebformAjaxFormTrait:: |
protected | function | Get redirect URL from the form's state. | |
WebformAjaxFormTrait:: |
protected | function | Get the form's Ajax wrapper id. | 1 |
WebformAjaxFormTrait:: |
protected | function | Determine if Ajax callback is callable. | |
WebformAjaxFormTrait:: |
protected | function | Is the current request for an Ajax modal/dialog. | |
WebformAjaxFormTrait:: |
protected | function | Is the current request for an off canvas dialog. | |
WebformAjaxFormTrait:: |
protected | function | Handle missing Ajax callback. | |
WebformAjaxFormTrait:: |
protected | function | Replace form via an Ajax response. | 1 |
WebformAjaxFormTrait:: |
protected | function | Reset announcements. | |
WebformAjaxFormTrait:: |
protected | function | Set announcements. | |
WebformAjaxFormTrait:: |
public | function | Submit form #ajax callback. | 1 |
WebformAjaxFormTrait:: |
public | function | Validate form #ajax callback. | 1 |
WebformDialogFormTrait:: |
protected | function | Add modal dialog support to a confirm form. | |
WebformDialogFormTrait:: |
protected | function | Build webform dialog delete link. | |
WebformDialogFormTrait:: |
protected | function | Add modal dialog support to a form. | |
WebformDialogFormTrait:: |
public | function |
Cancel form #ajax callback. Overrides WebformAjaxFormTrait:: |
1 |
WebformDialogFormTrait:: |
public | function | Close dialog. | |
WebformDialogFormTrait:: |
protected | function |
Returns if webform is using Ajax. Overrides WebformAjaxFormTrait:: |
1 |
WebformDialogFormTrait:: |
public | function |
Empty submit callback used to only have the submit button to use an #ajax submit callback. Overrides WebformAjaxFormTrait:: |
|
WebformDialogFormTrait:: |
public | function | Validate callback to clear validation errors. | 2 |
WebformEntityElementsForm:: |
protected | property | The element info manager. | |
WebformEntityElementsForm:: |
protected | property | The webform element manager. | |
WebformEntityElementsForm:: |
protected | property | The webform element validator. | |
WebformEntityElementsForm:: |
protected | property | The webform token manager. | |
WebformEntityElementsForm:: |
protected | function |
Returns an array of supported actions for the current entity form. Overrides EntityForm:: |
|
WebformEntityElementsForm:: |
protected | function | Remove 'webform_' prefix from #type. | |
WebformEntityElementsForm:: |
public | function |
Form constructor. Overrides EntityForm:: |
|
WebformEntityElementsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
WebformEntityElementsForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
|
WebformEntityElementsForm:: |
protected | function | Get elements without 'webform_' #type prefix. | |
WebformEntityElementsForm:: |
protected | function | Get elements with 'webform_' #type prefix. | |
WebformEntityElementsForm:: |
protected | function | Remove 'webform_' prefix from #type. | |
WebformEntityElementsForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
WebformEntityElementsForm:: |
public | function | Element validate callback: Add 'webform_' #type prefix to elements. | |
WebformEntityElementsForm:: |
public | function |
Form validation handler. Overrides FormBase:: |