I have a number of content types that should only get aliases via pathauto. We've hidden the url alias widget from the form as an easy way to deny access to editing them. Unfortunately, this seems to prevent aliases from auto-generating when they're initially saved. I traced this back to the generator, which checks the pathauto state ($entity->path->pathauto != PathautoState::CREATE) and exits early. So the pathauto state is not being set to CREATE for entities that do not have the url alias field on the form.

For context, we recently updated to Drupal 8.4. I'm not sure if this is related, but the timing seems suspect - we've had no reports of this issue before the update.

Comments

rbayliss created an issue. See original summary.

berdir’s picture

Status: Active » Postponed (maintainer needs more info)

There were various reports since 8.4 about strange effects. Only found time now to start looking into it. Can you check if #2945734: Fix automated test failures in 8.x-1.x is fixing this for you?

If not, a failing test would be helpful.

rbayliss’s picture

Status: Postponed (maintainer needs more info) » Needs review
StatusFileSize
new476 bytes

@berdir - Yeah, I do think this is related. I think I actually found the solution to my issue, which was super simple. Not sure if this will help with the other 8.4 ticket or not, but it seems like it should. I'm gonna switch this over to needs review to see what testbot says, but feel free to postpone again or take this over to #2945734.

rbayliss’s picture

Ah, just to give more context, the patch in #2945734: Fix automated test failures in 8.x-1.x did not solve my issue. This patch, however, did.

rbayliss’s picture

I believe this is the relevant 8.4 change that helped me get to this patch and might inform the other issue.

berdir’s picture

Issue tags: +Needs tests

Any chance you can create a failing test that shows the scenario this fixes?

Status: Needs review » Needs work

The last submitted patch, 3: pathauto-2946273-fix_default_value.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

rbayliss’s picture

I will give it a shot next week. It's definitely related to the URL field's presence on the form, although I'm not sure it's as simple as "when it's there it works, when it's not it doesn't." I'm ending up in ::isEmpty() with just a langcode, but I can't figure out why it's happening on some content types but not others. I really think it's related to what you're working on in #2945734: Fix automated test failures in 8.x-1.x though, because computed properties weren't staying set until I fixed the isEmpty() check.

rbayliss’s picture

Huh, ok. On closer review, it looks like this is reliably reproducible on a bare install using the following steps:

1. Enable the paragraphs module and create 2 paragraph types.
2. Create a content type and add a new Paragraph field that's allowed to reference either of the two types.
3. Hide the alias field for the content type from the form.
4. Create a new node of that type, **making sure to add a paragraph of either type**.
5. Observe that no alias is created.

I believe this is triggered by the ajax action prior to final submission. The alias works just fine if you either don't use ajax during the creation of the content, or have the path field exposed on the form. I'm not sure how to write an effective test for this. Any suggestions?

cgmonroe’s picture

I had a similar problem using the Feeds module. I distilled it down to the following problem case where the path is not defined in a node. Here's a some code that fails when run in the Dev execute PhP page.

The odd thing is that the ksm call is required to make it fail.

$node = \Drupal::entityTypeManager()->getStorage('node')->create([
    'type' => 'basic_page',
    'title' => 'Test article',
    'langcode' => 'en', 
    'uid' => 1 
]);
ksm($node->path);
$node->save();

As the report says, pathauto does not generate the URL because $node->path->pathauto is NULL when the the updateEntityAlias method checks if the create flag has been set ( ~ line 306 ).

Seems like the "skip if pathauto processing is disabled" code should include a test if an entity is new and pathauto is NULL that allow the process to continue.

rbayliss’s picture

Status: Needs work » Needs review
StatusFileSize
new835 bytes

Ah! Good call! Accessing any of the path properties prior to save simulates the error I'm describing. Knowing that, here's a test that should fail.

rbayliss’s picture

StatusFileSize
new1.28 KB

And a patch including both the test and the fix.

The last submitted patch, 11: pathauto_2946273-11_testonly.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

berdir’s picture

Those kind of problems are exactly what my changes in #2945734: Fix automated test failures in 8.x-1.x should solve. And they do, I verified that this test change is passing when combined with those changes.

However, I would prefer if it would be in \Drupal\Tests\pathauto\Kernel\PathautoKernelTest, possibly in an existing method. Can you do that as a test-only patch and a combined patch with my changes? Then we can commit that and I really think that most of the problems that people are reporting are fixed then.

Status: Needs review » Needs work

The last submitted patch, 12: pathauto_2946273-12.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

rbayliss’s picture

Status: Needs work » Needs review
StatusFileSize
new1.34 KB

Sure, here you go. FWIW, I did try the patch in #2945734: Fix automated test failures in 8.x-1.x, and it doesn't solve the issue for me without this additional isEmpty() change.

Status: Needs review » Needs work

The last submitted patch, 16: pathauto_2946273-16.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

rbayliss’s picture

Status: Needs work » Needs review
StatusFileSize
new1.34 KB

Fix typo (lancode -> langcode). Thanks Moshe!

Status: Needs review » Needs work

The last submitted patch, 18: pathauto_2946273-18.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

moshe weitzman’s picture

Status: Needs work » Reviewed & tested by the community
cgmonroe’s picture

+1 on Reviewed.

With this patch nodes with an pathauto pattern get created with the aliases and not /node/####.

berdir’s picture

Status: Reviewed & tested by the community » Needs work

As I suggested above, I moved the test to #2945734: Fix automated test failures in 8.x-1.x and committed it as part of that (with issue credit).

I do believe you that it doesn't fix your problem, but it would be great to have a test that actually covers that scenario. We could for example try to write an actual JS test that triggers an ajax operation (One option would be to do a file upload or maybe something as simple as a multi-value field with an add-another button?).

@cgmonroe: I would expect that your issue is resolved with the patch that I now committed.

cgmonroe’s picture

StatusFileSize
new476 bytes

FYI - Creating nodes via Feeds (or programatically) still fails after updating pathauto to the latest Dev. I still needed to patch the PathautoItem.isEmpty() method with the patch above.

I think part of this is Drupal core 8.4 vs 8.5. The testCreateNodeWhileAccessingPath is passing in the Drupal 8.5 test environment. However, if I run the test code in a Dev PhP window on my 8.4 site, it fails to create a node with an alias.

Anyway, attached is the same patch as #18 without the test which you have already committed. It applies cleanly to the new dev code.

daniel korte’s picture

#23 solves my issue of path aliases not being created on node creation with 8.x-1.1.

Never mind. This is no longer working. May be related to #2950701: Pathauto pattern is not applied on first node save.

seanb’s picture

StatusFileSize
new472 bytes

Rerolled the patch since I had some issues applying the patch in #23.

berdir’s picture

@seanB: Do you have a use case where this is needed? We still don't have a failing tests on 8.4.5+ that needs this change.

seanb’s picture

@Berdir I will have to get back to you on that, was just fixing a build and was not involved in the decision to use this patch. My collegue is not working rest of the week so I'll let you know when I had the chance to talk to him!

jrsouth’s picture

I can confirm that as of pathauto 8.x-1.2 within Drupal 8.5.2, new content nodes created via the GUI still do not have a pathauto pattern applied if the "URL Alias" form element is disabled within that content type's form display settings.

(The correct pathauto pattern is applied after the second "save".)

I'm not sure how to go about creating a test, but I'm happy to provide whatever info I can.

In the meantime, the patch in #25 has fixed the issue. (Exposing the URL Alias widget is not a viable option for us.)

andres.torres’s picture

DJMyles’s picture

I can also confirm that as of pathauto 8.x-1.3 within Drupal 8.6.4, new nodes created via the GUI still do not have a pathauto pattern applied if the "URL Alias" form element is disabled within that content type's form display settings.

The pathauto pattern is only applied after a resave.

Can confirm patch #25 has fixed the issue.

JvE’s picture

MikeHauden’s picture

Confirm the #25 patch still works applied to 1.4 on core 8.7.3.

cgmonroe’s picture

Status: Needs work » Needs review

Enabled testing on current patch.

honza pobořil’s picture

Status: Needs review » Reviewed & tested by the community

#25 works with core 8.7.5 and Pathauto 1.4.0

frob’s picture

Status: Reviewed & tested by the community » Needs work

Looks like the patch still needs failing tests that this patch can fix as per @Berdir in #2946273-26: Alias is not generated when Pathauto widget is hidden

amateescu’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new1.38 KB
new1.84 KB

Here's a test for this. Note that #2756703: URL Alias not saving in some cases does not fix this issue.

The last submitted patch, 37: 2946273-37-test-only.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

  • Berdir committed 13b9c4d on 8.x-1.x authored by amateescu
    Issue #2946273 by rbayliss, amateescu, cgmonroe, seanB: Alias is not...
berdir’s picture

Status: Needs review » Fixed

Thanks.

chris matthews’s picture

Any chance a Pathauto 8.x-1.5 release will ship soon? It would be great to have this in a stable release.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.