Redirect to custom path per node

Created on 27 January 2023, over 2 years ago

Problem/Motivation

Add custom redirect path field per node for content types. The field itself is enabled or disabled via checkbox in the content type configurations for Unpublished nodes redirect in admin. If enabled, chosen unpublished node can be redirected to custom path, different from the specified for the content type. When the field is disabled, we use the path from the content type configuration.

Proposed resolution

Added new field for custom per node path to redirect to in Entity class.

Created .module file to implement:

  • hook_entity_base_field_info() in order to add the custom redirect path field to all content types.
  • hook_form_alter() in order to alter add/edit forms and hide the field for where custom field is disabled.

In UnpublishedNodesRedirectUtils.php added helper functions:

  • To get node types with disabled custom redirect per node.
  • Get the form IDs for entity type add/edit forms.

In On403Subscriber Event added:

  • Check if custom field is enabled and if its value is not null, then use custom path over the one from content type redirect entity configuration.
Feature request
Status

Needs review

Version

2.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇹Italy apaderno Brescia, 🇮🇹
  • Found missing declarations, so added the patch again

  • 🇧🇬Bulgaria boychev

    I've tested the feature on my side by using the patch from comment #4 Redirect to custom path per node Needs review and it's working perfectly.

  • 🇧🇬Bulgaria boychev

    For me that new feature could be added in the new release. It's really useful to have an option to add a custom path redirect per node.

  • 🇬🇧United Kingdom xiwar

    Updating patch as the base field plugin type was set to textfield instead of string_textfield. This was causing errors on config import and field creation

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 8
    last update almost 2 years ago
    1 pass
  • 🇺🇦Ukraine Ressinel 🇺🇦 Rivne

    Identified issues in previous patch:

    1. The Unpublished Redirect Destination field is incorrectly displayed for all content types, even when disabled in settings.
    2. The field accepts URLs without a leading slash (/), causing redirect execution errors
    3. Error: Call to a member function isEnabledCustomPath() on null

    Attached updated patch with fixes (see interdiff file for changes).

  • 🇮🇳India prem suthar Ahemdabad- Gujrat , Jodhpur - Rajsthan

    @ressinel patch is Working Fine as per the issue scope but in that if we add the Internal redirect path as node/60 the it throw the error if we access the unpublished node

    The website encountered an unexpected error. Try again later.
    InvalidArgumentException: The internal path component 'node/60' is invalid. Its path component must have a leading slash, e.g. internal:/foo. in Drupal\Core\Url::fromInternalUri() (line 416 of core/lib/Drupal/Core/Url.php).

    I think we should add validation on the Internal Redirect Path field during configuration save. It should ensure that the path starts with a "/" and show a validation error if it doesn't.

    Thanks!

  • 🇮🇳India prem suthar Ahemdabad- Gujrat , Jodhpur - Rajsthan

    Or either we can add the description like this .
    The path has to start with a slash.

Production build 0.71.5 2024