I have build a custom block for Drupal 7 where users can display any nodes in block from chooseing any content type and choose to display any number of nodes:
I managed to get it worked with displaying nodes and chooseing what type of content to display. Only problem is how many of them to display.
Example of a code is:
function custom_block_configure() {
$types = node_type_get_types();
foreach ($types as $node_type) {
$nodetypes[$node_type->type] = $node_type->name;
}
$form['example_nodes_toshow'] = array(
'#type' => 'checkboxes',
'#title' => t('Select the nodes to show'),
'#options' => $nodetypes,
'#default_value' => variable_get('example_nodes_toshow', array('')),
'#description' => t('All the node types selected will be shown'),
);
$form['example_number_display'] = array(
'#type' => 'textfield',
'#title' => t('Enter number of node to display in block'),
'#default_value' => variable_get('example_number_display', array('')),
'#description' => t('Display any number of nodes'),
'#size' => 5,
);
return system_settings_form($form);
}
All forms are displayed in a block configuration. The first form is for users to choose to display content types and second form is only for chooseing how many of them to display, so from 1 - 5.
After this point I am not sure if its right and I did not finished any code for example_number_display, but you are there to help me.
Next code is:
function example_block_save($delta = '', $edit = array(), $number_limit = array()) {
if ($delta == 'customblock') {
variable_set('example_nodes_toshow', $edit['example_nodes_toshow']);
variable_set('example_number_display', $number_limit['example_number_display']);
}
}
Next code is block_view with some query for creating a list of nodes and displaying it:
function example_block_view($block_name = '') {
if ($block_name == 'exampleblock') {
//Get the selected node types and create a list of them
$show_nodes_list = array();
$show_nodes_array = variable_get('example_nodes_toshow', array(''));
foreach ($show_nodes_array as $key => $value) {
if ($value) {
array_push($show_nodes_list, $value);
}
}
global $user;
//Based on the node types create a query and then load the node types
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'node')
->entityCondition('bundle', $show_nodes_list)
->propertyCondition('status', 1)
->propertyCondition('uid', $user->uid)
->propertyOrderBy('created', 'DESC')
->range(0, 5);
$result = $query->execute();
$nodes = array();
if (isset($result['node'])) {
$nids = array_keys($result['node']);
$nodes = entity_load('node', $nids);
}
//Loop through the loded nodes to create a list
$list = array();
$i = 0;
foreach ($nodes as $node) {
$options = array('absolute' => TRUE);
$url = url('node/' . $node->nid, $options);
$list[$i] = '<a href=' . $url . '>' . $node->title . '</a>';
$i++;
}
//Return the content of the block
$theme_args = array('items' => $list, 'type' => 'ol');
$content = theme('item_list', $theme_args);
$block = array(
'subject' => t('Show Nodes Block'),
'content' => $content,
);
return $block;
}
}
In the abowe code I insert range (0, 5) to limit display of nodes in block but I cant manage to get it connected with example_number_display or is there any other simple solution.