Hi folks,
I created several custom Processor Plugins and have issues with them.
For example. I have paragraphs attached to node which has textfield for manual input language. I created processor plugin which parse them into array:
/**
* {@inheritdoc}
*/
public function addFieldValues(ItemInterface $item) {
$entity = $item->getOriginalObject()->getValue();
if ($entity instanceof Entity && $entity->hasField('field_price_table')) {
foreach ($entity->field_price_table as $price_table) {
/** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
$paragraph = $price_table->entity;
if (!$paragraph->field_language->isEmpty()) {
$this->addLanguage($paragraph->field_language->value);
}
}
}
if (!empty($this->languages)) {
$fields = $this->getFieldsHelper()
->filterForPropertyPath($item->getFields(), NULL, 'emasrussia_price_table_languages');
foreach ($fields as $field) {
$field->addValue($this->languages);
}
}
}
/**
* Add language to an array.
*
* @param string $language
*/
public function addLanguage($language) {
if (!in_array($language, $this->languages)) {
$this->languages[] = $language;
}
}
I added this field to the index:
The index is working great, but with mistakes. Index is running from Russian version of the site, and all indexed data for it is correct:
But then I switch to English site, here is an issue. The same views, same page, same index, same facets, but different result:
There must be only "English" available. There is no any node which contains Russian texts. This nodes even no translation to Russian ones, the are separate. Why did this happens? Maybe I have some mistake in my code. I check search_api DB table and it's contains 3 rows for each EN nodes with Russian labels. But where did it get them?
I have another bug, more interesting, but leave it before figure out how to fix current, maybe them are same.
I also use xdebug to see what happens, but no results, the array with languages is always correct.
Comment | File | Size | Author |
---|---|---|---|
Screenshot_20171103_204516.png | 75.68 KB | Niklan | |
Screenshot_20171103_204045.png | 5.65 KB | Niklan | |
Screenshot_20171103_203940.png | 3.31 KB | Niklan | |
Screenshot_20171103_203814.png | 9.57 KB | Niklan |
Comments
Comment #2
drunken monkeyaddValue()
, as the name suggests, takes a single value as its parameter, not an array of values. Please loop over the values and add each one separately (or usesetValues()
instead, but that has its pitfalls, too).Please report back whether this is already enough to resolve the issue.
Also, it seems like you're not resetting
$this->languages
for each new item? From this, it seems like when you're indexing first a Russian and then an English item, the English one would get both "ru" and "en" as its languages (since the "ru" from the first item is still included).But I could be mistaken, is just at a glance.
Comment #3
NiklanOh, thank you.
I added loop through my values and added each of them by
addValue()
. This is not fixed the problem, but make sense. Then I start trying to understand what you mean by resetting the value. I already have:But seems reset in constructor is not the right place for it. I added reset for property inside the
addFieldValues()
and this helps. Seems like the plugin instance created once per operation and passed through it multiple entities, I am right?Again, thank you for your help. After that issue can be closed, just need to understand about resetting values.
Comment #4
drunken monkeyExactly, the processor object is generally only created once per index (and page request).