You are here

class SearchApiAlgoliaHelper in Search API Algolia 3.0.x

Class Search Api Algolia Helper.

Hierarchy

Expanded class hierarchy of SearchApiAlgoliaHelper

1 file declares its use of SearchApiAlgoliaHelper
SearchApiAlgoliaBackend.php in src/Plugin/search_api/backend/SearchApiAlgoliaBackend.php
1 string reference to 'SearchApiAlgoliaHelper'
search_api_algolia.services.yml in ./search_api_algolia.services.yml
search_api_algolia.services.yml
1 service uses SearchApiAlgoliaHelper
search_api_algolia.helper in ./search_api_algolia.services.yml
Drupal\search_api_algolia\SearchApiAlgoliaHelper

File

src/SearchApiAlgoliaHelper.php, line 18

Namespace

Drupal\search_api_algolia
View source
class SearchApiAlgoliaHelper {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManager
   */
  protected $languageManager;

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * Constructs a new class instance.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager.
   * @param \Drupal\Core\Language\LanguageManager $language_manager
   *   The language manager.
   * @param \Drupal\Core\Database\Connection $connection
   *   Database connection object.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager, LanguageManager $language_manager, Connection $connection) {
    $this->entityTypeManager = $entityTypeManager;
    $this->languageManager = $language_manager;
    $this->connection = $connection;
  }

  /**
   * Implements hook_entity_delete().
   *
   * Deletes all entries for this entity from the tracking table for each index
   * that tracks this entity type.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The deleted entity.
   *
   * @see search_api_algolia_entity_delete()
   */
  public function entityDelete(EntityInterface $entity) {

    // Check if the entity is a content entity.
    if (!$entity instanceof ContentEntityInterface || $entity->search_api_skip_tracking) {
      return;
    }
    $indexes = ContentEntity::getIndexesForEntity($entity);
    if (!$indexes) {
      return;
    }

    // Remove the search items for all the entity's translations.
    foreach ($indexes as $index) {
      $object_id_field = $index
        ->getOption('object_id_field');
      if ($object_id_field) {
        $object_id = $entity
          ->get($object_id_field)
          ->getString();
        if ($object_id) {

          // @todo make this work with indexes having language suffix.
          $this
            ->scheduleForDeletion($index, [
            $object_id,
          ]);
        }
      }
    }
  }

  /**
   * Store deleted items in search_api_algolia_deleted_items table.
   *
   * This items will be deleted via drush command sapia-d.
   *
   * @param \Drupal\search_api\IndexInterface $index
   *   Index.
   * @param array $item_ids
   *   Items to delete.
   * @param string|null $language
   *   Language code if multi-lingual.
   */
  public function scheduleForDeletion(IndexInterface $index, array $item_ids, $language = NULL) {
    if ($index
      ->get('read_only')) {
      return;
    }
    foreach ($item_ids as $objectid) {
      $indexId = $index
        ->getOption('algolia_index_name');
      if ($index
        ->getOption('algolia_index_apply_suffix') && !empty($language)) {
        $indexId .= '_' . $language;
      }
      $this->connection
        ->insert('search_api_algolia_deleted_items')
        ->fields([
        'index_id',
        'object_id',
      ])
        ->values([
        'index_id' => $indexId,
        'object_id' => $objectid,
      ])
        ->execute();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SearchApiAlgoliaHelper::$connection protected property The database connection.
SearchApiAlgoliaHelper::$entityTypeManager protected property The entity type manager.
SearchApiAlgoliaHelper::$languageManager protected property The language manager.
SearchApiAlgoliaHelper::entityDelete public function Implements hook_entity_delete().
SearchApiAlgoliaHelper::scheduleForDeletion public function Store deleted items in search_api_algolia_deleted_items table.
SearchApiAlgoliaHelper::__construct public function Constructs a new class instance.