Problem/Motivation

The 'Select list' add mode can render an option list with a single item when a single type is allowed in its field configuration. Since there are no options to select, this interface element can be hidden instead.

This matches the implementation in D7 added in #2364003: Hide bundle selection on field widget if only one bundle is allowed

Proposed resolution

Hide type selection in 'Select list' add mode when only one type is allowed.

Remaining tasks

  1. Write a patch
  2. Review
  3. Commit

User interface changes

The 'Select list' add mode hides the option list when a single type is allowed in its field configuration.

API changes

None.

Data model changes

None.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

idebr created an issue. See original summary.

idebr’s picture

Attached patch hides the type selection in 'Select list' add mode when only one type is allowed.

The last submitted patch, 2: 3072638-2-test-only.patch, failed testing. View results

Berdir’s picture

+++ b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php
@@ -1624,13 +1624,20 @@ class ParagraphsWidget extends WidgetBase {
+    // Hide the type selection if only one type is allowed.
+    if (count($select_options) === 1) {
+      $add_more_elements['add_more_select']['#type'] = 'hidden';
+      $add_more_elements['add_more_select']['#value'] = key($select_options);
+    }

#type value might make more sense here, hidden is sent to the browser and could be changed.

Is the use case that the allowed paragraphs might vary per user?

If it's always just one then you can also use a different add mode like dropbutton, that will then just have a single button named "Add X"

idebr’s picture

#4.1 I did not know '#type' => 'value' was a thing, but that is a better fit for this code indeed. The primary goal is to reduce interface clutter.

#4.2 To provide some context: the page requires structured data in different fields, but using the same paragraph type. A live page is available at https://www.uu.nl/en/newton-hall

I have taken screenshots of the different form configurations:

#1 Add mode 'Select list', implementation in 8.x-1.x HEAD:

#2 Add mode 'Buttons'

#3 Add mode 'Select list': with patch #5

Option #3 provides context to the buttons and hides the generic Paragraph type label from the editor.

The last submitted patch, 5: 3072638-5-test-only.patch, failed testing. View results

Berdir’s picture

Ok, reusing the same paragraph type in different contexts is an interesting use case.

Would be nice to get the test moved to \Drupal\Tests\paragraphs\Functional\ParagraphsExperimentalWidgetButtonsTest, one thing less we have to convert to phpunit later on. Hopefully the tests shouldn't be too different.

idebr’s picture

#7 I moved the test to \Drupal\Tests\paragraphs\Functional\ParagraphsExperimentalWidgetButtonsTest. Both are based on PHPUnit BrowserTestBase, so the code itself is identical to #5.

The last submitted patch, 8: 3072638-8-test-only.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

  • Berdir committed 01f070c on 8.x-1.x authored by idebr
    Issue #3072638 by idebr: Hide type selection in 'Select list' add mode...
Berdir’s picture

Status: Needs review » Fixed

I was sure the test was in a simpletest-based test, sorry.

Thanks, committed.

Status: Fixed » Closed (fixed)

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