Commit c661eb28 authored by StryKaizer's avatar StryKaizer Committed by borisson_

Issue #2712511 by StryKaizer: Change admin controller routing for better site builder UX

parent e45dc7f7
......@@ -225,10 +225,10 @@ class IntegrationTest extends WebTestBase {
*/
protected function setShowAmountOfResults($facet_id, $show = TRUE) {
$facet_display_page = '/admin/config/search/facets/' . $facet_id . '/display';
$facet_edit_page = '/admin/config/search/facets/' . $facet_id . '/edit';
// Go to the facet edit page and make sure "edit facet %facet" is present.
$this->drupalGet($facet_display_page);
$this->drupalGet($facet_edit_page);
$this->assertResponse(200);
// Configure the text for empty results behavior.
......@@ -289,10 +289,10 @@ class IntegrationTest extends WebTestBase {
protected function setEmptyBehaviorFacetText($facet_name) {
$facet_id = $this->convertNameToMachineName($facet_name);
$facet_display_page = '/admin/config/search/facets/' . $facet_id . '/display';
$facet_edit_page = '/admin/config/search/facets/' . $facet_id . '/edit';
// Go to the facet edit page and make sure "edit facet %facet" is present.
$this->drupalGet($facet_display_page);
$this->drupalGet($facet_edit_page);
$this->assertResponse(200);
// Configure the text for empty results behavior.
......@@ -312,8 +312,8 @@ class IntegrationTest extends WebTestBase {
protected function setOptionShowOnlyWhenFacetSourceVisible($facet_name) {
$facet_id = $this->convertNameToMachineName($facet_name);
$facet_display_page = '/admin/config/search/facets/' . $facet_id . '/display';
$this->drupalGet($facet_display_page);
$facet_edit_page = '/admin/config/search/facets/' . $facet_id . '/edit';
$this->drupalGet($facet_edit_page);
$this->assertResponse(200);
$edit = [
......@@ -388,7 +388,7 @@ class IntegrationTest extends WebTestBase {
// Make sure that the redirection to the display page is correct.
$this->assertRaw(t('Facet %name has been created.', ['%name' => $facet_name]));
$this->assertUrl('admin/config/search/facets/' . $facet_id . '/display');
$this->assertUrl('admin/config/search/facets/' . $facet_id . '/edit');
$this->drupalGet('admin/config/search/facets');
}
......@@ -402,12 +402,12 @@ class IntegrationTest extends WebTestBase {
* The name of the facet.
*/
public function editFacetCheck($facet_id, $facet_name) {
$facet_edit_page = '/admin/config/search/facets/' . $facet_id . '/edit';
$facet_edit_page = '/admin/config/search/facets/' . $facet_id . '/settings';
// Go to the facet edit page and make sure "edit facet %facet" is present.
$this->drupalGet($facet_edit_page);
$this->assertResponse(200);
$this->assertRaw($this->t('Edit facet @facet', ['@facet' => $facet_name]));
$this->assertRaw($this->t('Facet settings for @facet facet', ['@facet' => $facet_name]));
// Change the facet name to add in "-2" to test editing of a facet works.
$form_values = ['name' => $facet_name . ' - 2'];
......@@ -419,7 +419,7 @@ class IntegrationTest extends WebTestBase {
// Make sure the "-2" suffix is still on the facet when editing a facet.
$this->drupalGet($facet_edit_page);
$this->assertRaw($this->t('Edit facet @facet', ['@facet' => $facet_name . ' - 2']));
$this->assertRaw($this->t('Facet settings for @facet facet', ['@facet' => $facet_name . ' - 2']));
// Edit the form and change the facet's name back to the initial name.
$form_values = ['name' => $facet_name];
......
entity.facets_facet.display_form:
title: 'Configure facet display'
route_name: 'entity.facets_facet.display_form'
group: facets_facet
\ No newline at end of file
entity.facets_facet.edit_form:
title: 'Edit facet'
route_name: 'entity.facets_facet.edit_form'
group: facets_facet
......@@ -2,7 +2,8 @@ entity.facets_facet.edit_form:
title: 'Edit'
route_name: entity.facets_facet.edit_form
base_route: entity.facets_facet.edit_form
entity.facets_facet.display:
title: 'Display'
route_name: entity.facets_facet.display_form
base_route: entity.facets_facet.edit_form
\ No newline at end of file
entity.facets_facet.settings_form:
title: 'Facet settings'
route_name: entity.facets_facet.settings_form
base_route: entity.facets_facet.edit_form
......@@ -35,10 +35,10 @@ entity.facets_facet.delete_form:
requirements:
_entity_access: 'facets_facet.delete'
entity.facets_facet.display_form:
path: '/admin/config/search/facets/{facets_facet}/display'
entity.facets_facet.settings_form:
path: '/admin/config/search/facets/{facets_facet}/settings'
defaults:
_entity_form: 'facets_facet.display'
_entity_form: 'facets_facet.settings'
requirements:
_entity_access: 'facets_facet.edit'
......
......@@ -15,9 +15,9 @@ use Drupal\facets\FacetInterface;
* "storage" = "Drupal\Core\Config\Entity\ConfigEntityStorage",
* "list_builder" = "Drupal\facets\FacetListBuilder",
* "form" = {
* "default" = "Drupal\facets\Form\FacetForm",
* "default" = "Drupal\facets\Form\FacetSettingsForm",
* "edit" = "Drupal\facets\Form\FacetForm",
* "display" = "Drupal\facets\Form\FacetDisplayForm",
* "settings" = "Drupal\facets\Form\FacetSettingsForm",
* "delete" = "Drupal\facets\Form\FacetDeleteConfirmForm",
* },
* },
......@@ -52,7 +52,7 @@ use Drupal\facets\FacetInterface;
* "canonical" = "/admin/config/search/facets",
* "add-form" = "/admin/config/search/facets/add-facet",
* "edit-form" = "/admin/config/search/facets/{facets_facet}/edit",
* "display-form" = "/admin/config/search/facets/{facets_facet}/display",
* "settings-form" = "/admin/config/search/facets/{facets_facet}/settings",
* "delete-form" = "/admin/config/search/facets/{facets_facet}/delete",
* }
* )
......
......@@ -36,11 +36,11 @@ class FacetListBuilder extends ConfigEntityListBuilder {
'url' => $entity->toUrl('edit-form'),
);
}
if ($entity->access('update') && $entity->hasLinkTemplate('display-form')) {
$operations['display'] = array(
'title' => $this->t('Display'),
if ($entity->access('update') && $entity->hasLinkTemplate('settings-form')) {
$operations['settings'] = array(
'title' => $this->t('Facet settings'),
'weight' => 20,
'url' => $entity->toUrl('display-form'),
'url' => $entity->toUrl('settings-form'),
);
}
if ($entity->access('delete') && $entity->hasLinkTemplate('delete-form')) {
......
This diff is collapsed.
This diff is collapsed.
<?php
namespace Drupal\facets\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\facets\FacetInterface;
use Drupal\facets\FacetSource\FacetSourcePluginManager;
use Drupal\facets\Processor\ProcessorPluginManager;
use Drupal\views\Views;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form for creating and editing facets.
*/
class FacetSettingsForm extends EntityForm {
/**
* The facet storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $facetStorage;
/**
* The plugin manager for facet sources.
*
* @var \Drupal\facets\FacetSource\FacetSourcePluginManager
*/
protected $facetSourcePluginManager;
/**
* The plugin manager for processors.
*
* @var \Drupal\facets\Processor\ProcessorPluginManager
*/
protected $processorPluginManager;
/**
* Constructs a FacetForm object.
*
* @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager
* The entity manager.
* @param \Drupal\facets\FacetSource\FacetSourcePluginManager $facet_source_plugin_manager
* The plugin manager for facet sources.
* @param \Drupal\facets\Processor\ProcessorPluginManager $processor_plugin_manager
* 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');
return new static($entity_type_manager, $facet_source_plugin_manager, $processor_plugin_manager);
}
/**
* Retrieves the facet storage controller.
*
* @return \Drupal\Core\Entity\EntityStorageInterface
* The facet storage controller.
*/
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');
}
/**
* Returns the processor plugin manager.
*
* @return \Drupal\facets\Processor\ProcessorPluginManager
* The processor plugin manager.
*/
protected function getProcessorPluginManager() {
return $this->processorPluginManager ?: \Drupal::service('plugin.manager.facets.processor');
}
/**
* {@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
// facet.
if ($this->getEntity()->isNew()) {
$form['#title'] = $this->t('Add facet');
}
else {
$form['#title'] = $this->t('Facet settings for %label facet', ['%label' => $this->getEntity()->label()]);
}
$this->buildEntityForm($form, $form_state, $this->getEntity());
$form['#attached']['library'][] = 'facets/drupal.facets.edit-facet';
return $form;
}
/**
* Builds the form for editing and creating a facet.
*
* @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) {
$facet_sources = [];
foreach ($this->getFacetSourcePluginManager()->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;
}
$form['facet_source_id'] = [
'#type' => 'select',
'#title' => $this->t('Facet source'),
'#description' => $this->t('The source where this facet can find its fields.'),
'#options' => $facet_sources,
'#default_value' => $facet->getFacetSourceId(),
'#required' => TRUE,
'#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,
];
$form['facet_source_configure_button'] = [
'#type' => 'submit',
'#name' => 'facet_source_configure',
'#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);
$form['name'] = [
'#type' => 'textfield',
'#title' => $this->t('Name'),
'#description' => $this->t('The administrative name used for this facet.'),
'#default_value' => $facet->label(),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $facet->id(),
'#maxlength' => 50,
'#required' => TRUE,
'#machine_name' => [
'exists' => [$this->getFacetStorage(), 'load'],
'source' => ['name'],
],
];
$form['status'] = [
'#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->setValue('id', NULL);
$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 !== '') {
/** @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)) {
$form['facet_source_configs'][$facet_source_id]['#type'] = 'container';
$form['facet_source_configs'][$facet_source_id]['#attributes'] = ['class' => ['facet-source-field-wrapper']];
$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 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);
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
/** @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.
$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])) {
$weights[$stage_id] = $processor['stages'][$stage_id];
}
}
$facet->addProcessor([
'processor_id' => $processor_id,
'weights' => $weights,
'settings' => [],
]);
}
}
// Set a default widget for new facets.
$facet->setWidget('links');
$facet->setUrlAlias($form_state->getValue('id'));
$facet->setWeight(0);
// Set default empty behaviour.
$facet->setEmptyBehavior(['behavior' => 'none']);
$facet->setOnlyVisibleWhenFacetSourceIsVisible(TRUE);
}
$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. Only apply this when the facet source is
// actually a view by exploding on :.
list($type,) = explode(':', $facet_source_id);
if ($type === 'search_api_views') {
list(, $view_id, $display) = explode(':', $facet_source_id);
}
if (isset($view_id)) {
$view = Views::getView($view_id);
$view->setDisplay($display);
$view->display_handler->overrideOption('cache', ['type' => 'none']);
$view->save();
$display_plugin = $view->getDisplay()->getPluginId();
}
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> 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.edit_form', ['facets_facet' => $facet->id()]);
}
if (isset($view_id) && $display_plugin === 'block') {
$facet->setOnlyVisibleWhenFacetSourceIsVisible(FALSE);
}
}
else {
drupal_set_message(t('Facet %name has been updated.', ['%name' => $facet->getName()]));
}
// Clear Drupal cache for blocks to reflect recent changes.
\Drupal::service('plugin.manager.block')->clearCachedDefinitions();
return $facet;
}
/**
* {@inheritdoc}
*/
public function delete(array $form, FormStateInterface $form_state) {
$form_state->setRedirect('entity.facets_facet.delete_form', ['facets_facet' => $this->getEntity()->id()]);
}
}
......@@ -182,20 +182,17 @@ class IntegrationTest extends WebTestBase {
// Add a new facet.
$this->addFacet($facet_name);
$facet_edit_page = '/admin/config/search/facets/' . $facet_id . '/edit';
$facet_edit_page = '/admin/config/search/facets/' . $facet_id . '/settings';
// Go to the facet edit page and make sure "edit facet %facet" is present.
$this->drupalGet($facet_edit_page);
$this->assertResponse(200);
$this->assertRaw($this->t('Edit facet @facet', ['@facet' => $facet_name]));
$this->assertRaw($this->t('Facet settings for @facet facet', ['@facet' => $facet_name]));
// Change the machine name to a new name and check that the redirected page
// is the correct url.
$form = ['id' => $new_facet_id];
$this->drupalPostForm($facet_edit_page, $form, $this->t('Save'));
$expected_url = '/admin/config/search/facets/' . $new_facet_id . '/edit';
$this->assertUrl($expected_url);
}
/**
......@@ -210,7 +207,6 @@ class IntegrationTest extends WebTestBase {
$facet_add_page = '/admin/config/search/facets/add-facet';
$facet_edit_page = '/admin/config/search/facets/' . $facet_id . '/edit';
$facet_display_page = '/admin/config/search/facets/' . $facet_id . '/display';
$this->drupalGet($facet_add_page);
$this->assertResponse(200);
......@@ -236,7 +232,7 @@ class IntegrationTest extends WebTestBase {
$url = Url::fromUserInput('/search-api-test-fulltext', ['query' => ['f[0]' => 'ab_facet:item']]);
$this->assertUrl($url);
$this->drupalGet($facet_display_page);
$this->drupalGet($facet_edit_page);
$this->drupalPostForm(NULL, ['facet_settings[url_alias]' => 'llama'], $this->t('Save'));
$this->drupalGet('search-api-test-fulltext');
......@@ -336,7 +332,7 @@ class IntegrationTest extends WebTestBase {
public function testAndOrFacet() {
$facet_name = 'test & facet';
$facet_id = 'test_facet';
$facet_edit_page = 'admin/config/search/facets/' . $facet_id . '/display';
$facet_edit_page = 'admin/config/search/facets/' . $facet_id . '/edit';
$this->drupalLogin($this->adminUser);
$this->addFacet($facet_name);
......@@ -412,7 +408,7 @@ class IntegrationTest extends WebTestBase {
public function testExcludeFacet() {
$facet_name = 'test & facet';
$facet_id = 'test_facet';
$facet_edit_page = 'admin/config/search/facets/' . $facet_id . '/display';
$facet_edit_page = 'admin/config/search/facets/' . $facet_id . '/edit';
$this->addFacet($facet_name);
$this->createFacetBlock($facet_id);
......@@ -463,7 +459,7 @@ class IntegrationTest extends WebTestBase {
$this->addFacet($facet_name, 'keywords');
$this->createFacetBlock($facet_id);
$this->drupalGet($facet_edit_page . '/display');
$this->drupalGet($facet_edit_page . '/edit');
$edit = ['facet_settings[show_only_one_result]' => TRUE];
$this->drupalPostForm(NULL, $edit, $this->t('Save'));
......@@ -554,7 +550,7 @@ class IntegrationTest extends WebTestBase {
protected function setEmptyBehaviorFacetText($facet_name) {
$facet_id = $this->convertNameToMachineName($facet_name);
$facet_display_page = '/admin/config/search/facets/' . $facet_id . '/display';
$facet_display_page = '/admin/config/search/facets/' . $facet_id . '/edit';
// Go to the facet edit page and make sure "edit facet %facet" is present.
$this->drupalGet($facet_display_page);
......@@ -578,8 +574,8 @@ class IntegrationTest extends WebTestBase {
protected function setOptionShowOnlyWhenFacetSourceVisible($facet_name) {
$facet_id = $this->convertNameToMachineName($facet_name);
$facet_display_page = '/admin/config/search/facets/' . $facet_id . '/display';
$this->drupalGet($facet_display_page);
$facet_edit_page = '/admin/config/search/facets/' . $facet_id . '/edit';
$this->drupalGet($facet_edit_page);
$this->assertResponse(200);
$edit = [
......@@ -657,7 +653,7 @@ class IntegrationTest extends WebTestBase {
// Make sure that the redirection to the display page is correct.
$this->assertRaw(t('Facet %name has been created.', ['%name' => $facet_name]));
$this->assertUrl('admin/config/search/facets/' . $facet_id . '/display');
$this->assertUrl('admin/config/search/facets/' . $facet_id . '/edit');
$this->drupalGet('admin/config/search/facets');
}
......@@ -704,12 +700,12 @@ class IntegrationTest extends WebTestBase {
protected function editFacet($facet_name) {
$facet_id = $this->convertNameToMachineName($facet_name);
$facet_edit_page = '/admin/config/search/facets/' . $facet_id . '/edit';
$facet_edit_page = '/admin/config/search/facets/' . $facet_id . '/settings';
// Go to the facet edit page and make sure "edit facet %facet" is present.
$this->drupalGet($facet_edit_page);
$this->assertResponse(200);
$this->assertRaw($this->t('Edit facet @facet', ['@facet' => $facet_name]));
$this->assertRaw($this->t('Facet settings for @facet facet', ['@facet' => $facet_name]));