diff --git a/src/Plugin/search_api/processor/AddHierarchy.php b/src/Plugin/search_api/processor/AddHierarchy.php index ea9549f..b9bb52a 100644 --- a/src/Plugin/search_api/processor/AddHierarchy.php +++ b/src/Plugin/search_api/processor/AddHierarchy.php @@ -2,12 +2,12 @@ namespace Drupal\search_api\Plugin\search_api\processor; +use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\TypedData\DataDefinition; use Drupal\search_api\Datasource\DatasourceInterface; use Drupal\search_api\Item\FieldInterface; use Drupal\search_api\Processor\ProcessorPluginBase; -use Drupal\taxonomy\TermInterface; use Drupal\taxonomy\TermStorageInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -24,7 +24,7 @@ class AddHierarchy extends ProcessorPluginBase implements ContainerFactoryPluginInterface { /** - * Term storage service. + * The taxonomy term storage. * * @var \Drupal\taxonomy\TermStorageInterface */ @@ -32,45 +32,63 @@ class AddHierarchy extends ProcessorPluginBase implements ContainerFactoryPlugin /** * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + /** @var static $processor */ + $processor = new static($configuration, $plugin_id, $plugin_definition); + + $term_storage = $container->get('entity_type.manager') + ->getStorage('taxonomy_term'); + $processor->setTermStorage($term_storage); + + return $processor; + } + + /** + * Retrieves the term storage. * - * Constructs the hierarchy plugin. + * @return \Drupal\Core\Entity\EntityStorageInterface + * The term storage. */ - public function __construct(array $configuration, $plugin_id, array $plugin_definition, TermStorageInterface $term_storage = NULL) { - parent::__construct($configuration, $plugin_id, $plugin_definition); - $this->termStorage = $term_storage; + public function getTermStorage() { + if (!$this->termStorage) { + return \Drupal::getContainer() + ->get('entity_type.manager') + ->getStorage('taxonomy_term'); + } + + return $this->termStorage; } /** - * {@inheritdoc} + * Sets the term storage. + * + * @param \Drupal\Core\Entity\EntityStorageInterface $termStorage + * The new term storage. + * + * @return $this */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - $entity_type_manager = $container->get('entity_type.manager'); - return new static( - $configuration, - $plugin_id, - $plugin_definition, - $entity_type_manager->hasHandler('taxonomy_term', 'storage') ? $entity_type_manager->getStorage('taxonomy_term') : NULL - ); + public function setTermStorage(EntityStorageInterface $termStorage) { + $this->termStorage = $termStorage; + return $this; } /** * {@inheritdoc} */ public function alterPropertyDefinitions(array &$properties, DatasourceInterface $datasource = NULL) { - - if ($datasource === NULL) { + if (!$datasource) { return; } $fields = $datasource->getIndex()->getFields(); foreach ($fields as $field) { if ($this->isEntityReferenceField($field)) { - $definition = [ - 'label' => $this->t('Hierarchy of %field', ['%field' => $field->getLabel()]), + $definition = array( + 'label' => $this->t('Hierarchy of %field', array('%field' => $field->getLabel())), 'description' => $this->t('The complete hierarchy for the entity reference'), 'type' => 'string', - 'locked' => TRUE, - ]; + ); $properties['search_api_hierarchy:' . $field->getFieldIdentifier()] = new DataDefinition($definition); } @@ -78,7 +96,7 @@ public function alterPropertyDefinitions(array &$properties, DatasourceInterface } /** - * Determine if a given field is an entity reference field. + * Determines if a given field is an entity reference field. * * @param \Drupal\search_api\Item\FieldInterface $field * The field to check. @@ -98,9 +116,9 @@ public function preprocessIndexItems(array &$items) { foreach ($items as $item) { foreach ($item->getFields() as $field) { if ($this->isEntityReferenceField($field)) { - // The field is an entity reference, so we have to traverse up the tree - // to find all parents. - $hierarchyValues = []; + // The field is an entity reference, so we have to traverse up the + // tree to find all parents. + $hierarchyValues = array(); $fieldValues = $field->getValues(); @@ -124,13 +142,13 @@ public function preprocessIndexItems(array &$items) { } } - // The output of all the parents is saved in a new, dynamic field. This - // is the list of all parents, concatenated and separated with a slash - // as the lucene/solr guidelines suggest: + // The output of all the parents is saved in a new, dynamic field. + // This is the list of all parents, concatenated and separated with a + // slash as the lucene/solr guidelines suggest: // https://wiki.apache.org/solr/HierarchicalFaceting. $hierarchyField = $item->getField('search_api_hierarchy:' . $field->getFieldIdentifier()); - if (!is_null($hierarchyField)) { + if ($hierarchyField) { $hierarchyField->setValues($hierarchyValues); } $field->setValues($hierarchyValues); diff --git a/tests/src/Kernel/Processor/AddHierarchyTest.php b/tests/src/Kernel/Processor/AddHierarchyTest.php index 5912c97..9d2bc38 100644 --- a/tests/src/Kernel/Processor/AddHierarchyTest.php +++ b/tests/src/Kernel/Processor/AddHierarchyTest.php @@ -12,7 +12,9 @@ use Drupal\Tests\search_api\Kernel\ResultsTrait; /** - * Tests the 'add_hierarchy' processor. + * Tests the "Hierarchy" processor. + * + * @see \Drupal\search_api\Plugin\search_api\processor\AddHierarchy * * @group search_api * @@ -28,21 +30,24 @@ class AddHierarchyTest extends ProcessorTestBase { /** * {@inheritdoc} */ - public static $modules = ['filter', 'taxonomy']; + public static $modules = array( + 'filter', + 'taxonomy', + ); /** * A hierarchy to test. */ - protected static $hierarchy = [ - 'fruit' => [ + protected static $hierarchy = array( + 'fruit' => array( 'apple', 'pear' - ], - 'vegetable' => [ + ), + 'vegetable' => array( 'radish', 'turnip', - ], - ]; + ), + ); /** * The nodes created for testing. @@ -54,7 +59,7 @@ class AddHierarchyTest extends ProcessorTestBase { /** * Hierarchical taxonomy terms. * - * This is keyed by `type.item`, for example: `fruit.pear`. + * This is keyed by "type.item", for example: "fruit.pear". * * @var \Drupal\taxonomy\TermInterface[] */ @@ -72,16 +77,29 @@ class AddHierarchyTest extends ProcessorTestBase { */ public function setUp($processor = NULL) { parent::setUp('add_hierarchy'); + $this->installConfig(['filter']); $this->installEntitySchema('taxonomy_term'); $this->createTaxonomyHierarchy(); // Create a node type for testing. - $type = NodeType::create(array('type' => 'page', 'name' => 'page')); + $type = NodeType::create(array( + 'type' => 'page', + 'name' => 'page', + )); $type->save(); // Add the taxonomy field to page type. - $this->createEntityReferenceField('node', 'page', 'term_field', NULL, 'taxonomy_term', 'default', [], FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); + $this->createEntityReferenceField( + 'node', + 'page', + 'term_field', + NULL, + 'taxonomy_term', + 'default', + array(), + FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED + ); // Index the taxonomy field. $term_field = new Field($this->index, 'term_field'); @@ -104,17 +122,19 @@ public function setUp($processor = NULL) { } /** - * Test taxonomy-based hierarchy indexing. + * Tests taxonomy-based hierarchy indexing. * * @covers ::preprocessIndexItems */ public function testPreprocessIndexItems() { // Add hierarchical terms to 3 nodes. - foreach (['vegetable.turnip', 'vegetable', 'fruit.pear'] as $i => $term) { - $this->nodes[$i] = $this->createNode([ + foreach (array('vegetable.turnip', 'vegetable', 'fruit.pear') as $i => $term) { + $this->nodes[$i] = $this->createNode(array( 'type' => 'page', - 'term_field' => ['target_id' => $this->terms[$term]->id()], - ]); + 'term_field' => array( + 'target_id' => $this->terms[$term]->id(), + ), + )); } $this->index->reindex(); $this->index->indexItems(); @@ -124,14 +144,14 @@ public function testPreprocessIndexItems() { $query = Utility::createQuery($this->index); $query->addCondition('term_field', $this->terms['vegetable']->id()); $result = $query->execute(); - $expected = ['node' => [0, 1]]; + $expected = array('node' => array(0, 1)); $this->assertResults($result, $expected); // A search for just turnips should return node 1 only. $query = Utility::createQuery($this->index); $query->addCondition('term_field', $this->terms['vegetable.turnip']->id()); $result = $query->execute(); - $expected = ['node' => [0]]; + $expected = array('node' => array(0)); $this->assertResults($result, $expected); } @@ -143,14 +163,14 @@ protected function createTaxonomyHierarchy() { foreach (static::$hierarchy as $type => $items) { // Add the 'type' item, and nest items underneath. - $this->terms[$type] = $type_term = $this->createTerm($this->vocabulary, [ + $this->terms[$type] = $type_term = $this->createTerm($this->vocabulary, array( 'name' => $type, - ]); + )); foreach ($items as $item) { - $this->terms["$type.$item"] = $this->createTerm($this->vocabulary, [ + $this->terms["$type.$item"] = $this->createTerm($this->vocabulary, array( 'name' => $item, 'parent' => $type_term, - ]); + )); } } } diff --git a/tests/src/Unit/Plugin/Processor/AddHierarchyTest.php b/tests/src/Unit/Plugin/Processor/AddHierarchyTest.php index 6ef647e..e3be2a3 100644 --- a/tests/src/Unit/Plugin/Processor/AddHierarchyTest.php +++ b/tests/src/Unit/Plugin/Processor/AddHierarchyTest.php @@ -2,21 +2,13 @@ namespace Drupal\Tests\search_api\Unit\Plugin\Processor; -use Drupal\Core\Field\FieldDefinitionInterface; -use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; -use Drupal\Core\Field\TypedData\FieldItemDataDefinition; -use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\TypedData\DataDefinition; -use Drupal\Core\TypedData\DataDefinitionInterface; use Drupal\search_api\Datasource\DatasourceInterface; use Drupal\search_api\IndexInterface; use Drupal\search_api\Item\FieldInterface; use Drupal\search_api\Plugin\search_api\processor\AddHierarchy; -use Drupal\search_api\Utility; use Drupal\Tests\UnitTestCase; -use Drupal\text\Plugin\Field\FieldType\TextItem; use Prophecy\Argument; -use Drupal\search_api\Item\ItemInterface; /** * Tests for the "Hierarchy" plugin. @@ -35,14 +27,22 @@ class AddHierarchyTest extends UnitTestCase { protected $processor; /** + * The string translaton service to use. + * + * @var \Drupal\Core\StringTranslation\TranslationInterface + */ + protected $translation; + + /** * {@inheritdoc} */ public function setUp() { parent::setUp(); // Test without term storage. - $this->processor = new AddHierarchy([], 'hierarchy', []); - $this->processor->setStringTranslation($this->getStringTranslationStub()); + $this->processor = new AddHierarchy(array(), 'hierarchy', array()); + $this->translation = $this->getStringTranslationStub(); + $this->processor->setStringTranslation($this->translation); } /** @@ -53,21 +53,21 @@ public function setUp() { */ public function testAlterPropertyDefinitions() { // Without a datasource, properties should be unaltered. - $properties = [ + $expected = $properties = array( 'foo' => 'bar', - ]; + ); $this->processor->alterPropertyDefinitions($properties); - $this->assertArrayEquals(['foo' => 'bar'], $properties); + $this->assertArrayEquals($expected, $properties); - // Test with a non entity reference. + // Test with a non-entity reference. $field = $this->prophesize(FieldInterface::class); $field->getOriginalType()->willReturn('string'); $index = $this->prophesize(IndexInterface::class); - $index->getFields()->willReturn([$field->reveal()]); + $index->getFields()->willReturn(array($field->reveal())); $datasource = $this->prophesize(DatasourceInterface::class); $datasource->getIndex()->willReturn($index->reveal()); $this->processor->alterPropertyDefinitions($properties, $datasource->reveal()); - $this->assertArrayEquals(['foo' => 'bar'], $properties); + $this->assertArrayEquals($expected, $properties); // Test with a field item, but not an entity reference field. $field = $this->prophesize(FieldInterface::class); @@ -77,7 +77,7 @@ public function testAlterPropertyDefinitions() { $datasource = $this->prophesize(DatasourceInterface::class); $datasource->getIndex()->willReturn($index->reveal()); $this->processor->alterPropertyDefinitions($properties, $datasource->reveal()); - $this->assertArrayEquals(['foo' => 'bar'], $properties); + $this->assertArrayEquals($expected, $properties); // Test with an entity reference field. $field = $this->prophesize(FieldInterface::class); @@ -89,16 +89,16 @@ public function testAlterPropertyDefinitions() { $datasource = $this->prophesize(DatasourceInterface::class); $datasource->getIndex()->willReturn($index->reveal()); $this->processor->alterPropertyDefinitions($properties, $datasource->reveal()); - $definition = [ - 'label' => new TranslatableMarkup('Hierarchy of %field', ['%field' => 'Foo field'], [], $this->getStringTranslationStub()), - 'description' => new TranslatableMarkup('The complete hierarchy for the entity reference', [], [], $this->getStringTranslationStub()), + $definition = array( + 'label' => $this->translation->translate('Hierarchy of %field', array('%field' => 'Foo field')), + 'description' => $this->translation->translate('The complete hierarchy for the entity reference', array()), 'type' => 'string', 'locked' => TRUE, - ]; - $expected = [ + ); + $expected = array( 'foo' => 'bar', 'search_api_hierarchy:foo_field' => new DataDefinition($definition), - ]; + ); $this->assertArrayEquals($expected, $properties); }