cloud-8.x-2.0-beta1/modules/cloud_service_providers/k8s/src/Plugin/Derivative/K8sMenuLinks.php
modules/cloud_service_providers/k8s/src/Plugin/Derivative/K8sMenuLinks.php
<?php
namespace Drupal\k8s\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface;
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;
/**
* The cloud service provider plugin manager (CloudConfigPluginManager).
*
* @var \Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface
*/
protected $cloudConfigPluginManager;
/**
* Constructs new K8sMenuLinks.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface $cloud_config_plugin_manager
* The cloud service provider plugin manager (CloudConfigPluginManager).
*/
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')
);
}
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
// Get k8s entities.
$entities = $this->cloudConfigPluginManager->loadConfigEntities('k8s');
$links = [];
$weight = 0;
foreach ($entities as $entity) {
$cloud_context = $entity->getCloudContext();
$entity_id = $entity->id();
$entity_label = $entity->label();
$base_id = "$entity_id.local_tasks.$cloud_context";
// Add menu items for k8s cluster.
$menu_data = [];
$menu_data[$base_id] = [
'title' => $this->t('@entity_label Cluster', ['@entity_label' => $entity_label]),
'route_name' => 'view.k8s_node.list',
'weight' => $weight++,
];
$entity_types = [
'k8s_node',
'k8s_namespace',
];
$this->addMenuItems(
$links,
$base_plugin_definition,
$cloud_context,
$entity_types,
$menu_data
);
// Add menu items for k8s namespaces.
$menu_data = [];
$menu_data["$base_id.all"] = [
'title' => $this->t('All @entity_label Resources', ['@entity_label' => $entity_label]),
'route_name' => 'view.k8s_namespace.list',
'weight' => $weight++,
];
$namespaces = $this->getNamespaceEntities($cloud_context);
foreach ($namespaces as $namespace) {
$name = $namespace->getName();
$menu_data["$base_id.$name"] = [
'title' => $this->t(
'@entity_label (@name)',
['@entity_label' => $entity_label, '@name' => $name]
),
'route_name' => 'entity.k8s_namespace.canonical',
'route_parameters' => [
'cloud_context' => $cloud_context,
'k8s_namespace' => $namespace->id(),
],
'weight' => $weight++,
];
}
$entity_types = [
'k8s_pod',
'k8s_deployment',
'k8s_replica_set',
'k8s_service',
'k8s_cron_job',
'k8s_job',
'k8s_resource_quota',
'k8s_limit_range',
'k8s_secret',
'k8s_config_map',
'k8s_network_policy',
];
$this->addMenuItems(
$links,
$base_plugin_definition,
$cloud_context,
$entity_types,
$menu_data
);
}
return $links;
}
/**
* Add menu items to the links.
*
* @param array &$links
* Links.
* @param array $base_plugin_definition
* An array of the base_plugin_definition.
* @param string $cloud_context
* The cloud context.
* @param array $entity_types
* The entity types.
* @param array $menu_data
* The data of menu.
*/
private function addMenuItems(
array &$links,
array $base_plugin_definition,
$cloud_context,
array $entity_types,
array $menu_data) {
foreach ($menu_data as $id => $link_data) {
$links[$id] = $base_plugin_definition;
$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' => $cloud_context,
];
$links[$id] = $link_data + $links[$id];
// Get extra route parameters.
$extra_route_parameters = [];
if (isset($link_data['route_parameters']['k8s_namespace'])) {
$namespace = $this->entityTypeManager
->getStorage('k8s_namespace')
->load($link_data['route_parameters']['k8s_namespace']);
$extra_route_parameters['namespace'] = $namespace->getName();
}
// Add child items.
$this->addChildItems(
$links,
$entity_types,
$base_plugin_definition,
$cloud_context,
$id,
$extra_route_parameters
);
}
}
/**
* Add child items to the links.
*
* @param array &$links
* The links.
* @param array $entity_types
* The entity types.
* @param array $base_plugin_definition
* An array of the base_plugin_definition.
* @param string $cloud_context
* The cloud context.
* @param string $parent_link_id
* The ID of the parent link.
* @param array $extra_route_parameters
* The extra route parameters.
*/
private function addChildItems(
array &$links,
array $entity_types,
array $base_plugin_definition,
$cloud_context,
$parent_link_id,
array $extra_route_parameters = []) {
$weight = 0;
foreach ($entity_types as $entity_type) {
$entity_definition = $this->entityTypeManager->getDefinition($entity_type);
if ($entity_definition == NULL) {
continue;
}
$title = preg_replace('/Kubernetes (.*)/', '${1}s', (string) $entity_definition->getLabel());
$id = "$parent_link_id.{$entity_definition->id()}";
$links[$id] = $base_plugin_definition;
$links[$id]['title'] = $this->t('@title', ['@title' => $title]);
$links[$id]['route_name'] = "view.{$entity_definition->id()}.list";
$links[$id]['menu_name'] = 'main';
$links[$id]['parent'] = "k8s.menu.cloud_context:$parent_link_id";
$links[$id]['route_parameters'] = ['cloud_context' => $cloud_context] + $extra_route_parameters;
$links[$id]['weight'] = $weight++;
}
}
/**
* Get k8s namespace entities.
*
* @param string $cloud_context
* The cloud context.
*
* @return array
* The k8s namespace entities.
*/
private function getNamespaceEntities($cloud_context) {
$entity_storage = $this->entityTypeManager->getStorage('k8s_namespace');
$entity_ids = $entity_storage
->getQuery()
->condition('cloud_context', $cloud_context)
->execute();
return $entity_storage->loadMultiple($entity_ids);
}
}
