diff --git a/includes/media_views_plugin_style_media_browser.inc b/includes/media_views_plugin_style_media_browser.inc index 12ba56f..e6a4958 100644 --- a/includes/media_views_plugin_style_media_browser.inc +++ b/includes/media_views_plugin_style_media_browser.inc @@ -37,7 +37,14 @@ class media_views_plugin_style_media_browser extends views_plugin_style_list { * Add the base field (fid) to the query. */ function query() { - $this->view->query->add_field($this->view->base_table, $this->view->base_field); + if (method_exists($this->view->query, 'add_field')) { + // Normal file_managed based view. + $this->view->query->add_field($this->view->base_table, $this->view->base_field); + } + if (method_exists($this->view->query, 'addField')) { + // Search API based view. + $this->view->query->addField('fid'); + } parent::query(); } diff --git a/media.views.inc b/media.views.inc index 08100c3..d55ed8b 100644 --- a/media.views.inc +++ b/media.views.inc @@ -11,13 +11,27 @@ function media_views_plugins() { $plugins = array(); + // Generate a list of which base-tables to enabled the plugins for. + $base = array('file_managed'); // Always allow the actual file-table + + if (module_exists('search_api')) { + // If the Search API module exists, also allow indices of the file-entity + // that has the `fid` field indexed. + $indices = search_api_index_load_multiple(NULL); + foreach ($indices AS $machine_name => $index) { + if ($index->item_type == 'file' && isset($index->options['fields']['fid'])) { + $base[] = 'search_api_index_' . $machine_name; + } + } + } + // Display plugin. $plugins['display']['media_browser'] = array( 'title' => t('Media browser tab'), 'help' => t('Display as a tab in the media browser.'), 'handler' => 'media_views_plugin_display_media_browser', 'theme' => 'views_view', - 'base' => array('file_managed'), + 'base' => $base, 'use ajax' => TRUE, 'use pager' => TRUE, 'accept attachments' => TRUE, @@ -29,7 +43,7 @@ function media_views_plugins() { 'help' => t('Displays rows as an HTML list.'), 'handler' => 'media_views_plugin_style_media_browser', 'theme' => 'media_views_view_media_browser', - 'base' => array('file_managed'), + 'base' => $base, 'uses row plugin' => FALSE, 'uses row class' => FALSE, 'uses options' => FALSE, @@ -48,6 +62,9 @@ function template_preprocess_media_views_view_media_browser(&$vars) { // Load file objects for each View result. $fids = array(); foreach ($vars['rows'] as $index => $row) { + // The Search API module returns the row in a slightly different format, + // so convert it to the format that the normal file_managed table returns. + if (!empty($row->entity->fid)) $vars['rows'][$index]->fid = $row->entity->fid; $fids[$index] = $row->fid; } $files = file_load_multiple($fids);