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\user\RoleInterface;
use Drupal\Component\Utility\Html;
use Drupal\node\NodeInterface;
use Drupal\Core\TypedData\ComplexDataInterface;
use Drupal\user\Entity\User;
class Role extends ProcessorPluginBase implements PluginFormInterface {
use PluginFormTrait;
protected $targetFieldId = 'role_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 Role'),
'description' => $this
->t('Sort Priority by Role.'),
'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'];
$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());
$user = User::load($node
->getOwnerId());
$user_roles = $user
->getRoles();
foreach ($user_roles as $role_id) {
$weight = $this->configuration['sorttable'][$role_id]['weight'];
$role_weights[$role_id]['role'] = $role_id;
$role_weights[$role_id]['weight'] = $weight;
}
uasort($role_weights, [
'Drupal\\Component\\Utility\\SortArray',
'sortByWeightElement',
]);
$highest_role_weight = array_values($role_weights)[0];
foreach ($fields as $field) {
$field
->addValue($highest_role_weight['weight']);
}
break;
}
}
public function defaultConfiguration() {
return [
'weight' => 0,
'allowed_entity_types' => [
'node',
'comment',
],
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['sorttable'] = [
'#type' => 'table',
'#header' => [
$this
->t('Role'),
$this
->t('Weight'),
],
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'sorttable-order-weight',
],
],
];
$master_roles = user_roles();
$roles = array_map(function (RoleInterface $role) {
return Html::escape($role
->label());
}, $master_roles);
foreach ($roles as $role_id => $role_name) {
$weight = $master_roles[$role_id]
->getWeight();
if (isset($this->configuration['sorttable']) && isset($this->configuration['sorttable'][$role_id]['weight'])) {
$weight = $this->configuration['sorttable'][$role_id]['weight'];
}
$role_weight[$role_id]['bundle_id'] = $role_id;
$role_weight[$role_id]['bundle_name'] = $role_name;
$role_weight[$role_id]['weight'] = $weight;
}
uasort($role_weight, [
'Drupal\\Component\\Utility\\SortArray',
'sortByWeightElement',
]);
foreach ($role_weight as $role_id => $role) {
$weight = $role['weight'];
$role_name = $role['bundle_name'];
$form['sorttable'][$role_id]['#attributes']['class'][] = 'draggable';
$form['sorttable'][$role_id]['#weight'] = $weight;
$form['sorttable'][$role_id]['label']['#plain_text'] = $role_name;
$form['sorttable'][$role_id]['weight'] = [
'#type' => 'weight',
'#title' => t('Weight for @title', [
'@title' => $role_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();
}
protected function getNode(ComplexDataInterface $item) {
$item = $item
->getValue();
if ($item instanceof CommentInterface) {
$item = $item
->getCommentedEntity();
}
if ($item instanceof NodeInterface) {
return $item;
}
return NULL;
}
}