4

Just wondering how to make the webform components labels translatable. I found this post with a link to a sandbox project but I don't where to download the module: Make Webform multilingual (i18n) aware through contributed modules

I also find this function to just make what I need but it doesn't work, maybe with a few change it will work: Source: work around

function module_name_form_webform_client_form_xx_alter(&$form, &$form_state) { 
  foreach (element_children($form['submitted']) as $element) { 
    // Pass each webform component title through the t() function. 
    $form['submitted'][$element]['#title'] = t($form['submitted'][$element]['#title']); 
  } 
} 

The function provided by @lenni does the job but I get these error messages.

Warning: strlen() expects parameter 1 to be string, array given en drupal_validate_utf8() (línea 1600 de/Applications/MAMP/htdocs/tonisacristan.com/includes/bootstrap.inc).

Notice: Array to string conversion en filter_xss() (línea 1366 de /Applications/MAMP/htdocs/tonisacristan.com/includes/common.inc).

Notice: Array to string conversion en format_string() (línea 1552 de /Applications/MAMP/htdocs/tonisacristan.com/includes/bootstrap.inc).

The source of the problem is a Markup component. This function is working fine.

function webform_extras_node_view($node, $view_mode, $langcode) {
  if($node->type == 'webform' && $view_mode == 'full') {
    // translate the webform component labels
    foreach(element_children($node->content['webform']['#form']['submitted']) as $key){
      //to skip Markup components
      if($key != 'my_markup_component'){
        $node->content['webform']['#form']['submitted'][$key]['#title'] = t($node->content['webform']['#form']['submitted'][$key]['#title']);
      }
    }
  }
}
1

Similar (a litte bit more generic) solution but still not a clean one:

function mymodule_node_view($node, $view_mode, $langcode) {
  if($node->type == 'webform' && $view_mode == 'full') {
    // translate the webform component labels
    foreach(element_children($node->content['webform']['#form']['submitted']) as $key){
      $node->content['webform']['#form']['submitted'][$key]['#title'] = t($node->content['webform']['#form']['submitted'][$key]['#title']);
    }
  }
}
  • It does the job, but I get some error messages. I added these message at the bottom of my question. Can you help with this? – chefnelone Apr 25 '12 at 11:20
  • Are you sure the errors are related to this workaround? – lenni Apr 25 '12 at 11:40
  • Yes, the source of the problem is a Markup component. I added 1 more line to your function. See it at bottom of my question. – chefnelone Apr 25 '12 at 11:46
0

And if you actually want to translate your markup fields as well, which is often the case:

function mymodule_node_view($node, $view_mode, $langcode) {  

  if ($node->type == 'webform') {
    // translate the webform component labels
    foreach(element_children($node->content['webform']['#form']['submitted']) as $key){
      if($key != 'your_markup_component' && $key != 'your_markup_component_2'){
        $node->content['webform']['#form']['submitted'][$key]['#title'] = t($node->content['webform']['#form']['submitted'][$key]['#title']);
      }
      else {
        $node->content['webform']['#form']['submitted'][$key]['#value'] = t($node->content['webform']['#form']['submitted'][$key]['#value']);
      }
    }
  }
}

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.