View source
<?php
namespace Drupal\content_kanban\Plugin\DashboardBlock;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\content_planner\DashboardBlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\workflows\Entity\Workflow;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class ContentStateStatistic extends DashboardBlockBase implements ContainerFactoryPluginInterface {
use MessengerTrait;
protected $kanbanStatisticService;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, MessengerInterface $messenger) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager);
$this
->setMessenger($messenger);
$this->kanbanStatisticService = \Drupal::service('content_kanban.kanban_statistic_service');
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('messenger'));
}
public function getConfigSpecificFormFields(FormStateInterface &$form_state, Request &$request, array $block_configuration) {
$form = [];
$workflow_options = [];
$workflows = Workflow::loadMultiple();
foreach ($workflows as $workflow) {
if ($workflow
->status()) {
$workflow_options[$workflow
->id()] = $workflow
->label();
}
}
$form['workflow_id'] = [
'#type' => 'select',
'#title' => t('Select workflow'),
'#required' => TRUE,
'#options' => $workflow_options,
'#default_value' => $this
->getCustomConfigByKey($block_configuration, 'workflow_id', ''),
];
return $form;
}
public function build() {
$config = $this
->getConfiguration();
$workflow_id = $this
->getCustomConfigByKey($config, 'workflow_id', '');
$workflow = Workflow::load($workflow_id);
if (!$workflow) {
$message = t('Content Status Statistic: Workflow with ID @id does not exist. Block will not be shown.', [
'@id' => $workflow_id,
]);
$this
->messenger()
->addError($message);
return [];
}
$data = $this->kanbanStatisticService
->getWorkflowStateContentCounts($workflow);
$build = [
'#theme' => 'content_state_statistic',
'#data' => $data,
'#attached' => [
'library' => [
'content_kanban/content_state_statistic',
],
],
];
return $build;
}
}