cloud-8.x-2.0-beta1/src/Plugin/cloud/server_template/CloudServerTemplatePluginManager.php

src/Plugin/cloud/server_template/CloudServerTemplatePluginManager.php
<?php

namespace Drupal\cloud\Plugin\cloud\server_template;

use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Plugin\Discovery\YamlDiscovery;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\cloud\Entity\CloudServerTemplateInterface;

/**
 * Provides the default cloud_server_template_plugin manager.
 */
class CloudServerTemplatePluginManager extends DefaultPluginManager implements CloudServerTemplatePluginManagerInterface {

  use StringTranslationTrait;

  /**
   * The Messenger service.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  private $messenger;

  /**
   * Provides default values for all cloud_server_template_plugin plugins.
   *
   * @var array
   */
  protected $defaults = [
    'id' => 'cloud_server_template',
    'entity_type' => 'cloud_server_template',
  ];

  /**
   * Constructs a new CloudServerTemplatePluginManager object.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
   *   Cache backend instance to use.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger service.
   */
  public function __construct(
    ModuleHandlerInterface $module_handler,
    CacheBackendInterface $cache_backend,
    TranslationInterface $string_translation,
    MessengerInterface $messenger
  ) {
    // Add more services as required.
    $this->moduleHandler = $module_handler;
    $this->setCacheBackend($cache_backend, 'cloud_server_template_plugin', ['cloud_server_template_plugin']);

    // Setup the $this->t().
    $this->stringTranslation = $string_translation;

    $this->messenger = $messenger;
  }

  /**
   * {@inheritdoc}
   */
  protected function getDiscovery() {
    if (!isset($this->discovery)) {
      $this->discovery = new YamlDiscovery('cloud.server.template.plugin', $this->moduleHandler->getModuleDirectories());
      $this->discovery->addTranslatableProperty('label', 'label_context');
      $this->discovery = new ContainerDerivativeDiscoveryDecorator($this->discovery);
    }
    return $this->discovery;
  }

  /**
   * {@inheritdoc}
   */
  public function processDefinition(&$definition, $plugin_id) {
    parent::processDefinition($definition, $plugin_id);

    if (empty($definition['id'])) {
      throw new PluginException(sprintf('CloudServerTemplatePlugin plugin property (%s) definition "is" is required.', $plugin_id));
    }

    if (empty($definition['entity_bundle'])) {
      throw new PluginException(sprintf('entity_bundle property is required for (%s)', $plugin_id));
    }

    if (empty($definition['cloud_context'])) {
      throw new PluginException(sprintf('cloud_context property is required for (%s)', $plugin_id));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function loadPluginVariant($cloud_context) {
    $plugin = FALSE;
    foreach ($this->getDefinitions() as $key => $definition) {
      if ($definition['cloud_context'] == $cloud_context) {
        $plugin = $this->createInstance($key);
        break;
      }
    }

    return $plugin;
  }

  /**
   * {@inheritdoc}
   */
  public function launch(CloudServerTemplateInterface $cloud_server_template, FormStateInterface $form_state = NULL) {
    $plugin = $this->loadPluginVariant($cloud_server_template->getCloudContext());
    if ($plugin === FALSE) {
      $message = $this->t(
        'Cannot load cloud server template plugin: %cloud_context', [
          '%cloud_context' => $cloud_server_template->getCloudContext(),
        ]
      );
      $this->messenger->addMessage($message);

      return [
        'route_name' => 'entity.cloud_server_template.canonical',
        'params' => [
          'cloud_server_template' => $cloud_server_template->id(),
          'cloud_context' => $cloud_server_template->getCloudContext(),
        ],
      ];
    }

    return $plugin->launch($cloud_server_template, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function buildListHeader($cloud_context) {
    $plugin = $this->loadPluginVariant($cloud_context);
    if ($plugin === FALSE) {
      $message = $this->t(
        'Cannot load cloud server template plugin: %cloud_context', [
          '%cloud_context' => $cloud_context,
        ]
      );
      $this->messenger->addMessage($message);
      return [];
    }

    return $plugin->buildListHeader();
  }

  /**
   * {@inheritdoc}
   */
  public function buildListRow(CloudServerTemplateInterface $entity) {
    $plugin = $this->loadPluginVariant($entity->getCloudContext());
    if ($plugin === FALSE) {
      $message = $this->t(
        'Cannot load cloud server template plugin: %cloud_context', [
          '%cloud_context' => $entity->getCloudContext(),
        ]
      );
      $this->messenger->addMessage($message);
      return [];
    }

    return $plugin->buildListRow($entity);
  }

}

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

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