You are here

contact_form_revisions.module in Config Entity Revisions 8.2

File

modules/contact_form_revisions/contact_form_revisions.module
View source
<?php

/**
 * @file
 * Contact form procedural code.
 */
require_once drupal_get_path('module', 'config_entity_revisions') . '/config_entity_revisions.inc';
use Drupal\contact_form_revisions\ContactRevisionFields;
use Drupal\contact_form_revisions\Controller\ContactFormRevisionsController;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Form\FormStateInterface;
use Drupal\contact_form_revisions\Entity\ContactFormRevisions;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;

/**
 * Implements hook_form_alter().
 */
function contact_form_revisions_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $contact = NULL;

  // Modify the label on the workflow form so it doesn't say "Config entities".
  if ($form_id == 'workflow_edit_form') {
    $entity_types =& $form['type_settings']['entity_types_container']['entity_types'];
    if (!empty($entity_types['config_entity_revisions'])) {
      $entity_types['config_entity_revisions']['type']['#context']['label'] = t('Configuration Entities');
    }
    return;
  }
  $add_edit_form_routes = [
    'entity.contact_form.edit_form',
    'entity.contact_form.source_form',
  ];
  $revision_routes = array_merge($add_edit_form_routes, [
    'contact_ui_element_form',
    'entity.contact_form.revision',
  ]);
  $routename = \Drupal::routeMatch()
    ->getRouteName();
  if (!in_array($routename, $revision_routes)) {
    return;
  }

  /* @var $contentEntity ConfigEntityRevisionsConfigEntityInterface */
  $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.
  $match = \Drupal::service('router')
    ->matchRequest(\Drupal::request());
  if (isset($match['contact_form'])) {
    $contact = $match['contact_form'];
    if ($contact instanceof ContactFormRevisions) {
      $contentEntity = $contact
        ->getContentEntity();
    }
  }
  if (in_array($routename, $revision_routes) && !$contact) {
    return;
  }

  // If we're displaying a non-current revision, add a message and remove the
  // submission buttons.
  if ($routename == 'entity.contact_form.revision') {
    if (!$contentEntity
      ->isDefaultRevision()) {
      \Drupal::messenger()
        ->addMessage('This is not the currently published revision of the contact form.', 'warning');
      $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 ($contact) {
      $form_state
        ->setValue('contact_form_revision', $contact
        ->get('loadedRevisionId'));
    }
    return;
  }
  ContactRevisionFields::addRevisionFormFields($form);
  if (!$contentEntity) {

    /* @var $revisionsController ConfigEntityRevisionsControllerInterface */
    $revisionsController = ContactFormRevisionsController::create(\Drupal::getContainer());
    $contentEntity = $revisionsController
      ->createInitialRevision($contact);
  }
  $entity_form_display = EntityFormDisplay::create([
    'targetEntityType' => 'config_entity_revisions',
    'bundle' => 'ContactFormRevisions',
    'mode' => 'default',
    'status' => TRUE,
  ]);
  $entity_form_display
    ->buildForm($contentEntity, $form, $form_state);
  $form['actions']['#weight'] = 200;
}

/**
 * Implements hook_ENTITY_TYPE_update().
 */
function contact_form_revisions_contact_form_update(ContactFormRevisions $contact) {
  $controller = ContactFormRevisionsController::create(\Drupal::getContainer());
  $controller
    ->createUpdateRevision($contact);
}

/**
 * Implements hook_menu_local_tasks_alter().
 */
function contact_form_revisions_menu_local_tasks_alter(&$data, $route_name, RefinableCacheableDependencyInterface &$cacheability) {
  if (strpos($route_name, 'entity.contact_form') === 0) {
    foreach ($data['tabs'] as $tab_level) {
      foreach ($tab_level as $tab) {

        /** @var Drupal\Core\Url $url */
        $url = $tab['#link']['url'];
        $tab_route_name = $url
          ->getRouteName();
        $tab_route_parameters = $url
          ->getRouteParameters();
        if (strpos($tab_route_name, 'entity.contact_form') !== FALSE && isset($tab_route_parameters['contact_form'])) {
          $url
            ->setRouteParameter('config_entity', $tab_route_parameters['contact_form']);
        }
      }
    }
  }
}

/**
 * Implements hook_entity_type_alter().
 */
function contact_form_revisions_entity_type_alter(&$entity_types) {
  if (isset($entity_types['contact_form'])) {
    $entity_types['contact_form']
      ->setClass('Drupal\\contact_form_revisions\\Entity\\ContactFormRevisions')
      ->setStorageClass('Drupal\\contact_form_revisions\\ContactFormRevisionsStorage')
      ->setLinkTemplate('revisions', '/admin/structure/contact/{contact_form}/revisions')
      ->setLinkTemplate('revision', '/admin/structure/contact/{contact_form}/revision/{revision_id}');
  }
}

/**
 * Implements hook_entity_operations().
 */
function contact_form_revisions_entity_operation(EntityInterface $entity) {
  if ($entity instanceof ContactFormRevisions) {
    return config_entity_revisions_entity_operation_helper($entity);
  }
}