class NodeViewCountController in Node view count 8
Controller for updating nodeviewcount statistics for node.
Hierarchy
- class \Drupal\nodeviewcount\Controller\NodeViewCountController implements ContainerInjectionInterface
Expanded class hierarchy of NodeViewCountController
File
- src/
Controller/ NodeViewCountController.php, line 16
Namespace
Drupal\nodeviewcount\ControllerView source
class NodeViewCountController implements ContainerInjectionInterface {
/**
* Node view count records manager.
*
* @var \Drupal\nodeviewcount\NodeViewCountRecordsManager
*/
protected $recordsManager;
/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a NodeViewCountController object.
*
* @param \Drupal\nodeviewcount\NodeViewCountRecordsManager $records_manager
* Node view count records manager.
* @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(NodeViewCountRecordsManager $records_manager, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_type_manager) {
$this->recordsManager = $records_manager;
$this->moduleHandler = $module_handler;
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('nodeviewcount.records_manager'), $container
->get('module_handler'), $container
->get('entity_type.manager'));
}
/**
* Ajax request handler for updating node's view count statistics.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* Symfony Request object.
* Request should have next query params:
* - nid : Id of node to update statistics for.
* - uid : Id of user which viewed the node.
* - view_mode : View mode if the node.
*
* @return \Symfony\Component\HttpFoundation\Response
* Update operation status response.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function updateCounter(Request $request) {
$data = [
'status' => FALSE,
];
$nid = $request->request
->filter('nid', FALSE, FILTER_VALIDATE_INT);
$uid = $request->request
->filter('uid', FALSE, FILTER_VALIDATE_INT);
$uip = $request->request
->filter('uip', FALSE, FILTER_VALIDATE_IP);
$view_mode = $request
->get('view_mode');
if ($nid !== FALSE && $uid !== FALSE) {
/** @var \Drupal\node\NodeTypeInterface[] $node_types */
$node_storage = $this->entityTypeManager
->getStorage('node');
$node = $node_storage
->load($nid);
$result = $this->moduleHandler
->invokeAll('nodeviewcount_insert', [
$node,
$view_mode,
]);
if (!in_array(FALSE, $result, TRUE)) {
$this->recordsManager
->insertRecord($uid, $nid, $uip);
$data['status'] = TRUE;
}
}
return new JsonResponse($data);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
NodeViewCountController:: |
protected | property | The entity type manager. | |
NodeViewCountController:: |
protected | property | The module handler service. | |
NodeViewCountController:: |
protected | property | Node view count records manager. | |
NodeViewCountController:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
NodeViewCountController:: |
public | function | Ajax request handler for updating node's view count statistics. | |
NodeViewCountController:: |
public | function | Constructs a NodeViewCountController object. |