1

I saw lot of questions and answers on this but I can't find solution. I need to show last posts trough tpl file and I can't figure how to do it. Also I don't know what to put in my tpl file.

Edit!: This is my hook_block_view () function and if I send data at the end of it like theme('item_list', ... it works perfectly. But I need to send it to my custom tpl file and when I do that it won't properly display list. I think that there is problem in .tpl file.

<?php
function last_5_user_posts_block_view($delta = '') {
$block = array();

switch ($delta) {
case 'last_5_user_posts':

  // Capture the image file path and form into HTML with attributes
$image_file = file_load(variable_get('last_5_user_post_image', ''));
$image_path = '';

if (isset($image_file->uri)) {
$image_path = $image_file->uri;
}

$image = theme_image(array(
    'path' => ($image_path),
    'alt' => t('Image description here.'),
    'attributes' => array('class' => 'class_name'),
    'width' => '12',
    'height' => '12',
    ));

  $block['subject'] = t('My last posts');
  if (user_access('access content')) {
    // Use our custom function to retrieve data.
    $result = last_5_user_posts_contents();
    // Array to contain items for the block to render.
    $items = array();
    // Iterate over the resultset and format as links.
    foreach ($result as $node) {
      $items[] = array(
        'data' => $image.l($node->title, 'node/' . $node->nid),
      );
    }
   // If there is no nodes
    if (empty($items)) {
      $block['content'] = t('No posts available.');
    }
    else {
      // Pass data through theme function.
   //   $block['content'] = theme('last_5_user_posts_output', array(
   //       'items' => $items));
        $govno = 'Govno!';
        $listitems = array('items' => $items);
      $block['content'] = theme('last_5_user_posts_output', array(
          'items' => $items));
    }
  }

  return $block;

}

}

And my .tpl file for now contain just:

 <div class="field-items"<?php print $content_attributes; ?>>
 <ol style="list-style: circle">
 <?php foreach ($items as $delta => $item): ?>
 <li>
 <div class="field-item" style="color: red;"><?php print render($item); ?></div>
 </li>
 <?php endforeach; ?>
 </ol>
</div>

If I create array:

$test[1] = 'Test!';
$test[2] = 'Test2!';
$test[3] = 'Test3!';

And send it trough $block['content'] like this

$block['content'] = theme('last_5_user_posts_output', array(
          'test' => $test));

It works pefectly! But for $items it won't work. How to optimize $items to get it in .tlp??

  • Have you considered using a Views block instead? – Geoff May 10 '14 at 20:46
  • How? I am using hook_block_post(). Theme file works if I put something simple there, like <p>Hello world</p> but I need to send content and subject to tpl file to print them trough theme, I can not figure how to properly send them, or how to set theme file. – I'm stupid May 10 '14 at 23:12
  • Related: stackoverflow.com/questions/16570599/… – doublejosh Apr 19 '17 at 1:39
0

Was too long for a comment...

If all you want is the last 5 posts of the current user - create a view:

  • choose content type
  • Create a limit under Pager (presumably of 5)
  • advanced - relationship author
  • filter by currently logged in user
  • sort by post date descending

This will create a view (be it a page or block) that shows the last 5 posts of whatever content you want for the currently logged in user. If you want to include it as a block, create as a block, and in the block configuration for that view (under structure/blocks), you can decide where on the page and for which pages you want the block to appear.

You can decide which content types are included via the filter, and decide the layout.

  • All of that a have in function content and function last_5_user_posts_block_view() This module is working if I set in block[content] = theme('item_list',.... but I want it to show trough theme file. – I'm stupid May 11 '14 at 5:19
  • I fail to see the point of a custom module that duplicates the results of an existing, well tested module. – Geoff May 11 '14 at 12:32
  • I need to display results trough custom theme file so developer can modify how it will look later. – I'm stupid May 11 '14 at 17:12
0

Resolved! In

function last_5_user_posts_block_view($delta = '') {

I changed $items in foreach to

foreach ($result as $node) {
        $items[] = $image.l($node->title, 'node/' . $node->nid);
    }

It is simple and I hope that I will help someone with this solution.

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.