/** * Tests custom data types integration. */ public function testCustomDataTypes() { $original_value = $this->entities[1]->get('name')->getValue()[0]['value']; $original_type = $this->index->getField('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('name'); $processed_value = $name_field->getValues()[0]; $processed_type = $name_field->getType(); $label = $name_field->getLabel(); $this->assertEquals($original_value, $processed_value, 'The processed value matches the original value'); $this->assertEquals($original_type, $processed_type, 'The processed type matches the original type.'); $this->assertEquals('Name', $label, 'The label is correctly set.'); // Reset the fields on the item and change to the supported data type. $item->setFieldsExtracted(FALSE); $item->setFields(array()); $field = $this->index->getField('name')->setType('search_api_test_data_type')->setLabel("Test"); $this->index->addField($field); $name_field = $item->getField('name'); $processed_value = $name_field->getValues()[0]; $processed_type = $name_field->getType(); $this->assertEquals($original_value, $processed_value, 'The processed value matches the original value'); $this->assertEquals('search_api_test_data_type', $processed_type, 'The processed type matches the new type.'); $this->assertEquals('Test', $name_field->getLabel(), 'The label is correctly set.'); // Reset the fields on the item and change to the non-supported data type. $item->setFieldsExtracted(FALSE); $item->setFields(array()); $field = $this->index->getField('name')->setType('search_api_unsupported_test_data_type'); $this->index->addField($field); $name_field = $item->getField('name'); $processed_value = $name_field->getValues()[0]; $processed_type = $name_field->getType(); $this->assertEquals($original_value, $processed_value, 'The processed value matches the original value'); $this->assertEquals('integer', $processed_type, '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()); $field = $this->index->getField('name')->setType('search_api_altering_test_data_type'); $this->index->addField($field); $name_field = $item->getField('name'); $processed_value = $name_field->getValues()[0]; $processed_type = $name_field->getType(); $this->assertEquals(strlen($original_value), $processed_value, 'The processed value matches the altered original value'); $this->assertEquals('search_api_altering_test_data_type', $processed_type, 'The processed type matches the defined type.'); }
/** * Finds a new unique field identifier on the given index. * * @param \Drupal\search_api\IndexInterface $index * The search index. * @param string $property_path * The property path on which the field identifier should be based. Only the * last component of the property path will be considered. * * @return string * A new unique field identifier on the given index. */ public static function getNewFieldId(IndexInterface $index, $property_path) { list(, $suggested_id) = static::splitPropertyPath($property_path); $field_id = $suggested_id; $i = 0; while ($index->getField($field_id)) { $field_id = $suggested_id . '_' . ++$i; } return $field_id; }
/** * {@inheritdoc} */ public function getField($field_id) { return $this->entity->getField($field_id); }