Commit 135a146f authored by borisson_'s avatar borisson_ Committed by StryKaizer

Issue #2917323 by borisson_, drunken monkey, mkalkbrenner: Critical error...

Issue #2917323 by borisson_, drunken monkey, mkalkbrenner: Critical error thrown when editing a facet based on an aggregated field
parent 3f08741e
......@@ -2,6 +2,7 @@
namespace Drupal\facets\Plugin\facets\processor;
use Drupal\Core\TypedData\ComplexDataDefinitionInterface;
use Drupal\facets\FacetInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\facets\Processor\BuildProcessorInterface;
......@@ -89,6 +90,13 @@ class BooleanItemProcessor extends ProcessorPluginBase implements BuildProcessor
*/
public function supportsFacet(FacetInterface $facet) {
$data_definition = $facet->getDataDefinition();
if ($data_definition->getDataType() == "boolean") {
return TRUE;
}
if (!($data_definition instanceof ComplexDataDefinitionInterface)) {
return FALSE;
}
$property_definitions = $data_definition->getPropertyDefinitions();
foreach ($property_definitions as $definition) {
if ($definition->getDataType() == "boolean") {
......
......@@ -5,6 +5,7 @@ namespace Drupal\facets\Plugin\facets\processor;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\TypedData\ComplexDataDefinitionInterface;
use Drupal\Core\TypedData\DataReferenceDefinitionInterface;
use Drupal\Core\TypedData\TranslatableInterface;
use Drupal\facets\Exception\InvalidProcessorException;
......@@ -141,9 +142,16 @@ class TranslateEntityProcessor extends ProcessorPluginBase implements BuildProce
*/
public function supportsFacet(FacetInterface $facet) {
$data_definition = $facet->getDataDefinition();
if ($data_definition->getDataType() === 'entity_reference') {
return TRUE;
}
if (!($data_definition instanceof ComplexDataDefinitionInterface)) {
return FALSE;
}
$property_definitions = $data_definition->getPropertyDefinitions();
foreach ($property_definitions as $definition) {
if ($definition instanceof DataReferenceDefinitionInterface && $definition->getDataType() === 'entity_reference') {
if ($definition instanceof DataReferenceDefinitionInterface) {
return TRUE;
}
}
......
......@@ -2,8 +2,8 @@
namespace Drupal\facets\Plugin\facets\processor;
use Drupal\Core\TypedData\DataReferenceDefinitionInterface;
use Drupal\facets\FacetInterface;
use Drupal\Core\TypedData\ComplexDataDefinitionInterface;
use Drupal\Core\TypedData\DataReferenceDefinitionInterface;use Drupal\facets\FacetInterface;
use Drupal\facets\Processor\BuildProcessorInterface;
use Drupal\facets\Processor\ProcessorPluginBase;
use Drupal\user\Entity\User;
......@@ -45,6 +45,15 @@ class UidToUserNameCallbackProcessor extends ProcessorPluginBase implements Buil
*/
public function supportsFacet(FacetInterface $facet) {
$data_definition = $facet->getDataDefinition();
if ($data_definition->getDataType() === 'entity_reference' &&
$data_definition->getTargetDefinition()->getConstraint('EntityType') === "user") {
return TRUE;
}
if (!($data_definition instanceof ComplexDataDefinitionInterface)) {
return FALSE;
}
$property_definitions = $data_definition->getPropertyDefinitions();
foreach ($property_definitions as $definition) {
if (
......
<?php
namespace Drupal\Tests\facets\Functional;
use Drupal\entity_test\Entity\EntityTestMulRevChanged;
use Drupal\search_api\Entity\Index;
use Drupal\search_api\Plugin\search_api\processor\Property\AggregatedFieldProperty;
use Drupal\search_api\Utility\Utility;
use Drupal\user\Entity\User;
/**
* Class AggregatedFieldTest.
*
* @group facets
*/
class AggregatedFieldTest extends FacetsTestBase {
/**
* Users created for this test.
*
* @var \Drupal\user\Entity\User[]
*/
protected $users;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->drupalLogin($this->adminUser);
$this->setUpExampleStructure();
$this->insertExampleContent();
foreach ([7 => 'Owl', 8 => 'Robin', 9 => 'Hawk'] as $i => $value) {
$this->users[$i] = User::create([
'uid' => $i,
'name' => "User $value",
]);
$this->users[$i]->save();
$this->entities[$i] = EntityTestMulRevChanged::create([
'id' => $i,
'user_id' => $i,
'name' => "Test entity $value name",
'body' => "Test entity $value body",
]);
$this->entities[$i]->save();
}
$plugin_creation_helper = \Drupal::getContainer()->get('search_api.plugin_helper');
$fields_helper = \Drupal::getContainer()->get('search_api.fields_helper');
/** @var \Drupal\search_api\IndexInterface $index */
$index = Index::load($this->indexId);
// Add the user as a datasource.
$index->addDatasource($plugin_creation_helper->createDatasourcePlugin($index, 'entity:user'));
// Create the aggregated field property.
$property = AggregatedFieldProperty::create('string');
// Add and configure the aggregated field.
$field = $fields_helper->createFieldFromProperty($index, $property, NULL, 'aggregated_field', 'aggregated_field', 'string');
$field->setLabel('Aggregated field');
$field->setConfiguration([
'type' => 'union',
'fields' => [
Utility::createCombinedId('entity:entity_test_mulrev_changed', 'name'),
Utility::createCombinedId('entity:user', 'name'),
],
]);
$index->addField($field);
$index->save();
// Index all items, users and content.
$this->assertEquals(16, $this->indexItems($this->indexId));
}
/**
* Tests aggregated fields.
*
* @see https://www.drupal.org/node/2917323
*/
public function testAggregatedField() {
$facet_id = 'test_agg';
// Go to the Add facet page and make sure that returns a 200.
$facet_add_page = '/admin/config/search/facets/add-facet';
$this->drupalGet($facet_add_page);
$this->assertSession()->statusCodeEquals(200);
$form_values = [
'name' => 'Test agg',
'id' => $facet_id,
'facet_source_id' => 'search_api:views_page__search_api_test_view__page_1',
'facet_source_configs[search_api:views_page__search_api_test_view__page_1][field_identifier]' => 'aggregated_field',
];
// Try filling out the form, and configure it to use the aggregated field.
$this->drupalPostForm(NULL, ['facet_source_id' => 'search_api:views_page__search_api_test_view__page_1'], 'Configure facet source');
$this->drupalPostForm(NULL, $form_values, 'Save');
// Check that nothing breaks.
$this->assertSession()->statusCodeEquals(200);
}
}
......@@ -99,7 +99,7 @@ abstract class FacetsTestBase extends BrowserTestBase {
* @return \Drupal\search_api\ServerInterface
* A search server.
*/
public function getTestServer($name = 'WebTest server', $id = 'webtest_server', $backend_id = 'search_api_test_backend', array $backend_config = [], $reset = FALSE) {
public function getTestServer($name = 'WebTest server', $id = 'webtest_server', $backend_id = 'search_api_db', array $backend_config = [], $reset = FALSE) {
if ($reset) {
$server = Server::load($id);
if ($server) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment