Blocks.php in DRD Agent 8.3
File
src/Agent/Action/Blocks.php
View source
<?php
namespace Drupal\drd_agent\Agent\Action;
use Drupal\block\BlockListBuilder;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
class Blocks extends Base {
private function listBlocks() : array {
$block_list = [];
try {
$blb = BlockListBuilder::createInstance($this->container, $this->entityTypeManager
->getDefinition('block'));
$blocks = $blb
->load();
foreach ($blocks as $id => $block) {
$definition = $block
->getPlugin()
->getPluginDefinition();
$block_list[$definition['provider']][$id] = $block
->label();
}
} catch (PluginNotFoundException $e) {
}
return $block_list;
}
private function renderBlock($delta) {
try {
$blb = BlockListBuilder::createInstance($this->container, $this->entityTypeManager
->getDefinition('block'));
} catch (PluginNotFoundException $e) {
return [];
}
$blocks = $blb
->load();
if (isset($blocks[$delta])) {
$block = $blocks[$delta];
$build = $block
->getPlugin()
->build();
return $this->container
->get('renderer')
->renderPlain($build);
}
return [];
}
public function execute() {
if (!$this->moduleHandler
->moduleExists('block')) {
return [];
}
$args = $this
->getArguments();
if (!empty($args['module']) && !empty($args['delta'])) {
return [
'data' => $this
->renderBlock($args['delta']),
];
}
return $this
->listBlocks();
}
}