ScheduledTransitionsLocalTask.php in Scheduled Transitions 8
File
src/Plugin/Menu/LocalTask/ScheduledTransitionsLocalTask.php
View source
<?php
declare (strict_types=1);
namespace Drupal\scheduled_transitions\Plugin\Menu\LocalTask;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Menu\LocalTaskDefault;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\scheduled_transitions\ScheduledTransitionsUtility;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class ScheduledTransitionsLocalTask extends LocalTaskDefault implements ContainerFactoryPluginInterface {
use StringTranslationTrait;
protected $routeMatch;
protected $scheduledTransitionsUtility;
protected $entityTypeManager;
protected $languageManager;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, RouteMatchInterface $routeMatch, EntityTypeManagerInterface $entityTypeManager, LanguageManagerInterface $languageManager, TranslationInterface $stringTranslation) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->routeMatch = $routeMatch;
$this->entityTypeManager = $entityTypeManager;
$this->languageManager = $languageManager;
$this->stringTranslation = $stringTranslation;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('current_route_match'), $container
->get('entity_type.manager'), $container
->get('language_manager'), $container
->get('string_translation'));
}
public function getTitle(Request $request = NULL) {
$entity = $this
->getEntityFromRouteMatch();
if ($entity) {
$transitionStorage = $this->entityTypeManager
->getStorage('scheduled_transition');
$count = $transitionStorage
->getQuery()
->condition('entity__target_type', $entity
->getEntityTypeId())
->condition('entity__target_id', $entity
->id())
->condition('entity_revision_langcode', $this->languageManager
->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
->getId())
->count()
->execute();
return $this
->t('@title (@count)', [
'@title' => parent::getTitle($request),
'@count' => $count,
]);
}
return NULL;
}
public function getCacheTags() : array {
$tags = parent::getCacheTags();
$entity = $this
->getEntityFromRouteMatch();
if ($entity) {
$tags[] = ScheduledTransitionsUtility::createScheduledTransitionsCacheTag($entity);
}
return $tags;
}
public function getCacheContexts() : array {
$contexts = parent::getCacheContexts();
$contexts[] = 'url';
return $contexts;
}
protected function getEntityFromRouteMatch() : ?ContentEntityInterface {
[
1 => $entityTypeId,
] = explode('.', $this->pluginDefinition['base_route']);
$parameters = $this->routeMatch
->getParameters()
->all();
foreach ($parameters as $parameter) {
if ($parameter instanceof ContentEntityInterface && $parameter
->getEntityTypeId() === $entityTypeId) {
return $parameter;
}
}
return NULL;
}
}