示例#1
0
  /**
   * Tests custom data types integration.
   */
  public function testCustomDataTypes() {
    $original_value = $this->entities[1]->get('name')->value;
    $original_type = $this->index->getFields()['entity:entity_test/name']->getType();

    $item = $this->index->loadItem('entity:entity_test/1:en');
    $item = Utility::createItemFromObject($this->index, $item, 'entity:entity_test/1:en');
    $name_field = $item->getField('entity:entity_test/name');

    $processed_value = $name_field->getValues()[0];
    $processed_type = $name_field->getType();

    $this->assertEqual($processed_value, $original_value, 'The processed value matches the original value');
    $this->assertEqual($processed_type, $original_type, 'The processed type matches the original type.');

    // Reset the fields on the item and change to the supported data type.
    $item->setFieldsExtracted(FALSE);
    $item->setFields(array());
    $this->index->getFields()['entity:entity_test/name']->setType('search_api_test_data_type');
    $name_field = $item->getField('entity:entity_test/name');

    $processed_value = $name_field->getValues()[0];
    $processed_type = $name_field->getType();

    $this->assertEqual($processed_value, $original_value, 'The processed value matches the original value');
    $this->assertEqual($processed_type, 'search_api_test_data_type', 'The processed type matches the new type.');

    // Reset the fields on the item and change to the non-supported data type.
    $item->setFieldsExtracted(FALSE);
    $item->setFields(array());
    $this->index->getFields()['entity:entity_test/name']->setType('search_api_unsupported_test_data_type');
    $name_field = $item->getField('entity:entity_test/name');

    $processed_value = $name_field->getValues()[0];
    $processed_type = $name_field->getType();

    $this->assertEqual($processed_value, $original_value, 'The processed value matches the original value');
    $this->assertEqual($processed_type, 'integer', 'The processed type matches the fallback type.');

    // Reset the fields on the item and change to the data altering data type.
    $item->setFieldsExtracted(FALSE);
    $item->setFields(array());
    $this->index->getFields()['entity:entity_test/name']->setType('search_api_altering_test_data_type');
    $name_field = $item->getField('entity:entity_test/name');

    $processed_value = $name_field->getValues()[0];
    $processed_type = $name_field->getType();

    $this->assertEqual($processed_value, strlen($original_value), 'The processed value matches the altered original value');
    $this->assertEqual($processed_type, 'search_api_altering_test_data_type', 'The processed type matches the defined type.');
  }
示例#2
0
 /**
  * {@inheritdoc}
  */
 public function indexSpecificItems(array $search_objects)
 {
     if (!$search_objects || $this->read_only) {
         return array();
     }
     if (!$this->status) {
         throw new SearchApiException(new FormattableMarkup("Couldn't index values on index %index (index is disabled)", array('%index' => $this->label())));
     }
     if (empty($this->getFields())) {
         throw new SearchApiException(new FormattableMarkup("Couldn't index values on index %index (no fields selected)", array('%index' => $this->label())));
     }
     /** @var \Drupal\search_api\Item\ItemInterface[] $items */
     $items = array();
     foreach ($search_objects as $item_id => $object) {
         $items[$item_id] = Utility::createItemFromObject($this, $object, $item_id);
         // This will cache the extracted fields so processors, etc., can retrieve
         // them directly.
         $items[$item_id]->getFields();
     }
     // Remember the items that were initially passed, to be able to determine
     // the items rejected by alter hooks and processors afterwards.
     $rejected_ids = array_keys($items);
     $rejected_ids = array_combine($rejected_ids, $rejected_ids);
     // Preprocess the indexed items.
     \Drupal::moduleHandler()->alter('search_api_index_items', $this, $items);
     $this->preprocessIndexItems($items);
     // Remove all items still in $items from $rejected_ids. Thus, only the
     // rejected items' IDs are still contained in $ret, to later be returned
     // along with the successfully indexed ones.
     foreach ($items as $item_id => $item) {
         unset($rejected_ids[$item_id]);
     }
     // Items that are rejected should also be deleted from the server.
     if ($rejected_ids) {
         $this->getServerInstance()->deleteItems($this, $rejected_ids);
     }
     $indexed_ids = array();
     if ($items) {
         $indexed_ids = $this->getServerInstance()->indexItems($this, $items);
     }
     // Return the IDs of all items that were either successfully indexed or
     // rejected before being handed to the server.
     $processed_ids = array_merge(array_values($rejected_ids), array_values($indexed_ids));
     if ($processed_ids) {
         if ($this->hasValidTracker()) {
             $this->getTrackerInstance()->trackItemsIndexed($processed_ids);
         }
         // Since we've indexed items now, triggering reindexing would have some
         // effect again. Therefore, we reset the flag.
         $this->hasReindexed = FALSE;
         \Drupal::moduleHandler()->invokeAll('search_api_items_indexed', array($this, $processed_ids));
     }
     return $processed_ids;
 }
示例#3
0
  /**
   * Generates some test items.
   *
   * @param array[] $items
   *   Array of items to be transformed into proper search item objects. Each
   *   item in this array is an associative array with the following keys:
   *   - datasource: The datasource plugin ID.
   *   - item: The item object to be indexed.
   *   - item_id: The datasource-specific raw item ID.
   *   - *: Any other keys will be treated as property paths, and their values
   *     as a single value for a field with that property path.
   *
   * @return \Drupal\search_api\Item\ItemInterface[]
   *   The generated test items.
   */
  public function generateItems(array $items) {
    /** @var \Drupal\search_api\Item\ItemInterface[] $extracted_items */
    $extracted_items = array();
    foreach ($items as $item) {
      $id = Utility::createCombinedId($item['datasource'], $item['item_id']);
      $extracted_items[$id] = Utility::createItemFromObject($this->index, $item['item'], $id);
      foreach (array(NULL, $item['datasource']) as $datasource_id) {
        foreach ($this->index->getFieldsByDatasource($datasource_id) as $key => $field) {
          /** @var \Drupal\search_api\Item\FieldInterface $field */
          $field = clone $field;
          if (isset($item[$field->getPropertyPath()])) {
            $field->addValue($item[$field->getPropertyPath()]);
          }
          $extracted_items[$id]->setField($key, $field);
        }
      }
    }

    return $extracted_items;
  }