It would be great if full name was available to Search API.

Verify that the name field works with Search API. If in does not work, update this issue.

Comments

rooby created an issue. See original summary.

alan d.’s picture

Status: Active » Postponed

Started playing but a bit pointless without anything configured to test and limited in time.

In case someone wants to pick this up, some code below. Absolutely everything is untested.

NameSearchAlterCallback::supportsIndex() was from Metatags implementation but checking fieldable flag.

Add to the file name.search_api.inc


/**
 * @file
 * Contains NameSearchAlterCallback.
 */

/**
 * Implements hook_search_api_alter_callback_info().
 */
function name_search_api_alter_callback_info() {
  return [
    'search_api_name_alter_callback' => [
      'name' => t('Name fields'),
      'description' => t("Adds the item's complete name field to the indexed data."),
      'class' => 'NameSearchAlterCallback',
    ],
  ];
}

/**
 * Only add the class if Search API is installed.
 */
if (class_exists('SearchApiAbstractAlterCallback')) {

  /**
   * Adds complete name field values to the indexed items.
   */
  class NameSearchAlterCallback extends SearchApiAbstractAlterCallback {

    /**
     * {@inheritdoc}
     */
    public function supportsIndex(SearchApiIndex $index) {
      // Check for multiple datasources.
      if (isset($index->item_type) && $index->item_type == 'multiple') {
        // Verify that atleast one datasource type is entity.
        foreach ($index->options['datasource']['types'] as $datasource_type) {
          if ($entity_info = entity_get_info($datasource_type)) {
            if ($entity_info['fieldable']) {
              return TRUE;
            }
          }
        }
      }
      elseif ($entity_type = $index->getEntityType()) {
        if ($entity_info = entity_get_info($entity_type)) {
          if ($entity_info['fieldable']) {
            return TRUE;
          }
        }
      }
      return FALSE;
    }

    /**
     * {@inheritdoc}
     */
    public function alterItems(array &$items) {
      if (isset($this->index->item_type) && $this->index->item_type == 'multiple') {
        foreach ($items as $id => $item) {
          $item_type = $item->item_type;
          if (isset($item->{$item_type})) {
            $entity_item = $item->{$item_type};
            // ....
          }
        }
      }
      else {
        $entity_type = $this->index->getEntityType();
        foreach ($items as $id => $item) {
          // ...
        }
      }
    }

    /**
     * {@inheritdoc}
     */
    public function propertyInfo() {
      $properties = array();
      
      foreach (field_info_field_map() as $field_name => $field) {
        if ($field['type'] == 'name') {
          $name_fields[$field_name] = field_info_field($field_name);
          $properties['name_' . $field_name] = [
            'label' => t('Name field: @label', ['@label' => $field_name]),
            'description' => t('@label attached to an item in default format.', ['@label' => $field_name]),
            'type' => 'text',
          ];          
        }
      }

      return $properties;
    }

  }
}
liam morland’s picture

Version: 7.x-1.x-dev » 8.x-1.x-dev
bluegeek9’s picture

Issue summary: View changes
Status: Postponed » Active
bluegeek9’s picture

Status: Active » Fixed

The name field works with Search API. I tested it.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

bluegeek9’s picture

//www.flaticon.com/free-icons/thank-you Thank you for your contribution! Your continued support makes this project sustainable.
There are multiple ways to show appreciation for the work contributed to this project including:

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.