I need to programmatically create a new block. The block must show 3 last news (site content): title as a link + date of publication (format dd-mm-yyyy).

Any suggestions?


The info hook defines your block in the system:

function my_module_block_info() {    
  $blocks['your_block'] = array(
    // info: The name of the block.
    'info' => t('Your Block Name'),

  return $blocks;

and the view hook creates the output (html) for your block:

function my_module_block_view($delta = '') {
  // The $delta parameter tells us which block is being requested.
  switch ($delta) {
    case 'your_block':
      // Create your block content here
      $block['subject'] = t('My last 3 news');

    $query = new EntityFieldQuery();
    //change news by the name of your content type
    $entities = $query->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'news')
    ->propertyOrderBy("created", "DESC")
    ->range(0, 3)

    $block['content'] = '';

    foreach($entities['node'] as $obj) {
      $node = node_load($obj->nid);
          $block['content'] .= l($node->title, "node/" . $node->nid) . ' - ' . date('d-m-Y', $node->created) . '<br>';


  return $block;

Remember assign a region to your block in YOUR_SITE/admin/content/blocks


enter image description here


How do I create a static block programmatically?

| improve this answer | |

You can do this non-programmatically.

If using Drupal 7

Download the Views module.

Enable Views and Views UI

Drupal 8 already comes with Views and Views UI enabled.

Create a new view, uncheck Page View and select Block View

View of: Content. For Type: Select your content type.

Set results to 3.

Click Continue and Edit.

In fields, click the add button, search for Created Date.

You can change/format how the date is displayed. If you want you can use custom and put d-m-Y

Save your view.

Then in Blocks, your view block will be listed there.

I am mentioning this because the asker perhaps is not aware of a non-programmatical solution

| improve this answer | |
  • The OP need programmatically ! – Adrian Cid Almaguer Dec 10 '15 at 0:36
  • Test my answer, that my answer have the word static don't want to say that my solution be static ;-) – Adrian Cid Almaguer Dec 10 '15 at 1:26
  • my answer references have the static word ;-) – Adrian Cid Almaguer Dec 10 '15 at 1:34

To simplify Adrian's Solution. You can achieve this with Views and some code. Create a view that shows last three nodes and then in your block_views function use function views_embed_view() to render your view in data part.

This will save you from complex database queries and at the same time you will achieve your block programmatically.

case 'top_three_nodes':
            $block['subject'] = t('My Top Three Nodes');

            $block['content'] = array(
                '#theme' => 'your_block_theme_name',
                '#top_three_favorites' => views_embed_view('three_nodes_view_name', 'display_name'),

            return $block;
| improve this answer | |
  • you refer to modify the block (core) module? – Adrian Cid Almaguer Dec 11 '15 at 4:54
  • @Adrian Its basically yours solution but the listing comes from views. So the process is to create a views and then embed your view in a block. – Shabir A. Dec 11 '15 at 13:30
  • I think that is a little recursive because you can have a view that make a block – Adrian Cid Almaguer Dec 11 '15 at 17:57

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