0

I want to render an array and print the output as Block[content]. There is either my Foreach or the way I use $item array or $row that is wrong. I will appreciate your help.

The result i get is the HTML I define in Block[content] without the values of $row->uri, $row->title, $row->body_value. They are empty. And it means I have done something wrong.

function produkt_block_view($delta = '')
{

  $block = array();

  $list = produkt_produkliste(1);    
  $items = array();
  foreach ($list as $row) {
    $items[1] = $row->uri;
    $items[2] = $row->title;
    $items[3] = $row->body_value;
  }


 switch($delta) {
  case 'produkt' :
    $block['content'] = '<a href="/19">';
    $block['content'] .= '<img class="pimg" src="'.$row->uri.'" alt="Se alle vores filter elementer her">';          
    $block['content'] .= '<p class="pname">"'.$row->title.'"</p>';
    $block['content'] .= '<p>"'.$row->body_value.'"</p>';      
    $block['content'] .= theme ('item_list',array('items' => $items, 'type' => 'ol',   'attribute' => 'produkt'));      
    break;   
  }

  return $block;
}

And I ended with this solution that get some results.

function produkt_block_view($delta = '')
{

   $query  = db_query("SELECT node.title,    field_data_field_kortbeskrivelse.field_kortbeskrivelse_value, file_managed.uri
    FROM node 
    JOIN field_data_field_tilh_rsforhold ON
    node.nid = field_data_field_tilh_rsforhold.entity_id

    JOIN field_data_field_kortbeskrivelse ON
    node.nid = field_data_field_kortbeskrivelse.entity_id

    JOIN field_data_field_billede ON
    node.nid = field_data_field_billede.entity_id

    JOIN file_managed
    ON field_data_field_billede.field_billede_fid = file_managed.fid  


    WHERE field_data_field_tilh_rsforhold.field_tilh_rsforhold_target_id = 30
    AND  field_data_field_billede.bundle = 'produkt_kategori'
    "

      );



    $rows   = array();
    foreach ($query as $row) 
    {
        $rows[] = array(
        $row->title,
        $row->field_kortbeskrivelse_value,
        $row->uri
      );
            print_r($row->uri);

            $block['content'] = '<a href="' . url(19) . '"><img class="pimg" src=/"'.   $row->uri . '" alt="Se alle vores filter elementer her" /></a>'; // assuming that anchor tag will wrap img          
            $block['content'] .= '<p class="pname">' . $row->title . '</p>';
            $block['content'] .= '<p>' . $row->field_kortbeskrivelse_value . '</p>';      
            $block['content'] .= theme ('item_list',array('type' => 'ol',     'attribute' => 'produkt'));      

    }


  return $block;
 }

closed as unclear what you're asking by Mołot, Елин Й., Free Radical, 4life, marcvangend Sep 16 '14 at 9:24

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • What you expected to happen and what actually happened? You want to do something, you have a piece of code, but what's your problem? We will not create test environments only to test your code, if you will not even tell us what exactly you are trying to ask. – Mołot Aug 29 '14 at 12:09
  • The result i get is the HTML I define in Block[content] without the values of $row->uri, $row->title, $row->body_value. They are empty. And it means I have done something wrong. – Mahyar Farshi Aug 29 '14 at 12:15
  • Please use edit link. – Mołot Aug 29 '14 at 12:16
0

I don't think $block['content'] = '<a href="/19">'; $block['content'] .= '<img class="pimg" src="'.$row->uri.'" alt="Se alle vores filter elementer her">';... will work, . is a string(concatination) operator. You should create render array and assign it to $block['content'], example:

$array = array('#markup' => 'SOME HTML');
$block['content'] = render($array);

EDIT:

After below comments, I played a little with hook_block_view hook, and I found out some information about it:

  • $block[subject] doesn't affect to its content even if you don't set it; content will be displayed without any subject (so, your missing subject doesn't cause any error here, as j.Reynolds mentioned);
  • $block['content'] can be string value and you don't need to render it, as Molot andj.Reynolds mentioned below, and you can concatinate it as many times as you want:)

So, if we return back to your snippet, you can try something like this:

$block['content'] = '<a href="' . url(19) . '"><img class="pimg" src="'. image_style_url('STYLE_NAME:E.G:LARGE', $row->uri) . '" alt="Se alle vores filter elementer her" /></a>'; // assuming that anchor tag will wrap img          
$block['content'] .= '<p class="pname">' . $row->title . '</p>';
$block['content'] .= '<p>' . $row->body_value . '</p>';      
$block['content'] .= theme ('item_list',array('items' => $items, 'type' => 'ol',   'attribute' => 'produkt'));

I don't know what $row->uri looks like in your code, but I assumed that it is something like public://FILE_NAME.EXT, if so you can use image_style_url function to generate image src..

  • Actually $block['content'] may be a string, nothing extremely wrong with that. Renderable array should be preferred, but string is not a bug in itself, either. – Mołot Aug 29 '14 at 12:38
  • @molot you are right. It can be a string. The code however has a lot wrong with it. I don't see a $block['subject'] which will be an issue. Of lesser issue is the non-closing anchor tag and the attribute key instead of attributes. And shouldn't attributes be an array? – J. Reynolds Aug 29 '14 at 12:47
  • @Mołot I see I've confused a little bit.. yes, you're right. But, block content won;t be displayed unless you render the content I think – xurshid29 Aug 29 '14 at 12:47
  • 1
    The content must not be rendered. Theme does the rendering and returns html. You must populate $block['content'] with a string or a render array. – J. Reynolds Aug 29 '14 at 12:53
  • This is from the documentation - subject: The default localized title of the block. If the block does not have a default title, this should be set to NULL. content: The content of the block's body. This may be a renderable array (preferable) or a string containing rendered HTML content. If the content is empty the block will not be shown. Which means OP's not setting the $block['subject'] is the show stopper. (or one of them) – J. Reynolds Aug 29 '14 at 12:56

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