You are here

apachesolr_paragraphs.module in Apachesolr Paragraphs 8

Same filename and directory in other branches
  1. 7 apachesolr_paragraphs.module

Functionality for including paragraphs field contents in Apache Solr index.

File

apachesolr_paragraphs.module
View source
<?php

/**
 * @file
 * Functionality for including paragraphs field contents in Apache Solr index.
 */

/**
 * Implements hook_apachesolr_field_mappings().
 */
function apachesolr_paragraphs_apachesolr_field_mappings() {
  $mappings['paragraphs'] = array(
    'indexing_callback' => 'apachesolr_paragraphs_indexing_callback',
    'query types' => array(
      'term',
    ),
  );
  return $mappings;
}

/**
 * Indexing callback for paragraph fields.
 */
function apachesolr_paragraphs_indexing_callback($entity, $field_name, $index_key, $field_info) {

  // Load each field collection item and add its fields to the index.
  $fields = array();
  if (!empty($entity->{$field_name})) {
    module_load_include('module', 'paragraphs');
    foreach ($entity->{$field_name} as $paragraphs) {
      foreach ($paragraphs as $delta => $item) {

        // Attempt to get paragraph entity.
        if ($paragraph_entity = paragraphs_field_get_entity($item)) {

          // Send paragraph entity to callback for adding fields.
          apachesolr_paragraphs_add_paragraph_fields($fields, $paragraph_entity);
        }
      }
    }
  }
  return $fields;
}

/**
 * Callback function to add paragraph entity fields to SOLR index.
 *
 * @param array &$fields
 *   Array of field to be indexed, passed by reference.
 * @param object $entity
 *   Paragraph entity object.
 */
function apachesolr_paragraphs_add_paragraph_fields(array &$fields, $entity) {

  // Set entity type.
  $entity_type = 'paragraphs_item';

  // Get information about the entity type.
  $info = \Drupal::entityManager()
    ->getDefinition($entity_type);

  // Is the entity type fieldable?
  if ($info['fieldable']) {

    // This function is modeled after field_apachesolr_index_document_build()
    // Need to include apachesolr.index.inc for field data.
    module_load_include('inc', 'apachesolr', 'apachesolr.index');
    $indexed_fields = apachesolr_entity_fields($entity_type);
    foreach ($indexed_fields as $index_key => $nodefields) {
      foreach ($nodefields as $field_info) {
        $field_name = $field_info['field']['field_name'];

        // See if the node has fields that can be indexed.
        if (isset($entity->{$field_name})) {

          // Got a field.
          $functions = $field_info['indexing_callback'];
          if (!is_array($functions)) {
            $functions = array(
              $functions,
            );
          }
          foreach ($functions as $function) {
            if ($function && function_exists($function)) {

              // NOTE: This function should always return an array.  One
              // entity field may be indexed to multiple Solr fields.
              $new_fields = $function($entity, $field_name, $index_key, $field_info);
              foreach ($new_fields as $new_field) {
                $fields[] = $new_field;
              }
            }
          }
        }
      }
    }
  }
}

Functions

Namesort descending Description
apachesolr_paragraphs_add_paragraph_fields Callback function to add paragraph entity fields to SOLR index.
apachesolr_paragraphs_apachesolr_field_mappings Implements hook_apachesolr_field_mappings().
apachesolr_paragraphs_indexing_callback Indexing callback for paragraph fields.