cloud-8.x-2.0-beta1/modules/cloud_service_providers/openstack/src/Form/InstanceEditForm.php

modules/cloud_service_providers/openstack/src/Form/InstanceEditForm.php
<?php

namespace Drupal\openstack\Form;

use Drupal\aws_cloud\Form\Ec2\AwsCloudContentForm;
use Drupal\Core\Form\FormStateInterface;

/**
 * Form controller for OpenStack Cloud Instance edit forms.
 *
 * @ingroup openstack
 */
class InstanceEditForm extends AwsCloudContentForm {

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $cloud_context = '') {
    /* @var \Drupal\openstack\Entity\Instance $entity */
    $form = parent::buildForm($form, $form_state);
    $entity = $this->entity;

    $weight = -50;

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

    $form['instance']['name'] = [
      '#type'          => 'item',
      '#title'         => $this->t('Name'),
      '#markup' => $entity->label(),
    ];

    $form['instance']['instance_id'] = [
      '#type'          => 'item',
      '#title'         => $this->getItemTitle($this->t('Instance ID')),
      '#markup'        => $entity->getInstanceId(),
    ];

    $form['instance']['instance_state'] = [
      '#type'          => 'item',
      '#title'         => $this->getItemTitle($this->t('Instance State')),
      '#markup'        => $entity->getInstanceState(),
    ];

    $form['instance']['instance_type'] = [
      '#type'          => 'item',
      '#title'         => $this->t('Instance Type'),
      '#markup' => $entity->getInstanceType(),
    ];

    $form['instance']['image_name'] = [
      '#type'          => 'item',
      '#title'         => $this->getItemTitle($this->t('AMI Image Name')),
      '#markup'        => $entity->getImageName(),
    ];

    $form['instance']['reservation'] = [
      '#type'          => 'item',
      '#title'         => $this->getItemTitle($this->t('Reservation')),
      '#markup'        => $entity->getReservation(),
    ];

    $form['instance']['launch_time'] = [
      '#type'          => 'item',
      '#title'         => $this->getItemTitle($this->t('Launch Time')),
      '#markup'        => $this->dateFormatter->format($entity->getLaunchTime(), 'short'),
    ];

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

    $form['network']['public_dns'] = [
      '#type'          => 'item',
      '#title'         => $this->getItemTitle($this->t('Public DNS')),
      '#markup'        => $entity->getPublicDns(),
    ];

    $form['network']['private_dns'] = [
      '#type'          => 'item',
      '#title'         => $this->getItemTitle($this->t('Private DNS')),
      '#markup'        => $entity->getPrivateDns(),
    ];

    $form['network']['private_ips'] = [
      '#type'          => 'item',
      '#title'         => $this->getItemTitle($this->t('Private IPs')),
      '#markup'        => $entity->getPrivateIps(),
    ];

    $form['network']['security_groups'] = [
      '#type'          => 'item',
      '#title'         => $this->t('Security Groups'),
      '#markup'        => $this->formatSecurityGroups($entity->getSecurityGroups()),
    ];

    $form['network']['key_pair_name'] = $this->entityLinkRenderer->renderFormElements(
      $entity->getKeyPairName(),
      'aws_cloud_key_pair',
      'key_pair_name',
      ['#title' => $this->getItemTitle($this->t('Key Pair Name'))]
    );

    $form['network']['availability_zone'] = [
      '#type'          => 'item',
      '#title'         => $this->getItemTitle($this->t('Availability Zone')),
      '#markup'        => $entity->getAvailabilityZone(),
    ];

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

    $form['storage']['root_device_type'] = [
      '#type'          => 'item',
      '#title'         => $this->getItemTitle($this->t('Root Device Type')),
      '#markup'        => $entity->getRootDeviceType(),
    ];

    $form['storage']['root_device'] = [
      '#type'          => 'item',
      '#title'         => $this->getItemTitle($this->t('Root Device')),
      '#markup'        => $entity->getRootDevice(),
    ];

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

    $form['options']['ami_launch_index'] = [
      '#type'          => 'item',
      '#title'         => $this->getItemTitle($this->t('AMI Launch Index')),
      '#markup'        => $entity->getAmiLaunchIndex(),
    ];

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

    unset($form['name']);
    return $form;
  }

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

    $status = parent::save($form, $form_state);

    switch ($status) {
      case SAVED_NEW:
        $this->messenger()->addMessage($this->t('Created the %label OpenStack Cloud Instance.', [
          '%label' => $entity->label(),
        ]));
        break;

      default:
        $this->messenger()->addMessage($this->t('Saved the %label OpenStack Cloud Instance.', [
          '%label' => $entity->label(),
        ]));
    }
    $form_state->setRedirect('entity.openstack_cloud_instance.canonical', ['openstack_cloud_instance' => $entity->id()]);
  }

  /**
   * Helper function to format the security group for display.
   *
   * @param array $groups
   *   Array of security groups.
   *
   * @return string
   *   Imploded string of security groups.
   */
  private function formatSecurityGroups(array $groups) {
    $output = [];
    foreach ($groups as $group) {
      $output[] = $group['value'];
    }
    return implode(',', $output);
  }

}

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

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