You are here

apachesolr_workbench_moderation.module in ApacheSolr Workbench Moderation 7

Created on May 21, 2013

ApacheSolr Workbench Moderation @author Nikolay Ignatov <nikolayi@propeople.dk>

@version 1.0

File

apachesolr_workbench_moderation.module
View source
<?php

/**
 * Created on May 21, 2013
 *
 * @file ApacheSolr Workbench Moderation
 * @author Nikolay Ignatov <nikolayi@propeople.dk>
 *
 * @version 1.0
 */

/**
 * Implements hook_apachesolr_entity_info_alter().
 */
function apachesolr_workbench_moderation_apachesolr_entity_info_alter(&$entity_info) {

  // Replace the standard Apache Solr status callback for nodes with our own.
  $key = array_search('apachesolr_index_node_status_callback', $entity_info['node']['status callback']);
  if ($key !== FALSE) {
    $entity_info['node']['status callback'][$key] = 'apachesolr_workbench_moderation_apachesolr_index_node_status_callback';
  }
}

/**
 * Apache Solr status callback for nodes that takes moderation into account.
 *
 * @param $entity_id
 *   The ID of the entity.
 * @param $entity_type
 *   The type of entity.
 * @param $entity
 *   In the case where the status is being checked while the entity is being
 *   saved, this contains the full entity object. In other cases, it will be
 *   NULL.
*/
function apachesolr_workbench_moderation_apachesolr_index_node_status_callback($entity_id, $entity_type, $entity = NULL) {

  // Make sure we have a boolean value.
  // Anything different from 1 becomes zero
  // Identical to status callback function in apachesolr module
  // Without it $entity argument is empty
  $entity = entity_load($entity_type, array(
    $entity_id,
  ));
  $entity = $entity ? reset($entity) : FALSE;
  if (empty($entity)) {

    // If the object failed to load, just stop.
    return FALSE;
  }
  if (workbench_moderation_node_type_moderated($entity->type)) {
    return $entity_type == 'node' && isset($entity) && !empty($entity->workbench_moderation['published']);
  }
  else {

    // If the normal status callback returns TRUE (i.e., the node itself is
    // published), return that.
    if ($status = apachesolr_index_node_status_callback($entity_id, $entity_type, $entity)) {
      return $status;
    }
  }

  // If the node is controlled by Workbench Moderation and is in the process of
  // being saved, return TRUE whenever it has a published revision. This keeps
  // it from being removed from Solr before workbench_moderation_store() makes
  // it the live revision.
  return $entity_type == 'node' && isset($entity) && workbench_moderation_node_type_moderated($entity->type) && !empty($entity->workbench_moderation['published']);
}

Functions