-
Notifications
You must be signed in to change notification settings - Fork 33
Replace "Linked Agent By Role" - new Plugin for filtering Typed Relations in search API #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c7fd8f8
Plugin for Filtered Typed Relation search.
rosiel 4b3ff5a
Coding standards.
rosiel 895e455
Put in appropriate datasource. Make multivalued.
rosiel 0e2e0b1
Increase select box size.
rosiel f5843fa
DI etc.
rosiel a504ff8
Bugfix - typed relation search plugin.
rosiel 20631f2
fix error causing no data to be indexed.
rosiel 2a8b7e8
Typo.
rosiel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
src/Plugin/search_api/processor/Property/TypedRelationFilteredProperty.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Drupal\controlled_access_terms\Plugin\search_api\processor\Property; | ||
|
||
use Drupal\Core\Form\FormStateInterface; | ||
use Drupal\Core\StringTranslation\StringTranslationTrait; | ||
use Drupal\search_api\Item\FieldInterface; | ||
use Drupal\search_api\Processor\ConfigurablePropertyBase; | ||
|
||
/** | ||
* Defines a "Typed relation by type" property. | ||
* | ||
* @see \Drupal\controlled_access_terms\Plugin\search_api\processor\TypedRelationFiltered | ||
*/ | ||
class TypedRelationFilteredProperty extends ConfigurablePropertyBase { | ||
|
||
use StringTranslationTrait; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function defaultConfiguration() { | ||
return [ | ||
'rel_types' => [], | ||
'bundle' => '', | ||
'base_field' => '', | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildConfigurationForm(FieldInterface $field, array $form, FormStateInterface $form_state) { | ||
$configuration = $field->getConfiguration(); | ||
$form['rel_types'] = [ | ||
'#type' => 'select', | ||
'#title' => $this->t('Relations to include'), | ||
'#options' => $field->getDataDefinition()->getSetting('options'), | ||
'#multiple' => TRUE, | ||
'#default_value' => $configuration['rel_types'], | ||
'#required' => TRUE, | ||
'#size' => 16, | ||
]; | ||
$form['bundle'] = [ | ||
'#type' => 'hidden', | ||
'#value' => $field->getDataDefinition()->getSetting('bundle'), | ||
]; | ||
$form['base_field'] = [ | ||
'#type' => 'hidden', | ||
'#value' => $field->getDataDefinition()->getSetting('base_field'), | ||
]; | ||
return $form; | ||
} | ||
|
||
} |
169 changes: 169 additions & 0 deletions
169
src/Plugin/search_api/processor/TypedRelationFiltered.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
<?php | ||
|
||
namespace Drupal\controlled_access_terms\Plugin\search_api\processor; | ||
|
||
use Drupal\controlled_access_terms\Plugin\search_api\processor\Property\TypedRelationFilteredProperty; | ||
use Drupal\Core\Entity\EntityTypeManager; | ||
use Drupal\search_api\Datasource\DatasourceInterface; | ||
use Drupal\search_api\Item\ItemInterface; | ||
use Drupal\search_api\Processor\ProcessorPluginBase; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
/** | ||
* Adds filterable fields for each Typed Relation field. | ||
* | ||
* @SearchApiProcessor( | ||
* id = "typed_relation_filtered", | ||
* label = @Translation("Typed Relation, filtered by type"), | ||
* description = @Translation("Filter Typed Relation fields by type"), | ||
* stages = { | ||
* "add_properties" = 0, | ||
* }, | ||
* locked = false, | ||
* hidden = false, | ||
* ) | ||
*/ | ||
class TypedRelationFiltered extends ProcessorPluginBase { | ||
|
||
/** | ||
* Entity Type Manager. | ||
* | ||
* @var \Drupal\Core\Entity\EntityTypeManager | ||
*/ | ||
protected EntityTypeManager $entityTypeManager; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param array $configuration | ||
* The plugin configuration, i.e. an array with configuration values keyed | ||
* by configuration option name. The special key 'context' may be used to | ||
* initialize the defined contexts by setting it to an array of context | ||
* values keyed by context names. | ||
* @param string $plugin_id | ||
* The plugin_id for the plugin instance. | ||
* @param mixed $plugin_definition | ||
* The plugin implementation definition. | ||
* @param \Drupal\Core\Entity\EntityTypeManager $entityTypeManager | ||
* The Entity Type Manager. | ||
*/ | ||
public function __construct( | ||
array $configuration, | ||
$plugin_id, | ||
$plugin_definition, | ||
EntityTypeManager $entityTypeManager | ||
) { | ||
parent::__construct($configuration, $plugin_id, $plugin_definition); | ||
$this->entityTypeManager = $entityTypeManager; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | ||
return new static( | ||
$configuration, | ||
$plugin_id, | ||
$plugin_definition, | ||
$container->get('entity_type.manager'), | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getPropertyDefinitions(DatasourceInterface $datasource = NULL): array { | ||
$properties = []; | ||
|
||
if (!$datasource || !$datasource->getEntityTypeId()) { | ||
return $properties; | ||
} | ||
|
||
$entity_type = $datasource->getEntityTypeId(); | ||
|
||
// Get all configured typed relation fields. | ||
$fields = $this->entityTypeManager->getStorage('field_config')->loadByProperties([ | ||
'entity_type' => $entity_type, | ||
'field_type' => 'typed_relation', | ||
]); | ||
|
||
foreach ($fields as $field) { | ||
// Create a "filtered" option. | ||
$definition = [ | ||
'label' => $this->t('@label (filtered by type) [@bundle]', [ | ||
'@label' => $field->label(), | ||
'@bundle' => $field->getTargetBundle(), | ||
]), | ||
'description' => $this->t('Typed relation field, filtered by type'), | ||
'type' => 'string', | ||
'processor_id' => $this->getPluginId(), | ||
'is_list' => TRUE, | ||
'settings' => [ | ||
'options' => $field->getSetting('rel_types'), | ||
'bundle' => $field->getTargetBundle(), | ||
'base_field' => $field->getName(), | ||
], | ||
]; | ||
$fieldname = 'typed_relation_filter__' . str_replace('.', '__', $field->id()); | ||
$property = new TypedRelationFilteredProperty($definition); | ||
$property->setSetting('options', $field->getSetting('rel_types')); | ||
$properties[$fieldname] = $property; | ||
} | ||
return $properties; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function addFieldValues(ItemInterface $item) { | ||
// Skip if no Typed Relation Filtered search_api_fields are configured. | ||
$relevant_search_api_fields = []; | ||
$search_api_fields = $item->getFields(FALSE); | ||
foreach ($search_api_fields as $search_api_field) { | ||
if (substr($search_api_field->getPropertyPath(), 0, 23) == 'typed_relation_filter__') { | ||
$relevant_search_api_fields[] = $search_api_field; | ||
} | ||
} | ||
if (empty($search_api_fields)) { | ||
return; | ||
} | ||
// Cycle over any typed relation fields on the original item. | ||
$content_entity = $item->getOriginalObject()->getValue(); | ||
|
||
foreach ($relevant_search_api_fields as $search_api_field) { | ||
$field_config = $search_api_field->getConfiguration(); | ||
|
||
// Exit if we're on the wrong bundle or the field isn't set. | ||
if (($content_entity->bundle() != $field_config['bundle']) | ||
|| !$content_entity->hasField($field_config['base_field'])) { | ||
return; | ||
} | ||
|
||
$vals = $content_entity->get($field_config['base_field'])->getValue(); | ||
foreach ($vals as $element) { | ||
$rel_type = $element['rel_type']; | ||
if (in_array($rel_type, $field_config['rel_types'])) { | ||
$tid = $element['target_id']; | ||
$taxo_term = $this->entityTypeManager | ||
->getStorage('taxonomy_term') | ||
->load($tid); | ||
if ($taxo_term) { | ||
$taxo_name = $taxo_term->name->value; | ||
$search_api_field->addValue($taxo_name); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function requiresReindexing(array $old_settings = NULL, array $new_settings = NULL) { | ||
if ($new_settings != $old_settings) { | ||
return TRUE; | ||
} | ||
return FALSE; | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.