devel_wizard-2.x-dev/src/Spell/SpellListPageController.php
src/Spell/SpellListPageController.php
<?php
declare(strict_types=1);
namespace Drupal\devel_wizard\Spell;
use Drupal\Component\Utility\Html;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SpellListPageController extends ControllerBase {
protected SpellManagerInterface $spellManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('plugin.manager.devel_wizard.spell'),
);
}
public function __construct(SpellManagerInterface $spellManager) {
$this->spellManager = $spellManager;
}
public function body() {
$htmlId = Html::getUniqueId('devel_wizard_spell_definition_list');
$build = [
'spell_definition_list' => [
'#theme_wrappers' => [
'devel_wizard_spell_definition_list' => [
'#attributes' => [
'class' => ['devel-wizard-spell-definition-list'],
],
'#search_attributes' => [
'id' => $htmlId,
'class' => ['controls'],
],
'#content_attributes' => [
'for' => $htmlId,
'class' => ['items'],
],
],
],
'#attached' => [
'drupalSettings' => [
'develWizard' => [
'spellDefinitionList' => [
'spells' => [],
'categories' => [],
'tags' => [],
],
],
],
],
],
];
$drupalSettings =& $build['spell_definition_list']['#attached']['drupalSettings']['develWizard']['spellDefinitionList'];
foreach ($this->spellManager->getDefinitions() as $spellDefinition) {
$build['spell_definition_list'][$spellDefinition->id()] = [
'#theme' => 'devel_wizard_spell_definition',
'#spell_definition' => $spellDefinition,
];
$values = $spellDefinition->jsonSerialize();
unset(
$values['provider'],
$values['class'],
);
$drupalSettings['spells'][$spellDefinition->id()] = $values;
foreach ($spellDefinition->getTags() as $tagKey => $tag) {
$drupalSettings['tags'][$tagKey] = (string) $tag;
}
}
$drupalSettings['categories'] = array_unique($drupalSettings['categories']);
sort($drupalSettings['categories']);
$drupalSettings['tags'] = array_unique($drupalSettings['tags']);
asort($drupalSettings['tags']);
return $build;
}
}
