Commit 5099d0e3 authored by borisson_'s avatar borisson_
Browse files

Add config schema for facet, make sure you can select a field when adding a...

Add config schema for facet, make sure you can select a field when adding a facet. Load those fields dynamically.
parent 2dffd9bc
facetapi.facet.*:
type: config_entity
label : 'Facet'
mapping:
id:
type: string
label: 'ID'
name:
type: label
label: Name'
uuid:
type: string
label: 'UUID'
status:
type: boolean
label: 'Status'
search_api_index:
type: string
label: 'Search API index'
field_identifier:
type: string
label: 'Field identifier'
......@@ -25,7 +25,8 @@ entity.facetapi_facet.canonical:
entity.facetapi_facet.edit_form:
path: '/admin/config/search/search-api/index/{search_api_index}/facets/{facetapi_facet}/edit'
defaults:
_entity_form: 'facetapi_facet.edit'
_controller: '\Drupal\facetapi\Controller\FacetController::editForm'
_title: 'Add facet'
requirements:
_entity_access: 'facetapi_facet.edit'
......
......@@ -48,7 +48,27 @@ class FacetController extends ControllerBase {
*/
public function addForm(IndexInterface $search_api_index) {
$facet = $this->entityManager()->getStorage('facetapi_facet')->create(array('search_api_index' => $search_api_index->id()));
return $this->entityFormBuilder()->getForm($facet);
$form_state_additions = ['search_api_index' => $search_api_index];
return $this->entityFormBuilder()->getForm($facet, 'default', $form_state_additions);
}
/**
* Returns a form to edit a facet on a search api index.
*
* @param \Drupal\search_api\IndexInterface $search_api_index
* The search api index this facet will be added to.
* @param \Drupal\facetapi\FacetInterface $facetapi_facet
* Facet currently being edited
*
* @return array
* The facet edit form.
*/
public function editForm(IndexInterface $search_api_index, 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);
}
/**
......
......@@ -38,9 +38,8 @@ use Drupal\facetapi\Result\ResultInterface;
* config_export = {
* "id",
* "name",
* "description",
* "search_api_index",
* "options",
* "field_identifier",
* },
* links = {
* "canonical" = "/admin/config/search/search-api/index/{search_api_index}/facets",
......@@ -212,6 +211,11 @@ class Facet extends ConfigEntityBase implements FacetInterface {
return $this->field_identifier;
}
public function setFieldIdentifier($field_identifier) {
$this->field_identifier = $field_identifier;
return $this;
}
public function getQueryType() {
return $this->query_type_name;
}
......
......@@ -15,6 +15,7 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\facetapi\FacetInterface;
use Drupal\facetapi\FacetApiException;
use Drupal\search_api\IndexInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -82,7 +83,8 @@ class FacetForm extends EntityForm {
$form['#title'] = $this->t('Edit facet %label', array('%label' => $facet->label()));
}
$this->buildEntityForm($form, $form_state, $facet);
$search_api_index = $form_state->get('search_api_index');
$this->buildEntityForm($form, $form_state, $facet, $search_api_index);
return $form;
}
......@@ -92,9 +94,10 @@ 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) {
$form['#attached']['library'][] = 'search_api/drupal.search_api.admin_css';
public function buildEntityForm(array &$form, FormStateInterface $form_state, FacetInterface $facet, IndexInterface $search_api_index) {
$form['name'] = array(
'#type' => 'textfield',
......@@ -103,6 +106,7 @@ class FacetForm extends EntityForm {
'#default_value' => $facet->label(),
'#required' => TRUE,
);
$form['id'] = array(
'#type' => 'machine_name',
'#default_value' => $facet->id(),
......@@ -113,6 +117,16 @@ class FacetForm extends EntityForm {
'source' => array('name'),
),
);
$form['field_identifier'] = [
'#type' => 'select',
'#options' => $this->getIndexedFields($search_api_index),
'#title' => $this->t('Facet field'),
'#description' => $this->t('Choose the indexed field.'),
'#required' => TRUE,
'#default_value' => $facet->getFieldIdentifier()
];
$form['status'] = array(
'#type' => 'checkbox',
'#title' => $this->t('Enabled'),
......@@ -121,6 +135,26 @@ class FacetForm extends EntityForm {
);
}
/**
* Gets all indexed fields for this search index.
*
* @param \Drupal\search_api\IndexInterface $search_index
* The search index we're creating a facet for.
* @return array
* An array of all indexed fields.
*/
protected function getIndexedFields(IndexInterface $search_api_index) {
$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();
}
}
return $indexed_fields;
}
/**
* {@inheritdoc}
*/
......
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