From ecc3b304290498e575dee6682d96cb56733609f2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Artis=20Baj=C4=81rs?= <artis.bajars@gmail.com>
Date: Wed, 1 Sep 2021 13:15:16 +0300
Subject: [PATCH] Introduce bundle-specific settings for HmSetupPlugin

---
 hierarchy_manager.install               | 39 ++++++++++++
 src/Form/HmMenuForm.php                 | 79 ++++++++++++++-----------
 src/Form/HmOverviewTerms.php            |  4 +-
 src/Plugin/HmSetupPlugin/HmMenu.php     | 15 +++++
 src/Plugin/HmSetupPlugin/HmTaxonomy.php | 13 ++++
 src/Plugin/HmSetupPluginBase.php        | 16 +++++
 src/Plugin/HmSetupPluginInterface.php   |  5 +-
 7 files changed, 134 insertions(+), 37 deletions(-)
 create mode 100644 hierarchy_manager.install

diff --git a/hierarchy_manager.install b/hierarchy_manager.install
new file mode 100644
index 0000000..310354c
--- /dev/null
+++ b/hierarchy_manager.install
@@ -0,0 +1,39 @@
+<?php
+
+/**
+ * @file
+ * Install, update and uninstall functions for the Hierarchy Manager module.
+ */
+
+use Drupal\system\Entity\Menu;
+
+/**
+ * Enable hierarchy manager for all bundles of active setup plugins.
+ */
+function hierarchy_manager_update_8001() {
+  if ($config = \Drupal::configFactory()->getEditable('hierarchy_manager.hmconfig')) {
+    if ($allowed_setup_plugins = $config->get('allowed_setup_plugins')) {
+      if (!empty($allowed_setup_plugins['hm_setup_menu'])) {
+        $menus = Menu::loadMultiple();
+        $bundles = [];
+        /** @var Menu $menu */
+        foreach ($menus as $menu) {
+          $id = $menu->id();
+          $bundles[$id] = $id;
+        }
+        $config->set('setup_plugin_settings.hm_setup_menu.bundle', $bundles);
+        $config->save();
+      }
+      if (!empty($allowed_setup_plugins['hm_setup_taxonomy'])) {
+        $vocabularies = \Drupal::service('entity_type.bundle.info')->getBundleInfo('taxonomy_term');
+        $bundles = [];
+        foreach ($vocabularies as $key => $value) {
+          $bundles[$key] = $key;
+        }
+        $config->set('setup_plugin_settings.hm_setup_taxonomy.bundle', $bundles);
+        $config->save();
+      }
+      drupal_flush_all_caches();
+    }
+  }
+}
diff --git a/src/Form/HmMenuForm.php b/src/Form/HmMenuForm.php
index 896102f..6dd7e8b 100644
--- a/src/Form/HmMenuForm.php
+++ b/src/Form/HmMenuForm.php
@@ -5,48 +5,48 @@ namespace Drupal\hierarchy_manager\Form;
 use Drupal\Core\Url;
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\menu_ui\MenuForm;
+use Drupal\system\MenuInterface;
 
 class HmMenuForm extends MenuForm {
-  
+
   /**
    * The indicator if the menu hierarchy manager is enabled.
-   * 
+   *
    * @var bool|NULL
    */
   private $isEnabled = NULL;
-  
+
   /**
    * The hierarchy manager plugin type manager.
    *
    * @var \Drupal\hierarchy_manager\PluginTypeManager
    */
   private $hmPluginTypeManager = NULL;
-  
+
   /**
    * {@inheritdoc}
    */
   public function form(array $form, FormStateInterface $form_state) {
     $form = parent::form($form, $form_state);
-    
-    // If the menu hierarchy manager plugin is enabled.
+
+    $menu = $this->entity;
+    // If the menu hierarchy manager plugin is enabled for this menu.
     // Override the menu overview form.
-    if ($this->isMenuPluginEnabled() && $this->loadPluginManager()) {
-          $menu = $this->entity;
-          
-          // Add menu links administration form for existing menus.
-          if (!$menu->isNew() || $menu->isLocked()) {
-            // We are removing the menu link overview form
-            // and using our own hierarchy manager tree instead.
-            // The overview form implemented by Drupal Menu UI module
-            // @see \Drupal\menu_ui\MenuForm::form()
-            unset($form['links']);
-            $form['hm_links'] = $this->buildOverviewTree([], $form_state);
-          }
+    if ($this->isMenuPluginEnabled($menu) && $this->loadPluginManager()) {
+      // Add menu links administration form for existing menus.
+      if (!$menu->isNew() || $menu->isLocked()) {
+        // We are removing the menu link overview form
+        // and using our own hierarchy manager tree instead.
+        // The overview form implemented by Drupal Menu UI module
+        // @see \Drupal\menu_ui\MenuForm::form()
+        unset($form['links']);
+        $form['hm_links'] = $this->buildOverviewTree([], $form_state);
+      }
     }
-    
+
     return $form;
   }
-  
+
   /**
    * Submit handler for the menu overview form.
    *
@@ -60,10 +60,10 @@ class HmMenuForm extends MenuForm {
       parent::submitOverviewForm($complete_form, $form_state);
     }
   }
-  
+
   /**
    * Build a menu links overview tree element.
-   * 
+   *
    * @param array $form
    *   Parent form array.
    * @param FormStateInterface $form_state
@@ -71,15 +71,15 @@ class HmMenuForm extends MenuForm {
    * @return NULL|array
    */
   protected function buildOverviewTree(array $form, FormStateInterface $form_state) {
-    
+
     $display_profile = $this->hmPluginTypeManager->getDisplayProfile('hm_setup_menu');
-    
+
     if (empty($display_profile)) {
       return [];
     }
 
     $display_plugin_instance = $this->hmPluginTypeManager->getDisplayPluginInstance($display_profile);
-    
+
     if (!empty($display_plugin_instance)) {
       if (method_exists($display_plugin_instance, 'getForm')) {
         // Menu ID.
@@ -96,41 +96,50 @@ class HmMenuForm extends MenuForm {
         }
         // Urls
         $source_url = Url::fromRoute('hierarchy_manager.menu.tree.json', ['mid' => $mid], ['query' => ['token' => $token, 'destination' => $destination]])->toString();
-        $update_url = Url::fromRoute('hierarchy_manager.menu.tree.update', ['mid' => $mid], ['query' => ['token' => $token]])->toString();       
+        $update_url = Url::fromRoute('hierarchy_manager.menu.tree.update', ['mid' => $mid], ['query' => ['token' => $token]])->toString();
         $config = $display_profile->get("config");
         return $display_plugin_instance->getForm($source_url, $update_url, $form, $form_state, $config);
       }
     }
-    
+
     return [];
   }
-  
+
   /**
    * Create a hierarchy manager plugin manager.
-   * 
+   *
    * @return \Drupal\hierarchy_manager\PluginTypeManager
    */
   protected function loadPluginManager() {
     if (empty($this->hmPluginTypeManager)) {
       $this->hmPluginTypeManager = \Drupal::service('hm.plugin_type_manager');
     }
-    
+
     return $this->hmPluginTypeManager;
   }
-  
+
   /**
    * Check if the menu hierarchy plugin is enabled.
-   * 
+   *
+   * @param \Drupal\system\MenuInterface $menu
+   *   The menu entity.
+   *
    * @return boolean|NULL
    *   Return TRUE if the menu plugin is enabled,
    *   otherwise return FALSE.
    */
-  protected function isMenuPluginEnabled() {
+  protected function isMenuPluginEnabled(MenuInterface $menu) {
     if ($this->isEnabled === NULL) {
       if ($config = \Drupal::config('hierarchy_manager.hmconfig')) {
         if ($allowed_setup_plugins = $config->get('allowed_setup_plugins')) {
           if (!empty($allowed_setup_plugins['hm_setup_menu'])) {
-            $this->isEnabled = TRUE;
+            $plugin_settings = $config->get('setup_plugin_settings');
+            if (!empty($plugin_settings['hm_setup_menu'])) {
+              $enabled_bundles = array_keys(array_filter($plugin_settings['hm_setup_menu']['bundle']));
+              if (in_array($menu->id(), $enabled_bundles)) {
+                $this->isEnabled = TRUE;
+              }
+            }
           }
           else {
             $this->isEnabled = FALSE;
@@ -138,7 +147,7 @@ class HmMenuForm extends MenuForm {
         }
       }
     }
-    
+
     return $this->isEnabled;
   }
 }
diff --git a/src/Form/HmOverviewTerms.php b/src/Form/HmOverviewTerms.php
index a229648..829551e 100644
--- a/src/Form/HmOverviewTerms.php
+++ b/src/Form/HmOverviewTerms.php
@@ -38,11 +38,13 @@ class HmOverviewTerms extends OverviewTerms {
           // Hierarchy Manager setup plugin configuration.
           $plugin_settings = $config->get('setup_plugin_settings');
           if (!empty($plugin_settings['hm_setup_taxonomy'])) {
+            // Enabled bundles.
+            $enabled_bundles = array_keys(array_filter($plugin_settings['hm_setup_taxonomy']['bundle']));
             // Display profile ID.
             $display_profile_id = $plugin_settings['hm_setup_taxonomy']['display_profile'];
             // Display profile.
             $display_profile = $this->entityTypeManager->getStorage('hm_display_profile')->load($display_profile_id);
-            if (!empty($display_profile)) {
+            if (!empty($display_profile) && in_array($taxonomy_vocabulary->id(), $enabled_bundles)) {
               // Display plugin instance.
               $instance = \Drupal::service('plugin.manager.hm.display_plugin')->createInstance($display_profile->get("plugin"));
               if (method_exists($instance, 'getForm')) {
diff --git a/src/Plugin/HmSetupPlugin/HmMenu.php b/src/Plugin/HmSetupPlugin/HmMenu.php
index 239d6dd..fa8ffa1 100644
--- a/src/Plugin/HmSetupPlugin/HmMenu.php
+++ b/src/Plugin/HmSetupPlugin/HmMenu.php
@@ -4,6 +4,7 @@ namespace Drupal\hierarchy_manager\Plugin\HmSetupPlugin;
 
 use Drupal\hierarchy_manager\Plugin\HmSetupPluginInterface;
 use Drupal\hierarchy_manager\Plugin\HmSetupPluginBase;
+use Drupal\system\Entity\Menu;
 
 /**
  * Menu link hierarchy setup plugin.
@@ -14,5 +15,19 @@ use Drupal\hierarchy_manager\Plugin\HmSetupPluginBase;
  * )
  */
 class HmMenu extends HmSetupPluginBase implements HmSetupPluginInterface {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getBundleOptions() {
+    $menus = Menu::loadMultiple();
+    $options = [];
+    /** @var Menu $menu */
+    foreach ($menus as $menu) {
+      $options[$menu->id()] = $menu->label();
+    }
+    return $options;
+  }
+
 }
 
diff --git a/src/Plugin/HmSetupPlugin/HmTaxonomy.php b/src/Plugin/HmSetupPlugin/HmTaxonomy.php
index 0acfc45..dc176c2 100644
--- a/src/Plugin/HmSetupPlugin/HmTaxonomy.php
+++ b/src/Plugin/HmSetupPlugin/HmTaxonomy.php
@@ -14,4 +14,17 @@ use Drupal\hierarchy_manager\Plugin\HmSetupPluginBase;
  * )
  */
 class HmTaxonomy extends HmSetupPluginBase implements HmSetupPluginInterface {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getBundleOptions() {
+    $bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo('taxonomy_term');
+    $options = [];
+    foreach ($bundles as $key => $value) {
+      $options[$key] = $value['label'];
+    }
+    return $options;
+  }
+
 }
diff --git a/src/Plugin/HmSetupPluginBase.php b/src/Plugin/HmSetupPluginBase.php
index 0999208..a6f89f6 100644
--- a/src/Plugin/HmSetupPluginBase.php
+++ b/src/Plugin/HmSetupPluginBase.php
@@ -18,6 +18,13 @@ abstract class HmSetupPluginBase extends PluginBase implements HmSetupPluginInte
    */
   protected  $displayProfile;
 
+  /**
+   * Enabled entity bundles.
+   *
+   * @var array
+   */
+  protected  $enabledBundles;
+
   /**
    * Constructs a new setup plugin object.
    *
@@ -34,9 +41,11 @@ abstract class HmSetupPluginBase extends PluginBase implements HmSetupPluginInte
     $plugin_settings = \Drupal::config('hierarchy_manager.hmconfig')->get('setup_plugin_settings');
     if (isset($plugin_settings[$this->pluginId])) {
       $this->displayProfile = $plugin_settings[$this->pluginId]['display_profile'];
+      $this->enabledBundles = $plugin_settings[$this->pluginId]['bundle'];
     }
     else {
       $this->displayProfile = '';
+      $this->enabledBundles = [];
     }
   }
 
@@ -58,6 +67,13 @@ abstract class HmSetupPluginBase extends PluginBase implements HmSetupPluginInte
       '#default_value' => $this->displayProfile,
       '#required' => TRUE,
     ];
+    $settings_form['bundle'] = [
+      '#type' => 'checkboxes',
+      '#title' => $this->t('Enabled bundles'),
+      '#options' => $this->getBundleOptions(),
+      '#default_value' => $this->enabledBundles,
+      '#description' => $this->t('Specify bundles for which hierarchy manager should be enabled.')
+    ];
 
     return $settings_form;
   }
diff --git a/src/Plugin/HmSetupPluginInterface.php b/src/Plugin/HmSetupPluginInterface.php
index 15c687d..fb782d9 100644
--- a/src/Plugin/HmSetupPluginInterface.php
+++ b/src/Plugin/HmSetupPluginInterface.php
@@ -9,6 +9,9 @@ use Drupal\Component\Plugin\PluginInspectionInterface;
  */
 interface HmSetupPluginInterface extends PluginInspectionInterface {
 
+  /**
+   * Get a list of bundles supported by the Setup Plugin.
+   */
+  public function getBundleOptions();
 
-  // Add get/set methods for your plugin type here.
 }
-- 
GitLab