From 285101ea9f74be587ba050c55895eff4a8535ae0 Mon Sep 17 00:00:00 2001 From: Jimmy Henderickx Date: Tue, 6 Oct 2015 22:35:54 +0200 Subject: [PATCH] Facets are now not bound to search_api_indexes anymore. UI moved to seperate location --- facetapi.links.action.yml | 2 +- facetapi.links.menu.yml | 6 ++++++ facetapi.links.task.yml | 5 ----- facetapi.module | 2 +- facetapi.routing.yml | 14 +++++++------- src/Controller/FacetController.php | 14 +++++--------- src/Entity/Facet.php | 20 ++++---------------- src/Form/FacetDeleteConfirmForm.php | 4 ++-- src/Form/FacetForm.php | 22 ++++++++++++---------- src/Tests/IntegrationTest.php | 2 +- 10 files changed, 39 insertions(+), 52 deletions(-) create mode 100644 facetapi.links.menu.yml delete mode 100644 facetapi.links.task.yml diff --git a/facetapi.links.action.yml b/facetapi.links.action.yml index 02ba42f9..5e85ecc5 100644 --- a/facetapi.links.action.yml +++ b/facetapi.links.action.yml @@ -2,4 +2,4 @@ entity.facetapi_facet.add_form: route_name: entity.facetapi_facet.add_form title: 'Add facet' appears_on: - - entity.search_api_index.facets + - facetapi.overview diff --git a/facetapi.links.menu.yml b/facetapi.links.menu.yml new file mode 100644 index 00000000..226fb1c3 --- /dev/null +++ b/facetapi.links.menu.yml @@ -0,0 +1,6 @@ +facetapi.overview: + title: Facet API + description: 'Configure facets for Search API searchers.' + route_name: facetapi.overview + weight: 30 + parent: system.admin_config_search diff --git a/facetapi.links.task.yml b/facetapi.links.task.yml deleted file mode 100644 index 2e5b28e5..00000000 --- a/facetapi.links.task.yml +++ /dev/null @@ -1,5 +0,0 @@ -entity.search_api_server.facets: - route_name: entity.search_api_index.facets - base_route: entity.search_api_index.canonical - title: 'Facets' - weight: 30 diff --git a/facetapi.module b/facetapi.module index 14c75e28..a8fb38e3 100644 --- a/facetapi.module +++ b/facetapi.module @@ -199,7 +199,7 @@ function facetapi_entity_operation_alter(array &$operations, \Drupal\Core\Entity $operations['facets'] = array( 'title' => t('Facets'), 'weight' => 40, - 'url' => Url::fromRoute('entity.search_api_index.facets', $route_parameters), + 'url' => Url::fromRoute('facetapi.overview', $route_parameters), ); } } diff --git a/facetapi.routing.yml b/facetapi.routing.yml index 201a1406..752c0a0f 100644 --- a/facetapi.routing.yml +++ b/facetapi.routing.yml @@ -1,13 +1,13 @@ -entity.search_api_index.facets: - path: '/admin/config/search/search-api/index/{search_api_index}/facets' +facetapi.overview: + path: '/admin/config/search/facet-api' defaults: - _title: 'Facets' + _title: 'Facet API' _entity_list: 'facetapi_facet' requirements: _entity_create_access: 'search_api_index' entity.facetapi_facet.add_form: - path: '/admin/config/search/search-api/{search_api_index}/facets/add-facet' + path: '/admin/config/search/facet-api/add-facet' defaults: _controller: '\Drupal\facetapi\Controller\FacetController::addForm' _title: 'Add facet' @@ -15,7 +15,7 @@ entity.facetapi_facet.add_form: _entity_create_access: 'facetapi_facet' entity.facetapi_facet.canonical: - path: '/admin/config/search/search-api/index/{search_api_index}/facets/{facetapi_facet}' + path: '/admin/config/search/facet-api/{facetapi_facet}' defaults: _controller: '\Drupal\facetapi\Controller\FacetController::page' _title_callback: '\Drupal\facetapi\Controller\FacetController::pageTitle' @@ -23,7 +23,7 @@ entity.facetapi_facet.canonical: _entity_access: 'facetapi_facet.view' entity.facetapi_facet.edit_form: - path: '/admin/config/search/search-api/index/{search_api_index}/facets/{facetapi_facet}/edit' + path: '/admin/config/search/facet-api/{facetapi_facet}/edit' defaults: _controller: '\Drupal\facetapi\Controller\FacetController::editForm' _title: 'Add facet' @@ -31,7 +31,7 @@ entity.facetapi_facet.edit_form: _entity_access: 'facetapi_facet.edit' entity.facetapi_facet.delete_form: - path: '/admin/config/search/search-api/index/{search_api_index}/facets/{facetapi_facet}/delete' + path: '/admin/config/search/facet-api/{facetapi_facet}/delete' defaults: _entity_form: 'facetapi_facet.delete' requirements: diff --git a/src/Controller/FacetController.php b/src/Controller/FacetController.php index 19b9d438..d572e84d 100644 --- a/src/Controller/FacetController.php +++ b/src/Controller/FacetController.php @@ -46,11 +46,9 @@ class FacetController extends ControllerBase { * @return array * The facet add form. */ - public function addForm(IndexInterface $search_api_index) { - $facet = $this->entityManager()->getStorage('facetapi_facet')->create(array('search_api_index' => $search_api_index->id())); - - $form_state_additions = ['search_api_index' => $search_api_index]; - return $this->entityFormBuilder()->getForm($facet, 'default', $form_state_additions); + public function addForm() { + $facet = $this->entityManager()->getStorage('facetapi_facet')->create(); + return $this->entityFormBuilder()->getForm($facet, 'default'); } /** @@ -64,11 +62,9 @@ class FacetController extends ControllerBase { * @return array * The facet edit form. */ - public function editForm(IndexInterface $search_api_index, FacetInterface $facetapi_facet) { + public function editForm(FacetInterface $facetapi_facet) { $facet = $this->entityManager()->getStorage('facetapi_facet')->load($facetapi_facet->id()); - - $form_state_additions = ['search_api_index' => $search_api_index]; - return $this->entityFormBuilder()->getForm($facet, 'default', $form_state_additions); + return $this->entityFormBuilder()->getForm($facet, 'default'); } /** diff --git a/src/Entity/Facet.php b/src/Entity/Facet.php index f837bc4e..f874fb67 100644 --- a/src/Entity/Facet.php +++ b/src/Entity/Facet.php @@ -44,10 +44,10 @@ use Drupal\facetapi\Result\ResultInterface; * "widget_configs", * }, * links = { - * "canonical" = "/admin/config/search/search-api/index/{search_api_index}/facets", - * "add-form" = "/admin/config/search/search-api/index/{search_api_index}/facets/add-facet", - * "edit-form" = "/admin/config/search/search-api/index/{search_api_index}/facets/{facetapi_facet}/edit", - * "delete-form" = "/admin/config/search/search-api/index/{search_api_index}/facets/{facetapi_facet}/delete", + * "canonical" = "/admin/config/search/facet-api", + * "add-form" = "/admin/config/search/facet-api/add-facet", + * "edit-form" = "/admin/config/search/facet-api/{facetapi_facet}/edit", + * "delete-form" = "/admin/config/search/facet-api/{facetapi_facet}/delete", * } * ) */ @@ -118,13 +118,6 @@ class Facet extends ConfigEntityBase implements FacetInterface { */ protected $searcher_name; - /** - * The search api index this facet belongs to. - * - * @var string - */ - protected $search_api_index; - /** * The plugin name of the url processor. * @@ -264,16 +257,11 @@ class Facet extends ConfigEntityBase implements FacetInterface { return $this->name; } - public function getSearchApiIndex() { - return $this->search_api_index; - } - /** * {@inheritdoc} */ protected function urlRouteParameters($rel) { $parameters = parent::urlRouteParameters($rel); - $parameters['search_api_index'] = $this->getSearchApiIndex(); return $parameters; } diff --git a/src/Form/FacetDeleteConfirmForm.php b/src/Form/FacetDeleteConfirmForm.php index 297a43de..9c11cc15 100644 --- a/src/Form/FacetDeleteConfirmForm.php +++ b/src/Form/FacetDeleteConfirmForm.php @@ -27,7 +27,7 @@ class FacetDeleteConfirmForm extends EntityConfirmFormBase { * {@inheritdoc} */ public function getCancelUrl() { - return new Url('entity.facetapi_facet.canonical', array('facetapi_facet' => $this->entity->id(), 'search_api_index' => $this->entity->getSearchApiIndex())); + return new Url('entity.facetapi_facet.canonical', array('facetapi_facet' => $this->entity->id())); } /** @@ -43,7 +43,7 @@ class FacetDeleteConfirmForm extends EntityConfirmFormBase { public function submitForm(array &$form, FormStateInterface $form_state) { $this->entity->delete(); drupal_set_message($this->t('The facet %name has been deleted.', array('%name' => $this->entity->label()))); - $form_state->setRedirect('entity.search_api_index.facets', array('search_api_index' => $this->entity->getSearchApiIndex())); + $form_state->setRedirect('facetapi.overview'); } } diff --git a/src/Form/FacetForm.php b/src/Form/FacetForm.php index 1e7c18ff..368de470 100644 --- a/src/Form/FacetForm.php +++ b/src/Form/FacetForm.php @@ -14,6 +14,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\facetapi\FacetInterface; use Drupal\facetapi\FacetApiException; use Drupal\facetapi\Widget\WidgetPluginManager; +use Drupal\search_api\Entity\Index; use Drupal\search_api\Form\SubFormState; use Drupal\search_api\IndexInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -116,10 +117,8 @@ class FacetForm extends EntityForm { * * @param \Drupal\facetapi\FacetInterface $facet * The server that is being created or edited. - * @param \Drupal\search_api\IndexInterface $search_api_index - * The search index we're creating a facet for. */ - public function buildEntityForm(array &$form, FormStateInterface $form_state, FacetInterface $facet, IndexInterface $search_api_index) { + public function buildEntityForm(array &$form, FormStateInterface $form_state, FacetInterface $facet) { $form['name'] = array( '#type' => 'textfield', @@ -142,7 +141,7 @@ class FacetForm extends EntityForm { $form['field_identifier'] = [ '#type' => 'select', - '#options' => $this->getIndexedFields($search_api_index), + '#options' => $this->getIndexedFields(), '#title' => $this->t('Facet field'), '#description' => $this->t('Choose the indexed field.'), '#required' => TRUE, @@ -205,13 +204,16 @@ class FacetForm extends EntityForm { * @return array * An array of all indexed fields. */ - protected function getIndexedFields(IndexInterface $search_api_index) { + protected function getIndexedFields() { $indexed_fields = []; - foreach ($search_api_index->getDatasources() as $datasource_id => $datasource) { - $fields = $search_api_index->getFieldsByDatasource($datasource_id); - foreach ($fields as $field) { - $indexed_fields[$field->getFieldIdentifier()] = $field->getLabel(); + $search_api_indexes = Index::loadMultiple(); + foreach($search_api_indexes as $search_api_index){ + foreach ($search_api_index->getDatasources() as $datasource_id => $datasource) { + $fields = $search_api_index->getFieldsByDatasource($datasource_id); + foreach ($fields as $field) { + $indexed_fields[$field->getFieldIdentifier()] = $field->getLabel(); + } } } return $indexed_fields; @@ -295,7 +297,7 @@ class FacetForm extends EntityForm { $facet = $this->getEntity(); $facet->save(); drupal_set_message($this->t('The facet was successfully saved.')); - $form_state->setRedirect('entity.search_api_index.facets', array('search_api_index' => $facet->getSearchApiIndex())); + $form_state->setRedirect('facetapi.overview'); } catch (FacetApiException $e) { $form_state->setRebuild(); diff --git a/src/Tests/IntegrationTest.php b/src/Tests/IntegrationTest.php index befcfd25..c2f75b93 100644 --- a/src/Tests/IntegrationTest.php +++ b/src/Tests/IntegrationTest.php @@ -45,7 +45,7 @@ class IntegrationTest extends FacetWebTestBase { } protected function addFacet() { - $facet_overview = $this->urlGenerator->generateFromRoute('entity.search_api_index.facets', ['search_api_index' => 'webtest_index']); + $facet_overview = $this->urlGenerator->generateFromRoute('facetapi.overview', ['search_api_index' => 'webtest_index']); $this->drupalGet($facet_overview); $this->assertResponse(200); $this->assertText($this->t('There are no facets defined.')); -- GitLab