Newer
Older
* Contains \Drupal\facets\Form\FacetForm.
namespace Drupal\facets\Form;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\facets\FacetInterface;
use Drupal\facets\FacetSource\FacetSourcePluginManager;
use Drupal\facets\Processor\ProcessorPluginManager;
Mattias Michaux
committed
use Drupal\views\Views;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form for creating and editing facets.
* The facet storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
Joris Vercammen
committed
protected $facetStorage;
/**
* The plugin manager for facet sources.
*
* @var \Drupal\facets\FacetSource\FacetSourcePluginManager
*/
protected $facetSourcePluginManager;
Joris Vercammen
committed
/**
* The plugin manager for processors.
*
* @var \Drupal\facets\Processor\ProcessorPluginManager
Joris Vercammen
committed
*/
protected $processorPluginManager;
Joris Vercammen
committed
* Constructs a FacetForm object.
* @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager
* @param \Drupal\facets\FacetSource\FacetSourcePluginManager $facet_source_plugin_manager
* The plugin manager for facet sources.
* @param \Drupal\facets\Processor\ProcessorPluginManager $processor_plugin_manager
Joris Vercammen
committed
* The plugin manager for processors.
public function __construct(EntityTypeManager $entity_type_manager, FacetSourcePluginManager $facet_source_plugin_manager, ProcessorPluginManager $processor_plugin_manager) {
$this->facetStorage = $entity_type_manager->getStorage('facets_facet');
$this->facetSourcePluginManager = $facet_source_plugin_manager;
$this->processorPluginManager = $processor_plugin_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
/** @var \Drupal\Core\Entity\EntityTypeManager $entity_type_manager */
$entity_type_manager = $container->get('entity_type.manager');
/** @var \Drupal\facets\FacetSource\FacetSourcePluginManager $facet_source_plugin_manager */
$facet_source_plugin_manager = $container->get('plugin.manager.facets.facet_source');
/** @var \Drupal\facets\Processor\ProcessorPluginManager $processor_plugin_manager */
$processor_plugin_manager = $container->get('plugin.manager.facets.processor');
Joris Vercammen
committed
return new static($entity_type_manager, $facet_source_plugin_manager, $processor_plugin_manager);
Joris Vercammen
committed
* Retrieves the facet storage controller.
*
* @return \Drupal\Core\Entity\EntityStorageInterface
Joris Vercammen
committed
* The facet storage controller.
Joris Vercammen
committed
protected function getFacetStorage() {
return $this->facetStorage ?: \Drupal::service('entity_type.manager')->getStorage('facets_facet');
/**
* Returns the facet source plugin manager.
*
* @return \Drupal\facets\FacetSource\FacetSourcePluginManager
* The facet source plugin manager.
*/
protected function getFacetSourcePluginManager() {
return $this->facetSourcePluginManager ?: \Drupal::service('plugin.manager.facets.facet_source');
Joris Vercammen
committed
/**
* Returns the processor plugin manager.
*
* @return \Drupal\facets\Processor\ProcessorPluginManager
Joris Vercammen
committed
* The processor plugin manager.
*/
protected function getProcessorPluginManager() {
return $this->processorPluginManager ?: \Drupal::service('plugin.manager.facets.processor');
Joris Vercammen
committed
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
// If the form is being rebuilt, rebuild the entity with the current form
// values.
if ($form_state->isRebuilding()) {
$this->entity = $this->buildEntity($form, $form_state);
}
$form = parent::form($form, $form_state);
// Set the page title according to whether we are creating or editing the
if ($this->getEntity()->isNew()) {
$form['#title'] = $this->t('Add facet');
}
else {
$form['#title'] = $this->t('Edit facet %label', ['%label' => $this->getEntity()->label()]);
$this->buildEntityForm($form, $form_state, $this->getEntity());
* Builds the form for editing and creating a facet.
* @param \Drupal\facets\FacetInterface $facet
* The facets facet entity that is being created or edited.
Jimmy Henderickx
committed
public function buildEntityForm(array &$form, FormStateInterface $form_state, FacetInterface $facet) {
'#type' => 'textfield',
'#title' => $this->t('Facet name'),
'#description' => $this->t('Enter the displayed name for the facet.'),
'#default_value' => $facet->label(),
'#required' => TRUE,
Joris Vercammen
committed
'#type' => 'machine_name',
'#default_value' => $facet->id(),
'#maxlength' => 50,
'#required' => TRUE,
'#machine_name' => [
'exists' => [$this->getFacetStorage(), 'load'],
'source' => ['name'],
],
];
Joris Vercammen
committed
Joris Vercammen
committed
$form['url_alias'] = [
Joris Vercammen
committed
'#type' => 'machine_name',
Joris Vercammen
committed
'#title' => $this->t('The name of the facet for usage in URLs'),
'#default_value' => $facet->getUrlAlias(),
'#maxlength' => 50,
'#required' => TRUE,
Joris Vercammen
committed
'#machine_name' => [
'exists' => [$this->getFacetStorage(), 'load'],
'source' => ['name'],
],
Joris Vercammen
committed
];
$facet_sources = [];
foreach ($this->getFacetSourcePluginManager()->getDefinitions() as $facet_source_id => $definition) {
$facet_sources[$definition['id']] = !empty($definition['label']) ? $definition['label'] : $facet_source_id;
}
Jimmy Henderickx
committed
$form['facet_source_id'] = [
'#type' => 'select',
'#title' => $this->t('Facet source'),
'#description' => $this->t('Select the source where this facet can find its fields.'),
Jimmy Henderickx
committed
'#default_value' => $facet->getFacetSourceId(),
'#ajax' => [
'trigger_as' => ['name' => 'facet_source_configure'],
'callback' => '::buildAjaxFacetSourceConfigForm',
'wrapper' => 'facets-facet-sources-config-form',
'method' => 'replace',
'effect' => 'fade',
],
$form['facet_source_configs'] = [
'#type' => 'container',
'#attributes' => [
'id' => 'facets-facet-sources-config-form',
],
'#tree' => TRUE,
Joris Vercammen
committed
];
$form['facet_source_configure_button'] = [
'#type' => 'submit',
'#name' => 'facet_source_configure',
Joris Vercammen
committed
'#value' => $this->t('Configure facet source'),
'#limit_validation_errors' => [['facet_source_id']],
'#submit' => ['::submitAjaxFacetSourceConfigForm'],
'#ajax' => [
'callback' => '::buildAjaxFacetSourceConfigForm',
'wrapper' => 'facets-facet-sources-config-form',
],
'#attributes' => ['class' => ['js-hide']],
];
$this->buildFacetSourceConfigForm($form, $form_state);
'#type' => 'checkbox',
'#title' => $this->t('Enabled'),
'#description' => $this->t('Only enabled facets can be displayed.'),
'#default_value' => $facet->status(),
* Handles form submissions for the facet source subform.
*/
public function submitAjaxFacetSourceConfigForm($form, FormStateInterface $form_state) {
$form_state->setRebuild();
}
/**
* Handles changes to the selected facet sources.
*/
public function buildAjaxFacetSourceConfigForm(array $form, FormStateInterface $form_state) {
return $form['facet_source_configs'];
}
/**
* Builds the configuration forms for all possible facet sources.
*
* @param array $form
* An associative array containing the initial structure of the plugin form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the complete form.
*/
public function buildFacetSourceConfigForm(array &$form, FormStateInterface $form_state) {
$facet_source_id = $this->getEntity()->getFacetSourceId();
if (!is_null($facet_source_id) && $facet_source_id !== '') {
Joris Vercammen
committed
/** @var \Drupal\facets\FacetSource\FacetSourcePluginInterface $facet_source */
Joris Vercammen
committed
$facet_source = $this->getFacetSourcePluginManager()->createInstance($facet_source_id, ['facet' => $this->getEntity()]);
Joris Vercammen
committed
if ($config_form = $facet_source->buildConfigurationForm([], $form_state)) {
$form['facet_source_configs'][$facet_source_id]['#type'] = 'container';
Jimmy Henderickx
committed
$form['facet_source_configs'][$facet_source_id]['#title'] = $this->t('%plugin settings', ['%plugin' => $facet_source->getPluginDefinition()['label']]);
$form['facet_source_configs'][$facet_source_id] += $config_form;
}
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
/** @var \Drupal\facets\FacetInterface $facet */
$is_new = $facet->isNew();
// On facet creation, enable all locked processors by default, using their
// default settings.
$stages = $this->getProcessorPluginManager()->getProcessingStages();
José Manuel Rodríguez Vélez
committed
$processors_definitions = $this->getProcessorPluginManager()->getDefinitions();
foreach ($processors_definitions as $processor_id => $processor) {
if (isset($processor['locked']) && $processor['locked'] == TRUE) {
$weights = [];
foreach ($stages as $stage_id => $stage) {
if (isset($processor['stages'][$stage_id])) {
José Manuel Rodríguez Vélez
committed
$weights[$stage_id] = $processor['stages'][$stage_id];
}
}
Christian Adamski
committed
$facet->addProcessor([
'processor_id' => $processor_id,
'weights' => $weights,
José Manuel Rodríguez Vélez
committed
'settings' => [],
Christian Adamski
committed
]);
}
}
// Set a default widget for new facets.
$facet->setWidget('links');
Christian Adamski
committed
$facet->setEmptyBehavior(['behavior' => 'none']);
$facet->setOnlyVisibleWhenFacetSourceIsVisible(TRUE);
Christian Adamski
committed
$facet_source_id = $form_state->getValue('facet_source_id');
if (!is_null($facet_source_id) && $facet_source_id !== '') {
/** @var \Drupal\facets\FacetSource\FacetSourcePluginInterface $facet_source */
Joris Vercammen
committed
$facet_source = $this->getFacetSourcePluginManager()->createInstance($facet_source_id, ['facet' => $this->getEntity()]);
$facet_source->submitConfigurationForm($form, $form_state);
Christian Adamski
committed
}
$facet->save();
Mattias Michaux
committed
// Ensure that the caching of the view display is disabled, so the search
// correctly returns the facets. This is a temporary fix, until the cache
// metadata is correctly stored on the facet block. Only apply this when the
// facet source type is actually something this is related to views.
Christian Adamski
committed
list($type,) = explode(':', $facet_source_id);
Mattias Michaux
committed
if ($type === 'search_api_views') {
Christian Adamski
committed
list(, $view_id, $display) = explode(':', $facet_source_id);
}
if (isset($view_id)) {
$view = Views::getView($view_id);
$display = &$view->storage->getDisplay($display);
$display['display_options']['cache']['type'] = 'none';
$view->storage->save();
}
Mattias Michaux
committed
if ($is_new) {
if (\Drupal::moduleHandler()->moduleExists('block')) {
Jimmy Henderickx
committed
$message = $this->t('Facet %name has been created. Go to the <a href=":block_overview">Block overview page</a> to place the new block in the desired region.', ['%name' => $facet->getName(), ':block_overview' => \Drupal::urlGenerator()->generateFromRoute('block.admin_display')]);
drupal_set_message($message);
$form_state->setRedirect('entity.facets_facet.display_form', ['facets_facet' => $facet->id()]);
drupal_set_message(t('Facet %name has been updated.', ['%name' => $facet->getName()]));
Joris Vercammen
committed
$form_state->setRedirect('entity.facets_facet.edit_form', ['facets_facet' => $facet->id()]);
return $facet;
}
/**
* {@inheritdoc}
*/
public function delete(array $form, FormStateInterface $form_state) {
$form_state->setRedirect('entity.facets_facet.delete_form', ['facets_facet' => $this->getEntity()->id()]);