ScheduledTransitionRevisionLinkField.php in Scheduled Transitions 8
File
src/Plugin/views/field/ScheduledTransitionRevisionLinkField.php
View source
<?php
declare (strict_types=1);
namespace Drupal\scheduled_transitions\Plugin\views\field;
use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\TranslatableInterface;
use Drupal\scheduled_transitions\Entity\ScheduledTransition;
use Drupal\views\Plugin\views\field\LinkBase;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ScheduledTransitionRevisionLinkField extends LinkBase {
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, AccessManagerInterface $access_manager, EntityTypeManagerInterface $entityTypeManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $access_manager);
$this->entityTypeManager = $entityTypeManager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('access_manager'), $container
->get('entity_type.manager'));
}
protected function checkUrlAccess(ResultRow $row) {
$scheduledTransition = $this
->getEntity($row);
$entity = $scheduledTransition
->getEntity();
if (!$entity || !$entity
->getEntityType()
->hasLinkTemplate('revision')) {
return AccessResult::neutral('Entity does not have a revision/canonical template.');
}
return parent::checkUrlAccess($row);
}
protected function getUrlInfo(ResultRow $row) {
$scheduledTransition = $this
->getEntity($row);
$entity = $scheduledTransition
->getEntity();
$entityRevisionId = $scheduledTransition
->getEntityRevisionId();
$entityRevision = $this->entityTypeManager
->getStorage($entity
->getEntityTypeId())
->loadRevision($entityRevisionId);
if (!$entityRevision) {
$entityRevision = $entity;
}
$language = $scheduledTransition
->getEntityRevisionLanguage();
if ($language && $entityRevision instanceof TranslatableInterface && $entityRevision
->hasTranslation($language)) {
$entityRevision = $entityRevision
->getTranslation($language);
}
$toUrlArgs = [];
if ($entityRevision
->hasLinkTemplate('revision')) {
$toUrlArgs[] = 'revision';
}
return $entityRevision
->toUrl(...$toUrlArgs);
}
protected function renderLink(ResultRow $row) {
$scheduledTransition = $this
->getEntity($row);
$entity = $scheduledTransition
->getEntity();
if (!$entity) {
return '';
}
$entityRevisionId = $scheduledTransition
->getEntityRevisionId();
$entityRevision = $this->entityTypeManager
->getStorage($entity
->getEntityTypeId())
->loadRevision($entityRevisionId);
if (!$entityRevision) {
$options = $scheduledTransition
->getOptions();
return isset($options[ScheduledTransition::OPTION_LATEST_REVISION]) ? $this
->t('Latest revision') : $this
->t('Dynamic');
}
$text = parent::renderLink($row);
$this->options['alter']['query'] = $this
->getDestinationArray();
return $text;
}
protected function getDefaultLabel() {
return $this
->t('View revision');
}
}