0

I have the following line of code in a user-profile-form.tpl.php which Drupal uses to render an input field in the user's edit profile page.

print render($form['field_machine_name']);
// this field can be found in the database table field_data_field_machine_name

The issue is that I want to run the value field_machine_name through a function like $form['field_machine_name'] = make_seo_url($form['field_machine_name']) before I run the render() function. But this totally breaks the form because $form['field_machine_name'] is some very large array and render() output html input tag instead of the actual field_machine_name itself.

How does one actually go about massaging values in the $form variable before outputting it as an html field in render()?

Additionally, i want to make sure anytime field_machine_name is printed anywhere on the drupal site, i want it to run through the make_seo_url() function. Is there a global place i can do this?

4 Answers 4

1

The Form API has #pre_render per element. You can use hook_form_alter to add your pre-render function to the list of functions that need to be called to show the value.

#pre_render

Used by: All elements and forms.

Description: Function(s) to call before rendering in drupal_render() has occured. The function(s) provided in #pre_render receive the element as an argument and must return the altered element.

Values: An array of function names to call.

Note: If you are altering an existing form via hook_form_alter() or a similar means, be careful with this property! As demonstrated here, you will probably want to add to the existing array rather than writing over it.

I recommend being careful of when your module runs when trying to interact with Forms, Ajax, etc (your modules' weight). If needed I also recommend seeing hook_module_implements_alter if you want to adjust when your hook is called, without affecting your modules' weight.

0

You need to hook into the form before processing the $form array not in a template file. See hook_form_alter. Also if you are wanting to make a field output a certain way when that field is rendered you need to look into changing the field template.

What you are doing is trying to render a form field inside a template which does not work.

0

You can do it in template_preprocess_field()

function mytheme_preprocess_field(&$variables, $hook) {
  $element = $variables['element'];
  if (isset($element['#field_name'])) {
    if ($element['#field_name'] == 'field_machine_name') {
          //do your changes
    }
  }
}
0

In the template.php of the appropriate theme, run code similar to the snippet i am giving:

function themename_form_user_profile_form_alter(&$form, &$form_state) {
  // dpm($form);
  // Use dpm to see the actual values that you are getting. Based on that change the lines below. Since $form is passed by refernce to this funciton, whatever change you make to the values, will be updated on the tpl file also.
  $old_value = $form['#user']->field_field_name['und'][0]['value'];
  $messaged_value = make_seo_url($old_value);
  $form['#user']->field_field_name['und'][0]['value'] = $messaged_value;
}

I think this should help. It uses hook_form_FORM_ID_alter(). To use dpm(), the Devel module needs to be enabled and the logged-in user needs to have the proper permissions to see the debug information.

Your Answer

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

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