Skip to content
Snippets Groups Projects
Commit ecc3b304 authored by artis.bajars's avatar artis.bajars
Browse files

Introduce bundle-specific settings for HmSetupPlugin

parent b2771c9f
No related branches found
No related tags found
No related merge requests found
<?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();
}
}
}
...@@ -5,48 +5,48 @@ namespace Drupal\hierarchy_manager\Form; ...@@ -5,48 +5,48 @@ namespace Drupal\hierarchy_manager\Form;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\menu_ui\MenuForm; use Drupal\menu_ui\MenuForm;
use Drupal\system\MenuInterface;
class HmMenuForm extends MenuForm { class HmMenuForm extends MenuForm {
/** /**
* The indicator if the menu hierarchy manager is enabled. * The indicator if the menu hierarchy manager is enabled.
* *
* @var bool|NULL * @var bool|NULL
*/ */
private $isEnabled = NULL; private $isEnabled = NULL;
/** /**
* The hierarchy manager plugin type manager. * The hierarchy manager plugin type manager.
* *
* @var \Drupal\hierarchy_manager\PluginTypeManager * @var \Drupal\hierarchy_manager\PluginTypeManager
*/ */
private $hmPluginTypeManager = NULL; private $hmPluginTypeManager = NULL;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function form(array $form, FormStateInterface $form_state) { public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $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. // Override the menu overview form.
if ($this->isMenuPluginEnabled() && $this->loadPluginManager()) { if ($this->isMenuPluginEnabled($menu) && $this->loadPluginManager()) {
$menu = $this->entity; // Add menu links administration form for existing menus.
if (!$menu->isNew() || $menu->isLocked()) {
// Add menu links administration form for existing menus. // We are removing the menu link overview form
if (!$menu->isNew() || $menu->isLocked()) { // and using our own hierarchy manager tree instead.
// We are removing the menu link overview form // The overview form implemented by Drupal Menu UI module
// and using our own hierarchy manager tree instead. // @see \Drupal\menu_ui\MenuForm::form()
// The overview form implemented by Drupal Menu UI module unset($form['links']);
// @see \Drupal\menu_ui\MenuForm::form() $form['hm_links'] = $this->buildOverviewTree([], $form_state);
unset($form['links']); }
$form['hm_links'] = $this->buildOverviewTree([], $form_state);
}
} }
return $form; return $form;
} }
/** /**
* Submit handler for the menu overview form. * Submit handler for the menu overview form.
* *
...@@ -60,10 +60,10 @@ class HmMenuForm extends MenuForm { ...@@ -60,10 +60,10 @@ class HmMenuForm extends MenuForm {
parent::submitOverviewForm($complete_form, $form_state); parent::submitOverviewForm($complete_form, $form_state);
} }
} }
/** /**
* Build a menu links overview tree element. * Build a menu links overview tree element.
* *
* @param array $form * @param array $form
* Parent form array. * Parent form array.
* @param FormStateInterface $form_state * @param FormStateInterface $form_state
...@@ -71,15 +71,15 @@ class HmMenuForm extends MenuForm { ...@@ -71,15 +71,15 @@ class HmMenuForm extends MenuForm {
* @return NULL|array * @return NULL|array
*/ */
protected function buildOverviewTree(array $form, FormStateInterface $form_state) { protected function buildOverviewTree(array $form, FormStateInterface $form_state) {
$display_profile = $this->hmPluginTypeManager->getDisplayProfile('hm_setup_menu'); $display_profile = $this->hmPluginTypeManager->getDisplayProfile('hm_setup_menu');
if (empty($display_profile)) { if (empty($display_profile)) {
return []; return [];
} }
$display_plugin_instance = $this->hmPluginTypeManager->getDisplayPluginInstance($display_profile); $display_plugin_instance = $this->hmPluginTypeManager->getDisplayPluginInstance($display_profile);
if (!empty($display_plugin_instance)) { if (!empty($display_plugin_instance)) {
if (method_exists($display_plugin_instance, 'getForm')) { if (method_exists($display_plugin_instance, 'getForm')) {
// Menu ID. // Menu ID.
...@@ -96,41 +96,50 @@ class HmMenuForm extends MenuForm { ...@@ -96,41 +96,50 @@ class HmMenuForm extends MenuForm {
} }
// Urls // Urls
$source_url = Url::fromRoute('hierarchy_manager.menu.tree.json', ['mid' => $mid], ['query' => ['token' => $token, 'destination' => $destination]])->toString(); $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"); $config = $display_profile->get("config");
return $display_plugin_instance->getForm($source_url, $update_url, $form, $form_state, $config); return $display_plugin_instance->getForm($source_url, $update_url, $form, $form_state, $config);
} }
} }
return []; return [];
} }
/** /**
* Create a hierarchy manager plugin manager. * Create a hierarchy manager plugin manager.
* *
* @return \Drupal\hierarchy_manager\PluginTypeManager * @return \Drupal\hierarchy_manager\PluginTypeManager
*/ */
protected function loadPluginManager() { protected function loadPluginManager() {
if (empty($this->hmPluginTypeManager)) { if (empty($this->hmPluginTypeManager)) {
$this->hmPluginTypeManager = \Drupal::service('hm.plugin_type_manager'); $this->hmPluginTypeManager = \Drupal::service('hm.plugin_type_manager');
} }
return $this->hmPluginTypeManager; return $this->hmPluginTypeManager;
} }
/** /**
* Check if the menu hierarchy plugin is enabled. * Check if the menu hierarchy plugin is enabled.
* *
* @param \Drupal\system\MenuInterface $menu
* The menu entity.
*
* @return boolean|NULL * @return boolean|NULL
* Return TRUE if the menu plugin is enabled, * Return TRUE if the menu plugin is enabled,
* otherwise return FALSE. * otherwise return FALSE.
*/ */
protected function isMenuPluginEnabled() { protected function isMenuPluginEnabled(MenuInterface $menu) {
if ($this->isEnabled === NULL) { if ($this->isEnabled === NULL) {
if ($config = \Drupal::config('hierarchy_manager.hmconfig')) { if ($config = \Drupal::config('hierarchy_manager.hmconfig')) {
if ($allowed_setup_plugins = $config->get('allowed_setup_plugins')) { if ($allowed_setup_plugins = $config->get('allowed_setup_plugins')) {
if (!empty($allowed_setup_plugins['hm_setup_menu'])) { 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 { else {
$this->isEnabled = FALSE; $this->isEnabled = FALSE;
...@@ -138,7 +147,7 @@ class HmMenuForm extends MenuForm { ...@@ -138,7 +147,7 @@ class HmMenuForm extends MenuForm {
} }
} }
} }
return $this->isEnabled; return $this->isEnabled;
} }
} }
......
...@@ -38,11 +38,13 @@ class HmOverviewTerms extends OverviewTerms { ...@@ -38,11 +38,13 @@ class HmOverviewTerms extends OverviewTerms {
// Hierarchy Manager setup plugin configuration. // Hierarchy Manager setup plugin configuration.
$plugin_settings = $config->get('setup_plugin_settings'); $plugin_settings = $config->get('setup_plugin_settings');
if (!empty($plugin_settings['hm_setup_taxonomy'])) { 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.
$display_profile_id = $plugin_settings['hm_setup_taxonomy']['display_profile']; $display_profile_id = $plugin_settings['hm_setup_taxonomy']['display_profile'];
// Display profile. // Display profile.
$display_profile = $this->entityTypeManager->getStorage('hm_display_profile')->load($display_profile_id); $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. // Display plugin instance.
$instance = \Drupal::service('plugin.manager.hm.display_plugin')->createInstance($display_profile->get("plugin")); $instance = \Drupal::service('plugin.manager.hm.display_plugin')->createInstance($display_profile->get("plugin"));
if (method_exists($instance, 'getForm')) { if (method_exists($instance, 'getForm')) {
......
...@@ -4,6 +4,7 @@ namespace Drupal\hierarchy_manager\Plugin\HmSetupPlugin; ...@@ -4,6 +4,7 @@ namespace Drupal\hierarchy_manager\Plugin\HmSetupPlugin;
use Drupal\hierarchy_manager\Plugin\HmSetupPluginInterface; use Drupal\hierarchy_manager\Plugin\HmSetupPluginInterface;
use Drupal\hierarchy_manager\Plugin\HmSetupPluginBase; use Drupal\hierarchy_manager\Plugin\HmSetupPluginBase;
use Drupal\system\Entity\Menu;
/** /**
* Menu link hierarchy setup plugin. * Menu link hierarchy setup plugin.
...@@ -14,5 +15,19 @@ use Drupal\hierarchy_manager\Plugin\HmSetupPluginBase; ...@@ -14,5 +15,19 @@ use Drupal\hierarchy_manager\Plugin\HmSetupPluginBase;
* ) * )
*/ */
class HmMenu extends HmSetupPluginBase implements HmSetupPluginInterface { 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;
}
} }
...@@ -14,4 +14,17 @@ use Drupal\hierarchy_manager\Plugin\HmSetupPluginBase; ...@@ -14,4 +14,17 @@ use Drupal\hierarchy_manager\Plugin\HmSetupPluginBase;
* ) * )
*/ */
class HmTaxonomy extends HmSetupPluginBase implements HmSetupPluginInterface { 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;
}
} }
...@@ -18,6 +18,13 @@ abstract class HmSetupPluginBase extends PluginBase implements HmSetupPluginInte ...@@ -18,6 +18,13 @@ abstract class HmSetupPluginBase extends PluginBase implements HmSetupPluginInte
*/ */
protected $displayProfile; protected $displayProfile;
/**
* Enabled entity bundles.
*
* @var array
*/
protected $enabledBundles;
/** /**
* Constructs a new setup plugin object. * Constructs a new setup plugin object.
* *
...@@ -34,9 +41,11 @@ abstract class HmSetupPluginBase extends PluginBase implements HmSetupPluginInte ...@@ -34,9 +41,11 @@ abstract class HmSetupPluginBase extends PluginBase implements HmSetupPluginInte
$plugin_settings = \Drupal::config('hierarchy_manager.hmconfig')->get('setup_plugin_settings'); $plugin_settings = \Drupal::config('hierarchy_manager.hmconfig')->get('setup_plugin_settings');
if (isset($plugin_settings[$this->pluginId])) { if (isset($plugin_settings[$this->pluginId])) {
$this->displayProfile = $plugin_settings[$this->pluginId]['display_profile']; $this->displayProfile = $plugin_settings[$this->pluginId]['display_profile'];
$this->enabledBundles = $plugin_settings[$this->pluginId]['bundle'];
} }
else { else {
$this->displayProfile = ''; $this->displayProfile = '';
$this->enabledBundles = [];
} }
} }
...@@ -58,6 +67,13 @@ abstract class HmSetupPluginBase extends PluginBase implements HmSetupPluginInte ...@@ -58,6 +67,13 @@ abstract class HmSetupPluginBase extends PluginBase implements HmSetupPluginInte
'#default_value' => $this->displayProfile, '#default_value' => $this->displayProfile,
'#required' => TRUE, '#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; return $settings_form;
} }
......
...@@ -9,6 +9,9 @@ use Drupal\Component\Plugin\PluginInspectionInterface; ...@@ -9,6 +9,9 @@ use Drupal\Component\Plugin\PluginInspectionInterface;
*/ */
interface HmSetupPluginInterface extends 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.
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment