You are here

class ContentStateStatistic in Content Planner 8

Provides a Dashboard block for Content Planner Dashboard.

Plugin annotation


@DashboardBlock(
  id = "content_state_statistic",
  name = @Translation("Content Status Widget")
)

Hierarchy

Expanded class hierarchy of ContentStateStatistic

File

modules/content_kanban/src/Plugin/DashboardBlock/ContentStateStatistic.php, line 24

Namespace

Drupal\content_kanban\Plugin\DashboardBlock
View source
class ContentStateStatistic extends DashboardBlockBase implements ContainerFactoryPluginInterface {
  use MessengerTrait;

  /**
   * The Kanban Statistic service.
   *
   * @var \Drupal\content_kanban\KanbanStatisticService
   */
  protected $kanbanStatisticService;

  /**
   * {@inheritdoc}
   */
  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');
  }

  /**
   * {@inheritdoc}
   */
  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'));
  }

  /**
   * {@inheritdoc}
   */
  public function getConfigSpecificFormFields(FormStateInterface &$form_state, Request &$request, array $block_configuration) {
    $form = [];
    $workflow_options = [];

    // Get all workflows.
    $workflows = Workflow::loadMultiple();

    /* @var $workflow \Drupal\workflows\Entity\Workflow */
    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;
  }

  /**
   * {@inheritdoc}
   */
  public function build() {

    // Get config.
    $config = $this
      ->getConfiguration();

    // Get Workflow ID from config.
    $workflow_id = $this
      ->getCustomConfigByKey($config, 'workflow_id', '');

    // Load workflow.
    $workflow = Workflow::load($workflow_id);

    // If workflow does not exist.
    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 [];
    }

    // Get data.
    $data = $this->kanbanStatisticService
      ->getWorkflowStateContentCounts($workflow);
    $build = [
      '#theme' => 'content_state_statistic',
      '#data' => $data,
      '#attached' => [
        'library' => [
          'content_kanban/content_state_statistic',
        ],
      ],
    ];
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
ContentStateStatistic::$kanbanStatisticService protected property The Kanban Statistic service.
ContentStateStatistic::build public function Build the block and return a renderable array. Overrides DashboardBlockBase::build
ContentStateStatistic::create public static function Creates an instance of the plugin. Overrides DashboardBlockBase::create
ContentStateStatistic::getConfigSpecificFormFields public function Add additonal form elements specific to the Plugin. Overrides DashboardBlockBase::getConfigSpecificFormFields
ContentStateStatistic::__construct public function Constructs a new UserLoginBlock instance. Overrides DashboardBlockBase::__construct
DashboardBlockBase::$entityTypeManager protected property The route match.
DashboardBlockBase::getBasicConfigStructure public static function Get basic configuration structure for the block configuration.
DashboardBlockBase::getConfiguration public function Get Configuration passed in by Plugin Manager. Overrides DashboardBlockInterface::getConfiguration
DashboardBlockBase::getCustomConfigByKey protected function Get custom config.
DashboardBlockBase::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
DashboardBlockBase::getName public function Return the name of the block. Overrides DashboardBlockInterface::getName
DashboardBlockBase::isConfigurable public function Determines if the plugin is configurable. Overrides PluginBase::isConfigurable
DashboardBlockBase::submitSettingsForm public function Submit form handler. Overrides DashboardBlockInterface::submitSettingsForm
DashboardBlockBase::validateForm public function Validates teh plugin config form. Overrides DashboardBlockInterface::validateForm
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId