View source
<?php
namespace Drupal\content_kanban\Plugin\DashboardBlock;
use Drupal\content_kanban\Entity\KanbanLog;
use Drupal\content_planner\DashboardBlockBase;
use Drupal\content_planner\UserProfileImage;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\user\Entity\User;
use Drupal\content_kanban\KanbanWorkflowService;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class RecentKanbanActivities extends DashboardBlockBase {
protected $defaultLimit = 10;
protected $dateFormatter;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, DateFormatterInterface $date_formatter) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager);
$this->dateFormatter = $date_formatter;
}
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('date.formatter'));
}
public function getConfigSpecificFormFields(FormStateInterface &$form_state, Request &$request, array $block_configuration) {
$form = [];
$limit_default_value = $this
->getCustomConfigByKey($block_configuration, 'limit', $this->defaultLimit);
$form['limit'] = [
'#type' => 'number',
'#title' => t('Quantity'),
'#required' => TRUE,
'#default_value' => $limit_default_value,
];
$user_picture_field_exists = !\Drupal::config('field.field.user.user.user_picture')
->isNew();
$show_user_thumb_default_value = $limit_default_value = $this
->getCustomConfigByKey($block_configuration, 'show_user_thumb', 0);
$form['show_user_thumb'] = [
'#type' => 'checkbox',
'#title' => t('Show thumbnail image of User image'),
'#description' => t('This option is only available, if the User account has the "user_picture" field. See Account configuration.'),
'#disabled' => !$user_picture_field_exists,
'#default_value' => $show_user_thumb_default_value,
];
return $form;
}
public function build() {
$build = [];
$config = $this
->getConfiguration();
$limit = $this
->getCustomConfigByKey($config, 'limit', $this->defaultLimit);
$kanban_log_service = \Drupal::service('content_kanban.kanban_log_service');
if ($logs = $kanban_log_service
->getRecentLogs($limit, [
'exclude_anonymous_users' => TRUE,
])) {
$entries = $this
->buildKanbanLogActivities($logs);
$build = [
'#theme' => 'content_kanban_log_recent_activity',
'#entries' => $entries,
'#show_user_thumb' => $this
->getCustomConfigByKey($config, 'show_user_thumb', 0),
];
}
return $build;
}
protected function buildKanbanLogActivities(array $logs) {
$entries = [];
foreach ($logs as $log) {
$user = $log
->getOwner();
$entity = $log
->getEntityObject();
if (!$entity || !$user) {
continue;
}
if ($message = $this
->composeMessage($log, $user, $entity)) {
$entry = [
'user_profile_image' => UserProfileImage::generateProfileImageUrl($user, 'content_kanban_user_thumb'),
'username' => $user
->getAccountName(),
'message' => $message,
];
$entries[] = $entry;
}
}
return $entries;
}
protected function composeMessage(KanbanLog $log, User $user, EntityInterface $entity) {
$state_from = $log
->getStateFrom();
$state_to = $log
->getStateTo();
$workflow_states = KanbanWorkflowService::getWorkflowStates($log
->getWorkflow());
if ($state_from == $state_to) {
$message = t('@username has updated @entity_type "@entity" @time ago', [
'@username' => $user
->getAccountName(),
'@entity' => $entity
->label(),
'@entity_type' => ucfirst($entity
->getEntityTypeId()),
'@time' => $this
->calculateTimeAgo($log),
]);
}
else {
$message = t('@username has changed the state of @entity_type "@entity" from "@state_from" to "@state_to" @time ago', [
'@username' => $user
->getAccountName(),
'@entity' => $entity
->label(),
'@entity_type' => ucfirst($entity
->getEntityTypeId()),
'@time' => $this
->calculateTimeAgo($log),
'@state_from' => $workflow_states[$state_from],
'@state_to' => $workflow_states[$state_to],
]);
}
return $message;
}
protected function calculateTimeAgo(KanbanLog $log) {
return $this->dateFormatter
->formatTimeDiffSince($log
->getCreatedTime());
}
}