cloud-8.x-2.0-beta1/modules/cloud_service_providers/k8s/src/Form/K8sNetworkPolicyCreateForm.php

modules/cloud_service_providers/k8s/src/Form/K8sNetworkPolicyCreateForm.php
<?php

namespace Drupal\k8s\Form;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Serialization\Yaml;
use Drupal\k8s\Service\K8sServiceException;

/**
 * Form controller for the Network Policy entity create form.
 *
 * @ingroup k8s
 */
class K8sNetworkPolicyCreateForm extends K8sContentForm {

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $cloud_context = '') {
    $form = parent::buildForm($form, $form_state);
    $this->k8sService->setCloudContext($cloud_context);

    $weight = -50;

    $form['network_policy'] = [
      '#type' => 'details',
      '#title' => $this->t('network_policy'),
      '#open' => TRUE,
      '#weight' => $weight++,
    ];

    $form['network_policy']['namespace'] = [
      '#type'          => 'select',
      '#title'         => $this->t('Namespace'),
      '#options'       => $this->getNamespaceOptions(),
      '#default_value' => 'default',
      '#required'      => TRUE,
    ];

    $form['network_policy']['detail'] = $form['detail'];
    unset($form['detail']);

    $this->addOthersFieldset($form, $weight++, $cloud_context);

    $form['actions'] = $this->actions($form, $form_state, $cloud_context);

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $this->trimTextfields($form, $form_state);

    $entity = $this->entity;
    $cloud_context = $this->routeMatch->getParameter('cloud_context');
    $this->k8sService->setCloudContext($cloud_context);

    $params = Yaml::decode($entity->getDetail());
    try {
      $result = $this->k8sService->createNetworkPolicy($entity->getNamespace(), $params);
      $entity->setName($result['metadata']['name']);
      $entity->setCreationYaml($entity->getDetail());
      $entity->save();

      // Update the network policy.
      $this->k8sService->updateNetworkPolicies([
        'metadata.name' => $entity->getName(),
      ], FALSE);

      $message = $this->t('The @label "%label" has been created.', [
        '@label'       => $entity->getEntityType()->getLabel(),
        '%label'       => $entity->label(),
      ]);

      $this->messenger->addMessage($message);
      $form_state->setRedirect('view.k8s_network_policy.list', ['cloud_context' => $entity->getCloudContext()]);
    }
    catch (K8sServiceException $e) {
      $message = $this->t('The @label "%label" couldn\'t create.', [
        '@label' => $entity->getEntityType()->getLabel(),
        '%label' => $entity->label(),
      ]);
      $this->messenger->addError($message);
    }
  }

}

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

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