View source
<?php
use Drupal\config_entity_revisions\ConfigEntityRevisionsControllerInterface;
use Drupal\config_entity_revisions\ConfigEntityRevisionsInterface;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform_revisions\Controller\WebformRevisionsController;
use Drupal\webform_revisions\Entity\WebformRevisions;
use Drupal\webform_revisions\WebformRevisionFields;
function webform_revisions_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$webform = null;
$add_edit_form_routes = [
'entity.webform.edit_form',
'entity.webform.source_form',
];
$revision_routes = array_merge($add_edit_form_routes, [
'webform_ui_element_form',
'entity.webform.revision',
]);
$routename = \Drupal::routeMatch()
->getRouteName();
$contentEntity = NULL;
$parameters = \Drupal::routeMatch()
->getParameters();
if ($parameters
->has('webform')) {
$webform = $parameters
->get('webform');
if (is_string($webform)) {
$revisionsController = WebformRevisionsController::create(\Drupal::getContainer());
$webform = $revisionsController
->loadConfigEntityRevision();
}
$contentEntity = $webform
->getContentEntity();
}
if (in_array($routename, $revision_routes) && !$webform) {
return;
}
if ($routename == 'entity.webform.revision') {
if (!$contentEntity
->isDefaultRevision()) {
\Drupal::messenger()
->addWarning('This is not the currently published revision of the webform.');
$form['actions'] = [];
}
return;
}
if (!in_array($routename, $add_edit_form_routes)) {
if ($webform) {
$form_state
->setValue('webform_revision', $webform
->get('loadedRevisionId'));
}
return;
}
WebformRevisionFields::addRevisionFormFields($form);
if (!$contentEntity) {
$revisionsController = WebformRevisionsController::create(\Drupal::getContainer());
$contentEntity = $revisionsController
->createInitialRevision($webform);
\Drupal::database()
->query('UPDATE {webform_submission} SET webform_revision = :rid WHERE webform_id = :form', [
':rid' => $contentEntity
->id(),
':form' => $webform
->id(),
]);
}
$entity_form_display = EntityFormDisplay::create([
'targetEntityType' => 'config_entity_revisions',
'bundle' => 'WebformRevisions',
'mode' => 'default',
'status' => TRUE,
]);
$entity_form_display
->buildForm($contentEntity, $form, $form_state);
$form['actions']['#weight'] = 200;
}
function webform_revisions_webform_presave(WebformRevisions $webform) {
if ($webform
->isNew()) {
$webform
->setContentEntityID(null);
}
}
function webform_revisions_webform_update(WebformRevisions $webform) {
$routename = \Drupal::routeMatch()
->getRouteName();
if ($routename !== 'entity.webform.revision_revert_confirm') {
$controller = WebformRevisionsController::create(\Drupal::getContainer());
$controller
->createUpdateRevision($webform);
}
}
function webform_revisions_webform_delete(WebformRevisions $webform) {
$controller = WebformRevisionsController::create(\Drupal::getContainer());
$controller
->deleteRevisions($webform);
}
function webform_revisions_menu_local_tasks_alter(&$data, $route_name, \Drupal\Core\Cache\RefinableCacheableDependencyInterface &$cacheability) {
if (strpos($route_name, 'entity.webform') === 0) {
foreach ($data['tabs'] as $tab_level) {
foreach ($tab_level as $tab) {
$url = $tab['#link']['url'];
$tab_route_name = $url
->getRouteName();
$tab_route_parameters = $url
->getRouteParameters();
if (strpos($tab_route_name, 'entity.webform') !== FALSE && isset($tab_route_parameters['webform'])) {
$url
->setRouteParameter('config_entity', $tab_route_parameters['webform']);
}
}
}
}
}
function webform_revisions_webform_submission_form_alter(array &$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
WebformRevisionsController::submission_form_alter($form, $form_state, $form_id);
}
function webform_revisions_entity_base_field_info_alter(&$fields, \Drupal\Core\Entity\EntityTypeInterface $entity_type) {
if ($entity_type
->id() == 'webform_submission') {
$fields['webform_revision'] = BaseFieldDefinition::create('entity_reference')
->setLabel('Webform revision')
->setName('webform_revision')
->setProvider('webform_revisions')
->setTargetEntityTypeId('config_entity_revisions')
->setTargetBundle(null)
->setTargetEntityTypeId('webform_submission');
}
}
function webform_revisions_entity_type_alter(&$entity_types) {
if (isset($entity_types['webform'])) {
$entity_types['webform']
->setClass('Drupal\\webform_revisions\\Entity\\WebformRevisions');
}
if (isset($entity_types['webform_submission'])) {
$entity_types['webform_submission']
->setClass('Drupal\\webform_revisions\\Entity\\WebformRevisionsSubmission');
$entity_types['webform_submission']
->setStorageClass('Drupal\\webform_revisions\\WebformRevisionsSubmissionStorage');
}
}