Skip to content
Snippets Groups Projects
Commit c566d05a authored by xiaohua guan's avatar xiaohua guan Committed by Yas Naoi
Browse files

Issue #3077829 by Xiaohua Guan, yas: Add K8sCloudMenuLinks to switch Cloud...

Issue #3077829 by Xiaohua Guan, yas: Add K8sCloudMenuLinks to switch Cloud Service Providers at /clouds
parent 98048ded
No related branches found
No related tags found
No related merge requests found
k8s.menu.cloud_context:
deriver: 'Drupal\k8s\Plugin\Derivative\K8sMenuLinks'
weight: 100
<?php
namespace Drupal\k8s\Plugin\Derivative;
use Drupal\cloud\Plugin\CloudConfigPluginManagerInterface;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides plugin definitions for custom local menu.
*
* @see \Drupal\k8s\Plugin\Derivative\K8sMenuLinks
*/
class K8sMenuLinks extends DeriverBase implements ContainerDeriverInterface {
use StringTranslationTrait;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* CloudConfigPlugin.
*
* @var \Drupal\cloud\Plugin\CloudConfigPluginManagerInterface
*/
protected $cloudConfigPluginManager;
/**
* Constructs new K8sMenuLinks.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\cloud\Plugin\CloudConfigPluginManagerInterface $cloud_config_plugin_manager
* The Cloud config plugin manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager,
CloudConfigPluginManagerInterface $cloud_config_plugin_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->cloudConfigPluginManager = $cloud_config_plugin_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static(
$container->get('entity.manager'),
$container->get('plugin.manager.cloud_config_plugin')
);
}
/**
* Add a tertiary level menu.
*
* @param array $base_plugin_definition
* An array of the base_plugin_definition.
* @param string $cloud_context
* The cloud context.
* @param string $entity_id
* The entity id.
* @param array $link_data
* An array of link data.
*
* @return array
* An array of menu link definitions.
*/
private function addChildLink(array $base_plugin_definition, $cloud_context, $entity_id, array $link_data) {
$links = [];
$weight = 0;
foreach ($link_data as $link) {
$id = $entity_id . '.local_tasks.' . $cloud_context . '.' . $link['key'];
$links[$id] = $base_plugin_definition;
$links[$id]['title'] = $link['title'];
$links[$id]['route_name'] = $link['route'];
$links[$id]['menu_name'] = 'main';
$links[$id]['parent'] = 'k8s.menu.cloud_context:' . $entity_id . '.local_tasks.' . $cloud_context;
$links[$id]['route_parameters'] = ['cloud_context' => $cloud_context];
$links[$id]['weight'] = $weight++;
}
return $links;
}
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
$entities = $this->cloudConfigPluginManager->loadConfigEntities('k8s');
$links = [];
foreach ($entities as $entity) {
// Add dropdown menus for Cloud Service Provider
// from CloudConfig (cloud_context).
/* @var \Drupal\cloud\Entity\CloudConfig $entity */
$id = "{$entity->id()}.local_tasks.{$entity->getCloudContext()}";
$links[$id] = $base_plugin_definition;
$links[$id]['title'] = $entity->label();
$links[$id]['route_name'] = 'view.k8s_node.list';
$links[$id]['base_route'] = 'cloud.service_providers.menu';
$links[$id]['parent'] = 'cloud.service_providers.menu';
$links[$id]['expanded'] = TRUE;
$links[$id]['route_parameters'] = ['cloud_context' => $entity->getCloudContext()];
// Generate a sort order list of Entity types (k8s_<resource_name>).
static $entity_types = [
'k8s_node',
'k8s_namespace',
'k8s_pod',
];
// Add sub menu items for <Resource Name>.
$data = [];
foreach ($entity_types as $entity_type) {
$entity_definition = $this->entityTypeManager->getDefinition($entity_type);
if ($entity_definition != NULL && $entity_definition->id() == $entity_type) {
$title = preg_replace('/K8s (.*)/', '${1}s', (string) $entity_definition->getLabel());
$data[] = [
'key' => $entity_definition->id(),
'title' => $this->t('@title', ['@title' => $title]),
'route' => "view.{$entity_definition->id()}.list",
];
}
}
$links += $this->addChildLink($base_plugin_definition, $entity->getCloudContext(), $entity->id(), $data);
}
return $links;
}
}
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