Problem/Motivation

Node edit forms have an extra field that displays redirects to that node. This functionality would be useful for other content entity types.

Proposed resolution

Change redirect_entity_extra_field_info() to apply to all entity types. Change redirect_form_node_form_alter() to redirect_form_alter() for the same reason.

Remaining tasks

  • Write tests
  • Review

User interface changes

The URL Redirects extra field will be added to all content entities.

Issue fork redirect-3245137

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

ericchew created an issue. See original summary.

ericchew’s picture

Status: Active » Needs review
StatusFileSize
new4.75 KB
ericchew’s picture

Title: Add Url Redirects details to all applicable content entity types. » Add a 'URL Redirects' tab to all applicable content entity edit forms
ericchew’s picture

StatusFileSize
new4.8 KB
new584 bytes

Small fix to make sure the element only renders on the edit page.

ericchew’s picture

StatusFileSize
new4.82 KB
new648 bytes

I just noticed that for taxonomy terms (and possibly other entity types) that there is no "edit" form operation. When on the edit form for taxonomy terms, the operation is "default". This is another small change to make it so the element only renders if the form operation is "default" or "edit". I'm not sure if there is a better way to determine when on a content entity edit form in a form alter.

mglaman’s picture

  1. +++ b/redirect.module
    @@ -275,17 +277,28 @@ function redirect_source_link_get_status_messages(array $form, FormStateInterfac
    +      $field_ui_base_route = $entity_type->get('field_ui_base_route');
    ...
    +      if (!empty($field_ui_base_route) && !empty($canonical_link)) {
    

    Do we need to check the Field UI base route property? I have plenty of custom content entities that don't have field UI support.

  2. +++ b/redirect.module
    @@ -293,23 +306,34 @@ function redirect_entity_extra_field_info() {
    +  $form_display_component = $form_object->getFormDisplay($form_state)->getComponent('url_redirects');
    ...
    +  if (!$entity->isNew() && \Drupal::currentUser()->hasPermission('administer redirects') && !empty($form_display_component)) {
    

    This seems new? It didn't check the display component before

  3. +++ b/redirect.module
    @@ -293,23 +306,34 @@ function redirect_entity_extra_field_info() {
    +    // The path alias, e.g. /category/term-title
    +    $canonical_url = $entity->toUrl('canonical')->toString();
    ...
    -      ->findByDestinationUri(["internal:/node/$nid", "entity:node/$nid"]);
    +      ->findByDestinationUri(["internal:/$internal_url", "internal:$canonical_url", "entity:$entity_type_id/$entity_id"]);
    

    This also looks new? Adding lookup by path alias vs internal/canonical

  4. +++ b/redirect.module
    @@ -293,23 +306,34 @@ function redirect_entity_extra_field_info() {
    -    $list_builder = \Drupal::service('entity_type.manager')->getListBuilder('redirect');
    +    $list_builder = \Drupal::service('entity_type.manager')->getListBuilder($entity_type_id);
    

    We need this to remain the redirect list builder, as it builds just those. Correct?

ericchew’s picture

StatusFileSize
new3.94 KB
new3.7 KB

1. Good point. My initial thinking was that if you can't configure whether it should/shouldn't be displayed, then don't show it at all. It probably does make more sense to show it by default given what you said.

2. After testing, this doesn't seem necessary. I put it there initially because I thought that the element may render regardless of it being enabled/disabled in the form display, but that isn't the case. I removed the check and tested to see if I disabled the element in the form display whether it was properly disabled in the edit form, and it was.

3. I added this because in my testing, some of my redirects were not showing up for products because the redirect was pointing to its alias instead of its internal url. I'll remove this and check to see if there is already an issue for that.

4. Correct. Not sure what I was thinking.

Attached a new patch that makes the changes discussed above and adds a few cleanups.

Status: Needs review » Needs work

The last submitted patch, 8: url-redirects-tab-3245137-8.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

ericchew’s picture

Issue summary: View changes

Tests are failing because the form alter is too generic. The test that fails is trying to edit a path alias entity. When the edit form for the path alias entity loads, the form alter logic is run because the operation is "default". It then crashes when we try to get the canonical url for a path alias entity, because no canonical url link template is defined. This did not crash previously because of the check for whether the url_redirect component was added in the form display (it isnt for path alias because it has no field ui base route).

Need to figure out a better way to make sure we are only form altering edit forms.

ericchew’s picture

StatusFileSize
new3.98 KB
new582 bytes

New patch that has a check for whether the canonical link template exists for the entity in the form alter. There doesn't appear to be a better way to target the edit forms we want in a form alter.

ericchew’s picture

Status: Needs work » Needs review
tonytheferg’s picture

Works for me on taxonomy term edit forms. Thanks!

skyredwang’s picture

Status: Needs review » Reviewed & tested by the community

Also works for me on commerce product edit forms

rossb89’s picture

Works for me, also tested on nodes & commerce product entity forms.

Manoj Raj.R’s picture

Works foe me also commerce product edit forms

berdir’s picture

Status: Reviewed & tested by the community » Needs work
  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.

phjou’s picture

StatusFileSize
new4.01 KB
new1.25 KB

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.

phjou’s picture

@Berdir Apparently $form_object->getFormDisplay($form_state)->getComponent('url_redirects') is not enough.
This behavior is bleeding into my webforms when logged in and it was not happening with #11.

vasyok’s picture

Issue summary: View changes

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?

ericchew’s picture

@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...

vasyok’s picture

Theanx Ericchew!

#18 worked with terms

anybody’s picture

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.

dcam made their first commit to this issue’s fork.

dcam’s picture

Issue tags: +Needs tests

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.

dcam’s picture

Issue summary: View changes
Issue tags: -Needs issue summary update
dcam’s picture

Issue summary: View changes
Issue tags: -Needs tests

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.

dcam’s picture

Status: Needs work » Needs review

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

anybody’s picture

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?

anybody’s picture

Status: Needs review » Needs work
anybody’s picture

Title: Add a 'URL Redirects' tab to all applicable content entity edit forms » Add a 'URL Redirects' tab to ALL applicable content entity edit forms (only nodes supported yet)
phjou’s picture

StatusFileSize
new6.53 KB

Rerolled the patch to work on the latest version.

The patch does not contain this line that was in the branch:

'visible' => $entity_type->id() == 'node',

That defies the purpose of the whole ticket to make it work with all entities and not just nodes.

phjou’s picture

StatusFileSize
new6.78 KB

OOps, I uploaded the wrong patch. This should work better with that one, sorry.

phjou’s picture

tonytheferg’s picture

Looks like this needs re-rolled for 1.12

anybody’s picture

Issue tags: +Needs rebase

This look quite close to the finish line, maybe someone would like to carry it over? :)

chris matthews’s picture

Status: Needs work » Needs review
Issue tags: -Needs rebase

@tonytheferg, the patch in #34 applies cleanly to 8.x-1.12 and 8.x-1.x-dev

anybody’s picture

Status: Needs review » Needs work

@chris matthews I think the MR was meant. At least we should have a MR from the patch to review? Maybe someone could do that?

chris matthews’s picture

Status: Needs work » Needs review

The MR should be ready for review now.