View source
<?php
namespace Drupal\gathercontent_ui\Controller;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Session\Session;
class TrackedEntitiesController extends ControllerBase {
protected $entityTypeManager;
protected $session;
protected $config;
public function __construct(Session $session, EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config) {
$this->session = $session;
$this->entityTypeManager = $entity_type_manager;
$this->config = $config;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('session'), $container
->get('entity_type.manager'), $container
->get('config.factory'));
}
public function listTrackedEntities() {
$tracked = $this->session
->get('gathercontent_tracked_entities', []);
$this->session
->remove('gathercontent_tracked_entities');
$storages = [];
$rows = [];
foreach ($tracked as $gcID => $item) {
if ($item['entity_type'] === 'paragraph') {
continue;
}
if (!isset($storages[$item['entity_type']])) {
$storages[$item['entity_type']] = $this->entityTypeManager
->getStorage($item['entity_type']);
}
$entity = $storages[$item['entity_type']]
->load($item['id']);
$gcStatus = '';
if (!empty($item['status'])) {
$gcStatus = [
'data' => [
'color' => [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => ' ',
'#attributes' => [
'style' => 'width:20px; height: 20px; float: left; margin-right: 5px; background: ' . $item['status']->color,
],
],
'label' => [
'#plain_text' => $item['status']->name,
],
],
'class' => [
'gc-item',
'status-item',
],
];
}
$status = '';
$statusKey = $entity
->getEntityType()
->getKey('published');
if ($statusKey && !$entity
->get($statusKey)
->isEmpty() && $entity
->get($statusKey)
->first()
->getValue()['value']) {
$status = $this
->t('Published');
}
$rows[] = [
'gc_status' => $gcStatus,
'status' => $status,
'id' => $item['id'],
'template_name' => $item['template_name'],
'name' => $entity
->label(),
'drupal_url' => $entity
->toLink(),
'gc_url' => $this
->getGcUrl($gcID, $entity
->label()),
];
}
return [
'#type' => 'table',
'#header' => [
'gc_status' => $this
->t('GatherContent status'),
'status' => $this
->t('Status'),
'id' => $this
->t('ID'),
'template_name' => $this
->t('Template name'),
'name' => $this
->t('Name'),
'drupal_url' => $this
->t('Drupal link'),
'gc_url' => $this
->t('Gathercontent link'),
],
'#empty' => $this
->t('No content available.'),
'#rows' => $rows,
];
}
protected function getGcUrl(int $gcId, $label) {
if (is_numeric($gcId)) {
$base_url = 'https://' . $this->config
->get('gathercontent.settings')
->get('gathercontent_urlkey') . '.gathercontent.com/item/';
$url = Url::fromUri($base_url . $gcId);
return Link::fromTextAndUrl($label, $url)
->toString();
}
else {
return $this
->t('Not available');
}
}
}