function contact_form_revisions_form_alter in Config Entity Revisions 8.2
Implements hook_form_alter().
File
- modules/
contact_form_revisions/ contact_form_revisions.module, line 21
Code
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;
}