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:

Field in 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:

correct

But then I switch to English site, here is an issue. The same views, same page, same index, same facets, but different result:

bug

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.

xdebug

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Niklan created an issue. See original summary.

drunken monkey’s picture

Status: Active » Postponed (maintainer needs more info)

addValue(), 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 use setValues() 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.

Niklan’s picture

Oh, 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:

  /**
   * Store languages for easier access.
   *
   * @var array
   */
  protected $languages;

  public function __construct(array $configuration, $plugin_id, array $plugin_definition) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->languages = [];
  }

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.

drunken monkey’s picture

Category: Bug report » Support request
Status: Postponed (maintainer needs more info) » Fixed

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?

Exactly, the processor object is generally only created once per index (and page request).

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.