View source
<?php
namespace Drupal\workbench\Controller;
use Drupal\node\Controller\NodeController;
use Drupal\views\Views;
use Drupal\views\Plugin\Block\ViewsBlock;
use Drupal\views\Plugin\views\display\Embed;
class WorkbenchContentController extends NodeController {
public function addPage() {
$output = parent::addPage();
\Drupal::moduleHandler()
->alter('workbench_create', $output);
return $output;
}
public function content() {
$blocks = [];
$settings = $this
->getSettings();
$blocks['workbench_current_user'] = [
'#view_id' => $settings['overview_left']['view_id'],
'#view_display' => $settings['overview_left']['display_id'],
'#attributes' => [
'class' => [
'workbench-left',
],
],
];
$blocks['workbench_edited'] = [
'#view_id' => $settings['overview_right']['view_id'],
'#view_display' => $settings['overview_right']['display_id'],
'#attributes' => [
'class' => [
'workbench-right',
],
],
];
$blocks['workbench_recent_content'] = [
'#view_id' => $settings['overview_main']['view_id'],
'#view_display' => $settings['overview_main']['display_id'],
'#attributes' => [
'class' => [
'workbench-full',
'workbench-spacer',
],
],
];
$context = 'overview';
\Drupal::moduleHandler()
->alter('workbench_content', $blocks, $context);
return $this
->renderBlocks($blocks);
}
public function editedContent() {
$blocks = [];
$settings = $this
->getSettings();
$blocks['workbench_edited_content'] = [
'#view_id' => $settings['edits_main']['view_id'],
'#view_display' => $settings['edits_main']['display_id'],
'#attributes' => [
'class' => [
'workbench-full',
],
],
];
$context = 'edits';
\Drupal::moduleHandler()
->alter('workbench_content', $blocks, $context);
return $this
->renderBlocks($blocks);
}
public function allContent() {
$blocks = [];
$settings = $this
->getSettings();
$blocks['workbench_recent_content'] = [
'#view_id' => $settings['all_main']['view_id'],
'#view_display' => $settings['all_main']['display_id'],
'#attributes' => [
'class' => [
'workbench-full',
],
],
];
$context = 'all';
\Drupal::moduleHandler()
->alter('workbench_content', $blocks, $context);
return $this
->renderBlocks($blocks);
}
public function renderBlocks($blocks) {
$output = [];
foreach ($blocks as $key => $block) {
if (empty($block['#view_id']) || !Views::getView($block['#view_id'])) {
$build = $block;
}
else {
$view_id = $block['#view_id'];
$display_id = $block['#view_display'];
$build = views_embed_view($view_id, $display_id);
}
if (!isset($build['#attributes'])) {
$build['#attributes'] = $block['#attributes'];
}
else {
$build['#attributes'] = array_merge_recursive($build['#attributes'], $block['#attributes']);
}
$output[] = $build;
}
return [
'blocks' => $output,
'#prefix' => '<div class="admin my-workbench">',
'#suffix' => '</div>',
'#attached' => [
'library' => [
'workbench/workbench.content',
],
],
];
}
public function getSettings() {
$config = $this
->config('workbench.settings');
$items = [
'overview_left' => $this
->t('Overview block left'),
'overview_right' => $this
->t('Overview block right'),
'overview_main' => $this
->t('Overview block main'),
'edits_main' => $this
->t('My edits main'),
'all_main' => $this
->t('All content main'),
];
foreach ($items as $key => $item) {
$setting = $config
->get($key);
$data = explode(':', $setting);
$settings[$key]['view_id'] = $data[0];
$settings[$key]['display_id'] = $data[1];
}
return $settings;
}
}