Commit b7cbb72b authored by joachim's avatar joachim Committed by borisson_

Issue #2747593 by borisson_, joachim: provide a facet clone form

parent 71abbb92
......@@ -34,6 +34,13 @@ entity.facets_facet.settings_form:
requirements:
_entity_access: 'facets_facet.edit'
entity.facets_facet.clone_form:
path: '/admin/config/search/facets/{facets_facet}/clone'
defaults:
_entity_form: 'facets_facet.clone'
requirements:
_entity_access: 'facets_facet.edit'
entity.facets_facet_source.edit_form:
path: '/admin/config/search/facets/facet-sources/{facets_facet_source}/edit'
defaults:
......
......@@ -20,6 +20,7 @@ use Drupal\facets\FacetInterface;
* "default" = "Drupal\facets\Form\FacetSettingsForm",
* "edit" = "Drupal\facets\Form\FacetForm",
* "settings" = "Drupal\facets\Form\FacetSettingsForm",
* "clone" = "Drupal\facets\Form\FacetCloneForm",
* "delete" = "Drupal\facets\Form\FacetDeleteConfirmForm",
* },
* },
......@@ -56,6 +57,7 @@ use Drupal\facets\FacetInterface;
* "add-form" = "/admin/config/search/facets/add-facet",
* "edit-form" = "/admin/config/search/facets/{facets_facet}/edit",
* "settings-form" = "/admin/config/search/facets/{facets_facet}/settings",
* "clone-form" = "/admin/config/search/facets/{facets_facet}/clone",
* "delete-form" = "/admin/config/search/facets/{facets_facet}/delete",
* }
* )
......
......@@ -45,6 +45,13 @@ class FacetListBuilder extends ConfigEntityListBuilder {
'url' => $entity->toUrl('settings-form'),
);
}
if ($entity->access('update') && $entity->hasLinkTemplate('clone-form')) {
$operations['clone'] = array(
'title' => $this->t('Clone facet'),
'weight' => 90,
'url' => $entity->toUrl('clone-form'),
);
}
if ($entity->access('delete') && $entity->hasLinkTemplate('delete-form')) {
$operations['delete'] = array(
'title' => $this->t('Delete'),
......
<?php
namespace Drupal\facets\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\facets\FacetSource\FacetSourcePluginManager;
use Drupal\search_api\Display\DisplayPluginManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form for creating and editing facets.
*/
class FacetCloneForm extends EntityForm {
/**
* Facet source plugin manager.
*
* @var \Drupal\facets\FacetSource\FacetSourcePluginManager
*/
protected $facetSourcePluginManager;
/**
* The facet entity storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $facetStorage;
/**
* Search API display source plugin manager.
*
* @var \Drupal\search_api\Display\DisplayPluginManager
*/
protected $displayPluginManager;
public function __construct(FacetSourcePluginManager $facetSourcePluginManager, DisplayPluginManager $displayPluginManager,EntityStorageInterface $facetStorage) {
$this->facetSourcePluginManager = $facetSourcePluginManager;
$this->displayPluginManager = $displayPluginManager;
$this->facetStorage = $facetStorage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('plugin.manager.facets.facet_source'),
$container->get('plugin.manager.search_api.display'),
$container->get('entity_type.manager')->getStorage('facets_facet')
);
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
/** @var \Drupal\facets\FacetInterface $facet */
$facet = $this->getEntity();
if (strpos($facet->getFacetSourceId(), 'search_api:') === FALSE) {
// We don't know how to clone other kinds of facets.
drupal_set_message('We can only clone Search API based facets.');
return [];
}
/** @var \Drupal\search_api\Display\DisplayInterface[] $facet_source_definitions */
$facet_source_definitions = $this->facetSourcePluginManager->getDefinitions();
// Get the base table from the facet source's view.
$facet_source_id = $facet->getFacetSourceId();
$search_api_display_id = $facet_source_definitions[$facet_source_id]['display_id'];
/** @var \Drupal\search_api\Display\DisplayInterface $current_display */
$current_display = $this->displayPluginManager
->createInstance($search_api_display_id);
$current_index = $current_display->getIndex()->id();
// Create a list of all other search api displays that have the same index.
$options = [];
foreach ($facet_source_definitions as $source) {
$current_display = $this->displayPluginManager
->createInstance($source['display_id']);
if ($current_display->getIndex()->id() !== $current_index) {
continue;
}
$options[$source['id']] = $source['label'];
}
$form['destination_facet_source'] = [
'#type' => 'radios',
'#title' => $this->t("Clone the facet to this facet source:"),
'#options' => $options,
'#required' => TRUE,
];
$form['name'] = [
'#type' => 'textfield',
'#title' => $this->t('Name'),
'#description' => $this->t('The administrative name used for this facet.'),
'#default_value' => $this->t('Duplicate of @label', ['@label' => $this->entity->label()]),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#maxlength' => 50,
'#required' => TRUE,
'#default_value' => '',
'#machine_name' => [
'exists' => [$this->facetStorage, 'load'],
'source' => ['name'],
],
];
return $form;
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
$actions['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Duplicate'),
];
return $actions;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\facets\FacetInterface $facet */
$facet = $this->entity->createDuplicate();
$facet->set('name', $form_state->getValue('name'));
$facet->set('id', $form_state->getValue('id'));
$facet->set('facet_source_id', $form_state->getValue('destination_facet_source'));
$facet->save();
drupal_set_message($this->t('Facet cloned to :label', [':label' => $facet->label()]));
// Redirect the user to the view admin form.
$form_state->setRedirectUrl($this->entity->toUrl('edit-form'));
}
}
......@@ -743,6 +743,32 @@ class IntegrationTest extends FacetsTestBase {
$this->assertFacetLabel('item_category (0)');
}
/**
* Tests cloning of a facet.
*/
public function testClone() {
$id = "western_screech_owl";
$name = "Western screech owl";
$this->createFacet($name, $id);
$this->drupalGet('admin/config/search/facets');
$this->assertText('Western screech owl');
$this->assertLink('Clone facet');
$this->clickLink('Clone facet');
$clone_edit = [
'destination_facet_source' => 'search_api:views_block__search_api_test_view__block_1',
'name' => 'Eastern screech owl',
'id' => 'eastern_screech_owl',
];
$this->submitForm($clone_edit, 'Duplicate');
$this->assertText('Facet cloned to Eastern screech owl');
$this->drupalGet('admin/config/search/facets');
$this->assertText('Western screech owl');
$this->assertText('Eastern screech owl');
}
/**
* Configures empty behavior option to show a text on empty results.
*
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment