ReportTypeBase.php in Commerce Reporting 8
File
src/Plugin/Commerce/ReportType/ReportTypeBase.php
View source
<?php
namespace Drupal\commerce_reports\Plugin\Commerce\ReportType;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class ReportTypeBase extends PluginBase implements ReportTypeInterface, ContainerFactoryPluginInterface {
protected $orderReportStorage;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->orderReportStorage = $entity_type_manager
->getStorage('commerce_order_report');
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'));
}
public function getLabel() {
return $this->pluginDefinition['label'];
}
public function getDescription() {
return $this->pluginDefinition['description'];
}
public function buildReportTable(array $results) {
$build = [
'#type' => 'table',
'#header' => $this
->doBuildReportTableHeaders(),
'#rows' => [],
'#empty' => t('No reports yet'),
];
foreach ($results as $result) {
$row = $this
->doBuildReportTableRow($result);
$build['#rows'][] = $row;
}
return $build;
}
public function createFromOrder(OrderInterface $order, array $values = []) {
$values += [
'type' => $this
->getPluginId(),
'order_id' => $order
->id(),
'created' => $order
->getPlacedTime(),
];
$order_report = $this->orderReportStorage
->create($values);
$order_report
->save();
}
protected abstract function doBuildReportTableHeaders();
protected abstract function doBuildReportTableRow(array $result);
}