field output dependend on mime type of another field 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?

UPDATE:

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)
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(&amp;$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) {
                hide($variables['content']['IMAGE_FIELD_MACHINE_NAME']);
              }

            }
          break;
      }
    }