You are here

auto_index.module in Auto Index 8

Same filename and directory in other branches
  1. 6 auto_index.module
  2. 7 auto_index.module

Auto-index: Automatically indexes node content on update.

Uses Drupal's search plugin to automatically index updated nodes as and when they are updated. This means that a user can update node content and that content is immediately searchable, by users of the site.

@author Steve Osguthorpe <sosguthorpe@1024300.no-reply.drupal.org>

File

auto_index.module
View source
<?php

/**
 * @file
 * Auto-index: Automatically indexes node content on update.
 * 
 * Uses Drupal's search plugin to automatically index updated nodes as and when they are updated.
 * This means that a user can update node content and that content is immediately searchable,
 * by users of the site.
 * 
 * @author Steve Osguthorpe <sosguthorpe@1024300.no-reply.drupal.org>
 */
use Drupal\node\NodeInterface;
use Drupal\comment\CommentInterface;

/**
 * Implementation of hook_ENTITY_TYPE_delete().
 */
function auto_index_node_delete(NodeInterface $node) {

  // Update search totals to reflect deleted node content.
  drupal_register_shutdown_function('search_update_totals');
}

/**
 * Implementation of hook_ENTITY_TYPE_update().
 */
function auto_index_node_update(NodeInterface $node) {
  _auto_index_node_index($node);
}

/**
 * Implementation of hook_ENTITY_TYPE_insert().
 */
function auto_index_node_insert(NodeInterface $node) {
  _auto_index_node_index($node);
}

/**
 * Implementation of hook_ENTITY_TYPE_insert().
 */
function auto_index_comment_insert(CommentInterface $comment) {
  _auto_index_comment_index($comment);
}

/**
 * Implementation of hook_ENTITY_TYPE_update().
 */
function auto_index_comment_update(CommentInterface $comment) {
  _auto_index_comment_index($comment);
}

/**
 * Implementation of hook_ENTITY_TYPE_delete().
 */
function auto_index_comment_delete(CommentInterface $comment) {
  _auto_index_comment_index($comment);
}

/**
 * Implementation of hook_ENTITY_TYPE_publish().
 */
function auto_index_comment_publish(CommentInterface $comment) {
  _auto_index_comment_index($comment);
}

/**
 * Implementation of hook_ENTITY_TYPE_unpublish().
 */
function auto_index_comment_unpublish(CommentInterface $comment) {
  _auto_index_comment_index($comment);
}

/**
 * Update the node attached to a comment.
 */
function _auto_index_comment_index(CommentInterface &$comment) {

  // Do the indexing.
  _auto_index_node_index($comment
    ->getCommentedEntity());
}

/**
 * Check to see whether a node ID has been acted on,
 * and if not then we can call on the search plugin,
 * to index the content.
 * 
 * @param unknown $node_id.
 */
function _auto_index_node_index($node) {

  // Load if numeric (node_id)
  if (is_numeric($node)) {
    $node = \Drupal::entityManager()
      ->getStorage('node')
      ->load($node);
    if (!$node) {
      return;
    }
  }

  // Static variable to keep track of any node ids already indexed.
  static $indexed_nodes = array();

  // Check if the node has already been acted upon.
  if (array_search($node
    ->id(), $indexed_nodes) === FALSE) {

    // Load the module plugin registry and look for automatic indexing in the config.
    $search_page_repository = \Drupal::service('search.search_page_repository');
    foreach ($search_page_repository
      ->getIndexableSearchPages() as $entity) {
      $plugin = $entity
        ->getPlugin();
      $conf = $plugin
        ->getConfiguration();
      if ($conf['automatic_indexing']) {
        $plugin
          ->indexSingleNode($node);
      }
    }

    // Append to array to ensure node only indexed once per action.
    $indexed_nodes[] = $node
      ->id();
  }
}

/**
 * Implementation of hook_search_plugin_alter
 * @param array $definitions Defintions of discovered search plugins.
 */
function auto_index_search_plugin_alter(array &$definitions) {

  // Ensure we use our new class instead.
  foreach ($definitions as $name => $def) {
    if ($def['class'] == 'Drupal\\node\\Plugin\\Search\\NodeSearch') {
      $definitions[$name]['class'] = 'Drupal\\auto_index\\Plugin\\Search\\AutoIndexNodeSearch';
    }
  }
}

Functions

Namesort descending Description
auto_index_comment_delete Implementation of hook_ENTITY_TYPE_delete().
auto_index_comment_insert Implementation of hook_ENTITY_TYPE_insert().
auto_index_comment_publish Implementation of hook_ENTITY_TYPE_publish().
auto_index_comment_unpublish Implementation of hook_ENTITY_TYPE_unpublish().
auto_index_comment_update Implementation of hook_ENTITY_TYPE_update().
auto_index_node_delete Implementation of hook_ENTITY_TYPE_delete().
auto_index_node_insert Implementation of hook_ENTITY_TYPE_insert().
auto_index_node_update Implementation of hook_ENTITY_TYPE_update().
auto_index_search_plugin_alter Implementation of hook_search_plugin_alter
_auto_index_comment_index Update the node attached to a comment.
_auto_index_node_index Check to see whether a node ID has been acted on, and if not then we can call on the search plugin, to index the content.