cloud-8.x-2.0-beta1/modules/cloud_service_providers/k8s/src/Plugin/Block/K8sNodeAllocatedResourcesBlock.php

modules/cloud_service_providers/k8s/src/Plugin/Block/K8sNodeAllocatedResourcesBlock.php
<?php

namespace Drupal\k8s\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\PageCache\ResponsePolicy\KillSwitch;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\k8s\Service\K8sServiceException;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a block of the allocated resources at a Node.
 *
 * @Block(
 *   id = "k8s_node_allocated_resources",
 *   admin_label = @Translation("K8s Node Allocated Resources"),
 *   category = @Translation("K8s")
 * )
 */
class K8sNodeAllocatedResourcesBlock extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * The entity.manager service.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
   * The route match service.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * The page cache kill switch service.
   *
   * @var \Drupal\Core\PageCache\ResponsePolicy\KillSwitch
   */
  protected $killSwitch;

  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Stores the configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Constructs a new K8sNodeHeatmapBlock instance.
   *
   * @param array $configuration
   *   The plugin configuration, i.e. an array with configuration values keyed
   *   by configuration option name. The special key 'context' may be used to
   *   initialize the defined contexts by setting it to an array of context
   *   values keyed by context names.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity.manager service.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match service.
   * @param \Drupal\Core\PageCache\ResponsePolicy\KillSwitch $kill_switch
   *   The page cache kill switch service.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   */
  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    EntityManagerInterface $entity_manager,
    RouteMatchInterface $route_match,
    KillSwitch $kill_switch,
    ModuleHandlerInterface $module_handler,
    ConfigFactoryInterface $config_factory
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);

    $this->entityManager = $entity_manager;
    $this->routeMatch = $route_match;
    $this->killSwitch = $kill_switch;
    $this->moduleHandler = $module_handler;
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(
    ContainerInterface $container,
    array $configuration,
    $plugin_id,
    $plugin_definition
  ) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('entity.manager'),
      $container->get('current_route_match'),
      $container->get('page_cache_kill_switch'),
      $container->get('module_handler'),
      $container->get('config.factory')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'display_view_k8s_node_list_only' => 1,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function blockForm($form, FormStateInterface $form_state) {
    $form['display_view_k8s_node_list_only'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Display this block in K8s Node list page only'),
      '#default_value' => $this->configuration['display_view_k8s_node_list_only'],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state) {
    $this->configuration['display_view_k8s_node_list_only']
      = $form_state->getValue('display_view_k8s_node_list_only');
  }

  /**
   * {@inheritdoc}
   */
  public function build() {

    $cloud_context = $this->routeMatch->getParameter('cloud_context');
    $route_name = $this->routeMatch->getRouteName();

    if ($route_name !== 'view.k8s_node.list'
    && $this->configuration['display_view_k8s_node_list_only']) {
      return [];
    }

    // The following $cloud_context is reference to view.k8s_node.list, not
    // entity.k8s_node.collection in out case.  See also:
    // ...
    // $this->derivatives[$id]['route_name'] = 'view.k8s_node.list';
    //
    // _OR_
    //
    // K8sLocalTask::getDerivativeDefinitions($base_plugin_definition) {
    // ...
    // $this->derivatives[$id]['route_name'] = 'view.k8s_node.list';
    // ...
    // Confirm whether the metrics API can be used or not.

    $metrics_enabled = TRUE;
    $k8s_service = \Drupal::service('k8s');
    if (!empty($cloud_context)) {
      $k8s_service->setCloudContext($cloud_context);
      try {
        $k8s_service->getMetricsNodes();
      } catch (K8sServiceException $e) {
        $metrics_enabled = FALSE;
      }
    }

    $fieldset_defs = [
      [
        'name' => 'k8s_allocated_resources',
        'title' => t('Allocated Resources'),
        'open' => TRUE,
        'fields' => [
          'k8s_node_allocated_resources',
        ],
      ],
    ];

    $build['k8s_node_allocated_resources'] = [
      '#markup' => '<div id="k8s_node_allocated_resources"></div>',
      '#attached' => [
        'library' => [
          'k8s/k8s_node_allocated_resources',
        ],
        'drupalSettings' => [
          'k8s' => [
            'k8s_js_refresh_interval' => $this->configFactory->get('k8s.settings')
              ->get('k8s_js_refresh_interval'),
            'metrics_enable' => $metrics_enabled,
          ],
        ],
      ],
    ];

    cloud_form_reorder($build, $fieldset_defs);

    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheMaxAge() {
    $this->killSwitch->trigger();
    // BigPipe/#cache/max-age is breaking my block javascript
    // https://www.drupal.org/forum/support/module-development-and-code-questions/2016-07-17/bigpipecachemax-age-is-breaking-my
    // "a slight delay of a second or two before the charts are built.
    // That seems to work, though it is a janky solution.".
    return $this->moduleHandler->moduleExists('big_pipe') ? 1 : 0;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc