View source
<?php
namespace Drupal\flag_search_api\Plugin\search_api\processor;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\flag\FlagService;
use Drupal\search_api\Datasource\DatasourceInterface;
use Drupal\search_api\Processor\ProcessorPluginBase;
use Drupal\search_api\Processor\ProcessorProperty;
use Drupal\search_api\SearchApiException;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\search_api\Item\ItemInterface;
class FlagIndexer extends ProcessorPluginBase implements PluginFormInterface, ContainerFactoryPluginInterface {
protected $flagService;
protected $logger;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('flag'), $container
->get('logger.factory')
->get('flag_search_api'));
}
public function __construct(array $configuration, $plugin_id, array $plugin_definition, FlagService $flagService, LoggerInterface $logger) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->flagService = $flagService;
$this->logger = $logger;
}
public function defaultConfiguration() {
return array(
'flag_index' => array(),
);
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$options = [];
$flags = $this->flagService
->getAllFlags();
foreach ($flags as $flag) {
$options[$flag
->get('id')] = $flag
->get('label');
}
$form['flag_index'] = array(
'#type' => 'checkboxes',
'#title' => $this
->t('Enable these flags on this index'),
'#description' => $this
->t('This will index IDs from users that flagged this content'),
'#options' => $options,
'#default_value' => isset($this->configuration['flag_index']) ? $this->configuration['flag_index'] : [],
);
return $form;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
$fields = array_filter($form_state
->getValues()['flag_index']);
if ($fields) {
$fields = array_keys($fields);
}
$form_state
->setValue('flag_index', $fields);
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this
->setConfiguration($form_state
->getValues());
}
public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
$properties = array();
if (!$datasource) {
$fields = $this
->getFieldsDefinition();
foreach ($fields as $field_id => $field_definition) {
$properties[$field_id] = new ProcessorProperty($field_definition);
}
}
return $properties;
}
protected function getFieldsDefinition() {
$config = $this->configuration['flag_index'];
$fields = [];
foreach ($config as $flag) {
$label = $this->flagService
->getFlagById($flag)
->get('label');
$fields['flag_' . $flag] = array(
'label' => $label,
'description' => $label,
'type' => 'integer',
'processor_id' => $this
->getPluginId(),
);
}
return $fields;
}
public function addFieldValues(ItemInterface $item) {
$config = $this->configuration['flag_index'];
$flags = $this->flagService
->getAllFlags();
try {
$entity = $item
->getOriginalObject()
->getValue();
foreach ($config as $flag_id) {
$fields = $this
->getFieldsHelper()
->filterForPropertyPath($item
->getFields(), NULL, 'flag_' . $flag_id);
foreach ($fields as $flag_field) {
$users = $this->flagService
->getFlaggingUsers($entity, $flags[$flag_id]);
foreach ($users as $user) {
$flag_field
->addValue($user
->id());
}
}
}
} catch (SearchApiException $exception) {
$this->logger
->error($exception
->getMessage());
}
}
public function preIndexSave() {
foreach ($this
->getFieldsDefinition() as $field_id => $field_definition) {
try {
$this
->ensureField(NULL, $field_id, $field_definition['type']);
} catch (SearchApiException $exception) {
$this->logger
->error($exception
->getMessage());
}
}
}
}