Commit 4a2477fd authored by xiaohua guan's avatar xiaohua guan Committed by Yas Naoi
Browse files

Issue #3356456 by Xiaohua Guan, yas: Add "approve" feature to OpenStack quota (1) (Add new fields)

parent 2fffb47e
Loading
Loading
Loading
Loading
+47 −0
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ use Drupal\Core\Database\Database;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Component\Serialization\Yaml;
use Drupal\openstack\Entity\OpenStackQuota;

/**
 * Implements hook_install().
@@ -2828,3 +2829,49 @@ function openstack_update_8321() {

  drupal_flush_all_caches();
}

/**
 * Add status and *_new fields to openstack quota.
 */
function openstack_update_8322() {

  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $entity_type = 'openstack_quota';

  $field_manager = \Drupal::service('entity_field.manager');
  $definitions = $field_manager->getBaseFieldDefinitions($entity_type);

  // Add *_new field for the editable field.
  foreach (OpenStackQuota::getEditableFieldSets() as $field_set) {
    foreach ($field_set['fields'] as $field_name) {
      if (empty($definitions[$field_name])) {
        continue;
      }

      // Copy field.
      $fields["{$field_name}_new"] = BaseFieldDefinition::createFromFieldStorageDefinition($definitions[$field_name]);
    }
  }

  $fields['status'] = BaseFieldDefinition::create('string')
    ->setRequired(TRUE)
    ->setLabel(t('Status'))
    ->setDescription(t('The status of the application.'))
    ->setDisplayOptions('view', [
      'label' => 'inline',
      'type' => 'string',
      'weight' => -5,
    ])
    ->setReadOnly(TRUE);

  foreach ($fields ?: [] as $name => $definition) {
    $definition_update_manager->installFieldStorageDefinition(
      $name,
      $entity_type,
      'openstack',
      $definition
    );
  }

  drupal_flush_all_caches();
}
+75 −0
Original line number Diff line number Diff line
@@ -652,6 +652,55 @@ class OpenStackQuota extends CloudContentEntityBase implements OpenStackQuotaInt
    return $this->set('refreshed', $time);
  }

  /**
   * Get editable field sets.
   *
   * @return array
   *   Editable field sets.
   */
  public static function getEditableFieldSets(): array {
    return [
      [
        'name' => 'quota_compute',
        'title' => t('Compute'),
        'fields' => [
          'instances',
          'cores',
          'ram',
          'metadata_items',
          'key_pairs',
          'server_groups',
          'server_group_members',
          'injected_files',
          'injected_file_content_bytes',
          'injected_file_path_bytes',
        ],
      ],
      [
        'name' => 'quota_volume',
        'title' => t('Volume'),
        'fields' => [
          'volumes',
          'snapshots',
          'gigabytes',
        ],
      ],
      [
        'name' => 'quota_network',
        'title' => t('Network'),
        'fields' => [
          'network',
          'subnet',
          'port',
          'router',
          'floatingip',
          'security_group',
          'security_group_rule',
        ],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
@@ -1239,6 +1288,32 @@ class OpenStackQuota extends CloudContentEntityBase implements OpenStackQuotaInt
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

    // Add *_new field for the editable field.
    foreach (self::getEditableFieldSets() as $field_set) {
      foreach ($field_set['fields'] as $field_name) {
        $definition = $fields[$field_name];
        $definition->setName("{$field_name}_new");

        // Create a new field.
        $new_definition = BaseFieldDefinition::createFromFieldStorageDefinition($definition);
        $fields["{$field_name}_new"] = $new_definition;

        // Restore the name of definition.
        $definition->setName($field_name);
      }
    }

    $fields['status'] = BaseFieldDefinition::create('string')
      ->setRequired(TRUE)
      ->setLabel(t('Status'))
      ->setDescription(t('The status of the application.'))
      ->setDisplayOptions('view', [
        'label' => 'inline',
        'type' => 'string',
        'weight' => -5,
      ])
      ->setReadOnly(TRUE);

    return $fields;
  }

+6 −43
Original line number Diff line number Diff line
@@ -2,7 +2,6 @@

namespace Drupal\openstack\Form;

use Drupal\openstack\Service\OpenStackServiceFactoryInterface;
use Drupal\aws_cloud\Form\Ec2\AwsCloudContentForm;
use Drupal\aws_cloud\Service\AwsCloud\AwsCloudOperationsServiceInterface;
use Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface;
@@ -23,8 +22,10 @@ use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\cloud\Service\CloudServiceInterface;
use Drupal\cloud\Traits\CloudContentEntityTrait;
use Drupal\openstack\Entity\OpenStackQuota;
use Drupal\openstack\Traits\OpenStackFormSelectTrait;
use Drupal\openstack\Service\OpenStackOperationsServiceInterface;
use Drupal\openstack\Service\OpenStackServiceFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
@@ -194,48 +195,7 @@ class OpenStackQuotaEditForm extends AwsCloudContentForm {
      '#markup'        => $entity->getName(),
    ];

    $field_sets = [
      [
        'name' => 'quota_compute',
        'title' => $this->t('Compute'),
        'fields' => [
          'instances',
          'cores',
          'ram',
          'metadata_items',
          'key_pairs',
          'server_groups',
          'server_group_members',
          'injected_files',
          'injected_file_content_bytes',
          'injected_file_path_bytes',
        ],
      ],
      [
        'name' => 'quota_volume',
        'title' => $this->t('Volume'),
        'fields' => [
          'volumes',
          'snapshots',
          'gigabytes',
        ],
      ],
      [
        'name' => 'quota_network',
        'title' => $this->t('Network'),
        'fields' => [
          'network',
          'subnet',
          'port',
          'router',
          'floatingip',
          'security_group',
          'security_group_rule',
        ],
      ],
    ];

    foreach ($field_sets as $field_set) {
    foreach (OpenStackQuota::getEditableFieldSets() as $field_set) {
      $form[$field_set['name']] = [
        '#type' => 'details',
        '#title' => $field_set['title'],
@@ -246,6 +206,9 @@ class OpenStackQuotaEditForm extends AwsCloudContentForm {
      foreach ($field_set['fields'] as $field) {
        $form[$field_set['name']][$field] = $form[$field];
        unset($form[$field]);

        // Hide *_new fields.
        unset($form["{$field}_new"]);
      }
    }