nuvoleweb / ui_patterns

[NOTE] Development has moved to https://drupal.org/project/ui_patterns
https://drupal.org/project/ui_patterns
GNU General Public License v2.0
85 stars 56 forks source link

Improve normalizing of UI Patterns submitted configuration #320

Open nedjo opened 3 years ago

nedjo commented 3 years ago

In PatternDisplayFormTrait:processFormStateValues() the submitted values from a UI Patterns configuration form are normalized before being saved.

There are a couple of places where edge cases lead to cruft being saved.

First, the $settings['variants'] key is unset only conditionally, that is, in the case that there's a selected variant:

    if (isset($settings['variants']) && isset($settings['variants'][$settings['pattern']])) {
      $settings['pattern_variant'] = $settings['variants'][$settings['pattern']];
      unset($settings['variants']);
    }

The result is that, when the current pattern doesn't have a selected variant (possibly because it doesn't support variants), the (always superfluous) 'variants' data are saved for all available patterns.

Proposed fix:

    if (isset($settings['variants'])) {
      if (isset($settings['variants'][$settings['pattern']]) {
        $settings['pattern_variant'] = $settings['variants'][$settings['pattern']];
      }
      unset($settings['variants']);
    }

Second, there are edge cases where the code normalizing the $settings['pattern_mapping'] can also leave cruft.

The current code is:

    // Normalize only when necessary.
    if (isset($settings['pattern_mapping'][$settings['pattern']]['settings'])) {
      ...
    }

If there is no selected pattern - for example, in a context where the pattern is not required - the non-normalized data for all patterns are saved.

Proposed fix:

    // Normalize only when necessary.
    if (isset($settings['pattern_mapping'][$settings['pattern']]['settings'])) {
      ...
    }
    else {
      unset($settings['pattern_mapping']);
    }