EntityImportStatusListBuilder.php in Entity Share 8.3
File
modules/entity_share_client/src/EntityImportStatusListBuilder.php
View source
<?php
declare (strict_types=1);
namespace Drupal\entity_share_client;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\entity_share_client\Entity\EntityImportStatus;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityImportStatusListBuilder extends EntityListBuilder {
const IMPORT_DATE_FORMAT = 'F j, Y - H:i:s';
protected $dateFormatter;
protected $entityTypeManager;
protected $entityTypeBundleInfo;
protected $languageManager;
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, LanguageManagerInterface $language_manager) {
parent::__construct($entity_type, $storage);
$this->dateFormatter = $date_formatter;
$this->entityTypeManager = $entity_type_manager;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->languageManager = $language_manager;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity_type.manager')
->getStorage($entity_type
->id()), $container
->get('date.formatter'), $container
->get('entity_type.manager'), $container
->get('entity_type.bundle.info'), $container
->get('language_manager'));
}
public function buildHeader() {
$header = [];
$header['id'] = $this
->t('ID');
$header['entity_uuid'] = $this
->t('Entity UUID');
$header['entity_id'] = $this
->t('Entity ID');
$header['langcode'] = $this
->t('Language');
$header['entity_label'] = $this
->t('Link to entity');
$header['entity_type_id'] = $this
->t('Entity type');
$header['entity_bundle'] = $this
->t('Bundle');
$header['remote_website'] = $this
->t('Remote');
$header['channel_id'] = $this
->t('Channel');
$header['last_import'] = $this
->t('Last import');
$header['policy'] = $this
->t('Policy');
return $header + parent::buildHeader();
}
public function buildRow(EntityInterface $entity) {
$row = [];
$row['id'] = $entity
->id();
$imported_entity_storage = $this->entityTypeManager
->getStorage($entity->entity_type_id->value);
$imported_entity = $imported_entity_storage
->load($entity->entity_id->value);
$row['entity_uuid'] = $entity->entity_uuid->value;
$row['entity_id'] = $entity->entity_id->value;
$row['langcode'] = $this->languageManager
->getLanguage($entity->langcode->value)
->getName();
$imported_entity_translation = $imported_entity
->getTranslation($entity->langcode->value);
try {
$row['entity_label'] = $imported_entity_translation
->toLink($imported_entity_translation
->label());
} catch (UndefinedLinkTemplateException $exception) {
$row['entity_label'] = $imported_entity_translation
->label();
}
$row['entity_type_id'] = $imported_entity_storage
->getEntityType()
->getLabel();
$bundle_info = $this->entityTypeBundleInfo
->getBundleInfo($entity->entity_type_id->value);
$row['entity_bundle'] = $bundle_info[$entity->entity_bundle->value]['label'] ?? $entity->entity_bundle->value;
$remote = $this->entityTypeManager
->getStorage('remote')
->load($entity->remote_website->value);
$row['remote_website'] = $remote
->label();
$row['channel_id'] = $entity->channel_id->value;
$row['last_import'] = $this->dateFormatter
->format($entity
->getLastImport(), 'custom', self::IMPORT_DATE_FORMAT);
$available_policies = EntityImportStatus::getAvailablePolicies();
$row['policy'] = $available_policies[$entity
->getPolicy()] ?? $entity
->getPolicy();
return $row + parent::buildRow($entity);
}
}