Commit 97a40c2c authored by Robert Ragas's avatar Robert Ragas Committed by Robert Ragas
Browse files

Merge pull request #2527 from goalgorilla/bugfix/social-tagging-index-field-overrides-not-applying

Issue #3233027 : Social tags not applied to search indexes correctly and therefore cannot be used in search
parent 737f1239
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -7,3 +7,4 @@ configure: social_tagging.settings
dependencies:
  - drupal:taxonomy
  - social:social_core
  - social:social_search
+118 −5
Original line number Diff line number Diff line
@@ -8,11 +8,12 @@
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\search_api\Item\Field;

/**
 * Install the module.
 */
function social_tagging_install() {
function social_tagging_install(): void {
  // Grant the default permissions for this feature.
  user_role_grant_permissions(
    'sitemanager',
@@ -23,6 +24,9 @@ function social_tagging_install() {
    ]
  );

  // Add the new search api fields.
  _social_tagging_add_fields_search_api();

  // If the search module is enabled trigger updating of the indexes affected
  // by tagging.
  try {
@@ -35,10 +39,96 @@ function social_tagging_install() {
  }
}

/**
 * Uninstall the module.
 */
function social_tagging_uninstall(): void {
  // Remove the search api fields.
  _social_tagging_remove_fields_search_api();

  try {
    if (\Drupal::moduleHandler()->moduleExists('social_search')) {
      social_search_resave_search_indexes(['social_content', 'social_groups']);
    }
  }
  catch (EntityStorageException $e) {
    \Drupal::logger('social_tagging')->info($e->getMessage());
  }
}

/**
 * Add fields to search API.
 */
function _social_tagging_search_api_fields(): array {
  return [
    'social_content' => 'node',
    'social_groups' => 'group',
    'social_users' => 'profile',
  ];
}

/**
 * Add fields to search API.
 */
function _social_tagging_add_fields_search_api(): void {
  $fields = _social_tagging_search_api_fields();

  foreach ($fields as $index => $type) {
    $index_storage = \Drupal::entityTypeManager()
      ->getStorage('search_api_index');

    /** @var \Drupal\search_api\IndexInterface $index */
    $index = $index_storage->load($index);

    $field_intro = new Field($index, 'social_tagging');
    $field_intro->setType('integer');
    $field_intro->setPropertyPath('social_tagging');
    $field_intro->setDatasourceId('entity:' . $type);
    $field_intro->setLabel('Social Tagging');
    $index->addField($field_intro);

    $index->save();
  }
}

/**
 * Remove fields from search API.
 */
function _social_tagging_remove_fields_search_api(): void {
  $fields = _social_tagging_search_api_fields();

  foreach ($fields as $index => $type) {
    $index_storage = \Drupal::entityTypeManager()
      ->getStorage('search_api_index');

    /** @var \Drupal\search_api\IndexInterface $index */
    $index = $index_storage->load($index);

    $index->removeField('social_tagging');
    $index->save();
  }
}

/**
 * Update the field definitions on install, or in an update hook.
 *
 * @param string $field
 *   The field definition you want to update.
 */
function _social_profile_field_definitions_update(string $field): void {
  // Update definitions and schema.
  $list = \Drupal::entityDefinitionUpdateManager()->getChangeList();
  if (!empty($list[$field])) {
    foreach ($list[$field] as $item) {
      \Drupal::entityDefinitionUpdateManager()->updateEntityType($item);
    }
  }
}

/**
 * Exclude landing pages from tagging.
 */
function social_tagging_update_8001() {
function social_tagging_update_8001(): void {
  // Set allow to true, since that's the case by default.
  $config = \Drupal::getContainer()->get('config.factory')->getEditable('social_tagging.settings');
  $config->set('tag_node_type_landing_page', FALSE)->save();
@@ -47,7 +137,7 @@ function social_tagging_update_8001() {
/**
 * Toggle group index.
 */
function social_tagging_update_8002() {
function social_tagging_update_8002(): void {
  // Toggle the index groups.
  try {
    // If the search module is enabled we need to update the group index.
@@ -63,7 +153,7 @@ function social_tagging_update_8002() {
/**
 * Install Tagging base field to profile entity type.
 */
function social_tagging_update_8003() {
function social_tagging_update_8003(): void {
  $field_storage_definition = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Tagging'))
    ->setDescription(t('Tagging field.'))
@@ -93,7 +183,30 @@ function social_tagging_update_8003() {
/**
 * Toggle user index.
 */
function social_tagging_update_8004() {
function social_tagging_update_8004(): void {
  _social_profile_field_definitions_update('social_tagging');

  // Toggle the index users.
  try {
    // If the search module is enabled we need to update the group index.
    if (\Drupal::moduleHandler()->moduleExists('social_search')) {
      social_search_resave_search_indexes(['social_users']);
    }
  }
  catch (EntityStorageException $e) {
    \Drupal::logger('social_tagging')->info($e->getMessage());
  }
}

/**
 * Add the search api fields.
 */
function social_tagging_update_8005(): void {
  _social_tagging_add_fields_search_api();

  // Update definitions and schema.
  _social_profile_field_definitions_update('social_tagging');

  // Toggle the index users.
  try {
    // If the search module is enabled we need to update the group index.
+1 −0
Original line number Diff line number Diff line
@@ -4,5 +4,6 @@ services:
    arguments: ['@entity_type.manager', '@config.factory', '@language_manager']
  social_tagging.overrider:
    class: Drupal\social_tagging\SocialTaggingOverrides
    arguments: ['@config.factory']
    tags:
      - {name: config.factory.override, priority: 5}
+36 −22
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ namespace Drupal\social_tagging;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigFactoryOverrideInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;

@@ -14,6 +15,23 @@ class SocialTaggingOverrides implements ConfigFactoryOverrideInterface {

  use StringTranslationTrait;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Constructs the service.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->configFactory = $config_factory;
  }

  /**
   * Whether this config override should apply to the provided configurations.
   *
@@ -69,36 +87,32 @@ class SocialTaggingOverrides implements ConfigFactoryOverrideInterface {

    /** @var \Drupal\social_tagging\SocialTaggingService $tag_service */
    $tag_service = \Drupal::service('social_tagging.tag_service');
    $config = $this->configFactory;

    // Check if tagging is active.
    if (!($tag_service->active() && $tag_service->hasContent())) {
      return $overrides;
    }

    // Add tagging field to the search index.
    $config_search = [
      'search_api.index.social_content' => 'node',
    ];
    if ($tag_service->groupActive()) {
      $config_search['search_api.index.social_groups'] = 'group';
    }
    if ($tag_service->profileActive()) {
      $config_search['search_api.index.social_users'] = 'profile';
    // Remove tagging field from search index if not needed.
    if (!$tag_service->groupActive()) {
      $field_settings = $config
        ->getEditable('search_api.index.social_groups')
        ->get('field_settings');

      unset($field_settings['social_tagging']);

      $overrides['search_api.index.social_groups']['field_settings'] = $field_settings;
    }

    foreach ($config_search as $config_name => $type) {
      if (in_array($config_name, $names)) {
        $field_settings = \Drupal::configFactory()->getEditable($config_name)
    if (!$tag_service->profileActive()) {
      $field_settings = $config
        ->getEditable('search_api.index.social_users')
        ->get('field_settings');

        $field_settings['social_tagging'] = [
          'label' => $this->t('Tags'),
          'datasource_id' => 'entity:' . $type,
          'property_path' => 'social_tagging',
          'type' => 'integer',
        ];
        $overrides[$config_name]['field_settings'] = $field_settings;
      }
      unset($field_settings['social_tagging']);

      $overrides['search_api.index.social_users']['field_settings'] = $field_settings;
    }

    // Prepare fields.
+1 −26
Original line number Diff line number Diff line
@@ -17954,31 +17954,6 @@ parameters:
			count: 1
			path: modules/social_features/social_swiftmail/src/Plugin/Mail/SocialSwiftMailer.php
		-
			message: "#^Function social_tagging_install\\(\\) has no return type specified\\.$#"
			count: 1
			path: modules/social_features/social_tagging/social_tagging.install
		-
			message: "#^Function social_tagging_update_8001\\(\\) has no return type specified\\.$#"
			count: 1
			path: modules/social_features/social_tagging/social_tagging.install
		-
			message: "#^Function social_tagging_update_8002\\(\\) has no return type specified\\.$#"
			count: 1
			path: modules/social_features/social_tagging/social_tagging.install
		-
			message: "#^Function social_tagging_update_8003\\(\\) has no return type specified\\.$#"
			count: 1
			path: modules/social_features/social_tagging/social_tagging.install
		-
			message: "#^Function social_tagging_update_8004\\(\\) has no return type specified\\.$#"
			count: 1
			path: modules/social_features/social_tagging/social_tagging.install
		-
			message: "#^Call to an undefined method Drupal\\\\Core\\\\Entity\\\\EntityInterface\\:\\:get\\(\\)\\.$#"
			count: 1
@@ -18171,7 +18146,7 @@ parameters:
		-
			message: "#^\\\\Drupal calls should be avoided in classes, use dependency injection instead$#"
			count: 2
			count: 1
			path: modules/social_features/social_tagging/src/SocialTaggingOverrides.php
		-