Skip to content
Snippets Groups Projects
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
Branches
Tags 8.x-1.4
No related merge requests found
Showing
with 164 additions and 56 deletions
- 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>'
......@@ -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;
}
}
aaa:
label: '(Label 2)'
bbb:
label: 'Label 2'
ccc:
label: 'Label 1'
lll:
label: 'Label 3'
zzz:
label: 'Label 1'
name: 'UI Patterns Definitions Sort Test'
description: 'Provides test plugins.'
type: module
package: 'Testing'
name: 'UI Patterns field source test'
name: 'UI Patterns Field Source Test'
description: 'Provides test plugin.'
type: module
package: 'Testing'
<?php
namespace Drupal\Tests\ui_patterns\Kernel;
use Drupal\ui_patterns\UiPatterns;
/**
* @coversDefaultClass \Drupal\ui_patterns\UiPatternsManager
*
* @group ui_patterns
*/
class UiPatternsManagerSortingTest extends AbstractUiPatternsTest {
/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
'ui_patterns',
'ui_patterns_library',
'ui_patterns_definitions_sort_test',
];
/**
* Test UiPatternsManager::getDefinitions.
*
* Source:
* aaa: (Label 2)
* bbb: Label 2
* ccc: Label 1
* lll: Label 3
* zzz: Label 1
*
* Expected sort:
* ccc
* zzz
* aaa
* bbb
* lll
*
* @covers ::getSortedDefinitions
*/
public function testDefinitionsSorting() {
$manager = UiPatterns::getManager();
$definitions = $manager->getDefinitions();
$keys = array_keys($definitions);
$expected_keys_order = [
'ccc',
'zzz',
'aaa',
'bbb',
'lll',
];
$this->assertEquals($expected_keys_order, $keys);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment