Problem/Motivation

When adding a node or taxonomy term with a "Url Alias" that already exists the error message "The alias is already in use." is displayed but the 'error' CSS class is not applied to the 'alias' element. This is because path_form_element_validate does not send correct $name parameter to form_set_error.

Proposed resolution

Change path_form_element_validate to call form_set_error with correct $name parameter
From

    if ($query->execute()->fetchField()) {
      form_set_error('alias', t('The alias is already in use.'));
    }

to

    if ($query->execute()->fetchField()) {
      form_set_error('path][alias', t('The alias is already in use.'));
    }

This cause the element to have the 'error' CSS class.

Patch coming...

Original report by tedbow

Comments

tedbow’s picture

Status: Active » Needs review
StatusFileSize
new513 bytes

Attaching patch

Niklas Fiekas’s picture

Status: Needs review » Needs work

When we have $element available anyway, we should rather use form_error(), as in this example: http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....

Niklas Fiekas’s picture

Version: 7.x-dev » 8.x-dev
Issue tags: +Needs backport to D7

Also against 8.x first, then backport.

tedbow’s picture

Status: Needs work » Needs review
StatusFileSize
new524 bytes

Ok, I have attached the D8 patch using form_error instead form_set_error.
Thanks for the help.

Niklas Fiekas’s picture

Status: Needs review » Reviewed & tested by the community

Thank you. The patch looks good and solves the problem.

tedbow’s picture

No problem. Glad I could finally give something back(however small) to core : )

Should submit a new D7 patch here or would that a new issue?

Niklas Fiekas’s picture

You could already prepare a 7.x patch and upload it. Since it will fail the automated testing when tested in an 8.x issue, you could name it something-d7.patch, so that it will be ignored by the testbots.

We also have a "patch (to be ported)" issue status that is used when an issue has been fixed but no backport is available by then, so that no new issue must be created.

tedbow’s picture

Thanks for explanations. I have attached the D7 patch.

catch’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Thanks for the patch. We should write an automated test for this though before committing.

Niklas Fiekas’s picture

@tedbow: Should I do that, or do you want to write a test case? Do you need help there?

tedbow’s picture

@Niklas Fiekas would I write a test that tries to use the same alias twice and then test the html response to see that html element has the "error" class applied? If so could you point me to an existing test that submits a form and checks html response?
Thanks for your help

Niklas Fiekas’s picture

Have a look at:

  // Create a simpletest_example node using the node form
  public function testSimpleTestExampleCreate() {
    // Create node to edit.
    $edit = array();
    $edit['title'] = $this->randomName(8);
    $edit["body[und][0][value]"] = $this->randomName(16);
    $this->drupalPost('node/add/simpletest-example', $edit, t('Save'));
    $this->assertText(t('Simpletest Example Node Type @title has been created.', array('@title' => $edit['title'])));
  }

from the example module: http://drupalcode.org/project/examples.git/blob_plain/refs/heads/8.x-1.x....

  1. It builds an $edit array of POST variables.
  2. Then drupalPosts's it using the url and the name of the submit button.
  3. After the POST request you can assertText to check for a text on the page and assertRaw to check for raw text, including html. There are also a lot more possible assertions: http://api.drupal.org/api/drupal/modules--simpletest--drupal_web_test_ca....

This is a real world example from the core test suite:
http://api.drupal.org/api/drupal/modules--node--node.test/function/PageE.... Click "View source" at the bottom of http://api.drupal.org/api/drupal/modules--node--node.test/class/PageEdit... for the full test case class.

tedbow’s picture

Thanks, a lot. 1 other question should the test be added to the original patch. Or should they be separate?

Niklas Fiekas’s picture

Both, probably. Just the testcase, so that we see the problem actually exists - we're expecting that test to fail. And a combined patch, that should pass. The latter will get comitted, eventually.

Niklas Fiekas’s picture

Component: node system » path.module
Priority: Minor » Normal
Status: Needs work » Needs review
Issue tags: -Needs tests +Novice
StatusFileSize
new1.66 KB
new1.15 KB

To push this forward: Here's a test that fails without the fix and should pass with tedbow's fix.

tedbow’s picture

Niklas Fiekas, Thanks for doing this, got sidetracked on another project. I will also watch and to make sure 2nd patch works.

tedbow’s picture

Niklas Fiekas, Looks like the test with patch worked. Does anything else need to happen with this?

Niklas Fiekas’s picture

Yeah ... it needs to be reviewed. Set it back to "needs work" when you find coding style issues or something doesn't work as expected. Someone who didn't yet work on this can also mark it as "reviewed & tested by the community" when everything looks good. That is the last step before committing.

xjm’s picture

+++ b/core/modules/path/path.testundefined
@@ -162,6 +162,23 @@ class PathTestCase extends DrupalWebTestCase {
+   * Tests that tries to create a duplicate alias are caught by validation.

I had to read this a couple times to understand it; I'd suggest:

Tests that duplicate aliases fail validation.

Looks good other than that. Thanks!

Edit: Screenshots and manual testing might be a good idea for this issue, too.

Niklas Fiekas’s picture

Ok, thanks for reviewing.

Doxygen reworded, these are the screenshots:
duplicate-path-alias-before.png
(before)
duplicate-path-alias-after.png
(after)

xjm’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Novice

This looks great to me. (I'm assuming tests will pass; the queue is a bit backed up.) Thanks @Niklas Fiekas and @tedbow!

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Great little bug fix, great little test! :)

Committed and pushed to 8.x and 7.x. Thanks!

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