Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
http_client_manager-3451503
Manage
Activity
Members
Labels
Plan
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Issue forks
http_client_manager-3451503
Commits
63729ee9
Commit
63729ee9
authored
6 years ago
by
Adriano Cori
Committed by
Adriano Cori
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3021186
by aronne: Support of nested type
parent
75fe84fc
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Form/HttpConfigRequestForm.php
+153
-29
153 additions, 29 deletions
src/Form/HttpConfigRequestForm.php
with
153 additions
and
29 deletions
src/Form/HttpConfigRequestForm.php
+
153
−
29
View file @
63729ee9
...
@@ -6,7 +6,7 @@ use Drupal\Core\Entity\EntityForm;
...
@@ -6,7 +6,7 @@ use Drupal\Core\Entity\EntityForm;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\http_client_manager
\HttpClientManagerFactoryInterface
;
use
Drupal\http_client_manager
\HttpClientManagerFactoryInterface
;
use
Drupal\http_client_manager
\HttpServiceApiHandlerInterface
;
use
Drupal\http_client_manager
\HttpServiceApiHandlerInterface
;
use
Guzzle
\Service\Description
\Parameter
;
use
Guzzle
Http\Command\Guzzle
\Parameter
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
use
Symfony\Component\HttpFoundation\RequestStack
;
use
Symfony\Component\HttpFoundation\RequestStack
;
...
@@ -129,18 +129,27 @@ class HttpConfigRequestForm extends EntityForm {
...
@@ -129,18 +129,27 @@ class HttpConfigRequestForm extends EntityForm {
'#description'
=>
$param
->
getDescription
()
?
$this
->
t
(
$param
->
getDescription
())
:
''
,
'#description'
=>
$param
->
getDescription
()
?
$this
->
t
(
$param
->
getDescription
())
:
''
,
];
];
switch
(
$param
->
getType
())
{
$type
=
$param
->
getType
();
switch
(
$type
)
{
case
'integer'
:
case
'integer'
:
$form
[
'parameters'
][
$name
][
'#type'
]
=
'number'
;
$form
[
'parameters'
][
$name
][
'#type'
]
=
'number'
;
$form
[
'parameters'
][
$name
][
'#value_callback'
]
=
[
$this
,
'integerValue'
];
$form
[
'parameters'
][
$name
][
'#value_callback'
]
=
[
$this
,
'integerValue'
,
];
break
;
break
;
case
'array'
:
case
'array'
:
case
'object'
:
$form
[
'parameters'
][
$name
][
'#type'
]
=
'textarea'
;
$form
[
'parameters'
][
$name
][
'#type'
]
=
'textarea'
;
$form
[
'parameters'
][
$name
][
'#rows'
]
=
3
;
$form
[
'parameters'
][
$name
][
'#rows'
]
=
12
;
$placeholder
=
$this
->
t
(
'Enter a list of values (one value per row).'
);
$form
[
'parameters'
][
$name
][
'#description'
]
.
=
'<div class="json-help">'
.
$this
->
t
(
'Example'
)
.
': <small><pre>'
.
$this
->
getJsonHelp
(
$param
)
.
'</pre></small></div>'
;
$form
[
'parameters'
][
$name
][
'#attributes'
][
'placeholder'
]
=
$placeholder
;
$form
[
'parameters'
][
$name
][
'#attributes'
][
'placeholder'
]
=
$this
->
t
(
'Enter data in JSON format.'
);
$form
[
'parameters'
][
$name
][
'#value_callback'
]
=
[
$this
,
'arrayValue'
];
$form
[
'parameters'
][
$name
][
'#value_callback'
]
=
[
$this
,
'jsonString'
];
$form
[
'parameters'
][
$name
][
'#element_validate'
][]
=
[
$this
,
'validateJson'
];
break
;
break
;
}
}
}
}
...
@@ -156,7 +165,15 @@ class HttpConfigRequestForm extends EntityForm {
...
@@ -156,7 +165,15 @@ class HttpConfigRequestForm extends EntityForm {
/**
/**
* Value callback: casts provided input to integer.
* Value callback: casts provided input to integer.
*
*
* @see form
* @param array $element
* The form element.
* @param string $input
* The input value.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The Form State instance.
*
* @return int|null
* The integer value or NULL if no value has been provided.
*/
*/
public
function
integerValue
(
&
$element
,
$input
,
FormStateInterface
$form_state
)
{
public
function
integerValue
(
&
$element
,
$input
,
FormStateInterface
$form_state
)
{
if
(
$input
!==
FALSE
&&
$input
!==
NULL
)
{
if
(
$input
!==
FALSE
&&
$input
!==
NULL
)
{
...
@@ -166,43 +183,150 @@ class HttpConfigRequestForm extends EntityForm {
...
@@ -166,43 +183,150 @@ class HttpConfigRequestForm extends EntityForm {
}
}
/**
/**
* Value callback: converts strings to array values.
* Validate JSON.
*
* @param array $element
* The Form element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The Form State instance.
*/
public
function
validateJson
(
array
&
$element
,
FormStateInterface
$form_state
)
{
$value
=
$element
[
'#value'
];
if
(
empty
(
$value
))
{
return
;
}
if
(
is_string
(
$value
))
{
$form_state
->
setError
(
$element
);
return
;
}
/** @var \GuzzleHttp\Command\Guzzle\Parameter $param */
$param
=
$element
[
'#command_param'
];
$type
=
$param
->
getType
();
switch
(
$type
)
{
case
'array'
:
$is_valid
=
is_array
(
$value
);
break
;
case
'object'
:
$is_valid
=
is_object
(
$value
)
||
(
array_values
(
$value
)
!==
$value
);
break
;
default
:
$is_valid
=
FALSE
;
}
if
(
!
$is_valid
)
{
$message
=
$this
->
t
(
'Field @title has to be an @type. @var_type provided.'
,
[
'@title'
=>
$element
[
'#title'
],
'@type'
=>
$type
,
'@var_type'
=>
ucfirst
(
gettype
(
$value
)),
]);
$form_state
->
setError
(
$element
,
$message
);
if
(
!
is_string
(
$value
))
{
$element
[
'#value'
]
=
json_encode
(
$value
,
JSON_PRETTY_PRINT
);
}
}
}
/**
* Value callback: converts strings to JSON array.
*
* @param array $element
* The form element.
* @param string $input
* The input value.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The Form State instance.
*
*
* @see form
* @return array|null|string
* The computed value.
*/
*/
public
function
arrayValue
(
&
$element
,
$input
,
FormStateInterface
$form_state
)
{
public
function
jsonString
(
array
&
$element
,
$input
,
FormStateInterface
$form_state
)
{
if
(
$input
!==
FALSE
&&
$input
!==
NULL
)
{
if
(
$input
!==
FALSE
&&
$input
!==
NULL
)
{
$input
=
trim
(
$input
);
$input
=
trim
(
$input
);
if
(
empty
(
$input
))
{
if
(
empty
(
$input
))
{
return
[];
return
[];
}
}
$value_callback
=
$this
->
getValueCallback
(
$element
[
'#command_param'
]);
/** @var \GuzzleHttp\Command\Guzzle\Parameter $param */
$items
=
explode
(
"
\n
"
,
$input
);
$param
=
$element
[
'#command_param'
];
$assoc
=
$param
->
getType
()
==
'array'
?
JSON_OBJECT_AS_ARRAY
:
JSON_FORCE_OBJECT
;
foreach
(
$items
as
&
$item
)
{
$item
=
json_decode
(
$input
,
$assoc
);
$item
=
trim
(
$item
);
if
(
json_last_error
()
!==
JSON_ERROR_NONE
)
{
if
(
$value_callback
)
{
$message
=
$this
->
t
(
'There was an error parsing your JSON: @error'
,
[
$item
=
$this
->
{
$value_callback
}(
$element
,
$item
,
$form_state
);
'@error'
=>
json_last_error_msg
(),
}
]);
$this
->
messenger
()
->
addError
(
$message
);
return
$input
;
}
}
return
$item
s
;
return
$item
;
}
}
return
!
empty
(
$element
[
'#default_value'
])
?
implode
(
"
\n
"
,
$element
[
'#default_value'
])
:
NULL
;
if
(
empty
(
$element
[
'#default_value'
]))
{
return
NULL
;
}
$item
=
json_encode
(
$element
[
'#default_value'
],
JSON_PRETTY_PRINT
);
if
(
json_last_error
()
!==
JSON_ERROR_NONE
)
{
$this
->
messenger
()
->
addError
(
json_last_error_msg
());
return
$input
;
}
return
$item
;
}
}
/**
/**
* Get
value callback
.
* Get
JSON Help
.
*
*
* @param \Guzzle
\Service\Description
\Parameter $param
* @param \Guzzle
Http\Command\Guzzle
\Parameter $param
*
A command
parameter
object
.
*
The Guzzle
parameter.
*
*
* @return
bool|
string
* @return string
* A
callback or FALSE
.
* A
JSON String
.
*/
*/
protected
function
getValueCallback
(
Parameter
$param
)
{
protected
function
getJsonHelp
(
Parameter
$param
)
{
$callback
=
$param
->
getItems
()
->
getType
()
.
'Value'
;
if
(
$param
->
getType
()
==
'array'
)
{
return
method_exists
(
$this
,
$callback
)
?
$callback
:
FALSE
;
$properties
=
$param
->
getItems
()
->
getProperties
();
}
else
{
$properties
=
$param
->
getProperties
();
}
$array
=
[];
foreach
(
$properties
as
$name
=>
$parameter
)
{
switch
(
$parameter
->
getType
())
{
case
'string'
:
$sample
=
'Lorem ipsum...'
;
break
;
case
'integer'
:
case
'number'
:
$sample
=
123
;
break
;
case
'float'
:
case
'decimal'
:
$sample
=
123.01
;
break
;
case
'bool'
:
case
'boolean'
:
$sample
=
TRUE
;
break
;
case
'array'
:
$sample
=
[];
break
;
case
'object'
:
$sample
=
new
\stdClass
();
break
;
default
:
$sample
=
'...'
;
}
$array
[
$name
]
=
$sample
;
}
if
(
$param
->
getType
()
==
'array'
)
{
$array
=
[
$array
];
}
return
json_encode
(
$array
,
JSON_PRETTY_PRINT
);
}
}
/**
/**
...
@@ -211,7 +335,7 @@ class HttpConfigRequestForm extends EntityForm {
...
@@ -211,7 +335,7 @@ class HttpConfigRequestForm extends EntityForm {
public
function
save
(
array
$form
,
FormStateInterface
$form_state
)
{
public
function
save
(
array
$form
,
FormStateInterface
$form_state
)
{
$http_config_request
=
$this
->
entity
;
$http_config_request
=
$this
->
entity
;
$status
=
$http_config_request
->
save
();
$status
=
$http_config_request
->
save
();
$messenger
=
\Drupal
::
messenger
();
$messenger
=
$this
->
messenger
();
switch
(
$status
)
{
switch
(
$status
)
{
case
SAVED_NEW
:
case
SAVED_NEW
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment