Skip to main content
added 350 characters in body; added 8 characters in body
Source Link
 <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:#FF0000;"> red;"><?php print render($item); ?></div>
 </li>
 <?php printendforeach; $items;?>
 </ol>
</div>

AsIf I am not sure what to put here.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! For now,But for $items it display "Array" in red colorwon't work. How to optimize $items to get list of links and show them as list? This is my first .tpl file in drupal and I can't figure out how it should look to show properly my listin .tlp??

<div style="color:#FF0000;">    
<?php print $items;?>
</div>

As I am not sure what to put here. For now, it display "Array" in red color. How to get list of links and show them as list? This is my first .tpl file in drupal and I can't figure out how it should look to show properly my list.

 <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??

deleted 4677 characters in body
Source Link

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
/**
* Implements hook_help().
*/
function last_5_user_posts_help($path, $arg) {
   switch ($path) {
    case "admin/help#last_5_user_posts":
      return '<p>' . t("Show last 5 nodes of active user") . '</p>';
      break;
  }
} 

/**
* Implements hook_block_info().
*/
function last_5_user_posts_block_info() {
  $blocks['last_5_user_posts'] = array(
     // The name that will appear in the block list.
    'info' => t('My last posts'),
    // Default setting.
    'cache' => DRUPAL_CACHE_PER_ROLE,
  );
  return $blocks;
}


/**
*Colecting data from database
*/
function last_5_user_posts_contents(){
$choosed_nodes = arraylast_5_user_posts_block_view();
$choosed_nodes$delta = variable_get('last_5_user_posts_nodes_toshow', array(''));
global $user;
$userid = $user->uid;  //current logged user
    //maximum number of posts to be shown
$max_num = variable_get('last_5_user_posts_max', 5); 
 {
$query$block = db_select('node', 'n')
    ->fields('n', array('nid', 'title', 'created'))
    ->condition('status', 1) //Published.
    ->condition('uid', $userid) //current user
    ->condition('type', $choosed_nodes) //selected node types
    ->orderBy('created', 'DESC') //Most recent first.
    ->range(0,$max_num) //number of posts
    ->execute();
    
    return $query;
}

/**
*Preparing data for showing.
*/
function last_5_user_posts_block_view($delta = '') {

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['list']$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''last_5_user_posts_output', array(                
     //       'items' => $items));
        
    }
  }

  return $block;

}

}


/**
* Page callback: My last posts settings
* Number of nodes to be shown
*/
function last_5_user_posts_block_configure($delta = '') {
$form$govno = array();'Govno!';
  if ($delta == 'last_5_user_posts') {
  $form['last_5_user_posts_max']$listitems = array(
'#type' => 'textfield',
'#title' => t('Maximum number of posts'),
'#default_value' => variable_get('last_5_user_posts_max', 5),
'#size' => 2,
'#maxlength' => 2,
'#description' => t('The maximum number of links to display in the block.'),
'#required''items' => TRUE,
);

/*
*Form for content type choose
*/ 
  $types = node_type_get_types($items);
  foreach($types as $node_type) {
  $nodetypes[$node_type->type] = $node_type->name;
  }
  $form['last_5_user_posts_nodes_toshow']$block['content'] = array(
  '#type' => 'radios',
  '#title' => t('Select the nodes to show'),
  '#options' => $nodetypes,
  '#default_value' => variable_gettheme('last_5_user_posts_nodes_toshow''last_5_user_posts_output', array('')),
  '#description' => t('Selected node type will be shown'),
  );

/*
*Form for image upload
*/ 
  $form['last_5_user_post_image'] = array(
    '#name' => 'block_image',
    '#type' => 'managed_file',
    '#title' => t('Choose an Image File'),
    '#description' => t('Select an Image for the My last posts content type list.  Only *.gif, *.png, *.jpg, and *.jpeg images allowed.'),
    '#default_value' => variable_get('last_5_user_post_image', ''),
    '#upload_location' => 'public://block_image/',
    '#upload_validators' => array(
      'file_validate_extensions''items' => array('gif png jpg jpeg'),
    ),
  );
  }

  return $form;

}
/**
* Implements hook_block_save()
*/
function last_5_user_posts_block_save($delta = '', $edit = array()) {
if ($delta == 'last_5_user_posts') {

//Set value for maximum number of posts shown
variable_set ('last_5_user_posts_max', $edit['last_5_user_posts_max']);

//Set value for content types to be shown
variable_set ('last_5_user_posts_nodes_toshow',     $edit['last_5_user_posts_nodes_toshow']);

// Saving the file, setting it to a permanent state
// setting a FID variable
$file = file_load($edit['last_5_user_post_image']);
  $file->status = FILE_STATUS_PERMANENT;
  file_save($file);
  $block = block_load('last_5_user_posts', $delta);
  file_usage_add($file, 'last_5_user_posts', 'block', $block->bid$items);
  variable_set('last_5_user_post_image', $file->fid);

}
}

/**
* Implements validation from the Form API.
*
* @param $form
*   A structured array containing the elements and properties of the form.
* @param $form_state
*   An array that stores information about the form's current state
*   during processing.
 */
function last_5_user_posts_form_validate($form, &$form_state){
$max_num = $form_state['values']['last_5_user_posts_max'];
if (!is_numeric($max_num)){
form_set_error('last_5_user_posts_max', t('You must enter a number for the maximum number of posts to display.'));
 }
 else if ($max_num <= 0){
form_set_error('last_5_user_posts_max', t('Maximum number of posts to display must be positive.'));
  }
}

/**
* Implements hook_theme().
*/
function last_5_user_posts_theme($existing, $type, $theme, $path) {
$module_path = drupal_get_path('module', 'last_5_user_posts');
$base = array(
    'path' => "$module_path"
);
return array(
'last_5_user_posts' => $base + array(
  'variables' => array(
    'items' => NULL,
   
  ),
    'template' => 'last_5_user_posts--block'$block;
),
}
 );
}
<div style="color:#FF0000;">    
<?php
$items = $variables['items'];
print render($items[]);
$items;?>
</div>

As I am not sure what to put here. For now, it display "Array" in red color. How to get list of links and show them as list? This is my first .tpl file in drupal and I can't figure out how it should look to show properly my list.

<?php
/**
* Implements hook_help().
*/
function last_5_user_posts_help($path, $arg) {
   switch ($path) {
    case "admin/help#last_5_user_posts":
      return '<p>' . t("Show last 5 nodes of active user") . '</p>';
      break;
  }
} 

/**
* Implements hook_block_info().
*/
function last_5_user_posts_block_info() {
  $blocks['last_5_user_posts'] = array(
     // The name that will appear in the block list.
    'info' => t('My last posts'),
    // Default setting.
    'cache' => DRUPAL_CACHE_PER_ROLE,
  );
  return $blocks;
}


/**
*Colecting data from database
*/
function last_5_user_posts_contents(){
$choosed_nodes = array();
$choosed_nodes = variable_get('last_5_user_posts_nodes_toshow', array(''));
global $user;
$userid = $user->uid;  //current logged user
    //maximum number of posts to be shown
$max_num = variable_get('last_5_user_posts_max', 5); 
 
$query = db_select('node', 'n')
    ->fields('n', array('nid', 'title', 'created'))
    ->condition('status', 1) //Published.
    ->condition('uid', $userid) //current user
    ->condition('type', $choosed_nodes) //selected node types
    ->orderBy('created', 'DESC') //Most recent first.
    ->range(0,$max_num) //number of posts
    ->execute();
    
    return $query;
}

/**
*Preparing data for showing.
*/
function last_5_user_posts_block_view($delta = '') {

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['list'] = 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', array(                
            'items' => $items));
        
    }
  }

  return $block;

}

}


/**
* Page callback: My last posts settings
* Number of nodes to be shown
*/
function last_5_user_posts_block_configure($delta = '') {
$form = array();
  if ($delta == 'last_5_user_posts') {
  $form['last_5_user_posts_max'] = array(
'#type' => 'textfield',
'#title' => t('Maximum number of posts'),
'#default_value' => variable_get('last_5_user_posts_max', 5),
'#size' => 2,
'#maxlength' => 2,
'#description' => t('The maximum number of links to display in the block.'),
'#required' => TRUE,
);

/*
*Form for content type choose
*/ 
  $types = node_type_get_types();
  foreach($types as $node_type) {
  $nodetypes[$node_type->type] = $node_type->name;
  }
  $form['last_5_user_posts_nodes_toshow'] = array(
  '#type' => 'radios',
  '#title' => t('Select the nodes to show'),
  '#options' => $nodetypes,
  '#default_value' => variable_get('last_5_user_posts_nodes_toshow', array('')),
  '#description' => t('Selected node type will be shown'),
  );

/*
*Form for image upload
*/ 
  $form['last_5_user_post_image'] = array(
    '#name' => 'block_image',
    '#type' => 'managed_file',
    '#title' => t('Choose an Image File'),
    '#description' => t('Select an Image for the My last posts content type list.  Only *.gif, *.png, *.jpg, and *.jpeg images allowed.'),
    '#default_value' => variable_get('last_5_user_post_image', ''),
    '#upload_location' => 'public://block_image/',
    '#upload_validators' => array(
      'file_validate_extensions' => array('gif png jpg jpeg'),
    ),
  );
  }

  return $form;

}
/**
* Implements hook_block_save()
*/
function last_5_user_posts_block_save($delta = '', $edit = array()) {
if ($delta == 'last_5_user_posts') {

//Set value for maximum number of posts shown
variable_set ('last_5_user_posts_max', $edit['last_5_user_posts_max']);

//Set value for content types to be shown
variable_set ('last_5_user_posts_nodes_toshow',     $edit['last_5_user_posts_nodes_toshow']);

// Saving the file, setting it to a permanent state
// setting a FID variable
$file = file_load($edit['last_5_user_post_image']);
  $file->status = FILE_STATUS_PERMANENT;
  file_save($file);
  $block = block_load('last_5_user_posts', $delta);
  file_usage_add($file, 'last_5_user_posts', 'block', $block->bid);
  variable_set('last_5_user_post_image', $file->fid);

}
}

/**
* Implements validation from the Form API.
*
* @param $form
*   A structured array containing the elements and properties of the form.
* @param $form_state
*   An array that stores information about the form's current state
*   during processing.
 */
function last_5_user_posts_form_validate($form, &$form_state){
$max_num = $form_state['values']['last_5_user_posts_max'];
if (!is_numeric($max_num)){
form_set_error('last_5_user_posts_max', t('You must enter a number for the maximum number of posts to display.'));
 }
 else if ($max_num <= 0){
form_set_error('last_5_user_posts_max', t('Maximum number of posts to display must be positive.'));
  }
}

/**
* Implements hook_theme().
*/
function last_5_user_posts_theme($existing, $type, $theme, $path) {
$module_path = drupal_get_path('module', 'last_5_user_posts');
$base = array(
    'path' => "$module_path"
);
return array(
'last_5_user_posts' => $base + array(
  'variables' => array(
    'items' => NULL,
   
  ),
    'template' => 'last_5_user_posts--block'
),

 );
}
<?php
$items = $variables['items'];
print render($items[]);
?>

As I am not sure what to put here.

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;

}

}
<div style="color:#FF0000;">    
<?php print $items;?>
</div>

As I am not sure what to put here. For now, it display "Array" in red color. How to get list of links and show them as list? This is my first .tpl file in drupal and I can't figure out how it should look to show properly my list.

added 13 characters in body
Source Link
<?php
/**
* Implements hook_help().
*/
function last_5_user_posts_help($path, $arg) {
   switch ($path) {
    case "admin/help#last_5_user_posts":
      return '<p>' . t("Show last 5 nodes of active user") . '</p>';
      break;
  }
} 

/**
* Implements hook_block_info().
*/
function last_5_user_posts_block_info() {
  $blocks['last_5_user_posts'] = array(
     // The name that will appear in the block list.
    'info' => t('My last posts'),
    // Default setting.
    'cache' => DRUPAL_CACHE_PER_ROLE,
  );
  return $blocks;
}


/**
*Colecting data from database
*/
function last_5_user_posts_contents(){
$choosed_nodes = array();
$choosed_nodes = variable_get('last_5_user_posts_nodes_toshow', array(''));
global $user;
$userid = $user->uid;  //current logged user
    //maximum number of posts to be shown
$max_num = variable_get('last_5_user_posts_max', 5); 

$query = db_select('node', 'n')
    ->fields('n', array('nid', 'title', 'created'))
    ->condition('status', 1) //Published.
    ->condition('uid', $userid) //current user
    ->condition('type', $choosed_nodes) //selected node types
    ->orderBy('created', 'DESC') //Most recent first.
    ->range(0,$max_num) //number of posts
    ->execute();
    
    return $query;
}

/**
*Preparing data for showing.
*/
function last_5_user_posts_block_view($delta = '') {

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['list'] = 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', array(                
            'items' => $items));
        
    }
  }

  return $block;

}

}


/**
* Page callback: My last posts settings
* Number of nodes to be shown
*/
function last_5_user_posts_block_configure($delta = '') {
$form = array();
  if ($delta == 'last_5_user_posts') {
  $form['last_5_user_posts_max'] = array(
'#type' => 'textfield',
'#title' => t('Maximum number of posts'),
'#default_value' => variable_get('last_5_user_posts_max', 5),
'#size' => 2,
'#maxlength' => 2,
'#description' => t('The maximum number of links to display in the block.'),
'#required' => TRUE,
);

/*
*Form for content type choose
*/ 
  $types = node_type_get_types();
  foreach($types as $node_type) {
  $nodetypes[$node_type->type] = $node_type->name;
  }
  $form['last_5_user_posts_nodes_toshow'] = array(
  '#type' => 'radios',
  '#title' => t('Select the nodes to show'),
  '#options' => $nodetypes,
  '#default_value' => variable_get('last_5_user_posts_nodes_toshow', array('')),
  '#description' => t('Selected node type will be shown'),
  );

/*
*Form for image upload
*/ 
  $form['last_5_user_post_image'] = array(
    '#name' => 'block_image',
    '#type' => 'managed_file',
    '#title' => t('Choose an Image File'),
    '#description' => t('Select an Image for the My last posts content type list.  Only *.gif, *.png, *.jpg, and *.jpeg images allowed.'),
    '#default_value' => variable_get('last_5_user_post_image', ''),
    '#upload_location' => 'public://block_image/',
    '#upload_validators' => array(
      'file_validate_extensions' => array('gif png jpg jpeg'),
    ),
  );
  }

  return $form;

}
/**
* Implements hook_block_save()
*/
function last_5_user_posts_block_save($delta = '', $edit = array()) {
if ($delta == 'last_5_user_posts') {

//Set value for maximum number of posts shown
variable_set ('last_5_user_posts_max', $edit['last_5_user_posts_max']);

//Set value for content types to be shown
variable_set ('last_5_user_posts_nodes_toshow',     $edit['last_5_user_posts_nodes_toshow']);

// Saving the file, setting it to a permanent state
// setting a FID variable
$file = file_load($edit['last_5_user_post_image']);
  $file->status = FILE_STATUS_PERMANENT;
  file_save($file);
  $block = block_load('last_5_user_posts', $delta);
  file_usage_add($file, 'last_5_user_posts', 'block', $block->bid);
  variable_set('last_5_user_post_image', $file->fid);

}
}

/**
* Implements validation from the Form API.
*
* @param $form
*   A structured array containing the elements and properties of the form.
* @param $form_state
*   An array that stores information about the form's current state
*   during processing.
 */
function last_5_user_posts_form_validate($form, &$form_state){
$max_num = $form_state['values']['last_5_user_posts_max'];
if (!is_numeric($max_num)){
form_set_error('last_5_user_posts_max', t('You must enter a number for the maximum number of posts to display.'));
 }
 else if ($max_num <= 0){
form_set_error('last_5_user_posts_max', t('Maximum number of posts to display must be positive.'));
  }
}

/**
* Implements hook_theme().
*/
function hook_themelast_5_user_posts_theme($existing, $type, $theme, $path) {
$module_path = drupal_get_path('module', 'last_5_user_posts');
$base = array(
    'path' => "$module_path"
);
return array(
'last_5_user_posts' => $base + array(
  'variables' => array(
    'items' => NULL,
   
  ),
    'template' => 'last_5_user_posts--block'
),

 );
}
<?php
/**
* Implements hook_help().
*/
function last_5_user_posts_help($path, $arg) {
   switch ($path) {
    case "admin/help#last_5_user_posts":
      return '<p>' . t("Show last 5 nodes of active user") . '</p>';
      break;
  }
} 

/**
* Implements hook_block_info().
*/
function last_5_user_posts_block_info() {
  $blocks['last_5_user_posts'] = array(
     // The name that will appear in the block list.
    'info' => t('My last posts'),
    // Default setting.
    'cache' => DRUPAL_CACHE_PER_ROLE,
  );
  return $blocks;
}


/**
*Colecting data from database
*/
function last_5_user_posts_contents(){
$choosed_nodes = array();
$choosed_nodes = variable_get('last_5_user_posts_nodes_toshow', array(''));
global $user;
$userid = $user->uid;  //current logged user
    //maximum number of posts to be shown
$max_num = variable_get('last_5_user_posts_max', 5); 

$query = db_select('node', 'n')
    ->fields('n', array('nid', 'title', 'created'))
    ->condition('status', 1) //Published.
    ->condition('uid', $userid) //current user
    ->condition('type', $choosed_nodes) //selected node types
    ->orderBy('created', 'DESC') //Most recent first.
    ->range(0,$max_num) //number of posts
    ->execute();
    
    return $query;
}

/**
*Preparing data for showing.
*/
function last_5_user_posts_block_view($delta = '') {

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['list'] = 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', array(                
            'items' => $items));
        
    }
  }

  return $block;

}

}


/**
* Page callback: My last posts settings
* Number of nodes to be shown
*/
function last_5_user_posts_block_configure($delta = '') {
$form = array();
  if ($delta == 'last_5_user_posts') {
  $form['last_5_user_posts_max'] = array(
'#type' => 'textfield',
'#title' => t('Maximum number of posts'),
'#default_value' => variable_get('last_5_user_posts_max', 5),
'#size' => 2,
'#maxlength' => 2,
'#description' => t('The maximum number of links to display in the block.'),
'#required' => TRUE,
);

/*
*Form for content type choose
*/ 
  $types = node_type_get_types();
  foreach($types as $node_type) {
  $nodetypes[$node_type->type] = $node_type->name;
  }
  $form['last_5_user_posts_nodes_toshow'] = array(
  '#type' => 'radios',
  '#title' => t('Select the nodes to show'),
  '#options' => $nodetypes,
  '#default_value' => variable_get('last_5_user_posts_nodes_toshow', array('')),
  '#description' => t('Selected node type will be shown'),
  );

/*
*Form for image upload
*/ 
  $form['last_5_user_post_image'] = array(
    '#name' => 'block_image',
    '#type' => 'managed_file',
    '#title' => t('Choose an Image File'),
    '#description' => t('Select an Image for the My last posts content type list.  Only *.gif, *.png, *.jpg, and *.jpeg images allowed.'),
    '#default_value' => variable_get('last_5_user_post_image', ''),
    '#upload_location' => 'public://block_image/',
    '#upload_validators' => array(
      'file_validate_extensions' => array('gif png jpg jpeg'),
    ),
  );
  }

  return $form;

}
/**
* Implements hook_block_save()
*/
function last_5_user_posts_block_save($delta = '', $edit = array()) {
if ($delta == 'last_5_user_posts') {

//Set value for maximum number of posts shown
variable_set ('last_5_user_posts_max', $edit['last_5_user_posts_max']);

//Set value for content types to be shown
variable_set ('last_5_user_posts_nodes_toshow',     $edit['last_5_user_posts_nodes_toshow']);

// Saving the file, setting it to a permanent state
// setting a FID variable
$file = file_load($edit['last_5_user_post_image']);
  $file->status = FILE_STATUS_PERMANENT;
  file_save($file);
  $block = block_load('last_5_user_posts', $delta);
  file_usage_add($file, 'last_5_user_posts', 'block', $block->bid);
  variable_set('last_5_user_post_image', $file->fid);

}
}

/**
* Implements validation from the Form API.
*
* @param $form
*   A structured array containing the elements and properties of the form.
* @param $form_state
*   An array that stores information about the form's current state
*   during processing.
 */
function last_5_user_posts_form_validate($form, &$form_state){
$max_num = $form_state['values']['last_5_user_posts_max'];
if (!is_numeric($max_num)){
form_set_error('last_5_user_posts_max', t('You must enter a number for the maximum number of posts to display.'));
 }
 else if ($max_num <= 0){
form_set_error('last_5_user_posts_max', t('Maximum number of posts to display must be positive.'));
  }
}

/**
* Implements hook_theme().
*/
function hook_theme($existing, $type, $theme, $path) {
$module_path = drupal_get_path('module', 'last_5_user_posts');
$base = array(
    'path' => "$module_path"
);
return array(
'last_5_user_posts' => $base + array(
  'variables' => array(
    'items' => NULL,
   
  ),
    'template' => 'last_5_user_posts--block'
),

 );
}
<?php
/**
* Implements hook_help().
*/
function last_5_user_posts_help($path, $arg) {
   switch ($path) {
    case "admin/help#last_5_user_posts":
      return '<p>' . t("Show last 5 nodes of active user") . '</p>';
      break;
  }
} 

/**
* Implements hook_block_info().
*/
function last_5_user_posts_block_info() {
  $blocks['last_5_user_posts'] = array(
     // The name that will appear in the block list.
    'info' => t('My last posts'),
    // Default setting.
    'cache' => DRUPAL_CACHE_PER_ROLE,
  );
  return $blocks;
}


/**
*Colecting data from database
*/
function last_5_user_posts_contents(){
$choosed_nodes = array();
$choosed_nodes = variable_get('last_5_user_posts_nodes_toshow', array(''));
global $user;
$userid = $user->uid;  //current logged user
    //maximum number of posts to be shown
$max_num = variable_get('last_5_user_posts_max', 5); 

$query = db_select('node', 'n')
    ->fields('n', array('nid', 'title', 'created'))
    ->condition('status', 1) //Published.
    ->condition('uid', $userid) //current user
    ->condition('type', $choosed_nodes) //selected node types
    ->orderBy('created', 'DESC') //Most recent first.
    ->range(0,$max_num) //number of posts
    ->execute();
    
    return $query;
}

/**
*Preparing data for showing.
*/
function last_5_user_posts_block_view($delta = '') {

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['list'] = 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', array(                
            'items' => $items));
        
    }
  }

  return $block;

}

}


/**
* Page callback: My last posts settings
* Number of nodes to be shown
*/
function last_5_user_posts_block_configure($delta = '') {
$form = array();
  if ($delta == 'last_5_user_posts') {
  $form['last_5_user_posts_max'] = array(
'#type' => 'textfield',
'#title' => t('Maximum number of posts'),
'#default_value' => variable_get('last_5_user_posts_max', 5),
'#size' => 2,
'#maxlength' => 2,
'#description' => t('The maximum number of links to display in the block.'),
'#required' => TRUE,
);

/*
*Form for content type choose
*/ 
  $types = node_type_get_types();
  foreach($types as $node_type) {
  $nodetypes[$node_type->type] = $node_type->name;
  }
  $form['last_5_user_posts_nodes_toshow'] = array(
  '#type' => 'radios',
  '#title' => t('Select the nodes to show'),
  '#options' => $nodetypes,
  '#default_value' => variable_get('last_5_user_posts_nodes_toshow', array('')),
  '#description' => t('Selected node type will be shown'),
  );

/*
*Form for image upload
*/ 
  $form['last_5_user_post_image'] = array(
    '#name' => 'block_image',
    '#type' => 'managed_file',
    '#title' => t('Choose an Image File'),
    '#description' => t('Select an Image for the My last posts content type list.  Only *.gif, *.png, *.jpg, and *.jpeg images allowed.'),
    '#default_value' => variable_get('last_5_user_post_image', ''),
    '#upload_location' => 'public://block_image/',
    '#upload_validators' => array(
      'file_validate_extensions' => array('gif png jpg jpeg'),
    ),
  );
  }

  return $form;

}
/**
* Implements hook_block_save()
*/
function last_5_user_posts_block_save($delta = '', $edit = array()) {
if ($delta == 'last_5_user_posts') {

//Set value for maximum number of posts shown
variable_set ('last_5_user_posts_max', $edit['last_5_user_posts_max']);

//Set value for content types to be shown
variable_set ('last_5_user_posts_nodes_toshow',     $edit['last_5_user_posts_nodes_toshow']);

// Saving the file, setting it to a permanent state
// setting a FID variable
$file = file_load($edit['last_5_user_post_image']);
  $file->status = FILE_STATUS_PERMANENT;
  file_save($file);
  $block = block_load('last_5_user_posts', $delta);
  file_usage_add($file, 'last_5_user_posts', 'block', $block->bid);
  variable_set('last_5_user_post_image', $file->fid);

}
}

/**
* Implements validation from the Form API.
*
* @param $form
*   A structured array containing the elements and properties of the form.
* @param $form_state
*   An array that stores information about the form's current state
*   during processing.
 */
function last_5_user_posts_form_validate($form, &$form_state){
$max_num = $form_state['values']['last_5_user_posts_max'];
if (!is_numeric($max_num)){
form_set_error('last_5_user_posts_max', t('You must enter a number for the maximum number of posts to display.'));
 }
 else if ($max_num <= 0){
form_set_error('last_5_user_posts_max', t('Maximum number of posts to display must be positive.'));
  }
}

/**
* Implements hook_theme().
*/
function last_5_user_posts_theme($existing, $type, $theme, $path) {
$module_path = drupal_get_path('module', 'last_5_user_posts');
$base = array(
    'path' => "$module_path"
);
return array(
'last_5_user_posts' => $base + array(
  'variables' => array(
    'items' => NULL,
   
  ),
    'template' => 'last_5_user_posts--block'
),

 );
}
deleted 3 characters in body
Source Link
Loading
Source Link
Loading