Skip to content
Snippets Groups Projects
Commit c9c61082 authored by Mingsong Hu's avatar Mingsong Hu
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
<?php
namespace Drupal\hierarchy_manager;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
/**
* Provides routes for HM Display Profile Entity entities.
*
* @see Drupal\Core\Entity\Routing\AdminHtmlRouteProvider
* @see Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider
*/
class HmDisplayProfileHtmlRouteProvider extends AdminHtmlRouteProvider {
/**
* {@inheritdoc}
*/
public function getRoutes(EntityTypeInterface $entity_type) {
$collection = parent::getRoutes($entity_type);
// Provide your custom entity routes here.
return $collection;
}
}
<?php
namespace Drupal\hierarchy_manager;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
/**
* Provides a listing of HM Display Profile Entity entities.
*/
class HmDisplayProfileListBuilder extends ConfigEntityListBuilder {
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['label'] = $this->t('HM Display Profile Entity');
$header['id'] = $this->t('Machine name');
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
$row['label'] = $entity->label();
$row['id'] = $entity->id();
// You probably want a few more properties here...
return $row + parent::buildRow($entity);
}
}
<?php
namespace Drupal\hierarchy_manager\Plugin\HmDisplayPlugin;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\hierarchy_manager\Plugin\HmDisplayPluginInterface;
use Drupal\hierarchy_manager\Plugin\HmDisplayPluginBase;
/**
* Fancytree display plugin.
*
* @HmDisplayPlugin(
* id = "hm_display_fancytree",
* label = @Translation("Fancytree")
* )
*/
class HmDisplayFancytree extends HmDisplayPluginBase implements HmDisplayPluginInterface {
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
public function getForm(string $url_source, string $url_update, array $form = [], FormStateInterface $form_state = NULL) {
if (!empty($url_source)) {
// Search input.
$form['title'] = [
'#type' => 'textfield',
'#title' => $this
->t('Search'),
'#description' => $this->t('Type in the search keyword here to filter the tree below. Empty the keyword or press ESC key to reset the tree.'),
'#attributes' => [
'name' => 'fancytree-search',
],
'#size' => 60,
'#maxlength' => 128,
];
$form['fancytree'] = [
'#type' => 'html_tag',
'#suffix' => '<div class="description">' . $this->t('You can double click a taxonomy term to edit it.') . '<br>' . $this->t('A taxonomy term in the tree above can be dragged and dropped') . '</div>',
'#tag' => 'div',
'#value' => '',
'#attributes' => [
'class' => [
'fancytree',
],
'data-source' => $url_source,
'url-update' => $url_update,
],
];
$form['#attached']['library'][] = 'hierarchy_manager/libraries.jquery.fancytree';
$form['#attached']['library'][] = 'hierarchy_manager/libraries.jquery.fancytree.skin-win8';
$form['#attached']['library'][] = 'hierarchy_manager/feature.hm.fancytree';
}
return $form;
}
/**
* Build the data array that FancyTree accepts.
*/
public function treeData(array $data) {
$tree_data = [];
foreach ($data as $tree_node) {
$tree_data[] = [
'title' => $tree_node['title'],
'folder' => $tree_node['has_children'],
'key' => $tree_node['id'],
'lazy' => $tree_node['has_children'],
'edit_url' => $tree_node['edit_url'],
];
}
return $tree_data;
}
}
<?php
namespace Drupal\hierarchy_manager\Plugin;
use Drupal\Component\Plugin\PluginBase;
/**
* Base class for Hierarchy manager display plugin plugins.
*/
abstract class HmDisplayPluginBase extends PluginBase implements HmDisplayPluginInterface {
// Add common methods and abstract methods for your plugin type here.
}
<?php
namespace Drupal\hierarchy_manager\Plugin;
use Drupal\Component\Plugin\PluginInspectionInterface;
/**
* Defines an interface for Hierarchy manager display plugin plugins.
*/
interface HmDisplayPluginInterface extends PluginInspectionInterface {
// Add get/set methods for your plugin type here.
}
<?php
namespace Drupal\hierarchy_manager\Plugin;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
/**
* Provides the Hierarchy manager display plugin plugin manager.
*/
class HmDisplayPluginManager extends DefaultPluginManager {
/**
* Constructs a new HmDisplayPluginManager object.
*
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* Cache backend instance to use.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler to invoke the alter hook with.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/HmDisplayPlugin', $namespaces, $module_handler, 'Drupal\hierarchy_manager\Plugin\HmDisplayPluginInterface', 'Drupal\hierarchy_manager\Annotation\HmDisplayPlugin');
$this->alterInfo('hierarchy_manager_hm_display_plugin_info');
$this->setCacheBackend($cache_backend, 'hierarchy_manager_hm_display_plugin_plugins');
}
}
<?php
namespace Drupal\hierarchy_manager\Plugin\HmSetupPlugin;
use Drupal\hierarchy_manager\Plugin\HmSetupPluginInterface;
use Drupal\hierarchy_manager\Plugin\HmSetupPluginBase;
/**
* Taxonomy hierarchy setup plugin.
*
* @HmSetupPlugin(
* id = "hm_setup_taxonomy",
* label = @Translation("Taxonomy hierarchy setup plugin")
* )
*/
class HmTaxonomy extends HmSetupPluginBase implements HmSetupPluginInterface {
}
<?php
namespace Drupal\hierarchy_manager\Plugin;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Base class for Hierarchy Manager Setup Plugin plugins.
*/
abstract class HmSetupPluginBase extends PluginBase implements HmSetupPluginInterface {
use StringTranslationTrait;
/**
* Display profile ID.
*
* @var string
*/
protected $displayProfile;
/**
* Constructs a new setup plugin object.
*
* @param array $configuration
* The plugin configuration.
* @param string $plugin_id
* The plugin id.
* @param mixed $plugin_definition
* The plugin definition.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$plugin_settings = \Drupal::config('hierarchy_manager.hmconfig')->get('setup_plugin_settings');
$settings = $plugin_settings[$this->pluginId] ?: [];
$this->displayProfile = $settings['display_profile'];
}
/**
* Common methods and abstract methods for HM setup plugin type.
*/
public function buildConfigurationForm($config, $state) {
// All display profiles.
$display_profiles = \Drupal::entityTypeManager()->getStorage('hm_display_profile')->loadMultiple();
$display_options = [];
foreach ($display_profiles as $profile) {
$display_options[$profile->id()] = $profile->label();
}
$settings_form['display_profile'] = [
'#type' => 'select',
'#title' => $this->t('Display Profile'),
'#options' => $display_options,
'#description' => 'Specify the display profile to render the hierarchy tree.',
'#default_value' => $this->displayProfile,
'#required' => TRUE,
];
return $settings_form;
}
/**
* Get the display profile ID.
*
* @return string
* The profile ID.
*/
public function getDispalyProfileId() {
return $this->displayProfile;
}
}
<?php
namespace Drupal\hierarchy_manager\Plugin;
use Drupal\Component\Plugin\PluginInspectionInterface;
/**
* Defines an interface for Hierarchy Manager Setup Plugin plugins.
*/
interface HmSetupPluginInterface extends PluginInspectionInterface {
// Add get/set methods for your plugin type here.
}
<?php
namespace Drupal\hierarchy_manager\Plugin;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
/**
* Provides the Hierarchy Manager Setup Plugin plugin manager.
*/
class HmSetupPluginManager extends DefaultPluginManager {
/**
* Constructs a new HmSetupPluginManager object.
*
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* Cache backend instance to use.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler to invoke the alter hook with.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/HmSetupPlugin', $namespaces, $module_handler, 'Drupal\hierarchy_manager\Plugin\HmSetupPluginInterface', 'Drupal\hierarchy_manager\Annotation\HmSetupPlugin');
$this->alterInfo('hierarchy_manager_HmSetup_info');
$this->setCacheBackend($cache_backend, 'hierarchy_manager_HmSetup_plugins');
}
}
<?php
namespace Drupal\hierarchy_manager\Routing;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
/**
* Listens to the dynamic route events.
*
* Class HmRouteSubscriber.
*
* @package Drupal\hierarchy_manager\Routing
*/
class HmRouteSubscriber extends RouteSubscriberBase {
/**
* Overrides entity.taxonomy_vocabulary.overview_form route.
*
* @param \Symfony\Component\Routing\RouteCollection $collection
* Route Collection.
*/
protected function alterRoutes(RouteCollection $collection) {
// Change path of taxonomy overview to our overridden form.
if ($route = $collection->get('entity.taxonomy_vocabulary.overview_form')) {
$route->setDefault('_form', '\Drupal\hierarchy_manager\Form\HmOverviewTerms');
}
}
}
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