Skip to content
Snippets Groups Projects
Commit 8ff6b482 authored by Yas Naoi's avatar Yas Naoi Committed by Yas Naoi
Browse files

Issue #3050106 by Xiaohua Guan, yas, baldwinlouie: Trim text fields in EC2 entity forms

parent 6ca4e575
No related branches found
Tags 8.x-1.1-beta1
No related merge requests found
Showing
with 112 additions and 42 deletions
......@@ -143,11 +143,16 @@ class AwsCloudContentForm extends CloudContentForm {
// will be saved as NULL.
$this->copyFormItemValues($form);
$this->trimTextfields($form, $form_state);
parent::save($form, $form_state);
}
/**
* Copy values from #type=item elements to its original element type.
*
* @param array $form
* The form array.
*/
protected function copyFormItemValues(array $form) {
$original_entity = $this->manager
......@@ -160,13 +165,20 @@ class AwsCloudContentForm extends CloudContentForm {
continue;
}
if (isset($item['#type']) && $item['#type'] == 'item') {
if (isset($item['#type'])
&& $item['#type'] == 'item'
&& (!isset($item['#not_field']) || $item['#not_field'] === FALSE)
) {
$item_field_names[] = $name;
}
if (isset($item['#type']) && $item['#type'] == 'details') {
foreach ($item as $sub_item_name => $sub_item) {
if (is_array($sub_item) && isset($sub_item['#type']) && $sub_item['#type'] == 'item') {
if (is_array($sub_item)
&& isset($sub_item['#type'])
&& $sub_item['#type'] == 'item'
&& (!isset($sub_item['#not_field']) || $sub_item['#not_field'] === FALSE)
) {
$item_field_names[] = $sub_item_name;
}
}
......@@ -192,6 +204,51 @@ class AwsCloudContentForm extends CloudContentForm {
}
}
/**
* Trim white spaces in the values of textfields.
*
* @param array $form
* The form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state object.
*/
protected function trimTextfields(array $form, FormStateInterface $form_state) {
$field_names = [];
foreach ($form as $name => $item) {
if (!is_array($item)) {
continue;
}
if (isset($item['#type'])
&& $item['#type'] == 'textfield'
) {
$field_names[] = $name;
}
if (isset($item['#type']) && $item['#type'] == 'details') {
foreach ($item as $sub_item_name => $sub_item) {
if (is_array($sub_item)
&& isset($sub_item['#type'])
&& $sub_item['#type'] == 'textfield'
) {
$field_names[] = $sub_item_name;
}
}
}
}
foreach ($field_names as $field_name) {
$value = $form_state->getValue($field_name);
if ($value === NULL) {
continue;
}
$value = trim($value);
$form_state->setValue($field_name, $value);
$this->entity->set($field_name, $value);
}
}
/**
* Helper method to clear cache values.
*/
......
......@@ -66,6 +66,7 @@ class ElasticIpCreateForm extends AwsCloudContentForm {
* Overrides Drupal\Core\Entity\EntityFormController::save().
*/
public function save(array $form, FormStateInterface $form_state) {
$this->trimTextfields($form, $form_state);
$entity = $this->entity;
......
......@@ -75,6 +75,7 @@ class ImageCreateForm extends AwsCloudContentForm {
* {@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;
......
......@@ -151,6 +151,7 @@ class InstanceEditForm extends AwsCloudContentForm {
'#type' => 'item',
'#title' => $this->getItemTitle($this->t('Elastic IP')),
'#markup' => $link,
'#not_field' => TRUE,
];
}
elseif (count($this->getNetworkInterfaceCount()) > 1) {
......
<?php
// Updated by yas 2016/06/03
// Updated by yas 2016/05/31
// Updated by yas 2016/05/30
// Updated by yas 2016/05/25
// Updated by yas 2016/05/20
// Created by yas 2016/05/19.
namespace Drupal\aws_cloud\Form\Ec2;
use Drupal\Core\Form\FormStateInterface;
......@@ -68,6 +62,7 @@ class KeyPairCreateForm extends AwsCloudContentForm {
* Overrides Drupal\Core\Entity\EntityFormController::save().
*/
public function save(array $form, FormStateInterface $form_state) {
$this->trimTextfields($form, $form_state);
/* @var \Drupal\aws_cloud\Entity\Ec2\KeyPair $entity */
$entity = $this->entity;
......
<?php
// Updated by yas 2016/06/04
// Updated by yas 2016/05/31
// Updated by yas 2016/05/30
// Updated by yas 2016/05/25
// Updated by yas 2016/05/20
// Created by yas 2016/05/19.
namespace Drupal\aws_cloud\Form\Ec2;
use Drupal\Core\Form\FormStateInterface;
......@@ -122,6 +116,7 @@ class NetworkInterfaceCreateForm extends AwsCloudContentForm {
* Overrides Drupal\Core\Entity\EntityFormController::save().
*/
public function save(array $form, FormStateInterface $form_state) {
$this->trimTextfields($form, $form_state);
$entity = $this->entity;
......
<?php
// Updated by yas 2016/09/11
// Updated by yas 2016/06/04
// Updated by yas 2016/06/03
// Updated by yas 2016/05/31
// Updated by yas 2016/05/30
// Updated by yas 2016/05/25
// Updated by yas 2016/05/20
// Created by yas 2016/05/19.
namespace Drupal\aws_cloud\Form\Ec2;
use Drupal\Core\Form\FormStateInterface;
......@@ -97,6 +89,7 @@ class SecurityGroupCreateForm extends AwsCloudContentForm {
* Overrides Drupal\Core\Entity\EntityFormController::save().
*/
public function save(array $form, FormStateInterface $form_state) {
$this->trimTextfields($form, $form_state);
$entity = $this->entity;
......
......@@ -2,8 +2,11 @@
namespace Drupal\aws_cloud\Form\Ec2;
use Drupal\aws_cloud\Entity\Ec2\SecurityGroup;
use Drupal\Core\Form\FormStateInterface;
use Drupal\aws_cloud\Entity\Ec2\SecurityGroup;
use Drupal\aws_cloud\Plugin\Field\FieldType\IpPermission;
use Aws\Result;
/**
* Form controller for the CloudScripting entity edit forms.
......@@ -100,6 +103,8 @@ class SecurityGroupEditForm extends AwsCloudContentForm {
// Call copyFormItemValues() to ensure the form array is intact.
$this->copyFormItemValues($form);
$this->trimTextfields($form, $form_state);
$entity = $this->entity;
$this->awsEc2Service->setCloudContext($entity->getCloudContext());
......@@ -139,9 +144,11 @@ class SecurityGroupEditForm extends AwsCloudContentForm {
/**
* Helper method to update the inbound permissions.
* @param $existing_group
*
* @param \Aws\Result $existing_group
* Existing group.
*/
private function updateInboundPermissions($existing_group) {
private function updateInboundPermissions(Result $existing_group) {
$permissions = [];
if (isset($existing_group['SecurityGroups']) && isset($existing_group['SecurityGroups'][0]['IpPermissions'])) {
......@@ -174,8 +181,11 @@ class SecurityGroupEditForm extends AwsCloudContentForm {
/**
* Helper method to update the outbound permissions.
*
* @param \Aws\Result $existing_group
* Existing group.
*/
private function updateOutboundPermissions($existing_group) {
private function updateOutboundPermissions(Result $existing_group) {
$permissions = [];
if (isset($existing_group['SecurityGroups']) && isset($existing_group['SecurityGroups'][0]['IpPermissionsEgress'])) {
$security_group = $this->formatIpPermissionForRevoke($existing_group['SecurityGroups'][0]['IpPermissionsEgress']);
......@@ -204,12 +214,18 @@ class SecurityGroupEditForm extends AwsCloudContentForm {
}
/**
* Format the IpPermission object returned from the DescribeSecurityGroup
* Format the IpPermission object.
*
* Format returned from the DescribeSecurityGroup
* EC2 api call. This method unset array objects that have no values.
* @param $security_group
* @return array Formatted IpPermission object.
*
* @param array $security_group
* The security group.
*
* @return array
* Formatted IpPermission object.
*/
private function formatIpPermissionForRevoke($security_group) {
private function formatIpPermissionForRevoke(array $security_group) {
foreach ($security_group as $key => $group) {
if (!isset($group['IpRanges']) || count($group['IpRanges']) == 0) {
unset($security_group[$key]['IpRanges']);
......@@ -224,7 +240,7 @@ class SecurityGroupEditForm extends AwsCloudContentForm {
unset($security_group[$key]['PrefixListIds']);
}
if (isset($group['UserIdGroupPairs']) && count($group['UserIdGroupPairs']) > 0) {
// loop them and unset GroupName
// Loop them and unset GroupName.
foreach ($group['UserIdGroupPairs'] as $pair_keys => $pairs) {
unset($security_group[$key]['UserIdGroupPairs'][$pair_keys]['GroupName']);
}
......@@ -234,12 +250,18 @@ class SecurityGroupEditForm extends AwsCloudContentForm {
}
/**
* Format the IpPermission object for use with the AuthorizeSecurityGroup[Ingress and Egress]
* EC2 api call
* @param $ip_permission
* Format the IpPermission object.
*
* Format the IpPermission object for use with
* the AuthorizeSecurityGroup[Ingress and Egress] EC2 api call.
*
* @param \Drupal\aws_cloud\Plugin\Field\FieldType\IpPermission $ip_permission
* The ip permission object.
*
* @return array
* The permission.
*/
private function formatIpPermissionForAuthorize($ip_permission) {
private function formatIpPermissionForAuthorize(IpPermission $ip_permission) {
$permission = [
'FromPort' => (int) $ip_permission->from_port,
'ToPort' => (int) $ip_permission->to_port,
......@@ -274,14 +296,17 @@ class SecurityGroupEditForm extends AwsCloudContentForm {
}
/**
* Verify the authorize call was successful. Since Ec2 does not return
* any error codes from any of the authorize* api calls, the only way to
* verify is to count the permissions array from the current entity,
* and the entity that is newly updated from the updateSecurityGroups API call.
* @param $group
* @return bool
* Verify the authorize call was successful.
*
* Since Ec2 does not return any error codes from any of the authorize
* api calls, the only way to verify is to count the permissions array
* from the current entity, and the entity that is newly updated from
* the updateSecurityGroups API call.
*
* @param \Drupal\aws_cloud\Entity\Ec2\SecurityGroup $group
* The security group.
*/
private function validateAuthorize($group) {
private function validateAuthorize(SecurityGroup $group) {
/* @var \Drupal\aws_cloud\Entity\Ec2\SecurityGroup $updated_group */
$updated_group = SecurityGroup::load($group->id());
......
......@@ -82,6 +82,7 @@ class SnapshotCreateForm extends AwsCloudContentForm {
* Overrides Drupal\Core\Entity\EntityFormController::save().
*/
public function save(array $form, FormStateInterface $form_state) {
$this->trimTextfields($form, $form_state);
$entity = $this->entity;
......
......@@ -179,6 +179,7 @@ class VolumeCreateForm extends AwsCloudContentForm {
* Overrides Drupal\Core\Entity\EntityFormController::save().
*/
public function save(array $form, FormStateInterface $form_state) {
$this->trimTextfields($form, $form_state);
$entity = $this->entity;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment