You are here

function apachesolr_paragraphs_add_paragraph_fields in Apachesolr Paragraphs 8

Same name and namespace in other branches
  1. 7 apachesolr_paragraphs.module \apachesolr_paragraphs_add_paragraph_fields()

Callback function to add paragraph entity fields to SOLR index.

Parameters

array &$fields: Array of field to be indexed, passed by reference.

object $entity: Paragraph entity object.

1 call to apachesolr_paragraphs_add_paragraph_fields()
apachesolr_paragraphs_indexing_callback in ./apachesolr_paragraphs.module
Indexing callback for paragraph fields.

File

./apachesolr_paragraphs.module, line 47
Functionality for including paragraphs field contents in Apache Solr index.

Code

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;
              }
            }
          }
        }
      }
    }
  }
}