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

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

namespace Drupal\k8s\Form;

use Drupal\Core\Form\FormStateInterface;
use Drupal\k8s\Service\K8sServiceException;

/**
 * Form controller for the Namespace entity create form.
 *
 * @ingroup k8s
 */
class K8sNamespaceCreateForm 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['namespace'] = [
      '#type' => 'details',
      '#title' => $this->t('Namespace'),
      '#open' => TRUE,
      '#weight' => $weight++,
    ];

    $form['namespace']['name'] = [
      '#type'          => 'textfield',
      '#title'         => $this->t('Name'),
      '#maxlength'     => 255,
      '#size'          => 60,
      '#required'      => TRUE,
    ];

    $form['namespace']['labels'] = $form['labels'];

    unset($form['labels']);

    $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);

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

    $entity = $this->entity;
    $entity->setCloudContext($cloud_context);

    // Get labels.
    $labels = [];
    foreach ($entity->getLabels() as $label) {
      $labels[$label['item_key']] = $label['item_value'];
    }

    $this->k8sService->setCloudContext($cloud_context);
    $params = [];
    $params['metadata']['name'] = $entity->getName();
    if (!empty($labels)) {
      $params['metadata']['labels'] = $labels;
    }

    try {
      $result = $this->k8sService->createNamespace($params);
      $entity->setStatus($result['status']['phase']);
      $entity->setCreated(strtotime($result['metadata']['creationTimestamp']));
      $entity->save();
      $message = $this->t('The @label "%label" has been created.', [
        '@label'       => $entity->getEntityType()->getLabel(),
        '%label'       => $entity->label(),
      ]);

      $this->messenger->addMessage($message);

      k8s_clear_cache();

      $form_state->setRedirect('view.k8s_namespace.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