V
V
Vlad1712015-09-23 23:39:54
Drupal
Vlad171, 2015-09-23 23:39:54

How to display the result of a query to the database in a block?

I continue to torment Drupal, and it torments me. The question is next. I am creating a module for displaying a custom block with the result of a selection from the database (news). I can not understand exactly the mechanism for outputting a sample to a block. The code is the following and probably contains many errors, because it does not work:

<?php
function blocknews_block_info() {
  $blocks['fresh_news']=array(
    'info'       => t('Fresh News'),
    'status'     =>TRUE,
    'region'     =>'content',
    'weight'     =>0,
    'visibility' =>1,
  );
  return $blocks;
}

function last_news_block_content()
{
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'node');
    $query->propertyCondition('status', NODE_PUBLISHED);
    $query->propertyCondition('type', 'news'); //Фильтр для простоты не задавал
    $result = $query->execute();
    if (isset($result['node'])) {
        $nodes = entity_load('node', array_keys($result['node'])); 
        $items = drupal_render(node_view_multiple($nodes)); //Вот здесь не понимаю вообще
    }
}

function blocknews_block_view($delta = '') {
    $block = array();

    if ($delta == 'fresh_news') {
        $block['subject'] = 'Last news';//Заголовок блока
        $block['content'] =last_news_block_content();
    }
    return $block;
}

Accordingly, after connecting the module and connecting the block with the region, there is no result. Can anyone help and explain where the error is and how block_view() and block_content() are related?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
heartdevil, 2015-09-24
@Vlad171

Hello.
I don't want to sound pushy, but remember when I told you to read drupal.org?
Do not be afraid to work with the site. There is no escape from this. Anyway, sooner or later you will start visiting the site yourself and working with api.
Here you are interested in blocknews_block_view and last_news_block_content. Go to the site and enter functions in the search engine (such as hook_block_view, be sure to precede the hook, and not your name of the blocknews module). You won't find anything for the second function, because it's a helper function. From your code, you can see that its result is returned in $block['content'] in the blocknews_block_view function. But since you don't actually return anything, content is also empty. And if it is empty, then the block is not displayed.
UPDATE:
Yes. In your case, apparently the block_content function does not return data. try putting return. And more advice. Don't try to figure out where the error is. It is very difficult. Better do it step by step. First, we wrote this blocknews_block_info() function and that's it! Immediately tested in Drupal. If the block is connected, then you can start outputting information to the block. Next, connect this blocknews_block_view function, but in the $block['content'] cell, assign a simple text $block['content'] = "hello world!"; By the name of the variable, it should be clear that it is responsible for the output of the content block. Further, if the text is displayed in the content, then connect the main news output function. Thus localizing your problem.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question