5

I tried this : dpm($button['#value']->string but got this error: access protected property Drupal\Core\StringTranslation\TranslatableMarkup::$string

Within a form object I need to be able to read that value and also change it. how can I access it? thanks

4 Answers 4

1

By casting the object to a (string), you will get the value.

Example:

$op = (string) $form_state->getValue('op');
3

You can get the string value by calling the magic function ->__tostring() or casting the object to a (string). But most time you don't need to do this, because if you use the translation object in a string context it will return the translated string automatically.

You can't change the original string in the translation object. You have to generate a new one with the t-function:

$button['#value'] = $this->t('new text');
2

You can get the string value by using the following method.

getUntranslatedString()

So if you get the object as $link_object then accessing the string would be done using

$link_object->getUntranslatedString();
0

Actually it is a combiantion of the answers of @yash-khandelwal and @binny.

$form_state->getValue('op')->getUntranslatedString();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.