You are here

function webform_revisions_form_alter in Config Entity Revisions 8

Same name and namespace in other branches
  1. 8.2 modules/webform_revisions/webform_revisions.module \webform_revisions_form_alter()
  2. 1.x modules/webform_revisions/webform_revisions.module \webform_revisions_form_alter()

Implements hook_form_alter().

File

modules/webform_revisions/webform_revisions.module, line 15

Code

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();

  /* @var $contentEntity ConfigEntityRevisionsInterface */
  $contentEntity = NULL;

  // Get the content entity if it will be needed. This needs to be the most
  // recent version of the entity - if we've saved a new revision, we want
  // the latest revision message, not the old one.
  // When logging in, I have seen a CacheableAccessDeniedHttpException thrown
  // that caused an infinite loop, so catch any exceptions thrown here and
  // treat them as a non-match for the webform until we find a situation in
  // which a webform should actually be loaded.
  $parameters = \Drupal::routeMatch()
    ->getParameters();
  if ($parameters
    ->has('webform')) {

    /* @var $webform WebformRevisions */
    $webform = $parameters
      ->get('webform');
    if (is_string($webform)) {

      /* @var $revisionsController ConfigEntityRevisionsControllerInterface */
      $revisionsController = WebformRevisionsController::create(\Drupal::getContainer());
      $webform = $revisionsController
        ->loadConfigEntityRevision();
    }
    $contentEntity = $webform
      ->getContentEntity();
  }
  if (in_array($routename, $revision_routes) && !$webform) {
    return;
  }

  // If we're displaying a non-current revision, add a message and remove the
  // submission buttons.
  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 we're not adding a form or rendering the main edit form, don't provide
  // the option of adding a new revision or modifying the revision message.
  if (!in_array($routename, $add_edit_form_routes)) {
    if ($webform) {
      $form_state
        ->setValue('webform_revision', $webform
        ->get('loadedRevisionId'));
    }
    return;
  }
  WebformRevisionFields::addRevisionFormFields($form);
  if (!$contentEntity) {

    /* @var $revisionsController ConfigEntityRevisionsControllerInterface */
    $revisionsController = WebformRevisionsController::create(\Drupal::getContainer());
    $contentEntity = $revisionsController
      ->createInitialRevision($webform);

    // Update any existing submissions to point at the original revision.
    \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;
}