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

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

namespace Drupal\aws_cloud\Form\Ec2;

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

/**
 * Form controller for the Image entity create form.
 *
 * @ingroup aws_cloud
 */
class ImageCreateForm 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\Image */
    $form = parent::buildForm($form, $form_state);

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

    $entity = $this->entity;

    $weight = -50;

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

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

    $form['image']['instance_id'] = [
      '#type'          => 'textfield',
      '#title'         => $this->t('Instance ID'),
      '#maxlength'     => 60,
      '#size'          => 60,
      '#default_value' => $entity->getInstanceId(),
      '#required'      => TRUE,
    ];

    $form['image']['description'] = [
      '#type'          => 'textfield',
      '#title'         => $this->t('Description'),
      '#maxlength'     => 255,
      '#size'          => 60,
      '#default_value' => $entity->getDescription(),
      '#required'      => FALSE,
    ];

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

    return $form;
  }

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

    /* @var \Drupal\aws_cloud\Entity\Ec2\Image $entity */
    $entity = $this->entity;

    $result = $this->ec2Service->createImage([
      'InstanceId'  => $entity->getInstanceId(),
      'Name'        => $entity->getName(),
      'Description' => $entity->getDescription(),
    ]);

    $account_id = $this->cloudConfigPluginManager->loadConfigEntity()->get('field_account_id')->value;
    if (isset($result['ImageId'])
      && ($entity->setName($form_state->getValue('name')))
      && ($entity->set('ami_name', $form_state->getValue('name')))
      && ($entity->setImageId($result['ImageId']))
      && ($entity->set('account_id', $account_id))
      && ($entity->save())) {

      $this->setTagsInAws($entity->getImageId(), [
        Image::TAG_CREATED_BY_UID => $entity->getOwner()->id(),
        'Name' => $entity->getName(),
      ]);

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

      $this->messenger->addMessage($message);
      $form_state->setRedirect('view.aws_cloud_image.list', ['cloud_context' => $entity->getCloudContext()]);
    }
    else {
      $message = $this->t('The @label "%label" couldn\'t create.', [
        '@label' => $entity->getEntityType()->getLabel(),
        '%label' => $entity->getName(),
      ]);
      $this->messenger->addError($message);
    }

  }

}

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

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