public function Role::addFieldValues in Search API Sort Priority 8
Adds the values of properties defined by this processor to the item.
Parameters
\Drupal\search_api\Item\ItemInterface $item: The item whose field values should be added.
Overrides ProcessorPluginBase::addFieldValues
File
- src/
Plugin/ search_api/ processor/ Role.php, line 81
Class
- Role
- Adds customized sort priority by Role.
Namespace
Drupal\search_api_sort_priority\Plugin\search_api\processorCode
public function addFieldValues(ItemInterface $item) {
// Get default weight.
$weight = $this->configuration['weight'];
// Only run for node and comment items.
// TODO Extend for other entities.
$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);
// TODO Extend for other entities.
switch ($entity_type_id) {
case 'node':
// Get the node object.
$node = $this
->getNode($item
->getOriginalObject());
// Get the user associated with this node.
$user = User::load($node
->getOwnerId());
// Get user roles.
$user_roles = $user
->getRoles();
// Construct array for role sorting.
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;
}
// Sort the roles by weight.
uasort($role_weights, [
'Drupal\\Component\\Utility\\SortArray',
'sortByWeightElement',
]);
$highest_role_weight = array_values($role_weights)[0];
// Set the weight on all the configured fields.
foreach ($fields as $field) {
$field
->addValue($highest_role_weight['weight']);
}
break;
}
}