Statistics.php in Search API Sort Priority 8
File
src/Plugin/search_api/processor/Statistics.php
View source
<?php
namespace Drupal\search_api_sort_priority\Plugin\search_api\processor;
use Drupal\search_api\Datasource\DatasourceInterface;
use Drupal\search_api\Item\ItemInterface;
use Drupal\search_api\Processor\ProcessorPluginBase;
use Drupal\search_api\Processor\ProcessorProperty;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\search_api\IndexInterface;
use Drupal\search_api\Plugin\PluginFormTrait;
use Drupal\node\NodeInterface;
use Drupal\Core\TypedData\ComplexDataInterface;
use Drupal\statistics\StatisticsViewsResult;
class Statistics extends ProcessorPluginBase implements PluginFormInterface {
use PluginFormTrait;
protected $targetFieldId = 'statistics_weight';
public static function supportsIndex(IndexInterface $index) {
foreach ($index
->getDatasources() as $datasource) {
if ($datasource
->getEntityTypeId() == 'node') {
return TRUE;
}
}
return FALSE;
}
public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
$properties = [];
if (!$datasource) {
$definition = [
'label' => $this
->t('Sort Priority by Statistics'),
'description' => $this
->t('Sort Priority by Statistics.'),
'type' => 'integer',
'processor_id' => $this
->getPluginId(),
'hidden' => TRUE,
];
$properties[$this->targetFieldId] = new ProcessorProperty($definition);
}
return $properties;
}
public function addFieldValues(ItemInterface $item) {
$entity_type_id = $item
->getDatasource()
->getEntityTypeId();
if (!in_array($entity_type_id, $this->configuration['allowed_entity_types'])) {
return;
}
$fields = $this
->getFieldsHelper()
->filterForPropertyPath($item
->getFields(), NULL, $this->targetFieldId);
switch ($entity_type_id) {
case 'node':
$node = $this
->getNode($item
->getOriginalObject());
$nodeStatistics = $this
->statisticsGet($node
->id());
foreach ($fields as $field) {
$field
->addValue($nodeStatistics['totalcount']);
}
break;
}
}
public function defaultConfiguration() {
return [
'weight' => 0,
'totalcount' => 0,
'daycount' => 0,
'timestamp' => 0,
'allowed_entity_types' => [
'node',
],
];
}
public function preIndexSave() {
$field = $this
->ensureField(NULL, $this->targetFieldId, 'integer');
$field
->setHidden();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
return [];
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function statisticsGet($id) {
if ($id > 0) {
$statistics = \Drupal::service('statistics.storage.node')
->fetchView($id);
if (!$statistics instanceof StatisticsViewsResult) {
return [
'totalcount' => $this->configuration['totalcount'],
'daycount' => $this->configuration['daycount'],
'timestamp' => $this->configuration['timestamp'],
];
}
return [
'totalcount' => $statistics
->getTotalCount(),
'daycount' => $statistics
->getDayCount(),
'timestamp' => $statistics
->getTimestamp(),
];
}
}
protected function getNode(ComplexDataInterface $item) {
$item = $item
->getValue();
if ($item instanceof NodeInterface) {
return $item;
}
return NULL;
}
}
Classes
Name |
Description |
Statistics |
Adds customized sort priority by Statistics. |