From 01a2e0779731feb64282a39dbaae33faac2ec08f Mon Sep 17 00:00:00 2001
From: Ide Braakman <ide@ezcompany.nl>
Date: Mon, 26 Feb 2018 12:39:19 +0100
Subject: [PATCH] Issue #2937294 by idebr, borisson_: Display the facet's
 source on the facet edit form

---
 src/Form/FacetForm.php                   | 30 +++++++++++++++++++++++++++---
 tests/src/Functional/IntegrationTest.php |  5 +++++
 2 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/src/Form/FacetForm.php b/src/Form/FacetForm.php
index f9ca27e..0c8218c 100644
--- a/src/Form/FacetForm.php
+++ b/src/Form/FacetForm.php
@@ -6,6 +6,7 @@ use Drupal\Component\Utility\Html;
 use Drupal\Core\Entity\EntityForm;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Form\FormStateInterface;
+use Drupal\facets\FacetSource\FacetSourcePluginManager;
 use Drupal\facets\Plugin\facets\facet_source\SearchApiDisplay;
 use Drupal\facets\FacetSource\SearchApiFacetSourceInterface;
 use Drupal\search_api\Plugin\search_api\display\ViewsRest;
@@ -28,7 +29,7 @@ class FacetForm extends EntityForm {
    *
    * @var \Drupal\facets\FacetInterface
    */
-  protected $facet;
+  protected $entity;
 
   /**
    * The processor manager.
@@ -45,6 +46,13 @@ class FacetForm extends EntityForm {
   protected $widgetPluginManager;
 
   /**
+   * The plugin manager for facet sources.
+   *
+   * @var \Drupal\facets\FacetSource\FacetSourcePluginManager
+   */
+  protected $facetSourcePluginManager;
+
+  /**
    * Constructs an FacetDisplayForm object.
    *
    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
@@ -53,11 +61,14 @@ class FacetForm extends EntityForm {
    *   The processor plugin manager.
    * @param \Drupal\facets\Widget\WidgetPluginManager $widget_plugin_manager
    *   The plugin manager for widgets.
+   * @param \Drupal\facets\FacetSource\FacetSourcePluginManager $facet_source_plugin_manager
+   *   The plugin manager for facet sources.
    */
-  public function __construct(EntityTypeManagerInterface $entity_type_manager, ProcessorPluginManager $processor_plugin_manager, WidgetPluginManager $widget_plugin_manager) {
+  public function __construct(EntityTypeManagerInterface $entity_type_manager, ProcessorPluginManager $processor_plugin_manager, WidgetPluginManager $widget_plugin_manager, FacetSourcePluginManager $facet_source_plugin_manager) {
     $this->entityTypeManager = $entity_type_manager;
     $this->processorPluginManager = $processor_plugin_manager;
     $this->widgetPluginManager = $widget_plugin_manager;
+    $this->facetSourcePluginManager = $facet_source_plugin_manager;
   }
 
   /**
@@ -67,7 +78,8 @@ class FacetForm extends EntityForm {
     return new static(
       $container->get('entity_type.manager'),
       $container->get('plugin.manager.facets.processor'),
-      $container->get('plugin.manager.facets.widget')
+      $container->get('plugin.manager.facets.widget'),
+      $container->get('plugin.manager.facets.facet_source')
     );
   }
 
@@ -124,6 +136,18 @@ class FacetForm extends EntityForm {
     /** @var \Drupal\facets\FacetInterface $facet */
     $facet = $this->entity;
 
+    $facet_sources = [];
+    foreach ($this->facetSourcePluginManager->getDefinitions() as $facet_source_id => $definition) {
+      $facet_sources[$definition['id']] = !empty($definition['label']) ? $definition['label'] : $facet_source_id;
+    }
+    if (isset($facet_sources[$facet->getFacetSourceId()])) {
+      $form['facet_source'] = [
+        '#type' => 'item',
+        '#title' => $this->t('Facet source'),
+        '#markup' => $facet_sources[$facet->getFacetSourceId()],
+      ];
+    }
+
     $widget_options = [];
     foreach ($this->widgetPluginManager->getDefinitions() as $widget_id => $definition) {
       $widget_options[$widget_id] = !empty($definition['label']) ? $definition['label'] : $widget_id;
diff --git a/tests/src/Functional/IntegrationTest.php b/tests/src/Functional/IntegrationTest.php
index 9f6d668..6434fd8 100644
--- a/tests/src/Functional/IntegrationTest.php
+++ b/tests/src/Functional/IntegrationTest.php
@@ -1051,6 +1051,11 @@ class IntegrationTest extends FacetsTestBase {
     // Make sure that the redirection back to the overview was successful and
     // the edited facet is shown on the overview page.
     $this->assertSession()->pageTextContains('Facet ' . $facet_name . ' has been updated.');
+
+    $facet_edit_page = '/admin/config/search/facets/' . $facet_id . '/edit';
+    $this->drupalGet($facet_edit_page);
+    $this->assertSession()->statusCodeEquals(200);
+    $this->assertSession()->pageTextContains('View Search API Test Fulltext search view, display Page');
   }
 
   /**
-- 
2.11.0 (Apple Git-81)

