View source
<?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;
class FacetCloneForm extends EntityForm {
protected $facetSourcePluginManager;
protected $facetStorage;
protected $displayPluginManager;
public function __construct(FacetSourcePluginManager $facetSourcePluginManager, DisplayPluginManager $displayPluginManager, EntityStorageInterface $facetStorage) {
$this->facetSourcePluginManager = $facetSourcePluginManager;
$this->displayPluginManager = $displayPluginManager;
$this->facetStorage = $facetStorage;
}
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'));
}
public function form(array $form, FormStateInterface $form_state) {
$facet = $this
->getEntity();
if (strpos($facet
->getFacetSourceId(), 'search_api:') === FALSE) {
$this
->messenger()
->addMessage($this
->t('We can only clone Search API based facets.'));
return [];
}
$facet_source_definitions = $this->facetSourcePluginManager
->getDefinitions();
$facet_source_id = $facet
->getFacetSourceId();
$search_api_display_id = $facet_source_definitions[$facet_source_id]['display_id'];
$current_display = $this->displayPluginManager
->createInstance($search_api_display_id);
$current_index = $current_display
->getIndex()
->id();
$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;
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Duplicate'),
];
return $actions;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$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();
$this
->messenger()
->addMessage($this
->t('Facet cloned to :label', [
':label' => $facet
->label(),
]));
$form_state
->setRedirectUrl($facet
->toUrl('edit-form'));
}
}