Commit f5ef2f74 authored by Nick_vh's avatar Nick_vh

removing search_api_facets

parent 255d8599
......@@ -6,6 +6,10 @@
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\search_api\Entity\Index;
use Drupal\search_api\Query\QueryInterface;
use Drupal\Component\Plugin\PluginBase;
/**
* Implements hook_help().
......@@ -45,4 +49,96 @@ function facetapi_get_enabled_facets() {
$module_handler = \Drupal::service('module_handler');
$facet_definitions = $module_handler->invokeAll('facetapi_facet_info');
return $facet_definitions;
}
/**
* Implements hook_entity_type_build().
*/
function facetapi_entity_type_build(array &$entity_types) {
/** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
$entity_types['search_api_index']
->setFormClass('facets', 'Drupal\facetapi\Form\IndexFacetsForm');
}
/**
* Implementation of hook_search_api_query_alter.
*
* @param \Drupal\search_api\Query\QueryInterface $query
*/
function facetapi_search_api_query_alter(QueryInterface &$query) {
// Get the adapter derivative.
// Get the plugin manager.
/** @var \Drupal\facetapi\Adapter\AdapterPluginManagerInterface $plugin_manager */
$plugin_manager = \Drupal::service('plugin.manager.facetapi.adapter');
// Get the searcher id.
// This is the id of the view.
$search_id = $query->getOption('search id');
$plugin_id = 'search_api';
$adapter = $plugin_manager->getMyOwnChangeLaterInstance($plugin_id, $search_id);
// Add the active filters.
$adapter->alterQuery($query);
}
function facetapi_facetapi_facet_info() {
$facet_info = array();
// Load all views.
/**
* @var EntityManagerInterface
*/
$entity_manager = \Drupal::entityManager();
// @TODO: filter on search api views.
$views = $entity_manager->getStorage('view')->loadMultiple();
// Get the views executable factory.
$views_executable_factory = Drupal::service('views.executable');
foreach ($views as $view_id => $view_entity) {
$view = $views_executable_factory->get($view_entity);
// Determine if the view is an search api view.
if (substr($view->storage->get('base_table'), 0, 17) == 'search_api_index_') {
// Get the view id.
$view_id = $view->id();
$index_id = substr($view->storage->get('base_table'), 17);
$index = Index::load($index_id);
// Get fields.
$fields = $index->getFields();
// Add facets for each view display which is of type page or block.
foreach ($view->storage->get('display') as $display) {
$supported_display_plugins = array(
'page',
'block',
);
if (in_array($display['display_plugin'], $supported_display_plugins)) {
// @TODO: This should be a function in search api. Look it up.
$searcher_name = 'search_api_views:' . $view_id . ':' . $display['id'];
foreach ($fields as $field_name => $field) {
if ( empty($facet_info[$searcher_name])) {
$facet_info[$searcher_name] = array();
}
// Determine the query type.
$term_types = array(
'string',
'integer',
);
if (in_array($field->getType(), $term_types)) {
$facet_info[$searcher_name][$field_name] = array(
'name' => $field_name,
'label' => $field->getLabel(),
'field' => $field->getFieldIdentifier(),
'query type plugin' => 'search_api_term',
'searcher' => $searcher_name,
);
}
}
}
}
}
}
return $facet_info;
}
\ No newline at end of file
entity.search_api_index.facets:
path: '/admin/config/search/search-api/index/{search_api_index}/facets'
defaults:
_entity_form: 'search_api_index.facets'
requirements:
_entity_access: 'search_api_index.facets'
\ No newline at end of file
type: module
name: 'Search API facets'
description: 'Provides facet integration for search api based on facetapi.'
package: Search
core: 8.x
dependencies:
- search_api
- facetapi
\ No newline at end of file
<?php
/**
* Hooks
*/
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\search_api\Entity\Index;
use Drupal\search_api\Query\QueryInterface;
use Drupal\Component\Plugin\PluginBase;
/**
* Implementation of hook_search_api_query_alter.
*
* @param \Drupal\search_api\Query\QueryInterface $query
*/
function search_api_facets_search_api_query_alter(QueryInterface &$query) {
// Get the adapter derivative.
// Get the plugin manager.
/** @var \Drupal\facetapi\Adapter\AdapterPluginManagerInterface $plugin_manager */
$plugin_manager = \Drupal::service('plugin.manager.facetapi.adapter');
// Get the searcher id.
// This is the id of the view.
$search_id = $query->getOption('search id');
$plugin_id = 'search_api';
$adapter = $plugin_manager->getMyOwnChangeLaterInstance($plugin_id, $search_id);
// Add the active filters.
$adapter->alterQuery($query);
}
function search_api_facets_facetapi_facet_info() {
$facet_info = array();
// Load all views.
/**
* @var EntityManagerInterface
*/
$entity_manager = \Drupal::entityManager();
// @TODO: filter on search api views.
$views = $entity_manager->getStorage('view')->loadMultiple();
// Get the views executable factory.
$views_executable_factory = Drupal::service('views.executable');
foreach ($views as $view_id => $view_entity) {
$view = $views_executable_factory->get($view_entity);
// Determine if the view is an search api view.
if (substr($view->storage->get('base_table'), 0, 17) == 'search_api_index_') {
// Get the view id.
$view_id = $view->id();
$index_id = substr($view->storage->get('base_table'), 17);
$index = Index::load($index_id);
// Get fields.
$fields = $index->getFields();
// Add facets for each view display which is of type page or block.
foreach ($view->storage->get('display') as $display) {
$supported_display_plugins = array(
'page',
'block',
);
if (in_array($display['display_plugin'], $supported_display_plugins)) {
// @TODO: This should be a function in search api. Look it up.
$searcher_name = 'search_api_views:' . $view_id . ':' . $display['id'];
foreach ($fields as $field_name => $field) {
if ( empty($facet_info[$searcher_name])) {
$facet_info[$searcher_name] = array();
}
// Determine the query type.
$term_types = array(
'string',
'integer',
);
if (in_array($field->getType(), $term_types)) {
$facet_info[$searcher_name][$field_name] = array(
'name' => $field_name,
'label' => $field->getLabel(),
'field' => $field->getFieldIdentifier(),
'query type plugin' => 'search_api_term',
'searcher' => $searcher_name,
);
}
}
}
}
}
}
return $facet_info;
}
\ No newline at end of file
<?php
/**
* Contains Drupal\facetapi\AdapterManager
* Contains Drupal\facetapi\Adapter\AdapterPluginManager
*/
namespace Drupal\facetapi\Adapter;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
......@@ -18,7 +17,7 @@ class AdapterPluginManager extends DefaultPluginManager implements AdapterPlugin
protected $adapters = [];
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/facetapi/Adapter', $namespaces, $module_handler, 'Drupal\facetapi\Adapter\AdapterInterface', 'Drupal\facetapi\Annotation\FacetApiAdapter');
parent::__construct('Plugin/facetapi/adapter', $namespaces, $module_handler, 'Drupal\facetapi\Adapter\AdapterInterface', 'Drupal\facetapi\Annotation\FacetApiAdapter');
$this->alterInfo('facetapi_adapter_info');
$this->setCacheBackend($cache_backend, 'facetapi_adapter_plugins');
}
......
<?php
/**
* @file Contains Drupal\facetapi\Adapter\AdapterPluginManagerInterface
*/
......
<?php
/**
* @file
* Contains \Drupal\facetapi\Form\IndexFacetsForm.
*/
namespace Drupal\facetapi\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form for configuring the processors of a search index.
*/
class IndexFacetsForm extends EntityForm {
/**
* The index being configured.
*
* @var \Drupal\search_api\IndexInterface
*/
protected $entity;
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* Constructs an IndexFacetsForm object.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
*/
public function __construct(EntityManagerInterface $entity_manager, \Drupal\facetapi\Adapter\AdapterPluginManager $adapter_manager) {
$this->entityManager = $entity_manager;
$this->adapterManager = $adapter_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
/** @var \Drupal\Core\Entity\EntityManagerInterface $entity_manager */
$entity_manager = $container->get('entity.manager');
/** @var \Drupal\facetapi\Adapter\AdapterPluginManager $adapter_manager */
$adapter_manager = $container->get('plugin.manager.facetapi.adapter');
return new static($entity_manager, $adapter_manager);
}
/**
* {@inheritdoc}
*/
public function getBaseFormID() {
return NULL;
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$index = $this->entity;
if (!$index->isServerEnabled()) {
return array('#markup' => t('Since this index is at the moment disabled, no facets can be activated.'));
}
if (!$index->getServer()->supportsFeature('search_api_facets')) {
return array('#markup' => t('This index uses a server that does not support facet functionality.'));
}
// Instantiates adapter, loads realm.
$adapter = $this->adapterManager->getDefinition('search_api');
// @todo inject realmManager to load realms. Look at the DataSourceDerivative
$realm_name = $index->getServerId() . ':' . $index->id();
return array('#markup' => t('Bla.'));
//$realm = $this->searcherManager->getInstance();
/*$realm = facetapi_realm_load($realm_name);
// @todo inject facetManager to get Facet Info from Searchers
$facet_info = facetapi_get_facet_info($searcher);
$form['#facetapi'] = array(
'adapter' => $adapter,
'realm' => $realm,
'facet_info' => $facet_info
);
$form['description'] = array(
'#prefix' => '<div class="facetapi-realm-description">',
'#markup' => filter_xss_admin($realm['description']),
'#suffix' => "</div>\n",
);
$form['performance'] = array(
'#prefix' => '<div class="facetapi-performance-note">',
'#markup' => t('For performance reasons, you should only enable facets that you intend to have available to users on the search page.'),
'#suffix' => "</div>\n",
);
$form['table'] = array(
'#theme' => 'facetapi_realm_settings_table',
'#facetapi' => &$form['#facetapi'],
'operations' => array('#tree' => TRUE),
'weight' => array('#tree' => TRUE),
);
// Builds "enabled_facets" options.
$options = $default_value = array();
foreach ($form['#facetapi']['facet_info'] as $facet_name => $facet) {
$settings = $adapter->getFacetSettings($facet, $realm);
$global_settings = $adapter->getFacetSettingsGlobal($facet);
// Builds array of operations to use in the dropbutton.
$operations = array();
$operations[] = array(
'title' => t('Configure display'),
'href' => facetapi_get_settings_path($searcher, $realm['name'], $facet_name, 'edit')
);
if ($facet['dependency plugins']) {
$operations[] = array(
'title' => t('Configure dependencies'),
'href' => facetapi_get_settings_path($searcher, $realm['name'], $facet_name, 'dependencies')
);
}
if (facetapi_filters_load($facet_name, $searcher, $realm['name'])) {
$operations[] = array(
'title' => t('Configure filters'),
'href' => facetapi_get_settings_path($searcher, $realm['name'], $facet_name, 'filters')
);
}
$operations[] = array(
'title' => t('Export configuration'),
'href' => facetapi_get_settings_path($searcher, $realm['name'], $facet_name, 'export')
);
if (facetapi_is_overridden($settings) || facetapi_is_overridden($global_settings)) {
$operations[] = array(
'title' => t('Revert configuration'),
'href' => facetapi_get_settings_path($searcher, $realm['name'], $facet_name, 'revert')
);
}
$form['table']['operations'][$facet_name] = array(
'#theme' => 'links__ctools_dropbutton',
'#links' => $operations,
'#attributes' => array(
'class' => array('inline', 'links', 'actions', 'horizontal', 'right')
),
);
// Adds weight if sortable.
if ($realm['sortable']) {
$form['#facetapi']['facet_info'][$facet_name]['weight'] = $settings->settings['weight'];
$form['table']['weight'][$facet_name] = array(
'#type' => 'select',
'#title' => t('Weight for @title', array('@title' => $facet['label'])),
'#title_display' => 'invisible',
'#options' => drupal_map_assoc(range(-50, 50)),
'#default_value' => $settings->settings['weight'],
'#attributes' => array('class' => array('facetapi-facet-weight')),
);
}
$options[$facet_name] = '';
$default_value[$facet_name] = (!$settings->enabled) ? 0 : $facet_name;
}
// Sorts by the weight appended above.
uasort($form['#facetapi']['facet_info'], 'drupal_sort_weight');
$form['table']['enabled_facets'] = array(
'#type' => 'checkboxes',
'#options' => $options,
'#default_value' => $default_value,
);
// Checks whether block caching is enabled, sets description accordingly.
if (!$disabled = (module_implements('node_grants') || !variable_get('block_cache', FALSE))) {
$description = t('Configure the appropriate cache setting for facet blocks.');
}
else {
$description = t(
'To enable block caching, visit the <a href="@performance-page">performance page</a>.',
array('@performance-page' => url('admin/config/development/performance', array('query' => array('destination' => current_path()))))
);
}
$form['block_cache'] = array(
'#type' => 'select',
'#access' => ('block' == $realm_name),
'#title' => t('Block cache settings'),
'#disabled' => $disabled,
'#options' => array(
DRUPAL_NO_CACHE => t('Do not cache'),
DRUPAL_CACHE_PER_ROLE | DRUPAL_CACHE_PER_PAGE => t('Per role'),
DRUPAL_CACHE_PER_USER | DRUPAL_CACHE_PER_PAGE => t('Per user'),
),
'#default_value' => variable_get('facetapi:block_cache:' . $searcher, DRUPAL_NO_CACHE),
'#description' => $description,
);
$form['actions'] = array(
'#type' => 'actions',
'#weight' => 20,
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
);
$form['#submit'][] = 'facetapi_realm_settings_form_submit';
return $form;
*/
}
}
......@@ -13,7 +13,7 @@ use Drupal\Core\Plugin\DefaultPluginManager;
class QueryTypePluginManager extends DefaultPluginManager {
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/facetapi/QueryType', $namespaces, $module_handler, 'Drupal\facetapi\QueryType\QueryTypeInterface', 'Drupal\facetapi\Annotation\FacetApiQueryType');
parent::__construct('Plugin/facetapi/querytype', $namespaces, $module_handler, 'Drupal\facetapi\QueryType\QueryTypeInterface', 'Drupal\facetapi\Annotation\FacetApiQueryType');
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment