From f22fe99ead26040865fa861bf940561d530df3fe Mon Sep 17 00:00:00 2001 From: Vitalii Nechai Date: Thu, 7 Nov 2024 13:09:48 +0200 Subject: [PATCH] Remove redundant "Content Access" and "Entity access by field" search api processors --- .../processor/EntityAccessByField.php | 142 ------------------ .../social_node/social_node.install | 31 ++++ .../social_node/social_node.module | 19 +++ .../install/search_api.index.social_all.yml | 13 -- .../search_api.index.social_content.yml | 15 -- .../social_search/social_search.module | 2 +- 6 files changed, 51 insertions(+), 171 deletions(-) delete mode 100644 modules/custom/entity_access_by_field/src/Plugin/search_api/processor/EntityAccessByField.php diff --git a/modules/custom/entity_access_by_field/src/Plugin/search_api/processor/EntityAccessByField.php b/modules/custom/entity_access_by_field/src/Plugin/search_api/processor/EntityAccessByField.php deleted file mode 100644 index 827a35e2716..00000000000 --- a/modules/custom/entity_access_by_field/src/Plugin/search_api/processor/EntityAccessByField.php +++ /dev/null @@ -1,142 +0,0 @@ -setLogger($container->get('logger.channel.search_api')); - $processor->setDatabase($container->get('database')); - $processor->setCurrentUser($container->get('current_user')); - - /** @var \Drupal\group\Entity\Storage\GroupRelationshipStorageInterface $group_relationship_storage */ - $group_relationship_storage = $container->get('entity_type.manager')->getStorage('group_content'); - $processor->groupRelationshipStorage = $group_relationship_storage; - - return $processor; - } - - /** - * {@inheritdoc} - */ - public function addFieldValues(ItemInterface $item) { - static $anonymous_user; - - if (!isset($anonymous_user)) { - // Load the anonymous user. - $anonymous_user = new AnonymousUserSession(); - } - - // Only run for node items. - $entity_type_id = $item->getDatasource()->getEntityTypeId(); - if (!in_array($entity_type_id, ['node'])) { - return; - } - - // Get the node object. - $node = $this->getNode($item->getOriginalObject()); - if (!$node) { - // Apparently we were active for a wrong item. - return; - } - - $fields = $item->getFields(); - $fields = $this->getFieldsHelper() - ->filterForPropertyPath($fields, NULL, 'search_api_node_grants'); - - // Get the field definitions of the node. - $field_definitions = $node->getFieldDefinitions(); - - // Get all group content entities. - $gnodes = $this->groupRelationshipStorage->loadByEntity($node); - - foreach ($fields as $field) { - /** @var \Drupal\Core\Field\FieldConfigInterface $field_definition */ - foreach ($field_definitions as $field_name => $field_definition) { - // Lets get a node access realm if the field is implemented. - if ($field_definition->getType() === 'entity_access_field') { - $realm = [ - 'view', - 'node', - $node->getType(), - $field_name, - ]; - $realm = implode('_', $realm); - // Collect grant records for the node. - $sql = 'SELECT gid, realm FROM {node_access} WHERE (nid = 0 OR nid = :nid) AND grant_view = 1 AND realm LIKE :realm'; - $args = [ - ':nid' => $node->id(), - ':realm' => $this->getDatabase()->escapeLike($realm) . '%', - ]; - $grant_records = $this->getDatabase()->query($sql, $args)->fetchAll(); - if ($grant_records) { - foreach ($grant_records as $grant) { - $field->addValue("node_access_{$grant->realm}:{$grant->gid}"); - } - } - } - } - - // Add node access check by node type in group. - foreach ($gnodes as $gnode) { - $realm = [ - 'node_access_gnode', - $node->getType(), - $gnode->getGroup()->id(), - ]; - $realm = implode(':', $realm); - $field->addValue($realm); - } - } - } - -} diff --git a/modules/social_features/social_node/social_node.install b/modules/social_features/social_node/social_node.install index fd5a236c2f6..1d9da893980 100644 --- a/modules/social_features/social_node/social_node.install +++ b/modules/social_features/social_node/social_node.install @@ -45,3 +45,34 @@ function social_node_install(): void { function social_node_update_last_removed() : int { return 11901; } + +/** + * Add new processors to search api indexes with node types. + */ +function social_node_update_11902(): void { + try { + if (\Drupal::moduleHandler()->moduleExists('social_search')) { + /** @var \Drupal\search_api\IndexInterface[] $indexes */ + $indexes = \Drupal::entityTypeManager() + ->getStorage('search_api_index') + ->loadMultiple(); + + foreach ($indexes as $index) { + if (in_array('node', $index->getEntityTypes())) { + // Remove deprecated processors. + $index->removeProcessor('content_access'); + $index->removeProcessor('entity_access_by_field'); + + $needs_reindex[] = $index->id(); + } + } + + if (!empty($needs_reindex)) { + social_search_resave_search_indexes($needs_reindex); + } + } + } + catch (InvalidPluginDefinitionException|PluginNotFoundException|EntityStorageException $e) { + \Drupal::logger('social_node')->info($e->getMessage()); + } +} diff --git a/modules/social_features/social_node/social_node.module b/modules/social_features/social_node/social_node.module index 64652147641..ea33fff85fa 100644 --- a/modules/social_features/social_node/social_node.module +++ b/modules/social_features/social_node/social_node.module @@ -5,7 +5,9 @@ * The social node module alterations. */ +use Drupal\Core\Entity\EntityInterface; use Drupal\node\NodeInterface; +use Drupal\search_api\SearchApiException; use Drupal\social_node\Entity\Node; use Drupal\social_node\SocialNodeForm; use Drupal\social_node\NodeViewBuilder; @@ -65,3 +67,20 @@ function social_node_node_access_records_alter(array &$grants, NodeInterface $no } } } + +/** + * Implements hook_ENTITY_TYPE_presave() for "search_api_index". + */ +function social_node_search_api_index_presave(EntityInterface $index): void { + /** @var \Drupal\search_api\IndexInterface $index */ + try { + if (!$index->getProcessor('content_access')) { + return; + } + + $index->removeProcessor('content_access'); + \Drupal::messenger() + ->addWarning(t('"Content Access" processor is not allowed for using (replaced by query alters).')); + } + catch (SearchApiException $e) {} +} diff --git a/modules/social_features/social_search/config/install/search_api.index.social_all.yml b/modules/social_features/social_search/config/install/search_api.index.social_all.yml index 16ab3067924..c9e3927c9a2 100644 --- a/modules/social_features/social_search/config/install/search_api.index.social_all.yml +++ b/modules/social_features/social_search/config/install/search_api.index.social_all.yml @@ -123,13 +123,6 @@ field_settings: module: - profile - user - node_grants: - label: 'Node access information' - property_path: search_api_node_grants - type: string - indexed_locked: true - type_locked: true - hidden: true rendered_item: label: 'Rendered HTML output' property_path: rendered_item @@ -237,9 +230,6 @@ datasource_settings: default: true selected: { } processor_settings: - entity_access_by_field: - weights: - preprocess_query: -30 ignorecase: all_fields: true fields: @@ -257,9 +247,6 @@ processor_settings: weights: preprocess_index: -20 preprocess_query: -20 - content_access: - weights: - preprocess_query: -30 language_with_fallback: { } stopwords: all_fields: false diff --git a/modules/social_features/social_search/config/install/search_api.index.social_content.yml b/modules/social_features/social_search/config/install/search_api.index.social_content.yml index 035f47424cb..7922cf3384f 100644 --- a/modules/social_features/social_search/config/install/search_api.index.social_content.yml +++ b/modules/social_features/social_search/config/install/search_api.index.social_content.yml @@ -43,13 +43,6 @@ field_settings: label: 'Language (with fallback)' property_path: language_with_fallback type: string - node_grants: - label: 'Node access information' - property_path: search_api_node_grants - type: string - indexed_locked: true - type_locked: true - hidden: true rendered_item: label: 'Rendered HTML output' property_path: rendered_item @@ -111,9 +104,6 @@ datasource_settings: default: true selected: { } processor_settings: - entity_access_by_field: - weights: - preprocess_query: -30 ignorecase: plugin_id: ignorecase all_fields: true @@ -125,11 +115,6 @@ processor_settings: weights: preprocess_index: -10 preprocess_query: -10 - content_access: - plugin_id: content_access - weights: - preprocess_index: -10 - preprocess_query: -10 language_with_fallback: { } stopwords: plugin_id: stopwords diff --git a/modules/social_features/social_search/social_search.module b/modules/social_features/social_search/social_search.module index 15b5137246d..7dc88f73300 100644 --- a/modules/social_features/social_search/social_search.module +++ b/modules/social_features/social_search/social_search.module @@ -199,7 +199,7 @@ function social_search_views_data_alter(array &$data) { * * Make the label/title translatable. */ -function social_block_view_search_hero_block_alter(array &$build, BlockPluginInterface $block) { +function social_search_block_view_search_hero_block_alter(array &$build, BlockPluginInterface $block) { $build['#configuration']['label'] = t('Search'); }