Repository navigation
Expand file tree
/
Copy pathBookmarkProcessor.php
More file actions
78 lines (71 loc) · 2.04 KB
/
Copy pathBookmarkProcessor.php
File metadata and controls
78 lines (71 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
* Created by PhpStorm.
* User: Dalibor Stojaković
* Date: 06.02.17.
* Time: 11:06
*/
namespace Drupal\drupal8_custom\Plugin\search_api\processor;
use Drupal\search_api\Datasource\DatasourceInterface;
use Drupal\search_api\Processor\ProcessorPluginBase;
use Drupal\search_api\Processor\ProcessorProperty;
/**
* @SearchApiProcessor(
* id = "bookmark_processor",
* label = @Translation("Bookmark indexing"),
* description = @Translation("Indexing followers of owner"),
* stages = {
* "add_properties" = 1,
* "pre_index_save" = -10,
* "preprocess_index" = -30
* }
* )
*/
class BookmarkProcessor extends ProcessorPluginBase {
/**
* {@inheritdoc}
*
*/
public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
$properties = array();
if (!$datasource) {
// Ensure that our fields are defined.
$fields['followers'] = array(
'label' => $this->t('Followers'),
'description' => $this->t('Followers from Owner'),
'type' => 'integer',
'processor_id' => $this->getPluginId(),
);
foreach ($fields as $field_id => $field_definition) {
$properties[$field_id] = new ProcessorProperty($field_definition);
}
}
return $properties;
}
/**
* {@inheritdoc}
*/
public function preprocessIndexItems(array $items) {
foreach ($items as $item) {
$entity = $item->getOriginalObject()->getValue();
$owner = $entity->getOwner();
$users = \Drupal::entityQuery('flagging')
->condition('entity_type','user')
->condition('entity_id',$owner->id())
->condition('flag_id','follow')
->execute();
$fields = $this->getFieldsHelper()->filterForPropertyPath($item->getFields(), NULL,'followers');
foreach ($fields as $field) {
foreach($users as $user){
$field->addValue($user);
}
}
}
}
/**
* {@inheritdoc}
*/
public function preIndexSave() {
$this->ensureField(NULL, 'followers', 'integer');
}
}