hook_block_view_alter Implementation

2.6k views Asked by At

I want to change the below function,

function user_block_view($delta = '') {         // Line 1 //
  global $user;                                 // Line 2 //
  $block = array();                             // Line 3 //
  switch ($delta) {                             // Line 4 //
    case 'login':                               // Line 5 //
      if (!$user->uid && !(arg(0) == 'user' && !is_numeric(arg(1)))) {
        $block['subject'] = t('User login');    // Line 7 //
        $block['content'] = drupal_get_form('user_login_block');
      }                                         // Line 9 //
    return $block; // Line 10
  ...
} // Line n

I actually want to add

else if($user -> uid){
  $block['subject'] = t('Subject'); 
  $block['content'] = someotherfunction();
}

after the line 9 of the above code.

How can I implement that in my template.php (custom theme)

I tried a lot and didn't succeed yet.

2

There are 2 answers

2
Michal On BEST ANSWER
<?php
function phptemplate_preprocess_block(&$vars) {    
    if (isset($vars['block'])) {
        if($vars['block']->module == 'user') {
            global $user;
            if (!$user->uid && !(arg(0) == 'user' && !is_numeric(arg(1)))) {
                $vars['block']->subject = t('User login');
                $vars['block']->content = drupal_get_form('user_login_block');
            }
            else if($user->uid){
                $vars['block']->subject = t('Subject');
                $vars['block']->content = someotherfunction();
            }
        }
    }
}

That should work.

Here is more info: how to override $block->content in drupal?

1
Bere On
  switch ($delta) {                             // Line 4 //
    case 'login':                               // Line 5 //
      if (!$user->uid && !(arg(0) == 'user' && !is_numeric(arg(1)))) {
        $block['subject'] = t('User login');    // Line 7 //
        $block['content'] = drupal_get_form('user_login_block');
      }  
     else if($user->uid){  // Line 9 //
       $block['subject'] = t('Subject'); 
       $block['content'] = someotherfunction();
      }                                     
    return $block; // 

But remove all the spaces between $user -> uid.