diff --git a/redirect.module b/redirect.module index 6fc2622..7ce5fb3 100644 --- a/redirect.module +++ b/redirect.module @@ -283,21 +283,16 @@ function redirect_entity_extra_field_info() { $entity_bundle_info = \Drupal::service('entity_type.bundle.info'); // Add the url redirects form element to all content entity types - // that specify a canonical url and allow managing fields. + // that specify a canonical url. foreach ($entity_type_manager->getDefinitions() as $entity_type) { - if ($entity_type instanceof ContentEntityTypeInterface) { - $field_ui_base_route = $entity_type->get('field_ui_base_route'); - $canonical_link = $entity_type->getLinkTemplate('canonical'); - - if (!empty($field_ui_base_route) && !empty($canonical_link)) { - $entity_bundles = $entity_bundle_info->getBundleInfo($entity_type->id()); - foreach ($entity_bundles as $bundle_id => $bundle_info) { - $extra[$entity_type->id()][$bundle_id]['form']['url_redirects'] = [ - 'label' => t('URL redirects'), - 'description' => t('Redirect module form elements'), - 'weight' => 50, - ]; - } + if ($entity_type instanceof ContentEntityTypeInterface && $entity_type->getLinkTemplate('canonical')) { + $entity_bundles = $entity_bundle_info->getBundleInfo($entity_type->id()); + foreach ($entity_bundles as $bundle_id => $bundle_info) { + $extra[$entity_type->id()][$bundle_id]['form']['url_redirects'] = [ + 'label' => t('URL redirects'), + 'description' => t('Redirect module form elements'), + 'weight' => 50, + ]; } } } @@ -315,25 +310,22 @@ function redirect_form_alter(&$form, FormStateInterface $form_state, $form_id) { return; } - $form_display_component = $form_object->getFormDisplay($form_state)->getComponent('url_redirects'); $entity = $form_object->getEntity(); - if (!$entity->isNew() && \Drupal::currentUser()->hasPermission('administer redirects') && !empty($form_display_component)) { + if (!$entity->isNew() && \Drupal::currentUser()->hasPermission('administer redirects')) { $entity_type_id = $entity->getEntityTypeId(); $entity_id = $entity->id(); // The internal url, e.g. taxonomy/term/1 (NOTE: doesn't include initial forward slash, it is added below) $internal_url = $entity->toUrl('canonical')->getInternalPath(); - // The path alias, e.g. /category/term-title - $canonical_url = $entity->toUrl('canonical')->toString(); // Find redirects to this entity. /** @var \Drupal\redirect\RedirectRepository $redirects */ $redirects = \Drupal::service('redirect.repository') - ->findByDestinationUri(["internal:/$internal_url", "internal:$canonical_url", "entity:$entity_type_id/$entity_id"]); + ->findByDestinationUri(["internal:/$internal_url", "entity:$entity_type_id/$entity_id"]); // Assemble the rows for the table. $rows = []; /** @var \Drupal\Core\Entity\EntityListBuilder $list_builder */ - $list_builder = \Drupal::service('entity_type.manager')->getListBuilder($entity_type_id); + $list_builder = \Drupal::service('entity_type.manager')->getListBuilder('redirect'); /** @var \Drupal\redirect\Entity\Redirect[] $redirects */ foreach ($redirects as $redirect) { $row = []; @@ -392,7 +384,7 @@ function redirect_form_alter(&$form, FormStateInterface $form_state, $form_id) { $form['url_redirects']['actions']['#links']['add'] = [ 'title' => t('Add URL redirect'), 'url' => Url::fromRoute('redirect.add', [ - 'redirect' => $entity->toUrl()->getInternalPath(), + 'redirect' => $internal_url, 'destination' => \Drupal::destination()->get(), ]), 'attributes' => [