How can I call the theme preprocessing function for a specific field?

I am on Drupal 7 and I have a specific tpl.php file for the content_image field: "field - field_image.tpl.php". I need to create a preprocess function for this field and for my theme.

Suppose my theme name is "My theme"

It should look like

function my_theme_preprocess_field(&$variables, $hook) {
  $variables['classes_array'][] = 'aClassName';
}

but that will not work. I am wrong. But where?

thank

+5
source share
3 answers

You can use template_preprocess_field()(as in your code above), but just check which field is right for you:

function my_theme_preprocess_field(&$variables, $hook) {
  $element = $variables['element'];
  if (isset($element['#field_name'])) {
    if ($element['#field_name'] == 'field_image') {
      $variables['classes_array'][] = 'aClassName';
    }
  }
}

, hook, , Drupal 7, , .

+13

mytheme_preprocess_field(&$variables, $hook) template.php, , , . , tpls. - .

function mytheme_preprocess_field(&$variables, $hook) {
  if ($variables['element']['#field_name'] == 'field_machine_name') {
        $variables['items'][0]['#markup'] = 'add custom markup';
  }
}

, -.

+4

In drupal 7, you can rewrite the output of a field to template_preprocess_node () by changing the value of "#markup" of that field.

You can also use regexp to change what you want in the content of the page :)

-3
source

All Articles