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

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

namespace Drupal\aws_cloud\Form\Ec2;

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

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

    $weight = -50;

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

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

    $volume_id = $this->getRequest()->query->get('volume_id');

    $form['snapshot']['volume_id'] = [
      '#type'          => 'select',
      '#title'         => $this->t('Volume ID'),
      '#options'       => $this->getVolumeOptions($cloud_context),
      '#default_value' => $volume_id,
      '#required'      => TRUE,
    ];

    $form['snapshot']['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;
  }

  /**
   * 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->createSnapshot([
      'VolumeId'    => $entity->getVolumeId(),
      'Description' => $entity->getDescription(),
    ]);

    if (isset($result['SnapshotId'])
    && ($entity->setSnapshotId($result['SnapshotId']))
    && ($entity->setStatus($result['State']))
    && ($entity->setStarted(strtotime($result['StartTime'])))
    && ($entity->setEncrypted($result['Encrypted']))
    && ($entity->save())) {

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

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

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

  }

  /**
   * Helper function to get volume options.
   *
   * @param string $cloud_context
   *   Cloud context to use in the query.
   *
   * @return array
   *   Volume options.
   */
  private function getVolumeOptions($cloud_context) {
    $options = [];
    $params = [
      'cloud_context' => $cloud_context,
    ];
    if (!$this->currentUser->hasPermission('view any aws cloud volume')) {
      $params['uid'] = $this->currentUser->id();
    }

    $volumes = $this->entityTypeManager
      ->getStorage('aws_cloud_volume')
      ->loadByProperties($params);
    foreach ($volumes as $volume) {
      if ($volume->getName() != $volume->getVolumeId()) {
        $options[$volume->getVolumeId()] = "{$volume->getName()} ({$volume->getVolumeId()})";
      }
      else {
        $options[$volume->getVolumeId()] = $volume->getVolumeId();
      }
    }
    return $options;
  }

}

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

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