View source
<?php
namespace Drupal\yamlform;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\Core\Url;
use Drupal\yamlform\Controller\YamlFormController;
use Drupal\yamlform\Utility\YamlFormArrayHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;
class YamlFormSubmissionForm extends ContentEntityForm {
use YamlFormDialogTrait;
protected $elementManager;
protected $storage;
protected $requestHandler;
protected $thirdPartySettingsManager;
protected $messageManager;
protected $tokenManager;
protected $settings;
protected $entity;
protected $sourceEntity;
public function __construct(EntityManagerInterface $entity_manager, YamlFormRequestInterface $request_handler, YamlFormElementManagerInterface $element_manager, YamlFormThirdPartySettingsManagerInterface $third_party_settings_manager, YamlFormMessageManagerInterface $message_manager, YamlFormTokenManagerInterface $token_manager) {
parent::__construct($entity_manager);
$this->requestHandler = $request_handler;
$this->elementManager = $element_manager;
$this->storage = $this->entityManager
->getStorage('yamlform_submission');
$this->thirdPartySettingsManager = $third_party_settings_manager;
$this->messageManager = $message_manager;
$this->tokenManager = $token_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.manager'), $container
->get('yamlform.request'), $container
->get('plugin.manager.yamlform.element'), $container
->get('yamlform.third_party_settings_manager'), $container
->get('yamlform.message_manager'), $container
->get('yamlform.token_manager'));
}
public function setEntity(EntityInterface $entity) {
$yamlform = $entity
->getYamlForm();
$this->sourceEntity = $this->requestHandler
->getCurrentSourceEntity([
'yamlform',
'yamlform_submission',
]);
if ($yamlform
->getSetting('token_update') && ($token = $this
->getRequest()->query
->get('token'))) {
if ($yamlform_submissions_token = $this->storage
->loadByProperties([
'token' => $token,
])) {
$entity = reset($yamlform_submissions_token);
}
}
elseif ($yamlform_submission_draft = $this->storage
->loadDraft($yamlform, $this->sourceEntity, $this
->currentUser())) {
$entity = $yamlform_submission_draft;
}
$this->messageManager
->setYamlFormSubmission($entity);
$this->messageManager
->setSourceEntity($this->sourceEntity);
return parent::setEntity($entity);
}
public function buildForm(array $form, FormStateInterface $form_state) {
$yamlform_submission = $this
->getEntity();
$yamlform = $this
->getYamlForm();
$form['#cache']['contexts'][] = 'url';
$form['#cache']['tags'][] = 'config:yamlform.settings';
\Drupal::service('renderer')
->addCacheableDependency($form, $this
->getYamlForm());
$this
->displayMessages($form, $form_state);
$form = parent::buildForm($form, $form_state);
$this
->getYamlForm()
->invokeHandlers('alterForm', $form, $form_state, $yamlform_submission);
$form_id = $this
->getFormId();
$this->thirdPartySettingsManager
->alter('yamlform_submission_form', $form, $form_state, $form_id);
return $form;
}
public function form(array $form, FormStateInterface $form_state) {
if ($custom_form = $this
->getCustomForm($form, $form_state)) {
$custom_form['#custom_form'] = TRUE;
return $custom_form;
}
$yamlform_submission = $this
->getEntity();
$yamlform = $this
->getYamlForm();
$form = parent::form($form, $form_state);
if (!$yamlform_submission
->isNew() && !$yamlform_submission
->isDraft()) {
$form['navigation'] = [
'#theme' => 'yamlform_submission_navigation',
'#yamlform_submission' => $yamlform_submission,
'#weight' => -20,
];
$form['information'] = [
'#theme' => 'yamlform_submission_information',
'#yamlform_submission' => $yamlform_submission,
'#source_entity' => $this->sourceEntity,
'#weight' => -19,
];
}
$data = $yamlform_submission
->getData();
$this
->prepopulateData($data);
$elements = $yamlform_submission
->getYamlForm()
->getElementsInitialized();
$this
->populateElements($elements, $data);
$this
->prepareElements($elements, $form, $form_state);
$current_page = $this
->getCurrentPage($form, $form_state);
if ($current_page && $this
->getYamlFormSetting('wizard_progress_bar') || $this
->getYamlFormSetting('wizard_progress_pages') || $this
->getYamlFormSetting('wizard_progress_percentage')) {
$form['progress'] = [
'#theme' => 'yamlform_progress',
'#yamlform' => $this
->getYamlForm(),
'#current_page' => $current_page,
];
}
$form['elements'] = $elements;
$this
->displayCurrentPage($form, $form_state);
$this
->setFormPropertiesFromElements($form, $elements);
$form['#attached']['library'][] = 'yamlform/yamlform.form';
$assets = [
'css' => $yamlform
->getCss(),
'javascript' => $yamlform
->getJavaScript(),
];
foreach ($assets as $type => $value) {
if ($value) {
$form['#attached']['library'][] = "yamlform/yamlform.assets.{$type}";
$form['#attached']['drupalSettings']['yamlform']['assets'][$type][$yamlform
->id()] = md5($value);
}
}
if ($this
->getYamlFormSetting('form_disable_back')) {
$form['#attached']['library'][] = 'yamlform/yamlform.form.disable_back';
}
if ($this
->getYamlFormSetting('form_unsaved')) {
$form['#attributes']['class'][] = 'js-yamlform-unsaved';
$form['#attached']['library'][] = 'yamlform/yamlform.form.unsaved';
$current_page = $this
->getCurrentPage($form, $form_state);
if ($current_page && $current_page != $this
->getFirstPage($form, $form_state)) {
$form['#attributes']['data-yamlform-unsaved'] = TRUE;
}
}
if ($this
->getYamlFormSetting('form_novalidate')) {
$form['#attributes']['novalidate'] = 'novalidate';
}
if ($this
->getYamlFormSetting('form_details_toggle')) {
$form['#attributes']['class'][] = 'yamlform-details-toggle';
$form['#attached']['library'][] = 'yamlform/yamlform.element.details.toggle';
}
if ($this->entity
->isNew() && $this
->getYamlFormSetting('form_autofocus')) {
$form['#attributes']['class'][] = 'js-yamlform-autofocus';
}
if ($this
->config('yamlform.settings')
->get('ui.details_save')) {
$form['#attached']['library'][] = 'yamlform/yamlform.element.details.save';
}
if ($this
->getPages($form, $form_state)) {
$form['#attributes']['class'][] = 'js-yamlform-disable-autosubmit';
}
$form['#after_build'][] = '::afterBuild';
return $form;
}
protected function getCustomForm(array $form, FormStateInterface $form_state) {
$yamlform_submission = $this
->getEntity();
$yamlform = $this
->getYamlForm();
if (!$yamlform_submission
->getYamlForm()
->getElementsInitialized()) {
$this->messageManager
->display(YamlFormMessageManagerInterface::FORM_EXCEPTION, 'warning');
return $form;
}
if ($yamlform
->hasManagedFile() && !empty($this
->getYamlFormSetting('results_disabled'))) {
$this->messageManager
->log(YamlFormMessageManagerInterface::FORM_FILE_UPLOAD_EXCEPTION, 'notice');
$this->messageManager
->display(YamlFormMessageManagerInterface::FORM_EXCEPTION, 'warning');
return $form;
}
if ($this
->getYamlFormSetting('confirmation_type') == 'inline' && $this
->getRequest()->query
->get('yamlform_id') == $yamlform
->id()) {
$yamlform_controller = new YamlFormController($this->requestHandler, $this->messageManager);
$form['confirmation'] = $yamlform_controller
->confirmation($this
->getRequest(), $yamlform);
return $form;
}
if ($yamlform_submission
->isNew() && $yamlform
->isClosed()) {
if ($yamlform
->isTemplate() && $yamlform
->access('duplicate')) {
if (!$this
->isModalDialog()) {
$this->messageManager
->display(YamlFormMessageManagerInterface::TEMPLATE_PREVIEW, 'warning');
}
}
elseif ($yamlform
->access('submission_update_any')) {
$this->messageManager
->display(YamlFormMessageManagerInterface::ADMIN_ACCESS, 'warning');
}
else {
$form['closed'] = $this->messageManager
->build(YamlFormMessageManagerInterface::FORM_CLOSED_MESSAGE);
return $form;
}
}
if ($this
->isConfidential() && $this
->currentUser()
->isAuthenticated() && $this->entity
->isNew()) {
$this->messageManager
->display(YamlFormMessageManagerInterface::FORM_CONFIDENTIAL_MESSAGE, 'warning');
return $form;
}
if ($this
->getYamlFormSetting('results_disabled') && !$this
->getYamlFormSetting('results_disabled_ignore') && !$yamlform
->getHandlers(NULL, TRUE, YamlFormHandlerInterface::RESULTS_PROCESSED)
->count()) {
$this->messageManager
->log(YamlFormMessageManagerInterface::FORM_SAVE_EXCEPTION, 'error');
if ($this
->currentUser()
->hasPermission('administer yamlform')) {
$this->messageManager
->display(YamlFormMessageManagerInterface::FORM_SAVE_EXCEPTION, 'error');
$this->messageManager
->display(YamlFormMessageManagerInterface::ADMIN_ACCESS, 'warning');
}
else {
$this->messageManager
->display(YamlFormMessageManagerInterface::FORM_EXCEPTION, 'warning');
return $form;
}
}
if ($this
->checkTotalLimit()) {
$this->messageManager
->display(YamlFormMessageManagerInterface::LIMIT_TOTAL_MESSAGE);
if ($yamlform
->access('submission_update_any')) {
$this->messageManager
->display(YamlFormMessageManagerInterface::ADMIN_ACCESS, 'warning');
}
else {
return $form;
}
}
if ($this
->checkUserLimit()) {
$this->messageManager
->display(YamlFormMessageManagerInterface::LIMIT_USER_MESSAGE, 'warning');
if ($yamlform
->access('submission_update_any')) {
$this->messageManager
->display(YamlFormMessageManagerInterface::ADMIN_ACCESS, 'warning');
}
else {
return $form;
}
}
return FALSE;
}
protected function displayMessages(array $form, FormStateInterface $form_state) {
$yamlform_submission = $this
->getEntity();
$yamlform = $this
->getYamlForm();
if ($this
->isGet() && $this
->isRoute('entity.yamlform.test')) {
$this->messageManager
->display(YamlFormMessageManagerInterface::SUBMISSION_TEST, 'warning');
}
if ($yamlform_submission
->isDraft()) {
if ($form_state
->get('draft_saved')) {
$this->messageManager
->display(YamlFormMessageManagerInterface::SUBMISSION_DRAFT_SAVED);
$form_state
->set('draft_saved', FALSE);
}
elseif ($this
->isGet()) {
$this->messageManager
->display(YamlFormMessageManagerInterface::SUBMISSION_DRAFT_LOADED);
}
}
if ($this
->isGet() && ($this
->isRoute('entity.yamlform.canonical') || $this
->isYamlFormEntityReferenceFromSourceEntity()) && $yamlform
->access('submission_view_own') && ($previous_total = $this->storage
->getTotal($yamlform, $this->sourceEntity, $this
->currentUser()))) {
if ($previous_total > 1) {
$this->messageManager
->display(YamlFormMessageManagerInterface::SUBMISSIONS_PREVIOUS);
}
elseif ($yamlform_submission
->id() != $this->storage
->getLastSubmission($yamlform, $this->sourceEntity, $this
->currentUser())
->id()) {
$this->messageManager
->display(YamlFormMessageManagerInterface::SUBMISSION_PREVIOUS);
}
}
}
public function afterBuild(array $form, FormStateInterface $form_state) {
if (strpos($form['#action'], 'form_action_') === FALSE) {
foreach (Element::children($form['actions']) as $child_key) {
unset($form['actions'][$child_key]['#name']);
}
unset($form['form_build_id'], $form['form_token'], $form['form_id']);
}
return $form;
}
protected function actionsElement(array $form, FormStateInterface $form_state) {
if (!empty($form['#custom_form'])) {
return NULL;
}
$element = parent::actionsElement($form, $form_state);
if (!empty($element)) {
$element['#theme'] = 'yamlform_actions';
}
return $element;
}
protected function actions(array $form, FormStateInterface $form_state) {
$yamlform_submission = $this->entity;
$yamlform = $this
->getYamlForm();
$element = parent::actions($form, $form_state);
$preview_mode = $this
->getYamlFormSetting('preview');
unset($element['delete']);
$element['submit']['#button_type'] = 'primary';
$element['submit']['#attributes'] = $this
->getYamlFormSetting('form_submit_attributes');
$element['submit']['#attributes']['class'][] = 'yamlform-button--submit';
if ($yamlform_submission
->isNew() || $yamlform_submission
->isDraft()) {
$element['submit']['#value'] = $this
->getYamlFormSetting('form_submit_label');
}
$element['submit']['#validate'][] = '::validateForm';
$element['submit']['#validate'][] = '::autosave';
$element['submit']['#validate'][] = '::complete';
$element['submit']['#submit'][] = '::confirmForm';
$pages = $this
->getPages($form, $form_state);
$current_page = $this
->getCurrentPage($form, $form_state);
if ($pages) {
$current_page_element = $this
->getYamlForm()
->getPage($current_page);
$is_first_page = $current_page == $this
->getFirstPage($form, $form_state) ? TRUE : FALSE;
$is_last_page = in_array($current_page, [
'preview',
'complete',
$this
->getLastPage($form, $form_state),
]) ? TRUE : FALSE;
$is_preview_page = $current_page == 'preview';
$is_next_page_preview = $this
->getNextPage($form, $form_state) == 'preview' ? TRUE : FALSE;
$is_next_page_optional_preview = $is_next_page_preview && $preview_mode != DRUPAL_REQUIRED;
$element['submit']['#access'] = $is_last_page || $is_preview_page || $is_next_page_optional_preview;
if (!$is_first_page) {
if ($is_preview_page) {
$previous_attributes = $this
->getYamlFormSetting('preview_prev_button_attributes');
$previous_label = $this
->getYamlFormSetting('preview_prev_button_label');
}
else {
$previous_attributes = $this
->getYamlFormSetting('wizard_prev_button_attributes');
$previous_label = isset($current_page_element['#prev_button_label']) ? $current_page_element['#prev_button_label'] : $this
->getYamlFormSetting('wizard_prev_button_label');
}
$previous_attributes['class'][] = 'js-yamlform-novalidate';
$previous_attributes['class'][] = 'yamlform-button--previous';
$element['previous'] = [
'#type' => 'submit',
'#value' => $previous_label,
'#validate' => [
'::noValidate',
],
'#submit' => [
'::previous',
],
'#attributes' => $previous_attributes,
];
}
if (!$is_last_page) {
if ($is_next_page_preview) {
$next_attributes = $this
->getYamlFormSetting('preview_next_button_attributes');
$next_label = $this
->getYamlFormSetting('preview_next_button_label');
$next_attributes['class'][] = 'yamlform-button--preview';
}
else {
$next_attributes = $this
->getYamlFormSetting('wizard_next_button_attributes');
$next_label = isset($current_page_element['#next_button_label']) ? $current_page_element['#next_button_label'] : $this
->getYamlFormSetting('wizard_next_button_label');
$next_attributes['class'][] = 'yamlform-button--next';
}
$element['next'] = [
'#type' => 'submit',
'#value' => $next_label,
'#validate' => [
'::validateForm',
],
'#submit' => [
'::next',
],
'#attributes' => $next_attributes,
];
}
}
if ($this
->draftEnabled()) {
$draft_attributes = $this
->getYamlFormSetting('draft_button_attributes');
$draft_attributes['class'][] = 'yamlform-button--draft';
$element['draft'] = [
'#type' => 'submit',
'#value' => $this
->getYamlFormSetting('draft_button_label'),
'#validate' => [
'::draft',
],
'#submit' => [
'::submitForm',
'::save',
'::rebuild',
],
'#attributes' => $draft_attributes,
];
}
return $element;
}
public function next(array &$form, FormStateInterface $form_state) {
if ($form_state
->getErrors()) {
return;
}
$form_state
->set('current_page', $this
->getNextPage($form, $form_state));
$this
->wizardSubmit($form, $form_state);
}
public function previous(array &$form, FormStateInterface $form_state) {
$form_state
->set('current_page', $this
->getPreviousPage($form, $form_state));
$this
->wizardSubmit($form, $form_state);
}
protected function wizardSubmit(array &$form, FormStateInterface $form_state) {
if ($this
->draftEnabled() && $this
->getYamlFormSetting('draft_auto_save') && !$this->entity
->isCompleted()) {
$form_state
->setValue('in_draft', TRUE);
$this
->submitForm($form, $form_state);
$this
->save($form, $form_state);
}
else {
$this
->submitForm($form, $form_state);
}
$this
->rebuild($form, $form_state);
}
public function autosave(array &$form, FormStateInterface $form_state) {
if ($form_state
->hasAnyErrors()) {
if ($this
->draftEnabled() && $this
->getYamlFormSetting('draft_auto_save') && !$this->entity
->isCompleted()) {
$form_state
->setValue('in_draft', TRUE);
$this
->submitForm($form, $form_state);
$this
->save($form, $form_state);
$this
->rebuild($form, $form_state);
}
}
}
public function draft(array &$form, FormStateInterface $form_state) {
$form_state
->clearErrors();
$form_state
->setValue('in_draft', TRUE);
$form_state
->set('draft_saved', TRUE);
$this->entity
->validate();
}
public function complete(array &$form, FormStateInterface $form_state) {
$form_state
->setValue('in_draft', FALSE);
}
public function noValidate(array &$form, FormStateInterface $form_state) {
$form_state
->clearErrors();
$this->entity
->validate();
}
public function rebuild(array &$form, FormStateInterface $form_state) {
$form_state
->setRebuild();
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$this
->getYamlForm()
->invokeHandlers('validateForm', $form, $form_state, $this->entity);
$trigger_element = $form_state
->getTriggeringElement();
if (isset($trigger_element['#validate'])) {
$handlers = array_filter($form['#validate'], function ($callback) {
return is_array($callback) || $callback != '::validateForm';
});
foreach ($handlers as $callback) {
call_user_func_array($form_state
->prepareCallback($callback), [
&$form,
&$form_state,
]);
}
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$yamlform_submission = $this->entity;
$yamlform = $yamlform_submission
->getYamlForm();
$values = array_intersect_key($form_state
->getValues(), $yamlform
->getElementsFlattenedAndHasValue());
$yamlform_submission
->setData($values + $yamlform_submission
->getData());
parent::submitForm($form, $form_state);
$this
->getYamlForm()
->invokeHandlers('submitForm', $form, $form_state, $yamlform_submission);
}
public function confirmForm(array &$form, FormStateInterface $form_state) {
$this
->setConfirmation($form_state);
$yamlform_submission = $this
->getEntity();
$this
->getYamlForm()
->invokeHandlers('confirmForm', $form, $form_state, $yamlform_submission);
}
public function save(array $form, FormStateInterface $form_state) {
$yamlform = $this
->getYamlForm();
$yamlform_submission = $this
->getEntity();
if ($current_page = $this
->getCurrentPage($form, $form_state)) {
$yamlform_submission
->setCurrentPage($current_page);
}
if ($yamlform_submission
->isNew()) {
$yamlform_submission
->set('uri', preg_replace('#^' . base_path() . '#', '/', $this
->getRequest()
->getRequestUri()));
$yamlform_submission
->set('remote_addr', $this
->isConfidential() ? '' : $this
->getRequest()
->getClientIp());
}
if ($yamlform
->isTemplate() && !$yamlform
->access('update')) {
return;
}
$yamlform_submission
->save();
if ($this
->checkTotalLimit() || $this
->checkUserLimit()) {
Cache::invalidateTags([
'yamlform:' . $this
->getYamlForm()
->id(),
]);
$form_state
->setRebuild();
}
}
protected function setFormPropertiesFromElements(array &$form, array &$elements) {
foreach ($elements as $key => $value) {
if (is_string($key) && $key[0] == '#') {
if (isset($form[$key]) && is_array($form[$key]) && is_array($value)) {
$form[$key] = NestedArray::mergeDeep($form[$key], $value);
}
else {
$form[$key] = $value;
}
unset($elements[$key]);
}
}
}
protected function getPages(array &$form, FormStateInterface $form_state) {
if ($form_state
->get('pages') === NULL) {
$pages = $this
->getYamlForm()
->getPages();
foreach ($pages as &$page) {
$page['#access'] = TRUE;
}
$form_state
->set('pages', $pages);
}
$pages = $form_state
->get('pages');
foreach ($pages as $page_key => $page) {
if ($page['#access'] === FALSE) {
unset($pages[$page_key]);
}
}
return $pages;
}
protected function getCurrentPage(array &$form, FormStateInterface $form_state) {
if ($form_state
->get('current_page') === NULL) {
$pages = $this
->getPages($form, $form_state);
if (empty($pages)) {
$form_state
->set('current_page', '');
}
else {
$current_page = $this->entity
->getCurrentPage();
if ($current_page && isset($pages[$current_page]) && $this
->draftEnabled()) {
$form_state
->set('current_page', $current_page);
}
else {
$form_state
->set('current_page', YamlFormArrayHelper::getFirstKey($pages));
}
}
}
return $form_state
->get('current_page');
}
protected function getFirstPage(array &$form, FormStateInterface $form_state) {
$pages = $this
->getPages($form, $form_state);
return YamlFormArrayHelper::getFirstKey($pages);
}
protected function getLastPage(array &$form, FormStateInterface $form_state) {
$pages = $this
->getPages($form, $form_state);
return YamlFormArrayHelper::getLastKey($pages);
}
protected function getNextPage(array &$form, FormStateInterface $form_state) {
$pages = $this
->getPages($form, $form_state);
$current_page = $this
->getCurrentPage($form, $form_state);
return YamlFormArrayHelper::getNextKey($pages, $current_page);
}
protected function getPreviousPage(array &$form, FormStateInterface $form_state) {
$pages = $this
->getPages($form, $form_state);
$current_page = $this
->getCurrentPage($form, $form_state);
return YamlFormArrayHelper::getPreviousKey($pages, $current_page);
}
protected function displayCurrentPage(array &$form, FormStateInterface $form_state) {
$current_page = $this
->getCurrentPage($form, $form_state);
if ($current_page == 'preview') {
$form['elements']['#access'] = FALSE;
$this->messageManager
->display(YamlFormMessageManagerInterface::FORM_PREVIEW_MESSAGE, 'warning');
$form['preview'] = [
'#theme' => 'yamlform_submission_html',
'#yamlform_submission' => $this->entity,
];
}
else {
$pages = $this
->getYamlForm()
->getPages();
foreach ($pages as $page_key => $page) {
if (isset($form['elements'][$page_key])) {
if ($page_key != $current_page) {
$form['elements'][$page_key]['#access'] = FALSE;
$this
->hideElements($form['elements'][$page_key]);
}
else {
$form['elements'][$page_key]['#type'] = 'container';
}
}
}
}
}
protected function setTrustedRedirectUrl(FormStateInterface $form_state, Url $url) {
$form_state
->setResponse(new TrustedRedirectResponse($url
->setAbsolute()
->toString()));
}
protected function setConfirmation(FormStateInterface $form_state) {
$yamlform_submission = $this
->getEntity();
$yamlform = $yamlform_submission
->getYamlForm();
$route_name = $this
->getRouteMatch()
->getRouteName();
$route_parameters = $this
->getRouteMatch()
->getRawParameters()
->all();
$route_options = [];
if ($query = $this
->getRequest()->query
->all()) {
$route_options['query'] = $query;
}
$state = $yamlform_submission
->getState();
if ($state == YamlFormSubmissionInterface::STATE_UPDATED) {
$this->messageManager
->display(YamlFormMessageManagerInterface::SUBMISSION_UPDATED);
$form_state
->setRedirect($route_name, $route_parameters, $route_options);
return;
}
if ($state == YamlFormSubmissionInterface::STATE_COMPLETED) {
$route_options['query']['token'] = $yamlform_submission
->getToken();
}
$confirmation_type = $this
->getYamlFormSetting('confirmation_type');
switch ($confirmation_type) {
case 'page':
$redirect_route_name = $this->requestHandler
->getRouteName($yamlform, $this->sourceEntity, 'yamlform.confirmation');
$redirect_route_parameters = $this->requestHandler
->getRouteParameters($yamlform, $this->sourceEntity);
$form_state
->setRedirect($redirect_route_name, $redirect_route_parameters, $route_options);
return;
case 'url':
case 'url_message':
$confirmation_url = trim($this
->getYamlFormSetting('confirmation_url', ''));
$confirmation_url = preg_replace('/^' . preg_quote(base_path(), '/') . '/', '/', $confirmation_url);
$path_alias_manager = \Drupal::service('path.alias_manager');
$confirmation_url = $path_alias_manager
->getPathByAlias($confirmation_url);
if ($redirect_url = \Drupal::pathValidator()
->getUrlIfValid($confirmation_url)) {
if ($confirmation_type == 'url_message') {
$this->messageManager
->display(YamlFormMessageManagerInterface::SUBMISSION_CONFIRMATION);
}
$this
->setTrustedRedirectUrl($form_state, $redirect_url);
return;
}
$this->messageManager
->display(YamlFormMessageManagerInterface::SUBMISSION_CONFIRMATION);
$route_options['query']['yamlform_id'] = $yamlform
->id();
break;
case 'inline':
$route_options['query']['yamlform_id'] = $yamlform
->id();
break;
case 'message':
default:
if (!$this->messageManager
->display(YamlFormMessageManagerInterface::SUBMISSION_CONFIRMATION)) {
$this->messageManager
->display(YamlFormMessageManagerInterface::SUBMISSION_DEFAULT_CONFIRMATION);
}
break;
}
$form_state
->setRedirect($route_name, $route_parameters, $route_options);
}
protected function hideElements(&$elements) {
foreach ($elements as $key => &$element) {
if (Element::property($key) || !is_array($element)) {
continue;
}
$element['#access'] = FALSE;
$element['#after_build'][] = [
get_class($this),
'hiddenElementAfterBuild',
];
$this
->hideElements($element);
}
}
public static function hiddenElementAfterBuild(array $element, FormStateInterface $form_state) {
if (!empty($element['#element_validate'])) {
$element['#_element_validate'] = $element['#element_validate'];
$element['#element_validate'] = [
[
get_called_class(),
'hiddenElementValidate',
],
];
}
return $element;
}
public static function hiddenElementValidate(array $element, FormStateInterface $form_state) {
$temp_form_state = clone $form_state;
$temp_form_state
->setLimitValidationErrors([]);
foreach ($element['#_element_validate'] as $callback) {
$complete_form =& $form_state
->getCompleteForm();
call_user_func_array($form_state
->prepareCallback($callback), [
&$element,
&$temp_form_state,
&$complete_form,
]);
}
$form_state
->setValues($temp_form_state
->getValues());
}
protected function prepareElements(array &$elements, array &$form, FormStateInterface $form_state) {
foreach ($elements as $key => &$element) {
if (Element::property($key) || !is_array($element)) {
continue;
}
$this->elementManager
->invokeMethod('prepare', $element, $this->entity);
$this->elementManager
->invokeMethod('setDefaultValue', $element);
$hooks = [
'yamlform_element',
];
if (!empty($element['#type'])) {
$hooks[] = 'yamlform_element_' . $element['#type'];
}
$context = [
'form' => $form,
];
$this->moduleHandler
->alter($hooks, $element, $form_state, $context);
$this
->prepareElements($element, $form, $form_state);
}
}
protected function prepopulateData(array &$data) {
if ($this
->getYamlFormSetting('form_prepopulate')) {
$data += $this
->getRequest()->query
->all();
}
}
protected function populateElements(array &$elements, array $values) {
foreach ($elements as $key => &$element) {
if (Element::property($key) || !is_array($element)) {
continue;
}
if (isset($element['#type']) && isset($values[$key])) {
$element['#default_value'] = $values[$key];
}
$this
->populateElements($element, $values);
}
}
protected function checkTotalLimit() {
$yamlform = $this
->getYamlForm();
$entity_limit_total = $this
->getYamlFormSetting('entity_limit_total');
if ($entity_limit_total && ($source_entity = $this
->getLimitSourceEntity())) {
if ($this->storage
->getTotal($yamlform, $source_entity) >= $entity_limit_total) {
return TRUE;
}
}
$limit_total = $this
->getYamlFormSetting('limit_total');
if ($limit_total && $this->storage
->getTotal($yamlform) >= $limit_total) {
return TRUE;
}
return FALSE;
}
protected function checkUserLimit() {
$account = $this
->currentUser();
$yamlform = $this
->getYamlForm();
if ($account
->isAnonymous()) {
return FALSE;
}
$entity_limit_user = $this
->getYamlFormSetting('entity_limit_user');
if ($entity_limit_user && ($source_entity = $this
->getLimitSourceEntity())) {
if ($this->storage
->getTotal($yamlform, $source_entity, $account) >= $entity_limit_user) {
return TRUE;
}
}
$limit_user = $this
->getYamlFormSetting('limit_user');
if ($limit_user && $this->storage
->getTotal($yamlform, NULL, $account) >= $limit_user) {
return TRUE;
}
return FALSE;
}
protected function draftEnabled() {
$account = $this
->currentUser();
return $account
->isAuthenticated() && $this
->getYamlFormSetting('draft') && !$this
->getYamlFormSetting('results_disabled') ? TRUE : FALSE;
}
protected function isConfidential() {
return $this
->getYamlFormSetting('form_confidential', FALSE);
}
protected function isFormNoValidate() {
return $this
->getYamlFormSetting('form_novalidate', FALSE);
}
protected function isGet() {
return $this
->getRequest()
->getMethod() == 'GET' ? TRUE : FALSE;
}
protected function isRoute($route_name) {
return $route_name == $this
->getRouteMatch()
->getRouteName() ? TRUE : FALSE;
}
protected function isYamlFormEntityReferenceFromSourceEntity() {
return $this->sourceEntity && method_exists($this->sourceEntity, 'hasField') && $this->sourceEntity
->hasField('yamlform') && $this->sourceEntity->yamlform->target_id == $this
->getYamlForm()
->id();
}
protected function getYamlForm() {
$yamlform_submission = $this
->getEntity();
return $yamlform_submission
->getYamlForm();
}
protected function getSourceEntity() {
return $this->sourceEntity;
}
protected function getLimitSourceEntity() {
$yamlform_submission = $this
->getEntity();
$source_entity = $yamlform_submission
->getSourceEntity();
if ($source_entity && $source_entity
->getEntityTypeId() != 'yamlform') {
return $source_entity;
}
return NULL;
}
protected function getYamlFormSetting($name, $default_value = NULL) {
if (empty($this->settings)) {
$this->settings = $this
->getYamlForm()
->getSettings();
$default_settings = $this
->config('yamlform.settings')
->get('settings');
foreach ($default_settings as $key => $value) {
$key = str_replace('default_', '', $key);
if (empty($this->settings[$key])) {
$this->settings[$key] = $value;
}
}
}
if (isset($this->settings[$name])) {
return $this->tokenManager
->replace($this->settings[$name], $this
->getEntity());
}
else {
return $default_value;
}
}
}