class FacetsSummarySettingsForm in Facets 8
Provides a form for configuring the processors of a facet.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\facets_summary\Form\FacetsSummarySettingsForm
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of FacetsSummarySettingsForm
File
- modules/
facets_summary/ src/ Form/ FacetsSummarySettingsForm.php, line 19
Namespace
Drupal\facets_summary\FormView source
class FacetsSummarySettingsForm extends EntityForm {
/**
* The facet storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $facetSummaryStorage;
/**
* The plugin manager for facet sources.
*
* @var \Drupal\facets\FacetSource\FacetSourcePluginManager
*/
protected $facetSourcePluginManager;
/**
* The facet manager service.
*
* @var \Drupal\facets\FacetManager\DefaultFacetManager
*/
protected $facetManager;
/**
* The facets_summary processor plugin manager service.
*
* @var \Drupal\facets_summary\Processor\ProcessorPluginManager
*/
protected $processorPluginManager;
/**
* The block manager.
*
* @var \Drupal\Core\Block\BlockManagerInterface
*/
protected $blockManager;
/**
* Constructs an FacetDisplayForm object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity manager.
* @param \Drupal\facets\FacetSource\FacetSourcePluginManager $facet_source_plugin_manager
* The plugin manager for facet sources.
* @param \Drupal\facets\FacetManager\DefaultFacetManager $facet_manager
* The Default Facet Manager.
* @param \Drupal\facets_summary\Processor\ProcessorPluginManager $processor_plugin_manager
* The Facets Summary Processor Plugin Manager.
* @param \Drupal\Core\Block\BlockManagerInterface $block_manager
* The block manager.
* @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
* The url generator.
*/
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;
}
/**
* {@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\FacetManager\DefaultFacetManager $facet_manager */
$facet_manager = $container
->get('facets.manager');
/** @var \Drupal\facets_summary\Processor\ProcessorPluginManager $processor_plugin_manager */
$processor_plugin_manager = $container
->get('plugin.manager.facets_summary.processor');
/** @var \Drupal\Core\Block\BlockManager $block_manager */
$block_manager = $container
->get('plugin.manager.block');
/** @var \Drupal\Core\Routing\UrlGeneratorInterface $url_generator */
$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);
}
/**
* {@inheritdoc}
*/
public function getBaseFormId() {
return NULL;
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
/** @var \Drupal\facets_summary\FacetsSummaryInterface $facets_summary */
$facets_summary = $this->entity;
$facet_sources = [];
// 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
// facet.
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;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
/** @var \Drupal\facets_summary\FacetsSummaryInterface $facets_summary */
$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(),
]);
}
// On facet creation, enable all locked processors by default, using their
// default settings.
$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(),
]));
}
// Clear Drupal cache for blocks to reflect recent changes.
$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;
}
// Ensure that the caching of the view display is disabled, so the search
// correctly returns the facets.
$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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityForm:: |
protected | property | The entity being used by this form. | 7 |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns an array of supported actions for the current entity form. | 29 |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
2 |
EntityForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
10 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties | 7 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityFormInterface:: |
41 |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
FacetsSummarySettingsForm:: |
protected | property | The block manager. | |
FacetsSummarySettingsForm:: |
protected | property | The facet manager service. | |
FacetsSummarySettingsForm:: |
protected | property | The plugin manager for facet sources. | |
FacetsSummarySettingsForm:: |
protected | property | The facet storage. | |
FacetsSummarySettingsForm:: |
protected | property | The facets_summary processor plugin manager service. | |
FacetsSummarySettingsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
FacetsSummarySettingsForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
|
FacetsSummarySettingsForm:: |
public | function |
Returns a string identifying the base form. Overrides EntityForm:: |
|
FacetsSummarySettingsForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides EntityForm:: |
|
FacetsSummarySettingsForm:: |
public | function | Constructs an FacetDisplayForm object. | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |