View source
<?php
namespace Drupal\homebox\Controller;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Drupal\Core\Menu\LocalActionManagerInterface;
use Drupal\Core\Plugin\Context\LazyContextRepository;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\Core\Utility\LinkGeneratorInterface;
use Drupal\homebox\Entity\HomeboxLayoutInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class HomeboxLayoutController extends ControllerBase implements ContainerInjectionInterface {
protected $dateFormatter;
protected $renderer;
protected $blockManager;
protected $localActionManager;
protected $routeMatch;
protected $contextRepository;
protected $linkGenerator;
public static function create(ContainerInterface $container) {
$date_formatter = $container
->get('date.formatter');
$renderer = $container
->get('renderer');
$block_manager = $container
->get('plugin.manager.block');
$local_action_manager = $container
->get('plugin.manager.menu.local_action');
$route_match = $container
->get('current_route_match');
$context_repository = $container
->get('context.repository');
$link_generator = $container
->get('link_generator');
return new static($date_formatter, $renderer, $block_manager, $local_action_manager, $route_match, $context_repository, $link_generator);
}
public function __construct(DateFormatterInterface $date_formatter, RendererInterface $renderer, BlockManagerInterface $block_manager, LocalActionManagerInterface $local_action_manager, RouteMatchInterface $route_match, LazyContextRepository $context_repository, LinkGeneratorInterface $link_generator) {
$this->dateFormatter = $date_formatter;
$this->renderer = $renderer;
$this->blockManager = $block_manager;
$this->localActionManager = $local_action_manager;
$this->routeMatch = $route_match;
$this->contextRepository = $context_repository;
$this->linkGenerator = $link_generator;
}
public function revisionShow($homebox_layout_revision) {
$homebox_layout = $this
->entityTypeManager()
->getStorage('homebox_layout')
->loadRevision($homebox_layout_revision);
$view_builder = $this
->entityTypeManager()
->getViewBuilder('homebox_layout');
return $view_builder
->view($homebox_layout);
}
public function revisionPageTitle($homebox_layout_revision) {
$homebox_layout = $this
->entityTypeManager()
->getStorage('homebox_layout')
->loadRevision($homebox_layout_revision);
return $this
->t('Revision of %title from %date', [
'%title' => $homebox_layout
->label(),
'%date' => $this->dateFormatter
->format($homebox_layout
->getRevisionCreationTime()),
]);
}
public function listBlocks(Request $request, $homebox) {
$definitions = $this->blockManager
->getDefinitions();
$definitions = $this->blockManager
->getSortedDefinitions($definitions);
if ($request->query
->get(MainContentViewSubscriber::WRAPPER_FORMAT) === 'drupal_modal') {
$build['local_actions'] = $this
->buildLocalActions();
}
$headers = [
[
'data' => $this
->t('Block'),
],
[
'data' => $this
->t('Category'),
],
[
'data' => $this
->t('Operations'),
],
];
$region = $request->query
->get('region');
$weight = $request->query
->get('weight');
$rows = [];
foreach ($definitions as $plugin_id => $plugin_definition) {
$row = [];
$row['title']['data'] = [
'#type' => 'inline_template',
'#template' => '<div class="block-filter-text-source">{{ label }}</div>',
'#context' => [
'label' => $plugin_definition['admin_label'],
],
];
$row['category']['data'] = $plugin_definition['category'];
$links['add'] = [
'title' => $this
->t('Place block'),
'url' => Url::fromRoute('homebox.settings_form', [
'homebox' => $homebox,
], [
'query' => [
'block-placement' => $plugin_id,
],
]),
];
if ($region) {
$links['add']['query']['region'] = $region;
}
if (isset($weight)) {
$links['add']['query']['weight'] = $weight;
}
$row['operations']['data'] = [
'#type' => 'operations',
'#links' => $links,
];
$rows[] = $row;
}
$build['#attached']['library'][] = 'block/drupal.block.admin';
$build['filter'] = [
'#type' => 'search',
'#title' => $this
->t('Filter'),
'#title_display' => 'invisible',
'#size' => 30,
'#placeholder' => $this
->t('Filter by block name'),
'#attributes' => [
'class' => [
'block-filter-text',
],
'data-element' => '.block-add-table',
'title' => $this
->t('Enter a part of the block name to filter by.'),
],
];
$build['blocks'] = [
'#type' => 'table',
'#header' => $headers,
'#rows' => $rows,
'#empty' => $this
->t('No blocks available.'),
'#attributes' => [
'class' => [
'block-add-table',
],
],
];
return $build;
}
protected function buildLocalActions() {
$build = $this->localActionManager
->getActionsForRoute($this->routeMatch
->getRouteName());
if (!empty($build)) {
$build['#prefix'] = '<ul class="action-links">';
$build['#suffix'] = '</ul>';
}
return $build;
}
public function revisionOverview(HomeboxLayoutInterface $homebox_layout) {
$account = $this
->currentUser();
$langcode = $homebox_layout
->language()
->getId();
$langname = $homebox_layout
->language()
->getName();
$languages = $homebox_layout
->getTranslationLanguages();
$has_translations = count($languages) > 1;
$homebox_layout_storage = $this
->entityTypeManager()
->getStorage('homebox_layout');
$build['#title'] = $has_translations ? $this
->t('@langname revisions for %title', [
'@langname' => $langname,
'%title' => $homebox_layout
->label(),
]) : $this
->t('Revisions for %title', [
'%title' => $homebox_layout
->label(),
]);
$header = [
$this
->t('Revision'),
$this
->t('Operations'),
];
$revert_permission = $account
->hasPermission('administer homebox');
$delete_permission = $account
->hasPermission('administer homebox');
$rows = [];
$vids = $homebox_layout_storage
->revisionIds($homebox_layout);
$latest_revision = TRUE;
foreach (array_reverse($vids) as $vid) {
$revision = $homebox_layout_storage
->loadRevision($vid);
if ($revision
->hasTranslation($langcode)) {
$username = [
'#theme' => 'username',
'#account' => $revision
->getRevisionUser(),
];
$date = $this->dateFormatter
->format($revision
->getRevisionCreationTime(), 'short');
if ($vid != $homebox_layout
->getRevisionId()) {
$link = $this->linkGenerator
->generate($date, new Url('entity.homebox_layout.revision', [
'homebox_layout' => $homebox_layout
->id(),
'homebox_layout_revision' => $vid,
]));
}
else {
$link = $homebox_layout
->toLink($date)
->toString();
}
$row = [];
$column = [
'data' => [
'#type' => 'inline_template',
'#template' => '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}',
'#context' => [
'date' => $link,
'username' => $this->renderer
->renderPlain($username),
'message' => [
'#markup' => $revision
->getRevisionLogMessage(),
'#allowed_tags' => Xss::getHtmlTagList(),
],
],
],
];
$row[] = $column;
if ($latest_revision) {
$row[] = [
'data' => [
'#prefix' => '<em>',
'#markup' => $this
->t('Current revision'),
'#suffix' => '</em>',
],
];
foreach ($row as &$current) {
$current['class'] = [
'revision-current',
];
}
$latest_revision = FALSE;
}
else {
$links = [];
if ($revert_permission) {
$links['revert'] = [
'title' => $this
->t('Revert'),
'url' => $has_translations ? Url::fromRoute('entity.homebox_layout.translation_revert', [
'homebox_layout' => $homebox_layout
->id(),
'homebox_layout_revision' => $vid,
'langcode' => $langcode,
]) : Url::fromRoute('entity.homebox_layout.revision_revert', [
'homebox_layout' => $homebox_layout
->id(),
'homebox_layout_revision' => $vid,
]),
];
}
if ($delete_permission) {
$links['delete'] = [
'title' => $this
->t('Delete'),
'url' => Url::fromRoute('entity.homebox_layout.revision_delete', [
'homebox_layout' => $homebox_layout
->id(),
'homebox_layout_revision' => $vid,
]),
];
}
$row[] = [
'data' => [
'#type' => 'operations',
'#links' => $links,
],
];
}
$rows[] = $row;
}
}
$build['homebox_layout_revisions_table'] = [
'#theme' => 'table',
'#rows' => $rows,
'#header' => $header,
];
return $build;
}
}