entity_redirect.module in Entity Redirect 8.2
Same filename and directory in other branches
Manages post edit/create redirection for entities.
File
entity_redirect.moduleView source
<?php
/**
* @file
* Manages post edit/create redirection for entities.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Render\Element\PathElement;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\Core\Url;
/**
* Implements hook_form_alter().
*/
function entity_redirect_form_alter(&$form, FormStateInterface $form_state, $form_id) {
/** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
$form_object = $form_state
->getFormObject();
if (!is_a($form_object, '\\Drupal\\Core\\Entity\\EntityFormInterface')) {
return;
}
$entity = $form_object
->getEntity();
if (is_a($entity, '\\Drupal\\Core\\Config\\Entity\\ConfigEntityBundleBase')) {
if (is_a($form_object, '\\Drupal\\Core\\Entity\\EntityDeleteForm')) {
return;
}
/** @var \Drupal\Core\Config\Entity\ConfigEntityBundleBase $entity */
$settings = $entity
->getThirdPartySettings('entity_redirect');
$user = \Drupal::currentUser();
$form['workflow']['entity_redirect'] = [
'#type' => 'fieldset',
'#title' => t('Redirect after Entity Operations'),
'#tree' => TRUE,
];
$actions = [
'add' => t('Add'),
'edit' => t('Edit'),
'delete' => t('Delete'),
'anonymous' => t('Override for Anonymous Users'),
];
foreach ($actions as $action => $title) {
$options = isset($settings['redirect'][$action]) ? isset($settings['redirect'][$action]) : [];
$form['workflow']['entity_redirect'][$action] = [
'#type' => 'details',
'#open' => FALSE,
'#title' => $title,
'active' => [
'#type' => 'checkbox',
'#title' => t('Enable'),
'#default_value' => isset($options['active']) ? $options['active'] : FALSE,
],
'destination' => [
'#type' => 'select',
'#title' => t('Redirect Destination'),
'#options' => [
'default' => t('- Default -'),
'add_form' => t('Add Form'),
'edit_form' => t('Return to Edit Form'),
'url' => t('Local Url'),
'created' => t('Created %entity_label', [
'%entity_label' => $entity
->label(),
]),
],
'#default_value' => isset($options['destination']) ? $options['destination'] : 'default',
'#states' => [
'visible' => [
"[name='entity_redirect[{$action}][active]']" => [
'checked' => TRUE,
],
],
],
],
'url' => [
'#type' => 'path',
'#title' => t('Local Destination Url'),
'#description' => t('Path to redirect the user to after submission of forms for this entity. For example, type "/about" to redirect to that page. Use a relative path with a slash in front.'),
'#default_value' => isset($options['url']) ? $options['url'] : '',
'#convert_path' => PathElement::CONVERT_NONE,
'#states' => [
'visible' => [
"[name='entity_redirect[{$action}][destination]']" => [
'value' => 'url',
],
"[name='entity_redirect[{$action}][active]']" => [
'checked' => TRUE,
],
],
],
],
'external' => [
'#type' => 'url',
'#title' => t('External Destination Url'),
'#description' => t('Enter a fully qualified url such as https://example.com/page.'),
'#default_value' => isset($options['external']) ? $options['external'] : '',
'#access' => $user
->hasPermission('set external entity redirects'),
'#states' => [
'visible' => [
"[name='entity_redirect[{$action}][destination]']" => [
'value' => 'external',
],
"[name='entity_redirect[{$action}][active]']" => [
'checked' => TRUE,
],
],
],
],
];
if ($action !== 'anonymous') {
$form['workflow']['entity_redirect'][$action]['personalizable'] = [
'#type' => 'checkbox',
'#title' => t('Allow Per User Settings'),
'#default_value' => isset($options['personalizable']) ? $options['personalizable'] : TRUE,
'#description' => t('Allow individual users to control their own redirect destination. If enabled and the user has permision they can change it on their profile edit pages.'),
];
}
if ($user
->hasPermission('set external entity redirects')) {
$form['workflow']['entity_redirect'][$action]['destination']['#options']['external'] = t('External URL');
}
}
$form['#entity_builders'][] = 'entity_redirect_bundle_builder';
}
elseif (is_a($entity, '\\Drupal\\Core\\Entity\\ContentEntityBase')) {
/** @var \Drupal\Core\Entity\ContentEntityBase $entity */
if (!($bundle_type = $entity
->getEntityType()
->getBundleEntityType())) {
return;
}
/** @var \Drupal\Core\Config\Entity\ConfigEntityBundleBase $bundle */
$bundle = \Drupal::entityTypeManager()
->getStorage($bundle_type)
->load($entity
->bundle());
if (!$bundle
->getThirdPartySetting('entity_redirect', 'redirect')) {
return;
}
$form['actions']['submit']['#submit'][] = $entity
->isNew() ? 'entity_redirect_new' : 'entity_redirect_submit';
}
}
/**
* Entity form builder for bundle forms to save values to 3rd party settings.
*/
function entity_redirect_bundle_builder($entity_type, ConfigEntityInterface $type, &$form, FormStateInterface $form_state) {
$type
->setThirdPartySetting('entity_redirect', 'redirect', $form_state
->getValue('entity_redirect'));
}
/**
* Submit function to handle the redirection per entity create/edit action.
*/
function entity_redirect_new($form, FormStateInterface $form_state) {
$form_state
->set('entity_redirect_new', TRUE);
entity_redirect_submit($form, $form_state);
}
/**
* Submit function to handle the redirection per entity create/edit action.
*/
function entity_redirect_submit($form, FormStateInterface $form_state) {
/** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
$form_object = $form_state
->getFormObject();
if (!is_a($form_object, '\\Drupal\\Core\\Entity\\EntityFormInterface')) {
return;
}
$entity = $form_object
->getEntity();
if (is_a($entity, '\\Drupal\\Core\\Entity\\ContentEntityBase')) {
/** @var \Drupal\Core\Entity\ContentEntityBase $entity */
$entity_type = $entity
->getEntityType();
if (!($bundle_type = $entity_type
->getBundleEntityType())) {
return;
}
/** @var \Drupal\Core\Config\Entity\ConfigEntityBundleBase $bundle */
$bundle = \Drupal::entityTypeManager()
->getStorage($bundle_type)
->load($entity
->bundle());
if (!($options = $bundle
->getThirdPartySetting('entity_redirect', 'redirect'))) {
return;
}
$user = \Drupal::currentUser();
if ($user
->isAnonymous() && !empty($options['anonymous']['active'])) {
$settings = $options['anonymous'];
}
elseif (is_a($form_object, 'Drupal\\Core\\Entity\\ContentEntityDeleteForm')) {
if (empty($options['delete']['active'])) {
return;
}
$settings = $options['delete'];
}
elseif ($form_state
->has('entity_redirect_new')) {
if (empty($options['add']['active'])) {
return;
}
$settings = $options['add'];
}
else {
if (empty($options['edit']['active'])) {
return;
}
$settings = $options['edit'];
}
switch ($settings['destination']) {
case 'add_form':
$route_provider = \Drupal::service('router.route_provider');
$routes = array_keys($route_provider
->getRoutesByNames([
"{$entity_type->id()}.add",
"entity.{$entity_type->id()}.add_form",
]));
$form_state
->setRedirect($routes[0], [
$bundle_type => $bundle
->id(),
]);
break;
case 'edit_form':
$route_provider = \Drupal::service('router.route_provider');
$routes = array_keys($route_provider
->getRoutesByNames([
"{$entity_type->id()}.edit",
"entity.{$entity_type->id()}.edit_form",
]));
$form_state
->setRedirect($routes[0], [
$entity_type
->id() => $entity
->id(),
]);
break;
case 'created':
$form_state
->setRedirect("entity.{$entity_type->id()}.canonical", [
$entity_type
->id() => $entity
->id(),
]);
break;
case 'url':
if (empty($settings['url'])) {
return;
}
$form_state
->setRedirectUrl(Url::fromUri('internal:' . $settings['url']));
break;
case 'external':
if (empty($settings['external'])) {
return;
}
$response = new TrustedRedirectResponse($settings['external']);
$form_state
->setResponse($response);
}
}
}
Functions
Name | Description |
---|---|
entity_redirect_bundle_builder | Entity form builder for bundle forms to save values to 3rd party settings. |
entity_redirect_form_alter | Implements hook_form_alter(). |
entity_redirect_new | Submit function to handle the redirection per entity create/edit action. |
entity_redirect_submit | Submit function to handle the redirection per entity create/edit action. |