class WebformEntityAddForm in Webform 6.x
Same name and namespace in other branches
- 8.5 src/WebformEntityAddForm.php \Drupal\webform\WebformEntityAddForm
Provides a webform add 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\WebformEntityAddForm uses WebformDialogFormTrait
- class \Drupal\Core\Entity\BundleEntityFormBase
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of WebformEntityAddForm
File
- src/
WebformEntityAddForm.php, line 16
Namespace
Drupal\webformView source
class WebformEntityAddForm extends BundleEntityFormBase {
use WebformDialogFormTrait;
/**
* The state service.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* The current route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->state = $container
->get('state');
$instance->routeMatch = $container
->get('current_route_match');
$instance->languageManager = $container
->get('language_manager');
return $instance;
}
/**
* {@inheritdoc}
*/
protected function prepareEntity() {
if ($this->operation === 'duplicate') {
$this
->setEntity($this
->getEntity()
->createDuplicate());
}
parent::prepareEntity();
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
/** @var \Drupal\webform\WebformInterface $webform */
$webform = $this
->getEntity();
// Customize title for duplicate webform.
if ($this->operation === 'duplicate') {
// Display custom title.
$form['#title'] = $this
->t("Duplicate '@label' form", [
'@label' => $webform
->label(),
]);
}
$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();
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $webform
->id(),
'#machine_name' => [
'exists' => '\\Drupal\\webform\\Entity\\Webform::load',
'source' => [
'title',
],
'label' => '<br/>' . $this
->t('Machine name'),
],
'#maxlength' => 32,
'#field_suffix' => ' (' . $this
->t('Maximum @max characters', [
'@max' => 32,
]) . ')',
'#disabled' => (bool) ($webform
->id() && $this->operation !== 'duplicate'),
'#required' => TRUE,
];
$form['title'] = [
'#type' => 'textfield',
'#title' => $this
->t('Title'),
'#maxlength' => 255,
'#default_value' => $webform
->label(),
'#required' => TRUE,
'#id' => 'title',
'#attributes' => [
'autofocus' => 'autofocus',
],
];
$form['description'] = [
'#type' => 'webform_html_editor',
'#title' => $this
->t('Administrative description'),
'#default_value' => $webform
->get('description'),
];
/** @var \Drupal\webform\WebformEntityStorageInterface $webform_storage */
$webform_storage = $this->entityTypeManager
->getStorage('webform');
$form['category'] = [
'#type' => 'webform_select_other',
'#title' => $this
->t('Category'),
'#options' => $webform_storage
->getCategories(),
'#empty_option' => $this
->t('- None -'),
'#default_value' => $webform
->get('category'),
];
$form['status'] = [
'#type' => 'radios',
'#title' => $this
->t('Status'),
'#default_value' => $webform
->get('status'),
'#options' => [
WebformInterface::STATUS_OPEN => $this
->t('Open'),
WebformInterface::STATUS_CLOSED => $this
->t('Closed'),
],
'#options_display' => 'side_by_side',
];
$form = $this
->protectBundleIdElement($form);
return parent::form($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
if ($this->operation === 'duplicate') {
$original_id = $this->routeMatch
->getRawParameter('webform');
$duplicate_id = $this
->getEntity()
->id();
// Copy translations.
if ($this->moduleHandler
->moduleExists('config_translation')) {
$original_name = 'webform.webform.' . $original_id;
$duplicate_name = 'webform.webform.' . $duplicate_id;
$current_langcode = $this->languageManager
->getConfigOverrideLanguage()
->getId();
$languages = $this->languageManager
->getLanguages();
foreach ($languages as $language) {
$langcode = $language
->getId();
if ($langcode !== $current_langcode) {
$original_translation = $this->languageManager
->getLanguageConfigOverride($langcode, $original_name)
->get();
if ($original_translation) {
$duplicate_translation = $this->languageManager
->getLanguageConfigOverride($langcode, $duplicate_name);
$duplicate_translation
->setData($original_translation);
$duplicate_translation
->save();
}
}
}
}
// Copy webform export and results from state.
$state = $this->state
->get("webform.webform.{$original_id}");
// Remove node (source entity) keys.
unset($state['results.export.node'], $state['results.custom.node']);
if ($state) {
$this->state
->set("webform.webform.{$duplicate_id}", $state);
}
}
$form_state
->setRedirectUrl(Url::fromRoute('entity.webform.edit_form', [
'webform' => $this
->getEntity()
->id(),
]));
}
/**
* {@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'), 'settings')
->toString(),
];
$t_args = [
'%label' => $webform
->label(),
];
$this
->logger('webform')
->notice('Webform @label created.', $context);
$this
->messenger()
->addStatus($this
->t('Webform %label created.', $t_args));
}
}
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 name of the current operation. | |
EntityForm:: |
protected | function | Returns an array of supported actions for the current entity form. | 35 |
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 | 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:: |
|
FormBase:: |
protected | property | The config factory. | 3 |
FormBase:: |
protected | property | The request stack. | 1 |
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. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
72 |
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 |
WebformEntityAddForm:: |
protected | property | The language manager. | |
WebformEntityAddForm:: |
protected | property |
The module handler. Overrides EntityForm:: |
|
WebformEntityAddForm:: |
protected | property |
The current route match. Overrides FormBase:: |
|
WebformEntityAddForm:: |
protected | property | The state service. | |
WebformEntityAddForm:: |
public | function |
Form constructor. Overrides EntityForm:: |
|
WebformEntityAddForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
WebformEntityAddForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
|
WebformEntityAddForm:: |
protected | function |
Prepares the entity object before the form is built first. Overrides EntityForm:: |
|
WebformEntityAddForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
WebformEntityAddForm:: |
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 EntityForm:: |