Add a 'URL Redirects' tab to all applicable content entity edit forms

Created on 21 October 2021, about 3 years ago
Updated 6 August 2024, 4 months ago
✨ Feature request
Status

Needs work

Version

1.0

Component

Code

Created by

🇺🇸United States ericchew

Live updates comments and jobs are added and updated live.
  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

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

  • 🇮🇳India Manoj Raj.R Chennai

    Works foe me also commerce product edit forms

  • Status changed to Needs work almost 2 years ago
  • 🇨🇭Switzerland berdir Switzerland
    1. +++ b/redirect.module
      @@ -293,18 +301,26 @@ function redirect_entity_extra_field_info() {
      -  /** @var \Drupal\node\NodeInterface $node */
      -  $node = $form_state->getFormObject()->getEntity();
      -  if (!$node->isNew() && \Drupal::currentUser()->hasPermission('administer redirects')) {
      +function redirect_form_alter(&$form, FormStateInterface $form_state, $form_id) {
      +  /** @var \Drupal\Core\Entity\ContentEntityForm $form_object */
      +  $form_object = $form_state->getFormObject();
      +  if (!$form_state->getFormObject() instanceof ContentEntityForm || !in_array($form_object->getOperation(), ['edit', 'default'])) {
      +    return;
      +  }
       
      -    $nid = $node->id();
      +  $entity = $form_object->getEntity();
      

      I see that this, already now for node, doesn't contain a check whether the extra field is actually enabled.

      That is IMHO more relevant now with making this visible in many more places. Instead of hardcoding the operations, what you want to do is call $form_object->getFormDisplay()->getComponent('url_redirects').

      I'd also suggest to to add visible => $entity_type->id() == 'node' so that this doesn't change on existing sites automatically.

    2. +++ b/redirect.module
      @@ -293,18 +301,26 @@ function redirect_entity_extra_field_info() {
      -    // Find redirects to this node.
      +    // The internal url, e.g. taxonomy/term/1 (NOTE: doesn't include initial forward slash, it is added below)
      

      nitpick: comment over 80 characters.

  • 🇨🇦Canada phjou Vancouver 🇨🇦 🇪🇺

    Patch #11 works well for me as well.

    I attached a new patch with the change for $form_object->getFormDisplay()->getComponent('url_redirects'), I just had to pass the form state to getFormDisplay which is a mandatory argument.

    And I broke the comment on two lines.

    And the only thing left that needs to be addressed is the visible => $entity_type->id() == 'node'. The URL redirects stayed visible for me even with the patch so I am not 100% sure of what was needed there.

  • 🇨🇦Canada phjou Vancouver 🇨🇦 🇪🇺

    @Berdir Apparently $form_object->getFormDisplay($form_state)->getComponent('url_redirects') is not enough.
    This behavior is bleeding into my webforms when logged in.

  • 🇺🇦Ukraine vasyok

    Peolple, please let me know: why there is 2 files for patching? url-redirects-tab-3245137-18.patch and interdiff.txt. I need apply them all?

  • 🇺🇸United States ericchew

    @VasyOK you only need to apply the patch file. The interdiff is uploaded to see what code has changed between the current patch and the previous patch because it makes it easier to review.

    https://www.drupal.org/docs/develop/git/using-git-to-contribute-to-drupa... →

  • 🇺🇦Ukraine vasyok

    Theanx Ericchew!

    #18 worked with terms

  • 🇩🇪Germany Anybody Porta Westfalica

    Yes this totally makes sense. I just searched for it on taxonomy term pages. Indeed this should be available on all kinds of entities that can be redirected! +1!

    Could someone maybe turn the latest implementation into a MR?
    We shouldn't work with patches anymore.

  • First commit to issue fork.
  • Merge request !120Converted the patch in #18 to an MR → (Open) created by dcam
  • 🇺🇸United States dcam

    I converted the patch from #18 to an MR. There's no need to give me credit for doing that.

    This needs tests. The existing functionality for the Node edit form has a test. It's possible that the test can be renamed and repurposed to check other entity types.

  • Pipeline finished with Success
    3 months ago
    Total: 189s
    #298223
  • 🇺🇸United States dcam
  • 🇺🇸United States dcam

    I added some new assertions to check for the Redirects element. I repurposed the existing Node form test for that purpose. Since Users are content entities, they also get the extra field with this patch. So I simply checked for its presence on the users' own edit forms.

  • Pipeline finished with Canceled
    3 months ago
    Total: 1695s
    #298250
  • Pipeline finished with Success
    3 months ago
    Total: 188s
    #298270
  • Pipeline finished with Success
    3 months ago
    Total: 220s
    #298299
  • 🇺🇸United States dcam

    I added the visibility check mentioned in #17 to the extra field. I think this is ready for review again.

  • 🇩🇪Germany Anybody Porta Westfalica

    Thanks for the tests @dcam, just had a short look at the code and added a comment. Nice to see this moving forward!

    Could we check all core entity types to be sure it works there?

  • 🇩🇪Germany Anybody Porta Westfalica
Production build 0.71.5 2024