View source
<?php
namespace Drupal\search_api_stats_block\Plugin\Block;
use Drupal\Core\Database\Database;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\search_api\Entity\Index;
class SearchApiStatsBlock extends BlockBase {
public function blockForm($form, FormStateInterface $form_state) {
$numPhrases = range(2, 20);
$numPhrases[] = 25;
$numPhrases[] = 30;
$form = parent::blockForm($form, $form_state);
$config = $this
->getConfiguration();
$form['num_phrases'] = [
'#type' => 'select',
'#title' => t('Number of top search phrases to display'),
'#default_value' => empty($config['num_phrases']) ? 8 : $config['num_phrases'],
'#options' => array_combine($numPhrases, $numPhrases),
];
$form['path'] = [
'#type' => 'textfield',
'#title' => t('Path of search page'),
'#default_value' => empty($config['path']) ? 'search' : $config['path'],
];
$form['param_name'] = [
'#type' => 'textfield',
'#title' => t('Parameter name for the search phrase'),
'#default_value' => empty($config['param_name']) ? 'search' : $config['param_name'],
];
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this
->setConfigurationValue('num_phrases', $form_state
->getValue('num_phrases'));
$this
->setConfigurationValue('path', $form_state
->getValue('path'));
$this
->setConfigurationValue('param_name', $form_state
->getValue('param_name'));
}
public function build() {
$config = $this
->getConfiguration();
$stats = $this
->getStats();
return [
'#theme' => 'search_api_stats_block',
'#path' => $config['path'],
'#param_name' => $config['param_name'],
'#stats' => $stats,
'#cache' => [
'max-age' => 0,
],
];
}
protected function getStats() {
$result = [];
$database = Database::getConnection();
$config = $this
->getConfiguration();
$serverName = $this
->getServer();
$indexName = $this
->getDerivativeId();
$stats = $database
->queryRange("SELECT keywords, COUNT(*) as num FROM search_api_stats WHERE s_name = :s_name AND i_name=:i_name AND keywords != '' GROUP BY `keywords` ORDER BY num DESC", 0, $config['num_phrases'], [
':s_name' => $serverName,
':i_name' => $indexName,
]);
foreach ($stats as $stat) {
$result[$stat->keywords] = $stat->num;
}
return $result;
}
protected function getServer() {
$result = '';
$index = $this
->getIndex();
if (!empty($index)) {
$result = $index
->get('server');
}
return $result;
}
protected function getIndex() {
$id = $this
->getDerivativeId();
$result = Index::load($id);
return $result;
}
}