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;
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;
}
}
......
......@@ -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')) {
......
......@@ -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;
}
}
......@@ -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;
}
}
......@@ -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;
}
......
......@@ -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.
}
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