Verified Commit 7d195a3a authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3145850 by smustgrave, natedouglas, nod_, Gauravvvv, s_leu, amateescu,...

Issue #3145850 by smustgrave, natedouglas, nod_, Gauravvvv, s_leu, amateescu, pankaj.singh, Abhijith S, quietone, jungle: Required summary element hidden when other text-with-summary fields do not require summary

(cherry picked from commit f908b84f)
parent c49b7360
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -81,7 +81,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
      '#title' => $this->t('Summary'),
      '#rows' => $this->getSetting('summary_rows'),
      '#description' => !$required ? $this->t('Leave blank to use trimmed value of full text as the summary.') : '',
      '#attributes' => ['class' => ['js-text-summary', 'text-summary']],
      '#attributes' => ['class' => ['text-summary']],
      '#prefix' => '<div class="js-text-summary-wrapper text-summary-wrapper">',
      '#suffix' => '</div>',
      '#weight' => -10,
@@ -89,6 +89,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
    ];

    if (!$this->getSetting('show_summary') && !$required) {
      $element['summary']['#attributes']['class'][] = 'js-text-summary';
      $element['summary']['#attached']['library'][] = 'text/drupal.text';
    }

+42 −2
Original line number Diff line number Diff line
@@ -94,10 +94,50 @@ public function testTextSummaryBehavior() {
    ]);

    $this->drupalGet('node/' . $node->id() . '/edit');
    $summary_field = $this->getSession()->getPage()->findField('edit-body-0-summary');

    $this->assertEquals(TRUE, $summary_field->isVisible());
  }

  /**
   * Tests that the textSummary behavior is not run for required summary fields.
   */
  public function testTextSummaryRequiredBehavior() {
    // Test with field defaults.
    $this->assertSummaryToggle();

    // Create a second field with a required summary.
    $field_name = mb_strtolower($this->randomMachineName());
    $field_storage = FieldStorageConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'node',
      'type' => 'text_with_summary',
    ]);
    $field_storage->save();
    FieldConfig::create([
      'field_storage' => $field_storage,
      'bundle' => 'page',
      'label' => $this->randomMachineName() . '_label',
      'settings' => [
        'display_summary' => TRUE,
        'required_summary' => TRUE,
      ],
    ])->save();

    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');

    $display_repository->getFormDisplay('node', 'page')
      ->setComponent($field_name, [
        'type' => 'text_textarea_with_summary',
      ])
      ->save();

    $this->drupalGet('node/add/page');
    $page = $this->getSession()->getPage();
    $summary_field = $page->findField('edit-body-0-summary');
    $summary_field = $page->findField('edit-' . $field_name . '-0-summary');

    $this->assertEquals(TRUE, $summary_field->isVisible(), 'Non-empty summary field is shown by default.');
    $this->assertEquals(TRUE, $summary_field->isVisible());
  }

}