2

Hi i want to Override views-view-fields.tpl.php in our custom module. kindly help me by example.

5

You need to define a theme hook for the field-template in your module and place a template in custom_module/theme/your-custom-template.tpl:

/**
* Implementation of hook_theme().
*/
function custom_module_theme($existing) {
  return array(
    'views_view_field__view_name__field_name' => array(
    'arguments' => array('view' => NULL, 'field' => NULL, 'row' => NULL),
    'template' => 'your-custom-template',
    'original hook' => 'views_view_field',
    'path' => drupal_get_path('module', 'custom_module') . '/theme',
   ),
  );
}

To narrow down which field-templates you want to replace you can name your return key to:

  • views_view_field__view_name: Override all field templates of the view named "view_name"
  • views_view_field__view_name__field_name: Override only this specific field

This example is taken from grayside.org

2
  • great, then please mark the answer which helped you most as "accepted" to close this issue
    – Nebel54
    Jun 12 '13 at 10:02
  • For Drupal 8 this worked but instead of arguments you need variables. Also this question should specify for which version of Drupal this is. Aug 22 '17 at 17:16
1

The view object is available in the tpl file, so by having an if-else statement it is possible to filter the views that need to the override by inspecting the name/display of the view.

A sample piece of code might help say for example using hook_field_attach_view_alter():

function MYMODULE_field_attach_view_alter(&$output, $context) {

  $field_name = 'test';
  $to_display = array('a');

  if (isset($output[$field_name]['#field_name'])) {
    if ($field_name == $output[$field_name]['#field_name']) {
      foreach ($output[$field_name]['#items'] as $delta => $item) {
        if (in_array($item['value'], $to_display)) {
          unset($output[$field_name][$delta]);
        }
      }
    }
  }
}
0

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.