You are here

public function YamlFormElementManager::getSortedDefinitions in YAML Form 8

Gets sorted plugin definitions.

Parameters

array[]|null $definitions: (optional) The plugin definitions to sort. If omitted, all plugin definitions are used.

string $sort_by: The property to sort plugin definitions by. Only 'label' and 'category' are supported. Defaults to label.

Return value

array[] An array of plugin definitions, sorted by category and label.

Overrides CategorizingPluginManagerTrait::getSortedDefinitions

1 call to YamlFormElementManager::getSortedDefinitions()
YamlFormElementManager::getInstances in src/YamlFormElementManager.php
Get all available form element plugin instances.

File

src/YamlFormElementManager.php, line 140

Class

YamlFormElementManager
Provides a plugin manager for form element plugins.

Namespace

Drupal\yamlform

Code

public function getSortedDefinitions(array $definitions = NULL, $sort_by = 'label') {
  $definitions = isset($definitions) ? $definitions : $this
    ->getDefinitions();
  switch ($sort_by) {
    case 'category':
      uasort($definitions, function ($a, $b) use ($sort_by) {
        return strnatcasecmp($a['category'] . '-' . $a[$sort_by], $b['category'] . '-' . $b[$sort_by]);
      });
      break;
    default:
      uasort($definitions, function ($a, $b) use ($sort_by) {
        return strnatcasecmp($a[$sort_by], $b[$sort_by]);
      });
      break;
  }
  return $definitions;
}