cloud-8.x-2.0-beta1/modules/cloud_service_providers/k8s/src/Form/K8sEditForm.php
modules/cloud_service_providers/k8s/src/Form/K8sEditForm.php
<?php
namespace Drupal\k8s\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Serialization\Yaml;
use Drupal\k8s\Service\K8sServiceException;
use Drupal\k8s\Traits\K8sFormTrait;
use Drupal\k8s\Entity\K8sEntityBase;
/**
* Form controller for the entity edit forms.
*
* @ingroup k8s
*/
class K8sEditForm extends K8sContentForm {
use K8sFormTrait;
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $cloud_context = '') {
$form = parent::buildForm($form, $form_state);
$entity = $this->entity;
$name_underscore = $this->getShortEntityTypeNameUnderscore($entity);
$name_whitespace = $this->getShortEntityTypeNameWhitespace($entity);
$weight = -50;
$form[$name_underscore] = [
'#type' => 'details',
'#title' => $name_whitespace,
'#open' => TRUE,
'#weight' => $weight++,
];
$form[$name_underscore]['name'] = [
'#type' => 'item',
'#title' => $this->getItemTitle($this->t('Name')),
'#markup' => $entity->getName(),
'#weight' => $weight++,
];
$form[$name_underscore]['namespace'] = [
'#type' => 'item',
'#title' => $this->getItemTitle($this->t('Namespace')),
'#markup' => $entity->getNamespace(),
'#weight' => $weight++,
];
$form[$name_underscore]['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;
$name_underscore = $this->getShortEntityTypeNameUnderscore($entity);
$name_camel = $this->getShortEntityTypeNameCamel($entity);
$this->k8sService->setCloudContext($entity->getCloudContext());
try {
$method_name = "update{$name_camel}";
$params = Yaml::decode($entity->getDetail());
if (!empty($entity->getOwner())) {
// Add owner uid to annotations.
$params['metadata']['annotations'][K8sEntityBase::ANNOTATION_CREATED_BY_UID] = $entity->getOwner()->id();
}
$this->k8sService->$method_name(
$entity->getNamespace(),
$params
);
$entity->save();
// Update the entity.
$method_name = 'update' .
$method_name = (preg_match('/y$/', $name_camel))
? preg_replace('/y$/', 'ies', $name_camel)
: ((preg_match('/s$|x$|ch$|sh$/', $name_camel))
? $name_camel . 'es'
: $name_camel . 's');
$this->k8sService->$method_name([
'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_{$name_underscore}.list", [
'cloud_context' => $entity->getCloudContext(),
]);
}
}
