use Drupal\Core\Url;
use Drupal\Core\Site\Settings;
use Drupal\redirect\Entity\Redirect;
+use Symfony\Component\Routing\Exception\RouteNotFoundException;
/**
* Implements hook_hook_info().
* Will delete redirects based on the entity URL.
*/
function redirect_entity_delete(EntityInterface $entity) {
- if (redirect_entity_has_path_field($entity)) {
- redirect_delete_by_path('internal:/' . $entity->toUrl()->getInternalPath());
- redirect_delete_by_path('entity:' . $entity->getEntityTypeId() . '/' . $entity->id());
+ try {
+ if ($entity->getEntityType()->hasLinkTemplate('canonical') && $entity->toUrl('canonical')->isRouted()) {
+ redirect_delete_by_path('internal:/' . $entity->toUrl('canonical')->getInternalPath());
+ redirect_delete_by_path('entity:' . $entity->getEntityTypeId() . '/' . $entity->id());
+ }
+ }
+ catch (RouteNotFoundException $e) {
+ // This can happen if a module incorrectly defines a link template, ignore
+ // such errors.
}
}
}
}
-/**
- * Checks if the entity has path field.
- *
- * @param EntityInterface $entity
- * The entity to check.
- *
- * @return bool
- * TRUE if the entity has the path field.
- */
-function redirect_entity_has_path_field(EntityInterface $entity) {
- if (isset($entity->path) && $entity->path instanceof FieldItemList) {
- return TRUE;
- }
-
- return FALSE;
-}
-
/**
* Gets the redirect repository service.
*
*/
function redirect_delete_by_path($path, $langcode = NULL, $match_subpaths_and_redirect = TRUE) {
$path = ltrim($path, '/');
- $query = \Drupal::database()->select('redirect');
+ $database = \Drupal::database();
+ $query = $database->select('redirect');
$query->addField('redirect', 'rid');
$query_or = db_or();
- $query_or->condition('redirect_source__path', db_like($path), 'LIKE');
+ $query_or->condition('redirect_source__path', $database->escapeLike($path), 'LIKE');
if ($match_subpaths_and_redirect) {
- $query_or->condition('redirect_source__path', db_like($path . '/') . '%', 'LIKE');
- $query_or->condition('redirect_redirect__uri', db_like($path), 'LIKE');
- $query_or->condition('redirect_redirect__uri', db_like($path . '/') . '%', 'LIKE');
+ $query_or->condition('redirect_source__path', $database->escapeLike($path . '/') . '%', 'LIKE');
+ $query_or->condition('redirect_redirect__uri', $database->escapeLike($path), 'LIKE');
+ $query_or->condition('redirect_redirect__uri', $database->escapeLike($path . '/') . '%', 'LIKE');
}
if ($langcode) {
function redirect_source_link_get_status_messages(array $form, FormStateInterface $form_state) {
return $form['redirect_source']['widget'][0]['status_box'];
}
+
+/**
+ * Implements hook_entity_extra_field_info().
+ */
+function redirect_entity_extra_field_info() {
+ $extra = [];
+
+ if (\Drupal::service('module_handler')->moduleExists('node')) {
+ $node_types = \Drupal::entityTypeManager()
+ ->getStorage('node_type')
+ ->loadMultiple();
+
+ foreach ($node_types as $node_type) {
+ $extra['node'][$node_type->id()]['form']['url_redirects'] = [
+ 'label' => t('URL redirects'),
+ 'description' => t('Redirect module form elements'),
+ 'weight' => 50,
+ ];
+ }
+ }
+
+ return $extra;
+}
+
+/**
+ * Implements hook_form_node_form_alter().
+ */
+function redirect_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
+ /** @var \Drupal\node\NodeInterface $node */
+ $node = $form_state->getFormObject()->getEntity();
+ if (!$node->isNew() && \Drupal::currentUser()->hasPermission('administer redirects')) {
+
+ $nid = $node->id();
+
+ // Find redirects to this node.
+ $redirects = \Drupal::service('redirect.repository')
+ ->findByDestinationUri(["internal:/node/$nid", "entity:node/$nid"]);
+
+ // Assemble the rows for the table.
+ $rows = [];
+ /** @var \Drupal\Core\Entity\EntityListBuilder $list_builder */
+ $list_builder = \Drupal::service('entity.manager')->getListBuilder('redirect');
+ /** @var \Drupal\redirect\Entity\Redirect[] $redirects */
+ foreach ($redirects as $redirect) {
+ $row = [];
+ $path = $redirect->getSourcePathWithQuery();
+ $row['path'] = [
+ 'class' => ['redirect-table__path'],
+ 'data' => ['#plain_text' => $path],
+ 'title' => $path,
+ ];
+ $row['operations'] = [
+ 'data' => [
+ '#type' => 'operations',
+ '#links' => $list_builder->getOperations($redirect),
+ ],
+ ];
+ $rows[] = $row;
+ }
+
+ // Add the list to the vertical tabs section of the form.
+ $header = [
+ ['class' => ['redirect-table__path'], 'data' => t('From')],
+ ['class' => ['redirect-table__operations'], 'data' => t('Operations')],
+ ];
+ $form['url_redirects'] = [
+ '#type' => 'details',
+ '#title' => t('URL redirects'),
+ '#group' => 'advanced',
+ '#open' => FALSE,
+ 'table' => [
+ '#type' => 'table',
+ '#header' => $header,
+ '#rows' => $rows,
+ '#empty' => t('No URL redirects available.'),
+ '#attributes' => ['class' => ['redirect-table']],
+ ],
+ '#attached' => [
+ 'library' => [
+ 'redirect/drupal.redirect.admin',
+ ],
+ ],
+ ];
+
+ if (!empty($rows)) {
+ $form['url_redirects']['warning'] = [
+ '#markup' => t('Note: links open in the current window.'),
+ '#prefix' => '<p>',
+ '#suffix' => '</p>',
+ ];
+ }
+ }
+}