Problem

Currently, when a link field's "title" element is set to DRUPAL_OPTIONAL, a user can enter link text, leaving the link URI blank, and the form will save, while not saving any link data (since the field is considered empty). This poses a usability problem: users entering content will feel like it "disappears."

Proposed Solution

Add widget validation when the title element is set to DRUPAL_OPTIONAL: if a title is entered but no link, notify the user of this with a form error, rather than silently saving without the field data.

Screenshot of field validation

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mark_fullmer created an issue. See original summary.

mark_fullmer’s picture

The attached patch provides said validation and refactors the existing LinkFieldTest to verify this validation behaves as expected.

rang501’s picture

Hi!
This looks really good improvement and works similarly to the opposite validation (title required), but I think the error should be set on the Url field, not Title.

Testing details

  • Pulled latest core 8.5.x from git and installed it with standard profile.
  • Added the link field to the basic page content type and configured "Allow link text" as optional, field itself optional, "Allowed link types" internal and external.
  • Created new page, filled title field and url field empty, page is saved and title field is lost.
  • Applied the patch, tried again and the validation kicks in, url field was set required, title field highlighted in red.

In my opinion, the Title field should be normal and Url field highlighted with error.

rang501’s picture

Status: Needs review » Needs work
mark_fullmer’s picture

Status: Needs work » Needs review
FileSize
2.85 KB

Alright, rang501, the attached patch sets the form error on the URI rather than the title.

Status: Needs review » Needs work

The last submitted patch, 5: validate-title-no-link_2923496_5.patch, failed testing. View results

mark_fullmer’s picture

Status: Needs work » Needs review
FileSize
2.77 KB
krina.addweb’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
48.3 KB
38.18 KB

@mark_fullmer, Your patch works for me as mentioned in "Proposed Solution"
I Checked your patch by following steps:
> Add Link Field to Article Content Type.
> Add Link Title text & leaving Blank the URL field.
> It fires the Validation to add the value in URL
> Without adding both values, It didn't fire any validation.
> Also, It saves the details while editing the content.

xjm’s picture

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

Thank you for working on this! Glad to see test coverage for the fix, and thanks for the screenshot which is very helpful.

Shouldn't the validation be flagging the URL field, since that's where the error is? Or both of them?

For the message, I'd suggest:

The @url field is required when the @text field is specified.

Also I'm not sure the URL field should be marked required during this validation step. If the field itself is not required, both could be left blank, but this validation might confuse the user into thinking they must enter something when it's also an option to leave both fields blank.

Thanks!

mark_fullmer’s picture

Thanks for the testing, krina.addweb, and thanks for the feedback, xjm. Patch #10 adds the following changes, per xjm's suggestions:

  • Removes the line that makes the URI field required during validation
  • Updates the validation text to read "The @url field is required when the @text field is specified."
  • Sets the validation to flag the URL field

Screenshot of validation

mark_fullmer’s picture

Title: [Link module] Check for title value, no link when title optional » [Link module] Validation for title but no link, when title optional
mark_fullmer’s picture

Issue summary: View changes

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

mark_fullmer’s picture

Component: field system » link.module
lreynaga’s picture

I can confirm that patch in #10 works as described. Code looks solid. Looking at the latest build in Jenkins everything passed, including the LinkFieldTest class (16:05:27 Drupal\Tests\link\Functional\LinkFieldTest 6 passes).

I feel confident enough to consider it reviewed.

Thanks!

lreynaga’s picture

Status: Needs review » Reviewed & tested by the community
catch’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php
@@ -242,6 +253,12 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
 
+    // If a title field is optional, ensure that if it is entered, there is
+    // a corresponding URI entered (instead of having save silently fail).

I had to read this comment a couple of times, would this be clearer:

"Ensure that a URI is always entered when an optional title field is submitted."

dpfitzsi’s picture

Status: Needs work » Needs review
FileSize
3.37 KB

Updated patch to incorporate catch's comments.

I have also added this same functionality when the title field is required. If you just enter a title and try and submit the form will still submit. So this functionality really shouldn't only apply to optional title fields.

lreynaga’s picture

Tested #18 this time with both an optional and required title text, both cases were behaving as described. Moving to RTBC.

lreynaga’s picture

Status: Needs review » Reviewed & tested by the community
dpfitzsi’s picture

Thanks lreynaga for testing the patch!

  • catch committed 7a4a8c9 on 8.6.x
    Issue #2923496 by mark_fullmer, dpfitzsi, krina.addweb: [Link module]...

  • catch committed 0bbf512 on 8.5.x
    Issue #2923496 by mark_fullmer, dpfitzsi, krina.addweb: [Link module]...
catch’s picture

Version: 8.6.x-dev » 8.5.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.6.x and cherry-picked to 8.5.x. Thanks!

Status: Fixed » Closed (fixed)

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