2

I'm using latest version of SearchAPI (1.18) and SearchAPI Solr (4.1.10).

I've a custom field that I want to have multiple values (I use it for faceted search with Facets module). In the previous version of SearchAPI the second example worked.

If I use this code, SearchAPI save "Array" as value:

<?php

namespace Drupal\archivio_utils\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;

/**
 *
 * @SearchApiProcessor(
 *   id = "periodo",
 *   label = @Translation("Periodo"),
 *   description = @Translation("Periodo"),
 *   stages = {
 *     "add_properties" = 0,
 *   },
 *   locked = true,
 *   hidden = false,
 * )
 */
class Periodo extends ProcessorPluginBase {

  /**
   * {@inheritdoc}
   */
  public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
    $properties = [];

    if (!$datasource) {
      $definition = [
        'label' => $this->t('Periodo'),
        'description' => $this->t('Periodo'),
        'type' => 'string',
        'processor_id' => $this->getPluginId(),
      ];
      $properties['search_api_persona_periodo'] = new ProcessorProperty($definition);
    }

    return $properties;
  }

  /**
   * {@inheritdoc}
   */
  public function addFieldValues(ItemInterface $item) {
    $entity = $item->getOriginalObject()->getValue();
    /* @var \Drupal\node\Entity\Node $entity */
    /* @var \Drupal\node\Entity\Node $carriera */
    if ($entity->bundle() == 'persona') {
      $fields = $this->getFieldsHelper()
        ->filterForPropertyPath($item->getFields(), NULL, 'search_api_persona_periodo');
      foreach ($fields as $field) {
        if (!$field->getDatasourceId()) {
          $values[] = 'test1';
          $values[] = 'test2';
          $field->addValue($values);
        }
      }
    }
  }
}

If I replace code where I wrote values with this:

$values[] = 'test1';
$values[] = 'test2';
foreach ($values as $value) {
   $field->addValue($value);
}

I obtain this error:

[error] Drupal\search_api_solr\SearchApiSolrException while indexing item entity:node/10008:it: Solr endpoint http://solr:8983/ bad request (400). ERROR: [doc=3igm00-archivio_indice-entity:node/10008:it] multiple values encountered for non multiValued field ss_persona_periodo: [test1, test1] in Drupal\search_api_solr\SolrConnector\SolrConnectorPluginBase->handleHttpException() (line 1016 of /var/www/html/web/modules/contrib/search_api_solr/src/SolrConnector/SolrConnectorPluginBase.php).

How can I add multiple values to this custom field?

2 Answers 2

4

Try to add

is_list => TRUE

  /**
   * {@inheritdoc}
   */
  public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
    $properties = [];

    if (!$datasource) {
      $definition = [
        'label' => $this->t('Periodo'),
        'description' => $this->t('Periodo'),
        'type' => 'string',
        'is_list' => TRUE, // Add this option to the definition
        'processor_id' => $this->getPluginId(),
      ];
      $properties['search_api_persona_periodo'] = new ProcessorProperty($definition);
    }

    return $properties;
  }
1
  • This is working!! it converts ss_ to sm_. Commented Nov 14, 2023 at 16:05
1

You can add this:

foreach ($fields as $field) {
    $field->addValue($custom_field);
    $field->addValue($custom_field);     
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.