Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / config_translation / src / Controller / ConfigTranslationMapperList.php
1 <?php
2
3 namespace Drupal\config_translation\Controller;
4
5 use Drupal\config_translation\ConfigMapperInterface;
6 use Drupal\Core\Controller\ControllerBase;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
8
9 /**
10  * Defines the configuration translation mapper list.
11  *
12  * Groups all defined configuration mapper instances by weight.
13  */
14 class ConfigTranslationMapperList extends ControllerBase {
15
16   /**
17    * A array of configuration mapper instances.
18    *
19    * @var \Drupal\config_translation\ConfigMapperInterface[]
20    */
21   protected $mappers;
22
23   /**
24    * Constructs a new ConfigTranslationMapperList object.
25    *
26    * @param \Drupal\config_translation\ConfigMapperInterface[] $mappers
27    *   The configuration mapper manager.
28    */
29   public function __construct(array $mappers) {
30     $this->mappers = $mappers;
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public static function create(ContainerInterface $container) {
37     return new static(
38       $container->get('plugin.manager.config_translation.mapper')->getMappers()
39     );
40   }
41
42   /**
43    * Builds the mappers as a renderable array for table.html.twig.
44    *
45    * @return array
46    *   Renderable array with config translation mappers.
47    */
48   public function render() {
49     $build = [
50       '#type' => 'table',
51       '#header' => $this->buildHeader(),
52       '#rows' => [],
53     ];
54
55     $mappers = [];
56
57     foreach ($this->mappers as $mapper) {
58       if ($row = $this->buildRow($mapper)) {
59         $mappers[$mapper->getWeight()][] = $row;
60       }
61     }
62
63     // Group by mapper weight and sort by label.
64     ksort($mappers);
65     foreach ($mappers as $weight => $mapper) {
66       usort($mapper, function ($a, $b) {
67         $a_title = (isset($a['label'])) ? $a['label'] : '';
68         $b_title = (isset($b['label'])) ? $b['label'] : '';
69         return strnatcasecmp($a_title, $b_title);
70       });
71       $mappers[$weight] = $mapper;
72     }
73
74     foreach ($mappers as $mapper) {
75       $build['#rows'] = array_merge($build['#rows'], $mapper);
76     }
77
78     return $build;
79   }
80
81   /**
82    * Builds a row for a mapper in the mapper listing.
83    *
84    * @param \Drupal\config_translation\ConfigMapperInterface $mapper
85    *   The mapper.
86    *
87    * @return array
88    *   A render array structure of fields for this mapper.
89    */
90   public function buildRow(ConfigMapperInterface $mapper) {
91     $row['label'] = $mapper->getTypeLabel();
92     $row['operations']['data'] = $this->buildOperations($mapper);
93     return $row;
94   }
95
96   /**
97    * Builds the header row for the mapper listing.
98    *
99    * @return array
100    *   A render array structure of header strings.
101    */
102   public function buildHeader() {
103     $row['Label'] = $this->t('Label');
104     $row['operations'] = $this->t('Operations');
105     return $row;
106   }
107
108   /**
109    * Builds a renderable list of operation links for the entity.
110    *
111    * @param \Drupal\config_translation\ConfigMapperInterface $mapper
112    *   The mapper.
113    *
114    * @return array
115    *   A renderable array of operation links.
116    *
117    * @see \Drupal\Core\Entity\EntityList::buildOperations()
118    */
119   protected function buildOperations(ConfigMapperInterface $mapper) {
120     // Retrieve and sort operations.
121     $operations = $mapper->getOperations();
122     uasort($operations, 'Drupal\Component\Utility\SortArray::sortByWeightElement');
123     $build = [
124       '#type' => 'operations',
125       '#links' => $operations,
126     ];
127     return $build;
128   }
129
130 }