Commit b91e6187 authored by Florent Torregrosa's avatar Florent Torregrosa
Browse files

Issue #3311464 by Grimreaper, G4MBINI, DuaelFr, barig, ademarco, drclaw,...

Issue #3311464 by Grimreaper, G4MBINI, DuaelFr, barig, ademarco, drclaw, pdureau: Sort UI Pattern definitions.
parent c5016904
Loading
Loading
Loading
Loading
+55 −55
Original line number Diff line number Diff line
- name: 'simple'
  label: 'Simple'
  description: 'A simple pattern'
  has_variants: false
  preview: '<div class="pattern-simple">Simple pattern field</div>'
  fields:
  - name: 'field'
    type: 'string'
    label: 'Field'
    description: 'Field description'

- name: 'with_variants'
  label: 'With variants'
  description: 'Pattern with variants'
  has_variants: true
  preview: ~
  fields:
  - name: 'field'
    type: 'string'
    label: 'Field'
    description: 'Field description'
  variants:
  - meta:
      name: 'one'
      label: 'One'
      description: 'First variant'
    preview: '<div class="pattern-with-variant-one">With variants pattern field</div>'
  - meta:
      name: 'two'
      label: 'Two'
      description: 'Second variant'
    preview: '<div class="pattern-with-variant-two">With variants pattern field</div>'

- name: 'with_custom_theme_hook'
  theme hook: 'custom_theme_hook'
  label: 'With custom theme hook'
  description: 'Pattern with custom theme hook.'
  has_variants: false
  preview: 'With custom theme hook: Pattern field value'
  fields:
  - name: 'field'
    type: 'string'
    label: 'Field'
    description: 'Field description'

- name: 'button'
  label: 'Button'
  description: 'A simple button.'
@@ -76,6 +31,29 @@
        description: 'A button for dangerous operations.'
      preview: '<a href="http://example.com" target="_blank" class="btn btn-danger button">Delete</a>'

- name: 'simple'
  label: 'Simple'
  description: 'A simple pattern'
  has_variants: false
  preview: '<div class="pattern-simple">Simple pattern field</div>'
  fields:
  - name: 'field'
    type: 'string'
    label: 'Field'
    description: 'Field description'

- name: 'with_custom_theme_hook'
  theme hook: 'custom_theme_hook'
  label: 'With custom theme hook'
  description: 'Pattern with custom theme hook.'
  has_variants: false
  preview: 'With custom theme hook: Pattern field value'
  fields:
  - name: 'field'
    type: 'string'
    label: 'Field'
    description: 'Field description'

- name: 'with_local_libraries'
  label: 'With local libraries'
  description: 'Pattern defining local libraries'
@@ -97,3 +75,25 @@
    type: 'string'
    label: 'Field'
    description: 'Field description'

- name: 'with_variants'
  label: 'With variants'
  description: 'Pattern with variants'
  has_variants: true
  preview: ~
  fields:
    - name: 'field'
      type: 'string'
      label: 'Field'
      description: 'Field description'
  variants:
    - meta:
        name: 'one'
        label: 'One'
        description: 'First variant'
      preview: '<div class="pattern-with-variant-one">With variants pattern field</div>'
    - meta:
        name: 'two'
        label: 'Two'
        description: 'Second variant'
      preview: '<div class="pattern-with-variant-two">With variants pattern field</div>'
+35 −0
Original line number Diff line number Diff line
@@ -73,6 +73,7 @@ class UiPatternsManager extends DefaultPluginManager implements PluginManagerInt
        $definitions[$definition['id']] = $definition;
        unset($definitions[$id]);
      }
      $definitions = $this->getSortedDefinitions($definitions);
      $this->setCachedDefinitions($definitions);
    }
    return $definitions;
@@ -107,4 +108,38 @@ class UiPatternsManager extends DefaultPluginManager implements PluginManagerInt
    return $this->moduleHandler->moduleExists($provider) || $this->themeHandler->themeExists($provider);
  }

  /**
   * Sort pattern definitions by label then ID.
   *
   * @param array $definitions
   *   The patterns plugin definitions.
   *
   * @return array
   *   The sorted definitions.
   */
  protected function getSortedDefinitions(array $definitions) {
    // Sort by label ignoring parenthesis.
    uasort($definitions, function ($item1, $item2) {
      $sort_result = 0;

      if (isset($item1['label'], $item2['label'])) {
        // Ignore parenthesis.
        $label1 = str_replace(['(', ')'], '', $item1['label']);
        $label2 = str_replace(['(', ')'], '', $item2['label']);
        $sort_result = $label1 <=> $label2;
      }

      // Fallback to pattern ID.
      if ($sort_result === 0) {
        // In case the pattern ID starts with an underscore.
        $id1 = str_replace('_', '', $item1['id']);
        $id2 = str_replace('_', '', $item2['id']);
        $sort_result = $id1 <=> $id2;
      }
      return $sort_result;
    });

    return $definitions;
  }

}
+2 −0
Original line number Diff line number Diff line
aaa:
  label: '(Label 2)'
+2 −0
Original line number Diff line number Diff line
bbb:
  label: 'Label 2'
Loading