1

I have the content type of "Front Page Section" where there are the fields "body" and "field_home_section_photo".

On the home page (page--front.tpl.php) I would like to be able to print the body and image fields separately for particular nodes with their formatting in place:

Print Node 42 Body Print Node 42 Image Print Node 43 Body Print Node 43 Image Print Node 44 Body Print Node 45 Image
0

You can do this by hook_preprocess_node(&$vars) function. By this function you can check the Node ID then if it is some custom IDs, fetch the appropriate data and assign them in $vars array.

In the aggregate we have

function THEME_preprocess_node(&$vars){
    if(drupal_is_front_page() == true){
         if($vars['node']->nid == 44){
             $newNode = node_load(45);
             $wrapper = entity_metadata_wrapper('node',$newNode);
             $image = $wrapper->field_home_section_photo->value();
 //here use kpr($vars) to get the correct syntax of  field_home_section_photo in $vars
// suppose it is $var['fields']['field_home_section_photo']->value
              $var['fields']['field_home_section_photo']->value = $image;
           }
      }
}  

don't forget to install entity module.

  • Does the above code go into the template.php file? If so what code do I use in the page--front.tpl.php? – IamOnStage Jul 18 '15 at 16:20
  • Yes it goes. This function (hook) is triggered before loading any node. Because you are displaying set of nodes on front page so this function is triggered in that time as well. You dont need to do anything in front page, just adjust this function with your website fields – M a m a D Jul 18 '15 at 17:08
  • I corrected the code – M a m a D Jul 31 '15 at 9:05
-1

If you're not familiar with the module layer of drupal I would suggest using the views module to build a view page of all the nodes of type "Front Page Section" and use this as the front page. https://www.drupal.org/project/views

  • The Views module doesn't work here. For node id = 44 he wants to print body with id=45 and image with id=44. – M a m a D Jul 18 '15 at 17:42
  • Exactly @Suranga. So for e.g. 'load node=42;Print node body; Print node image;end;Load node 44;print node body;print node image;end' – IamOnStage Jul 19 '15 at 6:32
  • @IamOnStage Why wouldn't a view of 'fields' (body and Image) of all nodes of the type "Front Page Section" work? Because your question seems to imply that it would. – Suranga Panagamuwa Gamage Jul 19 '15 at 22:41
  • I wanted to hard code into the front page the display of certain nodes which would allow more specific HTML wrapper of the image and body content (as views adds in loads of extra rubbish) it is just just drop the fields into some clean Bootstrap columns. Also from a permission point of view. – IamOnStage Jul 22 '15 at 8:14

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.