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

modules/cloud_service_providers/k8s/src/Form/K8sNetworkPolicyEditForm.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 Namespace entity edit forms.
 *
 * @ingroup k8s
 */
class K8sNetworkPolicyEditForm extends K8sContentForm {

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

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

    $form['network_policy']['name'] = [
      '#type'          => 'item',
      '#title'         => $this->getItemTitle($this->t('Name')),
      '#markup'        => $entity->getName(),
      '#weight'        => $weight++,
    ];

    $form['network_policy']['namespace'] = [
      '#type'          => 'item',
      '#title'         => $this->getItemTitle($this->t('Namespace')),
      '#markup'        => $entity->getNamespace(),
      '#weight'        => $weight++,
    ];

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

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

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

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    // Call copyFormItemValues() to ensure the form array is intact.
    $this->copyFormItemValues($form);

    $this->trimTextfields($form, $form_state);

    $entity = $this->entity;

    $this->k8sService->setCloudContext($entity->getCloudContext());
    try {
      $this->k8sService->updateNetworkPolicy(
        $entity->getNamespace(),
        Yaml::decode($entity->getDetail())
      );

      $entity->save();

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

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

      $this->messenger->addMessage($message);
    }
    catch (K8sServiceException $e) {
      $this->messenger->addError($this->t('Unable to update @label.', [
        '@label' => $entity->getEntityType()->getLabel(),
      ]));
    }

    $form_state->setRedirect('view.k8s_network_policy.list', [
      'cloud_context' => $entity->getCloudContext(),
    ]);
  }

}

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

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