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;
class FileMime extends ProcessorPluginBase implements PluginFormInterface {
use PluginFormTrait;
protected $targetFieldId = 'filemime_weight';
public static function supportsIndex(IndexInterface $index) {
foreach ($index
->getDatasources() as $datasource) {
if ($datasource
->getEntityTypeId() == 'file') {
return TRUE;
}
}
return FALSE;
}
public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
$properties = [];
if (!$datasource) {
$definition = [
'label' => $this
->t('Sort Priority by File mime'),
'description' => $this
->t('Sort Priority by File mime.'),
'type' => 'integer',
'processor_id' => $this
->getPluginId(),
'hidden' => TRUE,
];
$properties[$this->targetFieldId] = new ProcessorProperty($definition);
}
return $properties;
}
public function addFieldValues(ItemInterface $item) {
$weight = $this->configuration['weight'];
if ($item
->getDatasource()
->getEntityTypeId() == 'file') {
$mimeType = $item
->getOriginalObject()
->getValue()
->getMimeType();
if ($this->configuration['sorttable'][$mimeType]['weight']) {
$weight = $this->configuration['sorttable'][$mimeType]['weight'];
}
$fields = $this
->getFieldsHelper()
->filterForPropertyPath($item
->getFields(), NULL, $this->targetFieldId);
foreach ($fields as $field) {
$field
->addValue($weight);
}
}
}
public function defaultConfiguration() {
return [
'weight' => 0,
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['sorttable'] = [
'#type' => 'table',
'#header' => [
$this
->t('File mime'),
$this
->t('Weight'),
],
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'sorttable-order-weight',
],
],
];
$datasources = $this->index
->getDatasources();
foreach ($datasources as $datasource) {
if ($datasource
->getEntityTypeId() == 'file') {
$mimeTypes = $this
->getAvailableMimes();
if ($mimeTypes) {
foreach ($mimeTypes as $mimeType) {
$weight = $this->configuration['weight'];
if (isset($this->configuration['sorttable']) && isset($this->configuration['sorttable'][$mimeType]['weight'])) {
$weight = $this->configuration['sorttable'][$mimeType]['weight'];
}
$mime_weight[$mimeType]['mime_type'] = $mimeType;
$mime_weight[$mimeType]['weight'] = $weight;
}
uasort($mime_weight, [
'Drupal\\Component\\Utility\\SortArray',
'sortByWeightElement',
]);
foreach ($mime_weight as $mime) {
$weight = $mime['weight'];
$mimeType = $mime['mime_type'];
$form['sorttable'][$mimeType]['#attributes']['class'][] = 'draggable';
$form['sorttable'][$mimeType]['#weight'] = $weight;
$form['sorttable'][$mimeType]['label']['#plain_text'] = $mimeType;
$form['sorttable'][$mimeType]['weight'] = [
'#type' => 'weight',
'#title' => t('Weight for @title', [
'@title' => $mimeType,
]),
'#title_display' => 'invisible',
'#default_value' => $weight,
'#attributes' => [
'class' => [
'sorttable-order-weight',
],
],
];
}
}
}
}
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this
->setConfiguration($form_state
->getValues());
}
public function preIndexSave() {
$field = $this
->ensureField(NULL, $this->targetFieldId, 'integer');
$field
->setHidden();
}
private function getAvailableMimes() {
$mimeTypes = [];
$connection = \Drupal::database();
$query = $connection
->select('file_managed', 'f')
->condition('f.status', FILE_STATUS_PERMANENT)
->fields('f', [
'filemime',
])
->distinct()
->execute();
$results = $query
->fetchAll();
foreach ($results as $result) {
$mimeTypes[] = $result->filemime;
}
return $mimeTypes;
}
}