In #2829671: Implement edit perspectives with tabs when editing, tabs for perspectives were introduced. Unfortunately the implementation is not compatible with the Form API #states in Drupal core. The Form API #states property can hide certain fields depending on the value of another field. Comment #2868155-51: Add paragraph bundle to widget forms to allow easier editing of paragraph forms describes how to do that in a paragraphs subform using hook_field_widget_WIDGET_TYPE_form_alter(). However, when you load a form and the States API hides a field, paragraphs.admin.js comes along and reveals the hidden field again when it calls $parContent.show(); in the setUpTabs() function.

I think a solution should aim to hide not the fields on a tab, but a wrapper around those fields.

I'm marking this issue as major, because it is a regression and breaks compatibility with a core API.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

marcvangend created an issue. See original summary.

marcvangend’s picture

Issue summary: View changes
marcvangend’s picture

Status: Active » Needs review
FileSize
350 bytes

This patch fixes the issue for my situation.

As you can see it's a very simple solution, even simpler than adding a wrapper like I suggested in the initial post. I have to thank @berdir for suggesting this approach. In fact, I don't quite understand why it works in the first place - if we're calling show() on elements that are not hidden, why are we show()ing them to begin with? But anyway, it works.

If you want to reproduce the problem: I tested this patch with the combination of the lockable-behavior from the paragraphs_collection module and a form alter which looks like this:

/**
 * Implements hook_field_widget_WIDGET_TYPE_form_alter().
 */
function mymodule_field_widget_paragraphs_form_alter(&$element, &$form_state, $context) {
  if (!empty($element['subform']['field_dependee'])) {
    $parents_array = $element['subform']['#parents'];
    $parents = array_shift($parents_array) . '[' . implode('][', $parents_array) . ']';
    if (!empty($element['subform']['field_dependent'])) {
      $element['subform']['field_dependent']['#states'] = [
        'visible' => [
          ':input[name="' . $parents . '[field_dependee]"]' => [
            'value' => (string) 'some value',
          ],
        ],
      ];
    }
  }
}
hudri’s picture

Had the same problem with hidden fields being revealed again, using this patch fixed it for me.

miro_dietiker’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Combining our tabs with states needs test coverage. Otherwise it will break again with future "improvements".

Eli-T’s picture

Just a +1 for this - hit the issue where we were using states API with paragraphs, and wanted to move to the experimental widget to mitigate the clutter. This patch allowed us to do that. Thanks @marcvangend!

miro_dietiker’s picture

Polling...

nsciacca’s picture

+1 this worked for me and saved a headache or two, thanks!

carma03’s picture

+1 Worked successfully. Thanks!

Daniel Wentsch’s picture

+1 and thanks a lot for sharing. I was going mad because I thought my implementation of hook_conditionals_field_widget_paragraphs_form_alter was wrong.

aludescher’s picture

+1 this fixed states API with paragraphs edit forms in 8.x-1.5 (and also in previous versions). Thank you @marcvangend!

jibran’s picture

Status: Needs work » Reviewed & tested by the community

Can we please commit the fix and keep the issue open to add the tests? It has been more than a year and nobody volunteered to write the tests. We might as well have a fix in the meantime.

miro_dietiker’s picture

Status: Reviewed & tested by the community » Fixed
Related issues: +#3074269: Provide tests for Form API #states support

Finally committed (with a reroll) and create follow-up for test coverage.

Status: Fixed » Closed (fixed)

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

arpitk’s picture

I am still facing this fields showing up issue and the patch is not working for me.