Search code examples
imagedrupaldrupal-7

Content Type default image dependent on field value


I have a content type of resource that has five different resource types (video, article, book, etc.). Each of these resources has a main image field (field_image). If the user doesn't provide an image I want to fallback to a default image. However, I would like the default image to be based on the type of resource (video, article, book, etc.). Is there a module or solution to do this within the Drupal UI? I need the image to be attached to the content type (not hard coded in templates) so that it displays properly on detail, list, and other view pages.

Expected Functionality

  • Resource: field_type = article; field_image = default_article.jpg
  • Resource: field_type = book; field_image = default_book.jpg
  • Resource: field_type = video; field_image = default_video.jpg

What I've tried

Google: I've tried but failed to find anything that would make this possible. I understand by default Drupal allows only one default image per content type. I could make a new content type for each of my five types, but that seems unnecessary and clunky.

Hard code: I have a template for my resource detail page where I've hard coded the relevant default image to show if there is no image present, but this doesn't scale well as I have many views (resource list, related resources, etc.) where I am displaying resource images.

Conditional Fields: I've tried the module Field Dependencies (e.g., when field_type = x, set field_image = y), but it doesn't appear to work with files.

Thanks!


Solution

  • The hook_field_attach_view_alter() is your way to go, if your view uses the content display (and not field by field). If your view relies on field display, you can try hook_field_attach_load().

    function yourmodule_field_attach_view_alter(&$output, $context) {
      // Append RDF term mappings on displayed taxonomy links.
      if ($context['entity_type'] == 'node' && $context['entity']->type == 'resource') {
        foreach (element_children($output) as $field_name) {
            if ($field_name == 'field_resource') {
                $element = &$output[$field_name];
                foreach ($element ['#items'] as $delta => $item) {
                    yourmodule_defaultimg($item, $node->field_resource_type['und'][0]['value']);
                }
            }
        }  
      }
    }
    
    function yourmodule_defaultimg(&$item, $resource_type) {
    
        switch($resource_type) {
          case "Article":
            $filename = 'default_resource_article.jpg';
            $uri = 'public://default_images/default_resource_article.jpg';
            break;
          case "Book":
            $filename = 'default_resource_book.jpg';
            $uri = 'public://default_images/default_resource_book.jpg';
            break;
          case "Video":
            $filename = 'default_resource_video.jpg';
            $uri = 'public://default_images/default_resource_video.jpg';
            break;
          case "Tool":
            $filename = 'default_resource_tool.jpg';
            $uri = 'public://default_images/default_resource_tool.jpg';
            break;
          case "Picture of Practice":
            $filename = 'default_resource_picture-of-practice.jpg';
            $uri = 'public://default_images/default_resource_picture-of-practice.jpg';
            break;
        }
    
        $item['uri'] = $uri;
        $item['filename'] = $filename;
    }