Commit a5552052 authored by catch's avatar catch
Browse files

Issue #2879293 by digitaldonkey, tobiasb, Mac_Weber, paulocs, Meenakshi_j,...

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 c944285b)
parent 66130f62
Loading
Loading
Loading
Loading
+15 −2
Original line number Diff line number Diff line
@@ -229,6 +229,20 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
      $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 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen

      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) . ']';
+80 −0
Original line number Diff line number Diff line
<?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',
      ],
    ];
  }

}