Issue #2879293 by digitaldonkey, tobiasb, Mac_Weber, paulocs, Meenakshi_j, stefan.korn, ranjith_kumar_k_u, longwave, alexpott: Make Link URI required if there is Link Text input

(cherry picked from commit c944285b83)
merge-requests/1613/head
catch 2022-01-04 11:26:23 +00:00
parent 66130f62ac
commit a555205277
2 changed files with 95 additions and 2 deletions

View File

@ -229,6 +229,20 @@ class LinkWidget extends WidgetBase {
$element['uri']['#description'] = $this->t('This must be an external URL such as %url.', ['%url' => 'http://example.com']);
}
// 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 = $root = array_shift($parents);
if ($parents) {
$selector = $root . '[' . implode('][', $parents) . ']';
}
$element['uri']['#states']['required'] = [
':input[name="' . $selector . '[' . $delta . '][title]"]' => ['filled' => TRUE],
];
}
$element['title'] = [
'#type' => 'textfield',
'#title' => $this->t('Link text'),
@ -250,10 +264,9 @@ class LinkWidget extends WidgetBase {
if (!$element['title']['#required']) {
// Make title required on the front-end when URI filled-in.
$field_name = $this->fieldDefinition->getName();
$parents = $element['#field_parents'];
$parents[] = $field_name;
$parents[] = $this->fieldDefinition->getName();
$selector = $root = array_shift($parents);
if ($parents) {
$selector = $root . '[' . implode('][', $parents) . ']';

View File

@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\link\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
/**
* Tests link field form states functionality.
*
* @group link
*/
class LinkFieldFormStatesTest extends WebDriverTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'entity_test',
'link',
'node',
'link_test_base_field',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->drupalLogin($this->drupalCreateUser([
'administer entity_test content',
]));
}
/**
* @dataProvider linkFieldFormStatesData
*/
public function testLinkFieldFormStates(string $uri, string $title) {
$this->drupalGet('entity_test/add');
$session = $this->assertSession();
$session->elementNotExists('css', '#edit-links-0-uri[required]');
$session->elementNotExists('css', '#edit-links-0-title[required]');
$page = $this->getSession()->getPage();
if ($uri !== '') {
$page->fillField('links[0][uri]', $uri);
$session->elementNotExists('css', '#edit-links-0-uri[required]');
$session->elementExists('css', '#edit-links-0-title[required]');
}
else {
$page->fillField('links[0][title]', $title);
$session->elementExists('css', '#edit-links-0-uri[required]');
$session->elementNotExists('css', '#edit-links-0-title[required]');
}
}
/**
* Provides data for ::testLinkFieldJSFormStates.
*/
public function linkFieldFormStatesData() {
return [
'Fill uri, keep title empty' => [
'https://drupal.org',
'',
],
'Fill title, keep uri empty' => [
'',
'https://drupal.org',
],
];
}
}