View source
<?php
namespace Drupal\facets_summary\Form;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\facets\FacetManager\DefaultFacetManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\facets\FacetSource\FacetSourcePluginManager;
use Drupal\facets\FacetSource\SearchApiFacetSourceInterface;
use Drupal\facets_summary\Processor\ProcessorPluginManager;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FacetsSummarySettingsForm extends EntityForm {
protected $facetSummaryStorage;
protected $facetSourcePluginManager;
protected $facetManager;
protected $processorPluginManager;
protected $blockManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, FacetSourcePluginManager $facet_source_plugin_manager, DefaultFacetManager $facet_manager, ProcessorPluginManager $processor_plugin_manager, BlockManagerInterface $block_manager, UrlGeneratorInterface $url_generator) {
$this->entityTypeManager = $entity_type_manager;
$this->facetSourcePluginManager = $facet_source_plugin_manager;
$this->facetSummaryStorage = $entity_type_manager
->getStorage('facets_summary');
$this->facetManager = $facet_manager;
$this->processorPluginManager = $processor_plugin_manager;
$this->blockManager = $block_manager;
$this->urlGenerator = $url_generator;
}
public static function create(ContainerInterface $container) {
$entity_type_manager = $container
->get('entity_type.manager');
$facet_source_plugin_manager = $container
->get('plugin.manager.facets.facet_source');
$facet_manager = $container
->get('facets.manager');
$processor_plugin_manager = $container
->get('plugin.manager.facets_summary.processor');
$block_manager = $container
->get('plugin.manager.block');
$url_generator = $container
->get('url_generator');
return new static($entity_type_manager, $facet_source_plugin_manager, $facet_manager, $processor_plugin_manager, $block_manager, $url_generator);
}
public function getBaseFormId() {
return NULL;
}
public function form(array $form, FormStateInterface $form_state) {
$facets_summary = $this->entity;
$facet_sources = [];
if ($form_state
->isRebuilding()) {
$this->entity = $this
->buildEntity($form, $form_state);
}
$form = parent::form($form, $form_state);
if ($this
->getEntity()
->isNew()) {
$form['#title'] = $this
->t('Add facets summary');
}
else {
$form['#title'] = $this
->t('Facets settings for %label', [
'%label' => $this
->getEntity()
->label(),
]);
}
foreach ($this->facetSourcePluginManager
->getDefinitions() as $facet_source_id => $definition) {
$facet_sources[$definition['id']] = !empty($definition['label']) ? $definition['label'] : $facet_source_id;
}
if (count($facet_sources) == 0) {
$form['#markup'] = $this
->t('You currently have no facet sources defined. You should start by adding a facet source before creating facets.');
return TRUE;
}
$form['facet_source_id'] = [
'#type' => 'select',
'#title' => $this
->t('Facet source'),
'#description' => $this
->t('The source where this summary will be built from.'),
'#options' => $facet_sources,
'#default_value' => $facets_summary
->getFacetSourceId(),
'#required' => TRUE,
];
$form['name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Name'),
'#description' => $this
->t('The administrative name used for this summary.'),
'#default_value' => $facets_summary
->label(),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $facets_summary
->id(),
'#maxlength' => 50,
'#required' => TRUE,
'#machine_name' => [
'exists' => [
$this->facetSummaryStorage,
'load',
],
'source' => [
'name',
],
],
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$facets_summary = $this
->getEntity();
$is_new = $facets_summary
->isNew();
$facets_summary
->save();
if ($is_new) {
if ($this->moduleHandler
->moduleExists('block')) {
$message = $this
->t('Facet Summary %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' => $facets_summary
->getName(),
':block_overview' => $this->urlGenerator
->generateFromRoute('block.admin_display'),
]);
$this
->messenger()
->addMessage($message);
$form_state
->setRedirect('entity.facets_summary.edit_form', [
'facets_summary' => $facets_summary
->id(),
]);
}
$stages = $this->processorPluginManager
->getProcessingStages();
$processors_definitions = $this->processorPluginManager
->getDefinitions();
foreach ($processors_definitions as $processor_id => $processor) {
$is_locked = isset($processor['locked']) && $processor['locked'] == TRUE;
$is_default_enabled = isset($processor['default_enabled']) && $processor['default_enabled'] == TRUE;
if ($is_locked || $is_default_enabled) {
$weights = [];
foreach ($stages as $stage_id => $stage) {
if (isset($processor['stages'][$stage_id])) {
$weights[$stage_id] = $processor['stages'][$stage_id];
}
}
$facets_summary
->addProcessor([
'processor_id' => $processor_id,
'weights' => $weights,
'settings' => [],
]);
}
}
}
else {
$this
->messenger()
->addMessage($this
->t('Facet %name has been updated.', [
'%name' => $facets_summary
->getName(),
]));
}
$this->blockManager
->clearCachedDefinitions();
$facet_source_id = $form_state
->getValue('facet_source_id');
list($type, ) = explode(':', $facet_source_id);
if ($type !== 'search_api') {
return $facets_summary;
}
$facet_source = $this->facetSourcePluginManager
->createInstance($facet_source_id, [
'facet' => $this
->getEntity(),
]);
if (isset($facet_source) && $facet_source instanceof SearchApiFacetSourceInterface) {
$view = $facet_source
->getViewsDisplay();
if ($view !== NULL) {
$view->display_handler
->overrideOption('cache', [
'type' => 'none',
]);
$view
->save();
$this
->messenger()
->addMessage($this
->t('Caching of view %view has been disabled.', [
'%view' => $view->storage
->label(),
]));
}
}
return $facets_summary;
}
}