Version 1
[yaffs-website] / web / themes / contrib / bootstrap / src / Plugin / SettingManager.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\bootstrap\Plugin\SettingManager.
5  */
6
7 namespace Drupal\bootstrap\Plugin;
8
9 use Drupal\bootstrap\Theme;
10 use Drupal\Component\Utility\SortArray;
11
12 /**
13  * Manages discovery and instantiation of Bootstrap theme settings.
14  *
15  * @ingroup plugins_setting
16  */
17 class SettingManager extends PluginManager {
18
19   /**
20    * Constructs a new \Drupal\bootstrap\Plugin\SettingManager object.
21    *
22    * @param \Drupal\bootstrap\Theme $theme
23    *   The theme to use for discovery.
24    */
25   public function __construct(Theme $theme) {
26     parent::__construct($theme, 'Plugin/Setting', 'Drupal\bootstrap\Plugin\Setting\SettingInterface', 'Drupal\bootstrap\Annotation\BootstrapSetting');
27     $this->setCacheBackend(\Drupal::cache('discovery'), 'theme:' . $theme->getName() . ':setting', $this->getCacheTags());
28   }
29
30   /**
31    * {@inheritdoc}
32    */
33   public function getDefinitions($sorted = TRUE) {
34     $definitions = parent::getDefinitions(FALSE);
35     if ($sorted) {
36       $groups = [];
37       foreach ($definitions as $plugin_id => $definition) {
38         $key = !empty($definition['groups']) ? implode(':', array_keys($definition['groups'])) : '_default';
39         $groups[$key][$plugin_id] = $definition;
40       }
41       ksort($groups);
42       $definitions = [];
43       foreach ($groups as $settings) {
44         uasort($settings, [$this, 'sort']);
45         $definitions = array_merge($definitions, $settings);
46
47       }
48     }
49     return $definitions;
50   }
51
52   /**
53    * Sorts a structured array by either a set 'weight' property or by the ID.
54    *
55    * @param array $a
56    *   First item for comparison.
57    * @param array $b
58    *   Second item for comparison.
59    *
60    * @return int
61    *   The comparison result for uasort().
62    */
63   public static function sort(array $a, array $b) {
64     if (isset($a['weight']) || isset($b['weight'])) {
65       return SortArray::sortByWeightElement($a, $b);
66     }
67     else {
68       return SortArray::sortByKeyString($a, $b, 'id');
69     }
70   }
71
72 }