commerce_loyalty_points-8.x-1.x-dev/src/Plugin/views/field/AggregateLoyaltyPoints.php
src/Plugin/views/field/AggregateLoyaltyPoints.php
<?php
namespace Drupal\commerce_loyalty_points\Plugin\views\field;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Aggregates loyalty points for a user.
*
* @ViewsField("aggregate_loyalty_points")
*/
class AggregateLoyaltyPoints extends FieldPluginBase implements ContainerFactoryPluginInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The module handler.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $moduleHandler;
/**
* AggregateLoyaltyPoints constructor.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->moduleHandler = $module_handler;
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('module_handler'),
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function query() {}
/**
* {@inheritdoc}
*/
public function render(ResultRow $values) {
/** @var \Drupal\commerce_loyalty_points\Entity\LoyaltyPointsInterface $entity */
$entity = $this->getEntity($values);
/** @var \Drupal\commerce_loyalty_points\LoyaltyPointsStorageInterface $loyalty_points_storage */
$loyalty_points_storage = $this->entityTypeManager->getStorage('commerce_loyalty_points');
$uid = $entity->getUserId();
$aggregate_loyalty_points = $loyalty_points_storage->loadAndAggregateUserLoyaltyPoints($uid);
$key = 'table_aggregate';
$this->moduleHandler->alter('loyalty_points_view', $aggregate_loyalty_points, $key);
return $aggregate_loyalty_points;
}
}
