I have a node type with 2 fields: preview_image and file (multivalue).

I can upload image and video files.

For the output I need the following:

The preview_image should only be displayed when the first file of the file field (multivalue) has a specific mime type (in my case any video mime type).

Is there a solution for that?


Now we have a change in field structure. We use 2 mime types, images and videos. But for the images we need a cropping function. And we need an easy possibility to arrange the files within node editor.

Now we would like to create 2 new node types - for example 'image' and 'video'. In both node types we create the file fields - in 'image' with the cropping function.

In the main node type we now create 2 fields as entity references to get the files.

Now we need a solution to display the preview image only when the referenced node is of type 'video'.

Show preview image:

media element (referencing field)

  • entity of type 'video' (referenced entity)
  • entity of type 'image' (referenced entity)
  • entity of type 'image' (referenced entity)
  • entity of type 'video' (referenced entity)

Don't show preview image

  • entity of type 'image' (referenced entity)
  • entity of type 'image' (referenced entity)
  • entity of type 'video' (referenced entity)
  • entity of type 'image' (referenced entity)
up vote 1 down vote accepted

This can be done with a template preprocess added to the template.php file in your theme. You would need to test for the video mime type on the correct node and then use Drupal's hide function, to tell Drupal to skip the field on render.

     * Override or insert variables into the node templates.
     * @param $variables
     *   An array of variables to pass to the theme template.
     * @param $hook
     *   The name of the template being rendered ("node" in this case.)
    function THEMENAME_preprocess_node(&$variables, $hook) {
      $type = $variables['type'];

      switch ($type) {

          case 'NODE_TYPE_MACHINE_NAME':

            if (isset($variables['FILE_FIELD_MACHINE_NAME'][0])) {
              $mime_type = $variables['FILE_FIELD_MACHINE_NAME'][0]['filemime'];
              $mime_regex = '/^video/';
              // We have to check the string for the presence of "video", because it could be any of 18 video mime types. 
              $is_video = preg_match($mime_regex, $mime_type);

              if ($is_video) {

  • Big thanks for your help! But please have a look at my question update. – deelite Mar 9 '15 at 9:48

Per the update, your code inside your main nodes preprocess would need to change to check the type of the referenced node and hide dependent upon that.

 if (!empty($variables['REFERENCE_FIELD_MACHINE_NAME'])) {
     $referenced_nid = $variables['REFERENCE_FIELD_MACHINE_NAME'][LANGUAGE_NONE][0]['target_id'];

     // Load the referenced node to get the type
     $node = node_load($referenced_nid);
     $type = $node->type; 

     if ($type == 'video') {

You may need to use Devel and output $variables to get your tailor this to your field.

  • Thank you very much! Do I use this code in node.tpl of my node type? Or in template.php? – deelite Mar 10 '15 at 9:31
  • This is all template.php code. – jswitchback Mar 10 '15 at 14:26
  • Sorry, on my side it doesn't work. Let me explain my config in detail: I have a content type header_slider. In this ct I have 2 fields: header_slider_element (the referencing field, multivalue) and field_header_slider_previews_img (the preview image). header_slider_element refers to 2 content types: header_slider_element_img and header_slider_element_video. The field_header_slider_previews_img should only be displayed, when the first (referenced node) value of field_slider_element is of node type header_slider_element_video. My code at the moment: – deelite Mar 11 '15 at 7:29
  • if (!empty($variables['field_slider_element'])) { $referenced_nid = $variables['field_slider_element'][LANGUAGE_NONE][0]['target_id']; // Load the referenced node to get the type $node = node_load($referenced_nid); $type = $node->type; if ($type == 'header_slider_element_video') { hide($variables['content']['field_header_slider_previews_img']); } }; – deelite Mar 11 '15 at 7:30
  • I would start by using Devel module to dpm($variables). Check that $variables['field_slider_element'][LANGUAGE_NONE][0]['target_id'] is correct in your use case. Keep in mind LANGUAGE_NONE is replacing und. – jswitchback Mar 11 '15 at 15:21

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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