file SettingManager.php

Namespace

Drupal\bootstrap\Plugin
  1. <?php
  2. namespace Drupal\bootstrap\Plugin;
  3. use Drupal\bootstrap\Theme;
  4. use Drupal\bootstrap\Utility\SortArray;
  5. /**
  6. * Manages discovery and instantiation of Bootstrap theme settings.
  7. *
  8. * @ingroup plugins_setting
  9. */
  10. class SettingManager extends PluginManager {
  11. /**
  12. * Provides the order of top-level groups.
  13. *
  14. * @var string[]
  15. */
  16. protected static $groupOrder = [
  17. 'general',
  18. 'components',
  19. 'javascript',
  20. 'cdn',
  21. 'advanced',
  22. ];
  23. /**
  24. * Constructs a new \Drupal\bootstrap\Plugin\SettingManager object.
  25. *
  26. * @param \Drupal\bootstrap\Theme $theme
  27. * The theme to use for discovery.
  28. */
  29. public function __construct(Theme $theme) {
  30. parent::__construct($theme, 'Plugin/Setting', 'Drupal\bootstrap\Plugin\Setting\SettingInterface', 'Drupal\bootstrap\Annotation\BootstrapSetting');
  31. $this->setCacheBackend(\Drupal::cache('discovery'), 'theme:' . $theme->getName() . ':setting', $this->getCacheTags());
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. protected function sortDefinitions(array &$definitions) {
  37. uasort($definitions, [$this, 'sort']);
  38. }
  39. /**
  40. * Sorts the setting plugin definitions.
  41. *
  42. * Sorts setting plugin definitions in the following order:
  43. * - First by top level group.
  44. * - Then by sub-groups.
  45. * - Then by weight.
  46. * - Then by identifier.
  47. *
  48. * @param array $a
  49. * First plugin definition for comparison.
  50. * @param array $b
  51. * Second plugin definition for comparison.
  52. *
  53. * @return int
  54. * The comparison result.
  55. */
  56. public static function sort(array $a, array $b) {
  57. $aIndex = static::getTopLevelGroupIndex($a);
  58. $bIndex = static::getTopLevelGroupIndex($b);
  59. // Top level group isn't the same, sort by index.
  60. if ($aIndex !== $bIndex) {
  61. return $aIndex - $bIndex;
  62. }
  63. // Next sort by all groups (sub-groups).
  64. $result = SortArray::sortByKeyString($a, $b, 'groups');
  65. // Groups are the same.
  66. if ($result === 0) {
  67. // Sort by weight.
  68. $result = SortArray::sortByWeightElement($a, $b);
  69. // Weights are the same.
  70. if ($result === 0) {
  71. // Sort by plugin identifier.
  72. $result = SortArray::sortByKeyString($a, $b, 'id');
  73. }
  74. }
  75. return $result;
  76. }
  77. /**
  78. * Retrieves the index of the top level group.
  79. *
  80. * @param array $definition
  81. * A plugin definition.
  82. *
  83. * @return int
  84. * The array index of the top level group.
  85. */
  86. public static function getTopLevelGroupIndex(array $definition) {
  87. $groups = !empty($definition['groups']) ? array_keys($definition['groups']) : [];
  88. $index = array_search(reset($groups), static::$groupOrder);
  89. if ($index === FALSE) {
  90. $index = -1;
  91. }
  92. return $index;
  93. }
  94. }

Classes

Name Description
SettingManager Manages discovery and instantiation of Bootstrap theme settings.