You are here

public function DownloadLimit::render in Commerce File 8.2

Renders the field.

Parameters

\Drupal\views\ResultRow $values: The values retrieved from a single row of a view's query result.

Return value

string|\Drupal\Component\Render\MarkupInterface The rendered output. If the output is safe it will be wrapped in an object that implements MarkupInterface. If it is empty or unsafe it will be a string.

Overrides FieldPluginBase::render

File

src/Plugin/views/field/DownloadLimit.php, line 90

Class

DownloadLimit
Displays the download limit for a licensed file.

Namespace

Drupal\commerce_file\Plugin\views\field

Code

public function render(ResultRow $values) {

  // The file field is required in order to display the download limit.
  if (!isset($this->view->field['commerce_file'], $values->{$this->view->field['commerce_file']->aliases['commerce_file_target_id']})) {
    return '';
  }

  /** @var \Drupal\commerce_license\Entity\LicenseInterface $license */
  $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);

  // A count of 0 should be returned if the file was never downloaded, so
  // this shouldn't happen.
  if (!isset($counts[$file_id])) {
    return '';
  }
  return $counts[$file_id] . ' / ' . $download_limit;
}