2

Here is what I am trying to do.

If there is an empty image field on a node I want it replaced based on what taxonomy terms are on the page, but the key is I want to apply it site wide so it will affect not only the tpl files but Views as well. I'm guessing this needs to be done via mytheme_preprocess_field in template.php but I'm not quite sure how to do it.

I can easily do this in node.tpl.php but I need it done site wide, not just in the node-type template.

  • VID 1
    • TID 1
    • TID 2
    • TID 3
    • TID 4
  • VID 2
    • TID 5
    • TID 6
    • TID 7
  • VID 3
    • TID 8
    • TID 9
    • TID 10

Here is what I am trying to do.

// Set Variables
$term1 = $content['field_term1'][0]['#markup'];
// Must use if statement or else there will be an error
if ( isset( $content['field_term2'][0]['#markup'] ) ) {
  $term2 = $content['field_term2'][0]['#markup'];
}
// Must use if statement or else there will be an error
if ( isset( $content['field_term3'][0]['#markup'] ) ) {
  $travel_type = $content['field_term3'][0]['#markup'];
}
if (isset($content['field_node_image'][0]['#item']['uri'])) {
  $image_path = file_create_url($content['field_node_image'][0]['#item']['uri']);
} else {
  switch ($term1) {
    case 'TID 1':
      switch ($term2) {
        case 'TID 5':
          $term_type = '5';
          break;
        case 'TID 6':
          $term_type = '6';
          break;
        case 'TID 7':
          $term_type = '7';
          break;
      }
      break;
    case 'TID 2':
      $term_type = '2';
      break;
    case 'TID 3':
      switch ($term3) {
        case 'TID 8':
          $term_type = '8';
          break;
        case 'TID 9':
          $term_type = '9';
          break;
        case 'TID 10':
          $term_type = '10';
          break;
      }
      break;
    case 'TID 4':
      $term_type = '4';
      break;
  }
  $image_path = '/sites/all/themes/themename/term-images/' . $term_type . '.jpg';
}
3
  • 1
    all you need to do is described here: Achieve Taxonomy Image function in Drupal 7 with Imagefield.
    – Jack-PL
    Commented Aug 13, 2013 at 0:47
  • I've updated my question to make a bit more sense of what exactly I am trying to do.
    – Erik
    Commented Aug 13, 2013 at 15:05
  • If user will choose any term from your vocabulary, then his post will contain some image related with this term, right?
    – Jack-PL
    Commented Aug 13, 2013 at 16:52

2 Answers 2

0

I used to use the Taxonomy Image module. It has full views integration as well.

Features:

    Image configuration happens within existing admin/taxonomy menu structure.
    Admins can add custom tags to <img> markup when displaying images.
    Allows one-to-one term-to-image relationships.
    Allows many-to-one term-to-image relationships.
    Admin-permitted users can disable the images.
    Administrators can force images to be a standard size.
    May use the ImageCache module if it is available.
    Works with Views.
    Utilizes core file.inc and taxonomy.module APIs.
    Small module with specific, focused functionality.
    Add-on modules that virtually eliminate the need for theme changes.
0

If you want to do some custom coding based on what's in the node from the field template you can get to the node from $variables in preprocess_field like so:

// Parent entity could be a node or custom fieldable entity, usually going to be a node.
if(!empty($variables['element']['#object'])){
  $parent_entity = &$variables['element']['#object'];
}

At this point you can do whatever you were doing in the node preprocess in the field preprocessor. This can potentially get a bit heavy, so beware

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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