cloud-8.x-2.0-beta1/modules/cloud_service_providers/aws_cloud/src/Form/Ec2/ElasticIpCreateForm.php

modules/cloud_service_providers/aws_cloud/src/Form/Ec2/ElasticIpCreateForm.php
<?php

namespace Drupal\aws_cloud\Form\Ec2;

use Drupal\Core\Form\FormStateInterface;
use Drupal\aws_cloud\Entity\Ec2\ElasticIp;

/**
 * Form controller for the ElasticIp entity create form.
 *
 * @ingroup aws_cloud
 */
class ElasticIpCreateForm extends AwsCloudContentForm {

  /**
   * Overrides Drupal\Core\Entity\EntityFormController::buildForm().
   */
  public function buildForm(array $form, FormStateInterface $form_state, $cloud_context = '') {
    /* @var $entity \Drupal\aws_cloud\Entity\Ec2\ElasticIp */
    $form = parent::buildForm($form, $form_state);

    $this->ec2Service->setCloudContext($cloud_context);

    $entity = $this->entity;

    $weight = -50;

    $form['elastic_ip'] = [
      '#type' => 'details',
      '#title' => $this->t('AWS Cloud Elastic IP'),
      '#open' => TRUE,
      '#weight' => $weight++,
    ];

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

    $form['elastic_ip']['domain'] = [
      '#type'          => 'select',
      '#options'       => [
        'standard' => 'standard',
        'vpc' => 'vpc',
      ],
      '#title'         => $this->t('Domain (standard | vpc)'),
      '#default_value' => 'standard',
      '#required'      => TRUE,
    ];

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

    return $form;
  }

  /**
   * Overrides Drupal\Core\Entity\EntityFormController::save().
   */
  public function save(array $form, FormStateInterface $form_state) {
    $this->trimTextfields($form, $form_state);

    $entity = $this->entity;

    $result = $this->ec2Service->allocateAddress([
      'Domain' => $entity->getDomain(),
    ]);

    if (isset($result['PublicIp'])
    && ($entity->setPublicIp($result['PublicIp']))
    && ($entity->setAllocationId($result['AllocationId']))
    && ($entity->setDomain($result['Domain']))
    && ($entity->save())) {

      // Update the tags.
      $this->setTagsInAws($entity->getAllocationId(), [
        ElasticIp::TAG_CREATED_BY_UID => $entity->getOwner()->id(),
        'Name' => $entity->getName(),
      ]);

      $message = $this->t('The @label "%label (@elastic_ip)" has been created.', [
        '@label'      => $entity->getEntityType()->getLabel(),
        '%label'      => $entity->label(),
        '@elastic_ip' => $result['PublicIp'],
      ]);
      $this->messenger->addMessage($message);
    }
    else {
      $message = $this->t('The @label "%label" couldn\'t create.', [
        '@label' => $entity->getEntityType()->getLabel(),
        '%label' => $entity->label(),
      ]);
      $this->messenger->addError($message);
    }

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

}

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

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