-
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
Changes from 4 commits
c7fd8f8
4b3ff5a
895e455
0e2e0b1
f5843fa
a504ff8
20631f2
2a8b7e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <?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' => [], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * {@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, | ||
| ]; | ||
| return $form; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| <?php | ||
|
|
||
| namespace Drupal\controlled_access_terms\Plugin\search_api\processor; | ||
|
|
||
| use Drupal\controlled_access_terms\Plugin\search_api\processor\Property\TypedRelationFilteredProperty; | ||
| use Drupal\search_api\Datasource\DatasourceInterface; | ||
| use Drupal\search_api\Item\ItemInterface; | ||
| use Drupal\search_api\Processor\ProcessorPluginBase; | ||
|
|
||
| /** | ||
| * 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 = true, | ||
| * hidden = false, | ||
| * ) | ||
| */ | ||
| class TypedRelationFiltered extends ProcessorPluginBase { | ||
|
|
||
| /** | ||
| * {@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 = \Drupal::entityTypeManager()->getStorage('field_config')->loadByProperties([ | ||
rosiel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 'entity_type' => $entity_type, | ||
| 'field_type' => 'typed_relation', | ||
rosiel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ]); | ||
| 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')], | ||
| ]; | ||
| $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. | ||
| $skip = TRUE; | ||
| $search_api_fields = $item->getFields(FALSE); | ||
| foreach ($search_api_fields as $field) { | ||
| if (substr($field->getPropertyPath(), 0, 23) == 'typed_relation_filter__') { | ||
| $skip = FALSE; | ||
| } | ||
| } | ||
| if ($skip) { | ||
| return; | ||
| } | ||
| // Cycle over any typed relation fields on the original item. | ||
| $content_entity = $item->getOriginalObject()->getValue(); | ||
| $field_defs = $content_entity->getFieldDefinitions(); | ||
| foreach ($field_defs as $field) { | ||
| if ($field->getType() == 'typed_relation') { | ||
| $field_name = $field->getName(); | ||
| if (!$content_entity->get($field_name)->isEmpty()) { | ||
| // See if this field is being indexed. | ||
| $property_path = 'typed_relation_filter__' . str_replace('.', '__', $field->id()); | ||
| $search_api_fields = $this->getFieldsHelper() | ||
| ->filterForPropertyPath($search_api_fields, $item->getDatasourceId(), $property_path); | ||
| foreach ($search_api_fields as $search_api_field) { | ||
| // Load entity's field values. | ||
| $vals = $content_entity->$field_name->getValue(); | ||
|
||
| foreach ($vals as $element) { | ||
| $rel_type = $element['rel_type']; | ||
| if (in_array($rel_type, $search_api_field->getConfiguration()['rel_types'])) { | ||
| $tid = $element['target_id']; | ||
| $taxo_term = \Drupal::entityTypeManager() | ||
rosiel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ->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; | ||
| } | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.