Given a PHP switch statement, would it be a good (or bad) idea to use t() in case statements?

switch ($button_value) {
  case t('Clear'):
    // …
  case t('Search'):
    // …

2 Answers 2


Drupal 7 uses code similar to the following one. (See image_style_form_submit().)

  if ($form_state['values']['op'] == t('Update style')) {
    drupal_set_message(t('Changes to the style have been saved.'));

That code is necessary to handle the submission done from two different buttons with the same form submission handler. (See image_style_form(); comments starting with kll are mine.)

  // kll: This is the first button using that submission handler.
  $form['effects']['new']['add'] = array(
    '#type' => 'submit',
    '#value' => t('Add'),
    '#validate' => array('image_style_form_add_validate'),
    '#submit' => array('image_style_form_submit', 'image_style_form_add_submit'),

  // Show the Override or Submit button for this style.
  $form['actions'] = array('#type' => 'actions');
  $form['actions']['override'] = array(
    '#type' => 'submit',
    '#value' => t('Override defaults'),
    '#validate' => array(),
    '#submit' => array('image_style_form_override_submit'),
    '#access' => !$editable,

  // kll: This is the second button using that submission handler.
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Update style'),
    '#access' => $editable,

Avoiding to check the value of $form_state['values']['op'] can be easily accomplished by using different submission handlers for different buttons.

I think this is the only case Drupal 7 uses the value returned from t() in a control structure; it can be avoided, and Drupal 8 does avoid that.

Generally speaking, using a string that is shown in the user interface in a control structure should be avoided, since any change to the text presented to the user would require a change in the logic of a different function. Since t() is used with strings shown to the users, code like the one you shown should be avoided.


In general, it would be a bad development practice. The whole purpose of t() is to provide human-readable translated text. As such, it can't be guaranteed as consistent switch logic because the translation context might differ.

In practice (e.g. a input button submit value on a basic form with little-to-no translation contexts), you might get away with it, but you're adding technical debt; if your application grows and other features start to rely on the switch logic, it's only a matter of time before the inconsistent logic will bite you.

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.