DownloadLimit.php in Commerce File 8.2
File
src/Plugin/views/field/DownloadLimit.php
View source
<?php
namespace Drupal\commerce_file\Plugin\views\field;
use Drupal\commerce_file\DownloadLoggerInterface;
use Drupal\commerce_file\LicenseFileManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DownloadLimit extends FieldPluginBase {
protected $downloadLogger;
protected $licenseFileManager;
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, DownloadLoggerInterface $download_logger, LicenseFileManagerInterface $license_file_manager, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->downloadLogger = $download_logger;
$this->licenseFileManager = $license_file_manager;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('commerce_file.download_logger'), $container
->get('commerce_file.license_file_manager'), $container
->get('entity_type.manager'));
}
public function query() {
}
public function render(ResultRow $values) {
if (!isset($this->view->field['commerce_file'], $values->{$this->view->field['commerce_file']->aliases['commerce_file_target_id']})) {
return '';
}
$license = $this
->getEntity($values);
$purchased_entity = $license
->getPurchasedEntity();
if ($purchased_entity
->get('commerce_file')
->isEmpty()) {
return '';
}
$download_limit = $this->licenseFileManager
->getDownloadLimit($license);
if (!$download_limit) {
return '';
}
$file_id = $values->{$this->view->field['commerce_file']->aliases['commerce_file_target_id']};
$counts = $this->downloadLogger
->getDownloadCounts($license);
if (!isset($counts[$file_id])) {
return '';
}
return $counts[$file_id] . ' / ' . $download_limit;
}
}