Skip to content
<?php
/**
* @file
* Contains Drupal\facets\FacetSourceInterface.
*/
namespace Drupal\facets;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
/**
* The facet source entity.
*/
interface FacetSourceInterface extends ConfigEntityInterface {
/**
* Returns the label of the facet source.
*
* @return string
* The facet name.
*/
public function getName();
/**
* Returns the filter key for this facet source.
*
* @return string
* The filter key.
*/
public function getFilterKey();
/**
* Sets the filter key for this facet source.
*
* @param string $filter_key
* The filter key.
*/
public function setFilterKey($filter_key);
/**
* Sets the processor name to be used.
*
* @param string $processor_name
* Plugin name of the url processor.
*/
public function setUrlProcessor($processor_name);
/**
* Returns a string version of the url processor.
*
* @return string
* The url processor to be used as a string.
*/
public function getUrlProcessorName();
}
......@@ -2,12 +2,11 @@
/**
* @file
* Contains \Drupal\facetapi\Form\FacetDeleteConfirmForm.
* Contains \Drupal\facets\Form\FacetDeleteConfirmForm.
*/
namespace Drupal\facetapi\Form;
namespace Drupal\facets\Form;
use Drupal\block\Entity\Block;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
......@@ -28,7 +27,7 @@ class FacetDeleteConfirmForm extends EntityConfirmFormBase {
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('entity.facetapi_facet.canonical', array('facetapi_facet' => $this->entity->id()));
return new Url('entity.facets_facet.canonical', array('facets_facet' => $this->entity->id()));
}
/**
......@@ -42,17 +41,6 @@ class FacetDeleteConfirmForm extends EntityConfirmFormBase {
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$is_facet_used_by_block = $this->isFacetUsedBlock();
// The facet is currently used by a block so it can't be deleted.
if ($is_facet_used_by_block) {
$caption = '<p>' . $this->t("The facet is currently used in a block and thus can't be removed. Remove the block first.");
$form['#title'] = $this->getQuestion();
$form['description'] = array('#markup' => $caption);
return $form;
}
return parent::buildForm($form, $form_state);
}
......@@ -62,43 +50,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('facetapi.overview');
}
/**
* Returns a boolean flag if deletion is safe according to the block module.
*
* If the block module is enabled and this facet is being used by a block,
* deletion is not safe. So to make sure that this facet can safely be
* deleted, we're first checking if the block module is enabled. If it isn't
* then deletion is safe. If it is, we're checking if the facet is in use by
* a block.
*
* @return bool
*/
protected function isFacetUsedBlock() {
// If the block module is not installed, we should automatically return
// false and go ahead with deletion.
if (!\Drupal::moduleHandler()->moduleExists('block')) {
return FALSE;
}
$is_facet_used_by_block = FALSE;
// Check if any blocks are currently using this facet.
$blocks = Block::loadMultiple();
foreach ($blocks as $block) {
if ($block->getPlugin() instanceof \Drupal\facetapi\Plugin\Block\FacetBlock) {
$facet_context_mapping = $block->getPlugin()->getConfiguration()['context_mapping']['facet'];
list(, $block_facet_uuid) = explode(':', $facet_context_mapping);
if ($this->entity->uuid() === $block_facet_uuid) {
$is_facet_used_by_block = TRUE;
}
}
}
return $is_facet_used_by_block;
$form_state->setRedirect('facets.overview');
}
}
This diff is collapsed.
......@@ -2,18 +2,17 @@
/**
* @file
* Contains \Drupal\facetapi\Form\FacetForm.
* Contains \Drupal\facets\Form\FacetForm.
*/
namespace Drupal\facetapi\Form;
namespace Drupal\facets\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\facetapi\Exception\Exception;
use Drupal\facetapi\FacetInterface;
use Drupal\facetapi\FacetSource\FacetSourcePluginManager;
use Drupal\facetapi\Processor\ProcessorPluginManager;
use Drupal\facets\FacetInterface;
use Drupal\facets\FacetSource\FacetSourcePluginManager;
use Drupal\facets\Processor\ProcessorPluginManager;
use Drupal\views\Views;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -23,7 +22,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
class FacetForm extends EntityForm {
/**
* The facet storage controller.
* The facet storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
......@@ -32,14 +31,14 @@ class FacetForm extends EntityForm {
/**
* The plugin manager for facet sources.
*
* @var \Drupal\facetapi\FacetSource\FacetSourcePluginManager
* @var \Drupal\facets\FacetSource\FacetSourcePluginManager
*/
protected $facetSourcePluginManager;
/**
* The plugin manager for processors.
*
* @var \Drupal\facetapi\Processor\ProcessorPluginManager
* @var \Drupal\facets\Processor\ProcessorPluginManager
*/
protected $processorPluginManager;
......@@ -48,15 +47,15 @@ class FacetForm extends EntityForm {
*
* @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager
* The entity manager.
* @param \Drupal\facetapi\FacetSource\FacetSourcePluginManager $facetSourcePluginManager
* @param \Drupal\facets\FacetSource\FacetSourcePluginManager $facet_source_plugin_manager
* The plugin manager for facet sources.
* @param \Drupal\facetapi\Processor\ProcessorPluginManager $processorPluginManager
* @param \Drupal\facets\Processor\ProcessorPluginManager $processor_plugin_manager
* The plugin manager for processors.
*/
public function __construct(EntityTypeManager $entity_type_manager, FacetSourcePluginManager $facetSourcePluginManager, ProcessorPluginManager $processorPluginManager) {
$this->facetStorage = $entity_type_manager->getStorage('facetapi_facet');
$this->facetSourcePluginManager = $facetSourcePluginManager;
$this->processorPluginManager = $processorPluginManager;
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;
}
/**
......@@ -66,31 +65,15 @@ class FacetForm extends EntityForm {
/** @var \Drupal\Core\Entity\EntityTypeManager $entity_type_manager */
$entity_type_manager = $container->get('entity_type.manager');
/** @var \Drupal\facetapi\FacetSource\FacetSourcePluginManager $facet_source_plugin_manager */
$facet_source_plugin_manager = $container->get('plugin.manager.facetapi.facet_source');
/** @var \Drupal\facets\FacetSource\FacetSourcePluginManager $facet_source_plugin_manager */
$facet_source_plugin_manager = $container->get('plugin.manager.facets.facet_source');
/** @var \Drupal\facetapi\Processor\ProcessorPluginManager $processor_plugin_manager */
$processor_plugin_manager = $container->get('plugin.manager.facetapi.processor');
/** @var \Drupal\facets\Processor\ProcessorPluginManager $processor_plugin_manager */
$processor_plugin_manager = $container->get('plugin.manager.facets.processor');
return new static($entity_type_manager, $facet_source_plugin_manager, $processor_plugin_manager);
}
/**
* Gets the form entity.
*
* The form entity which has been used for populating form element defaults.
* This method is defined on the \Drupal\Core\Entity\EntityFormInterface and
* has the same contents there, we only extend to add the correct return type,
* this makes IDE's smarter about the other places where we use
* $this->getEntity().
*
* @return \Drupal\facetapi\FacetInterface
* The current form facet entity.
*/
public function getEntity() {
return $this->entity;
}
/**
* Retrieves the facet storage controller.
*
......@@ -98,27 +81,27 @@ class FacetForm extends EntityForm {
* The facet storage controller.
*/
protected function getFacetStorage() {
return $this->facetStorage ?: \Drupal::service('entity_type.manager')->getStorage('facetapi_facet');
return $this->facetStorage ?: \Drupal::service('entity_type.manager')->getStorage('facets_facet');
}
/**
* Returns the facet source plugin manager.
*
* @return \Drupal\facetapi\FacetSource\FacetSourcePluginManager
* @return \Drupal\facets\FacetSource\FacetSourcePluginManager
* The facet source plugin manager.
*/
protected function getFacetSourcePluginManager() {
return $this->facetSourcePluginManager ?: \Drupal::service('plugin.manager.facetapi.facet_source');
return $this->facetSourcePluginManager ?: \Drupal::service('plugin.manager.facets.facet_source');
}
/**
* Returns the processor plugin manager.
*
* @return \Drupal\facetapi\Processor\ProcessorPluginManager
* @return \Drupal\facets\Processor\ProcessorPluginManager
* The processor plugin manager.
*/
protected function getProcessorPluginManager() {
return $this->processorPluginManager ?: \Drupal::service('plugin.manager.facetapi.processor');
return $this->processorPluginManager ?: \Drupal::service('plugin.manager.facets.processor');
}
/**
......@@ -150,8 +133,8 @@ class FacetForm extends EntityForm {
/**
* Builds the form for editing and creating a facet.
*
* @param \Drupal\facetapi\FacetInterface $facet
* The facetapi facet entity that is being created or edited.
* @param \Drupal\facets\FacetInterface $facet
* The facets facet entity that is being created or edited.
*/
public function buildEntityForm(array &$form, FormStateInterface $form_state, FacetInterface $facet) {
......@@ -174,6 +157,18 @@ class FacetForm extends EntityForm {
],
];
$form['url_alias'] = [
'#type' => 'machine_name',
'#title' => $this->t('The name of the facet for usage in URLs'),
'#default_value' => $facet->getUrlAlias(),
'#maxlength' => 50,
'#required' => TRUE,
'#machine_name' => [
'exists' => [$this->getFacetStorage(), 'load'],
'source' => ['name'],
],
];
$facet_sources = [];
foreach ($this->getFacetSourcePluginManager()->getDefinitions() as $facet_source_id => $definition) {
$facet_sources[$definition['id']] = !empty($definition['label']) ? $definition['label'] : $facet_source_id;
......@@ -188,7 +183,7 @@ class FacetForm extends EntityForm {
'#ajax' => [
'trigger_as' => ['name' => 'facet_source_configure'],
'callback' => '::buildAjaxFacetSourceConfigForm',
'wrapper' => 'facetapi-facet-sources-config-form',
'wrapper' => 'facets-facet-sources-config-form',
'method' => 'replace',
'effect' => 'fade',
],
......@@ -196,7 +191,7 @@ class FacetForm extends EntityForm {
$form['facet_source_configs'] = [
'#type' => 'container',
'#attributes' => [
'id' => 'facetapi-facet-sources-config-form',
'id' => 'facets-facet-sources-config-form',
],
'#tree' => TRUE,
];
......@@ -208,14 +203,12 @@ class FacetForm extends EntityForm {
'#submit' => ['::submitAjaxFacetSourceConfigForm'],
'#ajax' => [
'callback' => '::buildAjaxFacetSourceConfigForm',
'wrapper' => 'facetapi-facet-sources-config-form',
'wrapper' => 'facets-facet-sources-config-form',
],
'#attributes' => ['class' => ['js-hide']],
];
$this->buildFacetSourceConfigForm($form, $form_state);
$form['status'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enabled'),
......@@ -225,9 +218,10 @@ class FacetForm extends EntityForm {
}
/**
* Form submission handler for the facet source subform.
* Handles form submissions for the facet source subform.
*/
public function submitAjaxFacetSourceConfigForm($form, FormStateInterface $form_state) {
$form_state->setValue('id', NULL);
$form_state->setRebuild();
}
......@@ -250,10 +244,10 @@ class FacetForm extends EntityForm {
$facet_source_id = $this->getEntity()->getFacetSourceId();
if (!is_null($facet_source_id) && $facet_source_id !== '') {
/** @var \Drupal\facetapi\FacetSource\FacetSourceInterface $facet_source */
$facet_source = $this->getFacetSourcePluginManager()->createInstance($facet_source_id);
/** @var \Drupal\facets\FacetSource\FacetSourcePluginInterface $facet_source */
$facet_source = $this->getFacetSourcePluginManager()->createInstance($facet_source_id, ['facet' => $this->getEntity()]);
if ($config_form = $facet_source->buildConfigurationForm([], $form_state, $this->getEntity(), $facet_source)) {
if ($config_form = $facet_source->buildConfigurationForm([], $form_state)) {
$form['facet_source_configs'][$facet_source_id]['#type'] = 'container';
$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;
......@@ -266,6 +260,13 @@ class FacetForm extends EntityForm {
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$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 */
$facet_source = $this->getFacetSourcePluginManager()->createInstance($facet_source_id, ['facet' => $this->getEntity()]);
$facet_source->validateConfigurationForm($form, $form_state);
}
}
/**
......@@ -274,57 +275,55 @@ class FacetForm extends EntityForm {
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
/** @var \Drupal\facetapi\FacetInterface $facet */
/** @var \Drupal\facets\FacetInterface $facet */
$facet = $this->getEntity();
$is_new = $facet->isNew();
if ($is_new) {
// On facet creation, enable all locked processors by default, using their
// default settings.
$initial_settings = [];
$stages = $this->getProcessorPluginManager()->getProcessingStages();
$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])) {
foreach ($stages as $stage_id => $stage) {
if (isset($processor['stages'][$stage_id])) {
$weights[$stage_id] = $processor['stages'][$stage_id];
}
}
$initial_settings[$processor_id] = array(
$facet->addProcessor([
'processor_id' => $processor_id,
'weights' => $weights,
'settings' => [],
);
]);
}
}
$facet->setOption('processors', $initial_settings);
// Set a default widget for new facets.
$facet->setWidget('links');
// Set default empty behaviour
$facet->setOption('empty_behavior', ['behavior' => 'none']);
// Set default empty behaviour.
$facet->setEmptyBehavior(['behavior' => 'none']);
$facet->setOnlyVisibleWhenFacetSourceIsVisible(TRUE);
}
// Make sure the field identifier is copied from within the facet source
// config to the facet object and saved there.
$facet_source = $form_state->getValue('facet_source_id');
$field_identifier = $form_state->getValue('facet_source_configs')[$facet_source]['field_identifier'];
$facet->setFieldIdentifier($field_identifier);
$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 */
$facet_source = $this->getFacetSourcePluginManager()->createInstance($facet_source_id, ['facet' => $this->getEntity()]);
$facet_source->submitConfigurationForm($form, $form_state);
}
$facet->save();
// 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.
list($type,) = explode(':', $facet_source);
list($type,) = explode(':', $facet_source_id);
if ($type === 'search_api_views') {
list(, $view_id, $display) = explode(':', $facet_source);
list(, $view_id, $display) = explode(':', $facet_source_id);
}
if (isset($view_id)) {
......@@ -337,12 +336,14 @@ class FacetForm extends EntityForm {
if ($is_new) {
if (\Drupal::moduleHandler()->moduleExists('block')) {
$message = $this->t('Facet %name has been created. Go to the <a href=":block_overview">Block overview page</a> and add a new "Facet block". If this is your first and only facet, just adding that block make it link to this facet, if you have addded more facets already, please make sure to select the correct Facet to render.', ['%name' => $facet->getName(), ':block_overview' => \Drupal::urlGenerator()->generateFromRoute('block.admin_display')]);
$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.facetapi_facet.display_form', ['facetapi_facet' => $facet->id()]);
$form_state->setRedirect('entity.facets_facet.display_form', ['facets_facet' => $facet->id()]);
}
}else{
}
else {
drupal_set_message(t('Facet %name has been updated.', ['%name' => $facet->getName()]));
$form_state->setRedirect('entity.facets_facet.edit_form', ['facets_facet' => $facet->id()]);
}
return $facet;
......@@ -352,7 +353,7 @@ class FacetForm extends EntityForm {
* {@inheritdoc}
*/
public function delete(array $form, FormStateInterface $form_state) {
$form_state->setRedirect('entity.facetapi_facet.delete_form', ['facetapi_facet' => $this->getEntity()->id()]);
$form_state->setRedirect('entity.facets_facet.delete_form', ['facets_facet' => $this->getEntity()->id()]);
}
}
This diff is collapsed.
......@@ -2,10 +2,10 @@
/**
* @file
* Contains \Drupal\facetapi\Form\SubFormState.
* Contains \Drupal\facets\Form\SubFormState.
*/
namespace Drupal\facetapi\Form;
namespace Drupal\facets\Form;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormInterface;
......@@ -515,8 +515,9 @@ class SubFormState implements FormStateInterface {
/**
* {@inheritdoc}
*
* @todo What are groups? Is this the way to handle them in a sub-form?
*/
// @todo What are groups? Is this the way to handle them in a sub-form?
public function setGroups(array $groups) {
$this->mainFormState->setGroups($groups);
return $this;
......
This diff is collapsed.
<?php
/**
* @file
* Contains Drupal\facets\Plugin\Block\FacetBlockDeriver.
*/
namespace Drupal\facets\Plugin\Block;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* This deriver creates a block for every facet that has been created.
*/
class FacetBlockDeriver implements ContainerDeriverInterface {
use StringTranslationTrait;
/**
* List of derivative definitions.
*
* @var array
*/
protected $derivatives = [];
/**
* The entity storage used for facets.
*
* @var \Drupal\Core\Entity\EntityStorageInterface $facetStorage
*/
protected $facetStorage;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
$deriver = new static($container, $base_plugin_id);
$deriver->facetStorage = $container->get('entity_type.manager')->getStorage('facets_facet');
return $deriver;
}
/**
* {@inheritdoc}
*/
public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
$derivatives = $this->getDerivativeDefinitions($base_plugin_definition);
return isset($derivatives[$derivative_id]) ? $derivatives[$derivative_id] : NULL;
}
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
$base_plugin_id = $base_plugin_definition['id'];
if (!isset($this->derivatives[$base_plugin_id])) {
$plugin_derivatives = [];
/** @var \Drupal\facets\FacetInterface[] $all_facets */
$all_facets = $this->facetStorage->loadMultiple();
foreach ($all_facets as $facet) {
$machine_name = $facet->id();
$plugin_derivatives[$machine_name] = [
'id' => $base_plugin_id . PluginBase::DERIVATIVE_SEPARATOR . $machine_name,
'label' => $this->t('Facet: :facet', [':facet' => $facet->getName()]),
'admin_label' => $facet->getName(),
'description' => $this->t('Facet'),
] + $base_plugin_definition;
$sources[] = $this->t('Facet: :facet', [':facet' => $facet->getName()]);
}
$this->derivatives[$base_plugin_id] = $plugin_derivatives;
}
return $this->derivatives[$base_plugin_id];
}
}
This diff is collapsed.
This diff is collapsed.