Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
12.00% covered (danger)
12.00%
3 / 25
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
IslandPluginManager
12.00% covered (danger)
12.00%
3 / 25
25.00% covered (danger)
25.00%
1 / 4
64.20
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getIslandsByTypes
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
30
 createInstances
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 sortListByWeight
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\display_builder;
6
7use Drupal\Core\Cache\CacheBackendInterface;
8use Drupal\Core\Extension\ModuleHandlerInterface;
9use Drupal\Core\Plugin\DefaultPluginManager;
10use Drupal\display_builder\Attribute\Island;
11
12/**
13 * Island plugin manager.
14 */
15final class IslandPluginManager extends DefaultPluginManager implements IslandPluginManagerInterface {
16
17  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
18    parent::__construct('Plugin/display_builder/Island', $namespaces, $module_handler, IslandInterface::class, Island::class);
19    $this->alterInfo('island_info');
20    $this->setCacheBackend($cache_backend, 'island_plugins');
21  }
22
23  /**
24   * {@inheritdoc}
25   */
26  public function getIslandsByTypes(array $contexts = [], array $configuration = [], ?array $filter_by_island = NULL): array {
27    $result = [];
28
29    foreach ($this->createInstances($this->getDefinitions(), $contexts, $configuration) as $island_id => $island) {
30      if ($filter_by_island && !isset($filter_by_island[$island_id])) {
31        continue;
32      }
33      /** @var \Drupal\display_builder\IslandType $type */
34      $type = $island->getPluginDefinition()['type'];
35      $result[$type->value][$island_id] = $island;
36    }
37
38    if ($filter_by_island) {
39      $result = $this->sortListByWeight($result, $filter_by_island);
40    }
41
42    return $result;
43  }
44
45  /**
46   * Create a plugin instances for each definition.
47   *
48   * @param array $definitions
49   *   An array of definitions.
50   * @param \Drupal\Core\Plugin\Context\ContextInterface[] $contexts
51   *   (Optional) An array of contexts, keyed by context name.
52   * @param array $configuration
53   *   (Optional) An array of configuration.
54   *
55   * @return array
56   *   A list of fully configured plugin instances.
57   */
58  public function createInstances(array $definitions, array $contexts = [], array $configuration = []): array {
59    return \array_map(
60      function ($definition) use ($configuration, $contexts) {
61        $config = $configuration[$definition['id']] ?? [];
62        $config['contexts'] = $contexts;
63
64        return $this->createInstance($definition['id'], $config);
65      },
66      $definitions,
67    );
68  }
69
70  /**
71   * Sort by comparison a list.
72   *
73   * @param array $list
74   *   The list to sort.
75   * @param array<int,mixed> $weight
76   *   The weigh mapping as id => weight.
77   *
78   * @return array<int,mixed>
79   *   The sorted list.
80   */
81  private function sortListByWeight(array $list, array $weight): array {
82    foreach ($list as &$items) {
83      \uksort($items, static function ($a, $b) use ($weight) {
84        return $weight[$a] <=> $weight[$b];
85      });
86    }
87
88    return $list;
89  }
90
91}