Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / redirect / redirect.module
index 1ab9ec382b6a9f17fb993f7da556696431d17609..bf4d4f9022f3b8b35d8202d25ed5d617d7677ebd 100644 (file)
@@ -22,6 +22,7 @@ use Drupal\Core\Routing\RouteMatchInterface;
 use Drupal\Core\Url;
 use Drupal\Core\Site\Settings;
 use Drupal\redirect\Entity\Redirect;
+use Symfony\Component\Routing\Exception\RouteNotFoundException;
 
 /**
  * Implements hook_hook_info().
@@ -79,9 +80,15 @@ function redirect_help($route_name, RouteMatchInterface $route_match) {
  * 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.
   }
 }
 
@@ -175,23 +182,6 @@ function redirect_page_build(&$page) {
   }
 }
 
-/**
- * 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.
  *
@@ -221,14 +211,15 @@ function redirect_repository() {
  */
 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) {
@@ -418,3 +409,96 @@ function redirect_redirect_operations() {
 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>',
+      ];
+    }
+  }
+}