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 ContentBundle extends ProcessorPluginBase implements PluginFormInterface {
use PluginFormTrait;
protected $targetFieldId = 'contentbundle_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 Content Bundle'),
'description' => $this
->t('Sort Priority by Content Bundle.'),
'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() == 'node') {
$bundle_type = $item
->getDatasource()
->getItemBundle($item
->getOriginalObject());
if (isset($this->configuration['sorttable'][$bundle_type]) && $this->configuration['sorttable'][$bundle_type]['weight']) {
$weight = $this->configuration['sorttable'][$bundle_type]['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('Content Bundle'),
$this
->t('Weight'),
],
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'sorttable-order-weight',
],
],
];
$datasources = $this->index
->getDatasources();
foreach ($datasources as $datasource) {
if ($datasource
->getEntityTypeId() == 'node') {
if ($bundles = $datasource
->getBundles()) {
foreach ($bundles as $bundle_id => $bundle_name) {
$weight = $this->configuration['weight'];
if (isset($this->configuration['sorttable']) && isset($this->configuration['sorttable'][$bundle_id]['weight'])) {
$weight = $this->configuration['sorttable'][$bundle_id]['weight'];
}
$bundle_weight[$bundle_id]['bundle_id'] = $bundle_id;
$bundle_weight[$bundle_id]['bundle_name'] = $bundle_name;
$bundle_weight[$bundle_id]['weight'] = $weight;
}
uasort($bundle_weight, [
'Drupal\\Component\\Utility\\SortArray',
'sortByWeightElement',
]);
foreach ($bundle_weight as $bundle_id => $bundle) {
$weight = $bundle['weight'];
$bundle_name = $bundle['bundle_name'];
$form['sorttable'][$bundle_id]['#attributes']['class'][] = 'draggable';
$form['sorttable'][$bundle_id]['#weight'] = $weight;
$form['sorttable'][$bundle_id]['label']['#plain_text'] = $bundle_name;
$form['sorttable'][$bundle_id]['weight'] = [
'#type' => 'weight',
'#title' => t('Weight for @title', [
'@title' => $bundle_name,
]),
'#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();
}
}