Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
project
address
Commits
21d629b5
Commit
21d629b5
authored
May 11, 2017
by
Bojan Živanović
Browse files
Issue
#2874058
by bojanz: [Zone] Hide the included/excluded postal code fields behind a checkbox
parent
27d1c839
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/Element/ZoneTerritory.php
View file @
21d629b5
...
...
@@ -45,6 +45,9 @@ class ZoneTerritory extends FormElement {
[
$class
,
'processTerritory'
],
[
$class
,
'processGroup'
],
],
'#element_validate'
=>
[
[
$class
,
'validatePostalCodeElements'
],
],
'#pre_render'
=>
[
[
$class
,
'preRenderGroup'
],
],
...
...
@@ -218,22 +221,60 @@ class ZoneTerritory extends FormElement {
return
$element
;
}
$element
[
'limit_by_postal_code'
]
=
[
'#type'
=>
'checkbox'
,
'#title'
=>
t
(
'Limit by postal code'
),
'#default_value'
=>
!
empty
(
$value
[
'included_postal_codes'
])
||
!
empty
(
$value
[
'excluded_postal_codes'
]),
];
$checkbox_parents
=
array_merge
(
$element
[
'#parents'
],
[
'limit_by_postal_code'
]);
$checkbox_path
=
array_shift
(
$checkbox_parents
);
$checkbox_path
.
=
'['
.
implode
(
']['
,
$checkbox_parents
)
.
']'
;
$element
[
'included_postal_codes'
]
=
[
'#type'
=>
'textfield'
,
'#title'
=>
t
(
'Included postal codes'
),
'#description'
=>
t
(
'A regular expression ("/(35|38)[0-9]{3}/") or comma-separated list, including ranges ("98, 100:200")'
),
'#default_value'
=>
$value
[
'included_postal_codes'
],
'#states'
=>
[
'visible'
=>
[
':input[name="'
.
$checkbox_path
.
'"]'
=>
[
'checked'
=>
TRUE
],
],
],
];
$element
[
'excluded_postal_codes'
]
=
[
'#type'
=>
'textfield'
,
'#title'
=>
t
(
'Excluded postal codes'
),
'#description'
=>
t
(
'A regular expression ("/(35|38)[0-9]{3}/") or comma-separated list, including ranges ("98, 100:200")'
),
'#default_value'
=>
$value
[
'excluded_postal_codes'
],
'#states'
=>
[
'visible'
=>
[
':input[name="'
.
$checkbox_path
.
'"]'
=>
[
'checked'
=>
TRUE
],
],
],
];
return
$element
;
}
/**
* Validates the postal code elements.
*
* @param array $element
* The existing form element array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
public
static
function
validatePostalCodeElements
(
array
$element
,
FormStateInterface
$form_state
)
{
$value
=
$form_state
->
getValue
(
$element
[
'#parents'
]);
if
(
empty
(
$value
[
'limit_by_postal_code'
]))
{
// Remove postal code values if the main checkbox was unchecked.
unset
(
$value
[
'included_postal_codes'
]);
unset
(
$value
[
'excluded_postal_codes'
]);
}
unset
(
$value
[
'limit_by_postal_code'
]);
$form_state
->
setValue
(
$element
[
'#parents'
],
$value
);
}
/**
* Ajax callback.
*/
...
...
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment