View source
<?php
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;
function contact_form_revisions_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$contact = NULL;
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;
}
$contentEntity = NULL;
$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 ($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 (!in_array($routename, $add_edit_form_routes)) {
if ($contact) {
$form_state
->setValue('contact_form_revision', $contact
->get('loadedRevisionId'));
}
return;
}
ContactRevisionFields::addRevisionFormFields($form);
if (!$contentEntity) {
$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;
}
function contact_form_revisions_contact_form_update(ContactFormRevisions $contact) {
$controller = ContactFormRevisionsController::create(\Drupal::getContainer());
$controller
->createUpdateRevision($contact);
}
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) {
$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']);
}
}
}
}
}
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}');
}
}
function contact_form_revisions_entity_operation(EntityInterface $entity) {
if ($entity instanceof ContactFormRevisions) {
return config_entity_revisions_entity_operation_helper($entity);
}
}