diff --git a/contrib/search_api_facetapi/search_api_facetapi.module b/contrib/search_api_facetapi/search_api_facetapi.module index b452734..681e5f5 100644 --- a/contrib/search_api_facetapi/search_api_facetapi.module +++ b/contrib/search_api_facetapi/search_api_facetapi.module @@ -80,7 +80,7 @@ function search_api_facetapi_facetapi_facet_info(array $searcher_info) { if (!empty($index->options['fields'])) { $wrapper = $index->entityWrapper(); $bundle_key = NULL; - if (($entity_info = entity_get_info($index->item_type)) && !empty($entity_info['bundle keys']['bundle'])) { + if ($index->getEntityType() && ($entity_info = entity_get_info($index->getEntityType())) && !empty($entity_info['bundle keys']['bundle'])) { $bundle_key = $entity_info['bundle keys']['bundle']; } @@ -144,7 +144,7 @@ function search_api_facetapi_facetapi_facet_info(array $searcher_info) { if ($bundle_key) { if ($key === $bundle_key) { // Set entity type this field contains bundle information for. - $facet_info[$key]['field api bundles'][] = $index->item_type; + $facet_info[$key]['field api bundles'][] = $index->getEntityType(); } else { // Add "bundle" as possible dependency plugin. diff --git a/contrib/search_api_views/includes/query.inc b/contrib/search_api_views/includes/query.inc index f30beaa..df53fa4 100644 --- a/contrib/search_api_views/includes/query.inc +++ b/contrib/search_api_views/includes/query.inc @@ -369,7 +369,7 @@ class SearchApiViewsQuery extends views_plugin_query { * query backend. */ public function get_result_wrappers($results, $relationship = NULL, $field = NULL) { - $is_entity = (boolean) entity_get_info($this->index->item_type); + $is_entity = (boolean) $this->index->getEntityType() && entity_get_info($this->index->getEntityType()); $wrappers = array(); $load_entities = array(); foreach ($results as $row_index => $row) { @@ -386,14 +386,14 @@ class SearchApiViewsQuery extends views_plugin_query { // If the results are entities, we pre-load them to make use of a multiple // load. (Otherwise, each result would be loaded individually.) if (!empty($load_entities)) { - $entities = entity_load($this->index->item_type, array_keys($load_entities)); + $entities = entity_load($this->index->getEntityType(), array_keys($load_entities)); foreach ($entities as $entity_id => $entity) { $wrappers[$load_entities[$entity_id]] = $this->index->entityWrapper($entity); } } // Apply the relationship, if necessary. - $type = $this->index->item_type; + $type = $this->index->getEntityType(); $selector_suffix = ''; if ($field && ($pos = strrpos($field, ':'))) { $selector_suffix = substr($field, 0, $pos); diff --git a/contrib/search_api_views/search_api_views.views.inc b/contrib/search_api_views/search_api_views.views.inc index 3bb0fc0..9a122cd 100644 --- a/contrib/search_api_views/search_api_views.views.inc +++ b/contrib/search_api_views/search_api_views.views.inc @@ -20,9 +20,9 @@ function search_api_views_views_data() { 'help' => t('Use the %name search index for filtering and retrieving data.', array('%name' => $index->name)), 'query class' => 'search_api_views_query', ); - if (isset($entity_types[$index->item_type])) { + if (isset($entity_types[$index->getEntityType()])) { $table['table'] += array( - 'entity type' => $index->item_type, + 'entity type' => $index->getEntityType(), 'skip entity load' => TRUE, ); } diff --git a/includes/callback_add_viewed_entity.inc b/includes/callback_add_viewed_entity.inc index bd0f8d8..1f6a63f 100644 --- a/includes/callback_add_viewed_entity.inc +++ b/includes/callback_add_viewed_entity.inc @@ -11,14 +11,16 @@ class SearchApiAlterAddViewedEntity extends SearchApiAbstractAlterCallback { * @see SearchApiAlterCallbackInterface::supportsIndex() */ public function supportsIndex(SearchApiIndex $index) { - return (bool) entity_get_info($index->item_type); + return (bool) $index->getEntityType() && entity_get_info($index->getEntityType()); } public function configurationForm() { - $info = entity_get_info($this->index->item_type); $view_modes = array(); - foreach ($info['view modes'] as $key => $mode) { - $view_modes[$key] = $mode['label']; + if (($entity_type = $this->index->getEntityType())) { + $info = entity_get_info($entity_type); + foreach ($info['view modes'] as $key => $mode) { + $view_modes[$key] = $mode['label']; + } } $this->options += array('mode' => reset($view_modes)); if (count($view_modes) > 1) { @@ -60,7 +62,7 @@ class SearchApiAlterAddViewedEntity extends SearchApiAbstractAlterCallback { $original_user = $GLOBALS['user']; $GLOBALS['user'] = drupal_anonymous_user(); - $type = $this->index->item_type; + $type = $this->index->getEntityType(); $mode = empty($this->options['mode']) ? 'full' : $this->options['mode']; foreach ($items as $id => &$item) { // Since we can't really know what happens in entity_view() and render(), diff --git a/includes/callback_bundle_filter.inc b/includes/callback_bundle_filter.inc index 4c758c3..469750b 100644 --- a/includes/callback_bundle_filter.inc +++ b/includes/callback_bundle_filter.inc @@ -7,25 +7,30 @@ class SearchApiAlterBundleFilter extends SearchApiAbstractAlterCallback { public function supportsIndex(SearchApiIndex $index) { - return ($info = entity_get_info($index->item_type)) && self::hasBundles($info); + return $index->getEntityType() && ($info = entity_get_info($index->getEntityType())) && self::hasBundles($info); } public function alterItems(array &$items) { - $info = entity_get_info($this->index->item_type); - if (self::hasBundles($info) && isset($this->options['bundles'])) { - $bundles = array_flip($this->options['bundles']); - $default = (bool) $this->options['default']; - $bundle_prop = $info['entity keys']['bundle']; - foreach ($items as $id => $item) { - if (isset($bundles[$item->$bundle_prop]) == $default) { - unset($items[$id]); + if (($entity_type = $this->index->getEntityType())) { + $info = entity_get_info($entity_type); + if (self::hasBundles($info) && isset($this->options['bundles'])) { + $bundles = array_flip($this->options['bundles']); + $default = (bool) $this->options['default']; + $bundle_prop = $info['entity keys']['bundle']; + foreach ($items as $id => $item) { + if (isset($bundles[$item->$bundle_prop]) == $default) { + unset($items[$id]); + } } } } } public function configurationForm() { - $info = entity_get_info($this->index->item_type); + $info = array(); + if (($entity_type = $this->index->getEntityType())) { + $info = entity_get_info($entity_type); + } if (self::hasBundles($info)) { $options = array(); foreach ($info['bundles'] as $bundle => $bundle_info) { diff --git a/includes/callback_node_access.inc b/includes/callback_node_access.inc index ce671d7..5acc76c 100644 --- a/includes/callback_node_access.inc +++ b/includes/callback_node_access.inc @@ -22,7 +22,7 @@ class SearchApiAlterNodeAccess extends SearchApiAbstractAlterCallback { */ public function supportsIndex(SearchApiIndex $index) { // Currently only node access is supported. - return $index->item_type === 'node'; + return $index->getEntityType() === 'node'; } /** diff --git a/includes/callback_node_status.inc b/includes/callback_node_status.inc index bfe35e2..ee57c38 100644 --- a/includes/callback_node_status.inc +++ b/includes/callback_node_status.inc @@ -22,7 +22,7 @@ class SearchApiAlterNodeStatus extends SearchApiAbstractAlterCallback { * TRUE if the callback can run on the given index; FALSE otherwise. */ public function supportsIndex(SearchApiIndex $index) { - return $index->item_type === 'node'; + return $index->getEntityType() === 'node'; } /** diff --git a/includes/datasource.inc b/includes/datasource.inc index 21a6157..b0315ab 100755 --- a/includes/datasource.inc +++ b/includes/datasource.inc @@ -237,6 +237,13 @@ interface SearchApiDataSourceControllerInterface { */ public function getIndexStatus(SearchApiIndex $index); + /** + * Get the entity_type if any of this data source + * + * @return string + * An entity type string or NULL. + */ + public function getEntityType(); } /** @@ -265,6 +272,11 @@ abstract class SearchApiAbstractDataSourceController implements SearchApiDataSou protected $type; /** + * The entity_type or NULL for this controller instance. + */ + protected $entityType = NULL; + + /** * The info array for the item type, as specified via * hook_search_api_item_type_info(). * @@ -314,6 +326,20 @@ abstract class SearchApiAbstractDataSourceController implements SearchApiDataSou public function __construct($type) { $this->type = $type; $this->info = search_api_get_item_type_info($type); + + if (!empty($this->info['entity_type'])) { + $this->entityType = $this->info['entity_type']; + } + } + + /** + * Get the entity_type if any of this data source + * + * @return string + * An entity type string or ''. + */ + public function getEntityType() { + return $this->entityType; } /** @@ -333,7 +359,7 @@ abstract class SearchApiAbstractDataSourceController implements SearchApiDataSou */ public function getMetadataWrapper($item = NULL, array $info = array()) { $info += $this->getPropertyInfo(); - return entity_metadata_wrapper($this->type, $item, $info); + return entity_metadata_wrapper($this->getEntityType(), $item, $info); } /** @@ -687,10 +713,12 @@ abstract class SearchApiAbstractDataSourceController implements SearchApiDataSou */ protected function checkIndex(SearchApiIndex $index) { if ($index->item_type != $this->type) { - $index_type = search_api_get_item_type_info($index->item_type); - $index_type = empty($index_type['name']) ? $index->item_type : $index_type['name']; - $msg = t('Invalid index @index of type @index_type passed to data source controller for type @this_type.', - array('@index' => $index->name, '@index_type' => $index_type, '@this_type' => $this->info['name'])); + $index_type = search_api_get_item_type_info($index->getEntityType()); + $index_type = empty($index_type['name']) ? $index->getEntityType() : $index_type['name']; + $msg = t( + 'Invalid index @index of type @index_type passed to data source controller for type @this_type.', + array('@index' => $index->name, '@index_type' => $index_type, '@this_type' => $this->info['name']) + ); throw new SearchApiDataSourceException($msg); } } diff --git a/includes/datasource_entity.inc b/includes/datasource_entity.inc index 641a939..6f15ec5 100644 --- a/includes/datasource_entity.inc +++ b/includes/datasource_entity.inc @@ -20,8 +20,8 @@ class SearchApiEntityDataSourceController extends SearchApiAbstractDataSourceCon * search_api_field_types(). List types ("list<*>") are not allowed. */ public function getIdFieldInfo() { - $info = entity_get_info($this->type); - $properties = entity_get_property_info($this->type); + $info = entity_get_info($this->entityType); + $properties = entity_get_property_info($this->entityType); if (empty($info['entity keys']['id'])) { throw new SearchApiDataSourceException(t("Entity type @type doesn't specify an ID key.", array('@type' => $info['label']))); } @@ -52,7 +52,7 @@ class SearchApiEntityDataSourceController extends SearchApiAbstractDataSourceCon * The loaded items, keyed by ID. */ public function loadItems(array $ids) { - $items = entity_load($this->type, $ids); + $items = entity_load($this->entityType, $ids); // If some items couldn't be loaded, remove them from tracking. if (count($items) != count($ids)) { $ids = array_flip($ids); @@ -80,7 +80,7 @@ class SearchApiEntityDataSourceController extends SearchApiAbstractDataSourceCon * @see entity_metadata_wrapper() */ public function getMetadataWrapper($item = NULL, array $info = array()) { - return entity_metadata_wrapper($this->type, $item, $info); + return entity_metadata_wrapper($this->entityType, $item, $info); } /** @@ -93,7 +93,7 @@ class SearchApiEntityDataSourceController extends SearchApiAbstractDataSourceCon * Either the unique ID of the item, or NULL if none is available. */ public function getItemId($item) { - $id = entity_id($this->type, $item); + $id = entity_id($this->entityType, $item); return $id ? $id : NULL; } @@ -107,7 +107,7 @@ class SearchApiEntityDataSourceController extends SearchApiAbstractDataSourceCon * Either a human-readable label for the item, or NULL if none is available. */ public function getItemLabel($item) { - $label = entity_label($this->type, $item); + $label = entity_label($this->entityType, $item); return $label ? $label : NULL; } @@ -123,7 +123,7 @@ class SearchApiEntityDataSourceController extends SearchApiAbstractDataSourceCon * item has no URL of its own. */ public function getItemUrl($item) { - if ($this->type == 'file') { + if ($this->entityType == 'file') { return array( 'path' => file_create_url($item->uri), 'options' => array( @@ -132,7 +132,7 @@ class SearchApiEntityDataSourceController extends SearchApiAbstractDataSourceCon ), ); } - $url = entity_uri($this->type, $item); + $url = entity_uri($this->entityType, $item); return $url ? $url : NULL; } @@ -158,7 +158,7 @@ class SearchApiEntityDataSourceController extends SearchApiAbstractDataSourceCon // all items again without any key conflicts. $this->stopTracking($indexes); - $entity_info = entity_get_info($this->type); + $entity_info = entity_get_info($this->entityType); if (!empty($entity_info['base table'])) { // Use a subselect, which will probably be much faster than entity_load(). @@ -200,7 +200,7 @@ class SearchApiEntityDataSourceController extends SearchApiAbstractDataSourceCon * An array containing all item IDs for this type. */ protected function getAllItemIds() { - return array_keys(entity_load($this->type)); + return array_keys(entity_load($this->entityType)); } } diff --git a/search_api.module b/search_api.module index bba0681..c64114b 100644 --- a/search_api.module +++ b/search_api.module @@ -725,6 +725,7 @@ function search_api_search_api_item_type_info() { $types[$type] = array( 'name' => $info['label'], 'datasource controller' => 'SearchApiEntityDataSourceController', + 'entity_type' => $type, ); } }