4

The property 'string' is protected as I can see here https://api.drupal.org/api/drupal/core!lib!Drupal!Core!StringTranslation!TranslatableMarkup.php/class/TranslatableMarkup/8.2.x

but I need to change word "- Any -" in my exposed filter form to my string "All terms"?

I cannot do this cause I do not know how to use this class and how to change output.

This hook says: cannot get value of protected property.

function MYtheme_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ( ( $form['#attributes']['class'][0] == 'views-exposed-form' ) &&  
       ( $form['#info']['filter-field_color_target_id']['operator'] == 'field_color_target_id_op' ) )
  {
      dpm($form['color']['#options']['All']->string);
      //$form['color']['#options']['All']->string = t('All terms');
  }
}
5

The object TranslatableMarkup is the result of t(). It's not possible to modify the content of this object. You overwrite the object with a new instance:

form['color']['#options']['All'] = t('All terms');
  • I give you big thanks, cause all this time I googled and read Drupal API. I've found here $this->t('new'); but did not work drupal.stackexchange.com/questions/202331/… – tlito Aug 25 '16 at 20:55
  • and not convenient api description api.drupal.org/api/drupal/… did not help me at all. – tlito Aug 25 '16 at 20:57
  • The difference is, the linked question was for a form class, where you use $this->t(). You have a hook, where you use t(). – 4k4 Aug 25 '16 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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