Problem/Motivation

Follow-up of #2613924: Link text isn't marked as required

When the user creates a link field with URI not required and Link text required it is expected to make the URI required in case the user inputs the Link text.

Actual

User inputs only the Link text at the node edit form and saves the node.
The node node is saved without the link data.

Expected

Require the link URI in case the user inputs the Link text

Proposed resolution

Make URI required if there is Link text input.

CommentFileSizeAuthor
#36 screencast-www.drupal.org-2021.11.23-16_02_38.gif4.08 MBpaulocs
#36 2879293-36.patch3.97 KBpaulocs
#36 interdiff-32-36.txt808 bytespaulocs
#32 interdiff-2879293-31-32.txt780 bytestobiasb
#32 2879293-32.patch3.98 KBtobiasb
#31 2879293-31.patch3.98 KBtobiasb
#29 After--patch--pic--2879293.png28.59 KBvikashsoni
#29 Before--patch--pic--2879293.png31.89 KBvikashsoni
#26 afterpatch.png88.97 KBRinku Jacob 13
#26 beforepatch.png87.32 KBRinku Jacob 13
#25 interdiff_18-25.txt1.29 KBMeenakshi_j
#25 2879293-25.patch1.8 KBMeenakshi_j
#22 Before_patch#18.png42.95 KBsonam.chaturvedi
#22 After_patch#18.png48.86 KBsonam.chaturvedi
#19 before--patch.jpg204.81 KBranjith_kumar_k_u
#19 After---patch.jpg184.85 KBranjith_kumar_k_u
#18 make_uri_required_on_the_front-end_when_title_filled_in-3203656-002.patch1.28 KBstefan.korn
#15 require_uri_if_link_text-2879293-15.patch877 bytesdigitaldonkey
#14 require_uri_if_link_text-2879293-14.patch2.8 KBdigitaldonkey
#13 require_uri_if_link_text-2879293-13.patch946 bytesdigitaldonkey
#3 require_uri_if_link_text-2879293-3.patch3.53 KBMac_Weber
2613924-followup.patch3.53 KBMac_Weber
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Mac_Weber created an issue. See original summary.

Mac_Weber’s picture

Status: Active » Needs review

.

Mac_Weber’s picture

Title: Make Link URI marked as required if there is Link Text input » Make Link URI required if there is Link Text input
FileSize
3.53 KB
Mac_Weber’s picture

Status: Needs review » Needs work

The last submitted patch, 3: require_uri_if_link_text-2879293-3.patch, failed testing.

Mac_Weber’s picture

Status: Needs work » Postponed

Postponed in favor of the parent issue. The patch file was created over the last one in the parent issue and I would like to use the same test function.

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

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

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.

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

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

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

digitaldonkey’s picture

Updated Patch (only the front end Part was missing. Backend validation was comitted somehow else).

digitaldonkey’s picture

Re-add the new test from patch #3.
Sorry. This didn't work. #15 works, but does not have an additional test.

digitaldonkey’s picture

tobiasb’s picture

Assigned: Mac_Weber » Unassigned
Status: Postponed » Needs review

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

stefan.korn’s picture

Slightly changed patch taken from #3203656: Make uri required on the front-end when title filled-in. The patch from #15 does only apply if the link title is set to required, but it should also apply if the link title is set to optional. Link uri will still be required in that case.

ranjith_kumar_k_u’s picture

FileSize
184.85 KB
204.81 KB

The last patch works fine , when we type anything on the link text field the red asterisk will show above the URL field

Before patch
before patch

After patch
after patch

RTBC

stefan.korn’s picture

anyone interested in this behavior without patching core can use Link Field Tweak module until this might get into core.

sonam.chaturvedi’s picture

Assigned: Unassigned » sonam.chaturvedi
sonam.chaturvedi’s picture

Assigned: sonam.chaturvedi » Unassigned
Status: Needs review » Reviewed & tested by the community
FileSize
48.86 KB
42.95 KB

Verified and tested patch#18. Patch applied successfully.

Testing Steps:
1. Goto "Create content" page with Link field
2. Check Link field is optional
3. Enter Link text field
4. Check URI is not required
5. Now apply patch#18
6. Again enter Link text field
7. Verify URI is required

Test Result: On entering value in Link text field > URI field is required

Before Patch:
before patch18

After Patch:
after patch 18

Moving to RBTC

alexpott’s picture

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

Thanks for filing this bug report and for fixing the issue. Bug fixing is very valuable. In order to commit a bug fix, we need an automated test to prove that we've fixed the bug and ensure that we don't break it again in the future. For more information about writing tests in Drupal, see the following links:

  1. https://www.drupal.org/docs/testing/phpunit-in-drupal/phpunit-javascript...
  2. https://api.drupal.org/api/drupal/core%21core.api.php/group/testing/9.1.x
+++ b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php
@@ -229,6 +229,22 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
+    // Make uri required on the front-end when title filled-in.
+    if ((!$this->isDefaultValueWidget($form_state)) && !($this->getFieldSetting('title') === DRUPAL_DISABLED) && !$element['uri']['#required']) {
+      $field_name = $this->fieldDefinition->getName();
+
+      $parents = $element['#field_parents'];
+      $parents[] = $field_name;
+      $selector = $root = array_shift($parents);
+      if ($parents) {
+        $selector = $root . '[' . implode('][', $parents) . ']';
+      }
+
+      $element['uri']['#states']['required'] = [
+        ':input[name="' . $selector . '[' . $delta . '][title]"]' => ['filled' => TRUE],
+      ];
+    }

I think several minor tweaks can be made to this code.

    // Make uri required on the front-end when title filled-in.
    if (!$this->isDefaultValueWidget($form_state) && $this->getFieldSetting('title') !== DRUPAL_DISABLED && !$element['uri']['#required']) {
      $parents = $element['#field_parents'];
      $parents[] = $this->fieldDefinition->getName();
      $selector = array_shift($parents);
      if ($parents) {
        $selector .= '[' . implode('][', $parents) . ']';
      }
      $element['uri']['#states']['required'] = [
        ':input[name="' . $selector . '[' . $delta . '][title]"]' => ['filled' => TRUE],
      ];
    }

The if has lass brackets and work out if they mean anything. And less local variables.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Meenakshi_j’s picture

Status: Needs work » Needs review
FileSize
1.8 KB
1.29 KB

Automation testing needs to be required to implement this patch.

Rinku Jacob 13’s picture

FileSize
87.32 KB
88.97 KB

Verified and tested patch #25 for drupal 9.3.x-dev.Patch applied successfully thanks@Meenakshi_j. Adding screenshot for reference.

Meenakshi_j’s picture

Status: Needs review » Needs work

Required Automation Testing.

vakulrai’s picture

Status: Needs work » Needs review

The LinkFiledTest has already a test written for the logic above, I think its good enough.

if ($title_setting === DRUPAL_OPTIONAL) {
          // Verify that the URL is required, if the link text is non-empty.
          $edit = [
            "{$field_name}[0][title]" => 'Example',
          ];
          $this->submitForm($edit, 'Save');
          $this->assertSession()->pageTextContains('The URL field is required when the Link text field is specified.');
        } 

and After validating, the form is submitted with url only.

$value = 'http://www.example.com/';
    $edit = [
      "{$field_name}[0][uri]" => $value,
      "{$field_name}[0][title]" => '',
    ];  
vikashsoni’s picture

Applied patch #25 working fine and applied successfully
Patch looks good for me
For ref sharing screenshots....

longwave’s picture

Status: Needs review » Needs work

@alexpott's comments in #23 have not yet been addressed.

tobiasb’s picture

FileSize
3.98 KB
780 bytes

Fixed CS and change theme to stark.

longwave’s picture

This patch fixes the frontend but is there any backend validation done here? The states API for required only shows/hides the asterisk, it doesn't actually do any validation when the form is submitted, as far as I'm aware.

+++ b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php
@@ -229,6 +229,21 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
+    if ((!$this->isDefaultValueWidget($form_state)) && !($this->getFieldSetting('title') === DRUPAL_DISABLED) && !$element['uri']['#required']) {

This line is still confusing and @alexpott's changes haven't been done.

tobiasb’s picture

The server side is tested via core/modules/link/tests/src/Functional/LinkFieldTest.php:315.

So it is not possible to save the link just with the link text (without the patch).

And I do not see what is missing.

But I believe a better test class/method name would be better.

Class: LinkWidgetConditionalFormFieldTest
method: testLinkRequiredIsSet

?

paulocs’s picture

Assigned: Unassigned » paulocs
paulocs’s picture

I removed the unnecessary blank line and unnecessary brackets.

Btw @tobiasb is right about the server side validation. It has already.

I'm attaching a new patch, interdiff and a gif to confirm the server side validation (before patch is applied) which it has tests already.
Please review it.

longwave’s picture

Status: Needs review » Reviewed & tested by the community

Thanks! The new test looks good to me and proves that the new #states configuration works as expected.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 36: 2879293-36.patch, failed testing. View results

longwave’s picture

Status: Needs work » Reviewed & tested by the community

Random fail, back to RTBC.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 36: 2879293-36.patch, failed testing. View results

Spokje’s picture

Status: Needs work » Reviewed & tested by the community

Back to RTBC per #40 after #3255836: Test fails due to Composer 2.2 solved the unrelated test failure.

  • catch committed c944285 on 10.0.x
    Issue #2879293 by digitaldonkey, tobiasb, Mac_Weber, paulocs,...

  • catch committed 17c529f on 9.4.x
    Issue #2879293 by digitaldonkey, tobiasb, Mac_Weber, paulocs,...

  • catch committed a555205 on 9.3.x
    Issue #2879293 by digitaldonkey, tobiasb, Mac_Weber, paulocs,...
catch’s picture

Version: 9.4.x-dev » 9.3.x-dev
Status: Reviewed & tested by the community » Fixed

I removed some issue credit where screenshots were uploaded duplicating previous manual testing - this only needs to be done once (or not at all if there is sufficient automated test coverage).

Committed/pushed to 10.0.x and cherry-picked back through to 9.3.x, thanks!

Status: Fixed » Closed (fixed)

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