Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
getPossibleValues example
public
function
getPossibleOptions
(
AccountInterface
$account
= NULL
)
{
$languages
= \Drupal::
languageManager
(
)
->
getLanguages
(
LanguageInterface::STATE_ALL
)
;
return
array_map
(
function
DLanguageInterface
$language
)
{
return
$language
->
getName
(
)
;
}
,
$languages
)
;
}
/** * {@inheritdoc} */
public
function
getSettableValues
(
AccountInterface
$account
= NULL
)
{
return
$this
->
getPossibleValues
(
$account
)
;
}
/** * {@inheritdoc} */
public
function
getSettableOptions
(
AccountInterface
$account
= NULL
)
{
return
$this
->
getPossibleValues
(
$account
)
;
}
}
// Test with anonymous user.
$user
=
new
AnonymousUserSession
(
)
;
\Drupal::
currentUser
(
)
->
setAccount
(
$user
)
;
$expected_available_options
=
[
'filtered_html' => 'Filtered HTML',
'full_html' => 'Full HTML',
'filter_test' => 'Test format',
'plain_text' => 'Plain text',
]
;
$available_values
=
$data
->
getPossibleValues
(
)
;
$this
->
assertEquals
(
array_keys
(
$expected_available_options
)
,
$available_values
)
;
$available_options
=
$data
->
getPossibleOptions
(
)
;
$this
->
assertEquals
(
$expected_available_options
,
$available_options
)
;
$allowed_values
=
$data
->
getSettableValues
(
$user
)
;
$this
->
assertEquals
(
[
'plain_text'
]
,
$allowed_values
)
;
$allowed_options
=
$data
->
getSettableOptions
(
$user
)
;
$this
->
assertEquals
(
[
'plain_text' => 'Plain text'
]
,
$allowed_options
)
;
$data
->
setValue
(
'foo'
)
;
$violations
=
$data
->
validate
(
)
;