class KanbanController in Content Planner 8
Class KanbanController.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\content_kanban\Controller\KanbanController
Expanded class hierarchy of KanbanController
File
- modules/
content_kanban/ src/ Controller/ KanbanController.php, line 21
Namespace
Drupal\content_kanban\ControllerView source
class KanbanController extends ControllerBase {
/**
* The current user service.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* The Kanban Service.
*
* @var \Drupal\content_kanban\KanbanService
*/
protected $kanbanService;
/**
* The Moderation information service.
*
* @var \Drupal\content_moderation\ModerationInformation
*/
protected $moderationInformation;
/**
* The State Transition Validation.
*
* @var \Drupal\content_moderation\StateTransitionValidation
*/
protected $stateTransitionValidation;
/**
* Constructs a new KanbanController object.
*/
public function __construct(AccountInterface $current_user, KanbanService $kanban_service, ModerationInformation $moderation_information, StateTransitionValidation $state_transition_validation) {
$this->currentUser = $current_user;
$this->kanbanService = $kanban_service;
$this->moderationInformation = $moderation_information;
$this->stateTransitionValidation = $state_transition_validation;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('current_user'), $container
->get('content_kanban.kanban_service'), $container
->get('content_moderation.moderation_information'), $container
->get('content_moderation.state_transition_validation'));
}
/**
* Show Kanbans.
*
* @return array
* A renderable array with the Kanbans.
*
* @throws \Exception
*/
public function showKanbans() {
$build = [];
$workflows = Workflow::loadMultiple();
if (!$workflows) {
$this
->messenger()
->addMessage($this
->t('There are no Workflows configured yet.'), 'error');
return [];
}
foreach ($workflows as $workflow) {
if (Kanban::isValidContentModerationWorkflow($workflow)) {
$kanban = new Kanban($this->currentUser, $this->kanbanService, $workflow);
$build[] = $kanban
->build();
}
}
// If there are no Kanbans, display a message.
if (!$build) {
$link = Url::fromRoute('entity.workflow.collection')
->toString();
$message = $this
->t('To use Content Kanban, you need to have a valid Content Moderation workflow with at least one Entity Type configured. Please go to the <a href="@link">Workflow</a> configuration.', [
'@link' => $link,
]);
$this
->messenger()
->addMessage($message, 'error');
}
return $build;
}
/**
* Updates the Workflow state of a given Entity.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The current entity.
* @param string $state_id
* The target state id for the current entity.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* Returns a JSON response with the result of the update process.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public function updateEntityWorkflowState(ContentEntityInterface $entity, $state_id) {
$data = [
'success' => FALSE,
'message' => NULL,
];
// Check if entity is moderated.
if (!$this->moderationInformation
->isModeratedEntity($entity)) {
$data['message'] = $this
->t('Entity @type with ID @id is not a moderated entity.', [
'@id' => $entity
->id(),
'@type' => $entity
->getEntityTypeId(),
]);
return new JsonResponse($data);
}
// Get Workflow from entity.
$workflow = $this->moderationInformation
->getWorkflowForEntity($entity);
// If Workflow does not exist.
if (!$workflow) {
$data['message'] = $this
->t('Workflow not found for Entity @type with ID @id.', [
'@id' => $entity
->id(),
'@type' => $entity
->getEntityTypeId(),
]);
return new JsonResponse($data);
}
// Get Workflow States.
$workflow_states = KanbanWorkflowService::getWorkflowStates($workflow);
// Check if state given by request matches any of the Workflow's states.
if (!array_key_exists($state_id, $workflow_states)) {
$data['message'] = $this
->t('Workflow State @state_id is not a valid state of Workflow @workflow_id.', [
'@state_id' => $state_id,
'@workflow_id' => $workflow
->id(),
]);
return new JsonResponse($data);
}
// Load current workflow state of entity.
$current_state = $entity
->get('moderation_state')
->getValue()[0]['value'];
// Load all valid transitions.a1
$allowed_transitions = $this->stateTransitionValidation
->getValidTransitions($entity, $this->currentUser);
// Load all available transitions.
$transitions = $workflow
->get('type_settings')['transitions'];
foreach ($transitions as $key => $transition) {
if (in_array($current_state, $transition['from']) && $transition['to'] == $state_id) {
$transition_id = $key;
continue;
}
}
if (empty($transition_id)) {
$data['message'] = $this
->t('Invalid transition');
return new JsonResponse($data);
}
if (!array_key_exists($transition_id, $allowed_transitions)) {
$data['message'] = $this
->t('You do not have permissions to perform the action @transition_id for this content. Please contact the site administrator.', [
'@transition_id' => $transition_id,
]);
return new JsonResponse($data);
}
// Set new state.
$entity->moderation_state->value = $state_id;
// Save.
if ($entity
->save() == SAVED_UPDATED) {
$data['success'] = TRUE;
$data['message'] = $this
->t('Workflow state of Entity @type with @id has been updated to @state_id', [
'@type' => $entity
->getEntityTypeId(),
'@id' => $entity
->id(),
'@state_id' => $state_id,
]);
}
return new JsonResponse($data);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity manager. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity manager service. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
ControllerBase:: |
protected | function | Returns the state storage service. | |
KanbanController:: |
protected | property |
The current user service. Overrides ControllerBase:: |
|
KanbanController:: |
protected | property | The Kanban Service. | |
KanbanController:: |
protected | property | The Moderation information service. | |
KanbanController:: |
protected | property | The State Transition Validation. | |
KanbanController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
KanbanController:: |
public | function | Show Kanbans. | |
KanbanController:: |
public | function | Updates the Workflow state of a given Entity. | |
KanbanController:: |
public | function | Constructs a new KanbanController object. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |