Verified Commit 2b9d2a95 authored by Lauri Timmanee's avatar Lauri Timmanee
Browse files

Issue #3382802 by srishtiiee, kunal.sachdev, marcoliver, ckrina: Disable the...

Issue #3382802 by srishtiiee, kunal.sachdev, marcoliver, ckrina: Disable the 'Remove' button if there is a single row in the allowed values storage setting of the list fields
parent cb6824ae
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -185,6 +185,10 @@ public function storageSettingsForm(array &$form, FormStateInterface $form_state
        '#default_value' => 0,
        '#attributes' => ['class' => ['weight']],
      ];
      // Disable the remove button if there is only one row in the table.
      if ($max === 0) {
        $element['allowed_values']['table'][0]['delete']['#attributes']['disabled'] = 'disabled';
      }
      if ($delta < count($allowed_values)) {
        $query = \Drupal::entityQuery($entity_type_id)
          ->accessCheck(FALSE)
@@ -261,6 +265,10 @@ public static function addMoreAjax(array $form, FormStateInterface $form_state)
    $delta = $element['table']['#max_delta'];
    $element['table'][$delta]['item']['#prefix'] = '<div class="ajax-new-content" data-drupal-selector="field-list-add-more-focus-target">' . ($element['table'][$delta]['item']['#prefix'] ?? '');
    $element['table'][$delta]['item']['#suffix'] = ($element['table'][$delta]['item']['#suffix'] ?? '') . '</div>';
    // Enable the remove button for the first row if there are more rows.
    if ($delta > 0 && isset($element['table'][0]['delete']['#attributes']['disabled']) && !isset($element['table'][0]['item']['key']['#attributes']['disabled'])) {
      unset($element['table'][0]['delete']['#attributes']['disabled']);
    }

    $response = new AjaxResponse();
    $response->addCommand(new InsertCommand(NULL, $element));
+14 −1
Original line number Diff line number Diff line
@@ -454,8 +454,21 @@ public function testRequiredPropertyForAllowedValuesList() {
      $this->createOptionsField($field_type);
      $page = $this->getSession()->getPage();

      // Try to proceed without entering any value.
      $this->drupalGet($this->adminPath);
      // Assert that the delete button for a single row is disabled.
      $this->assertCount(1, $page->findAll('css', '#allowed-values-order tr.draggable'));
      $delete_button_0 = $page->findById('remove_row_button__0');
      $this->assertTrue($delete_button_0->hasAttribute('disabled'), 'Button is disabled');
      $page->findButton('Add another item')->click();
      // Assert that the delete button for the first row is enabled if there are
      // more that one rows.
      $this->assertCount(2, $page->findAll('css', '#allowed-values-order tr.draggable'));
      $this->assertFalse($delete_button_0->hasAttribute('disabled'), 'Button is enabled');
      // Delete a row.
      $delete_button_0->click();
      // Assert that the button is disabled again.
      $this->assertTrue($delete_button_0->hasAttribute('disabled'), 'Button is disabled');
      // Try to proceed without entering any value.
      $page->findButton('Save field settings')->click();

      if ($field_type == 'list_string') {