Altering the contents of a block with a link click

Dear everyone,

I’m building a module that constructs 2 blocks: messaging-menu and messaging-body. The messaging-body block has the content “This is the original content”. I have added a hyper link within the content of the messaging-menu block, and I want the module to change the content of the messaging-body block to “This is the new content” using hook_block_view_alter() when the user clicks the link. Here is what I did: I created a menu item ‘change_content_url’. When the user clicks the link, this will be loaded and the ‘change_content_function()’ will be called. the ‘change_content_function()’ changes the variable $selection from ‘original’ to ‘new’. Which is when I planned that ‘user_messaging_block_view_alter’ will kick in and do the required changes. This is the module:

function user_messaging_menu() {
    global $selection;
    $selection = 'original';
	
 $items['change_content_url] = array(
    'title' => 'Change content',
    'page callback' => 'change_content_function',
    'access callback' => TRUE,
    );
    return $items;
} 

function user_messaging_block_info() {
    $blocks['messaging-menu'] = array(
               'info' => t('User messaging: Menu'),
                );
    $blocks['messaging-body'] = array(
                'info' => t('User messaging: Body'),
                );
  return $blocks;
}

function user_messaging_block_view($delta = '') {
  switch ($delta) {
    case 'messaging-menu':
      $block['subject'] = t('messaging-menu');
      $block['content'] = user_messaging_contents($delta);
      break;
	
    case 'messaging-body':
      $block['subject'] = t('messaging-body');
      $block['content'] = user_messaging_contents($delta);
      break;
     }
  return $block;
}

function user_messaging_contents($which_block) {
  switch ($which_block) {
  case 'messaging-menu':
      $result = 'http://localhost/drupal/change_content_url/';
	  return $result;

  case 'messaging-body':
	  $result = 'This is the original content';
	  return $result;
	  return;
  }
}

function change_content_function() {
   global $selection;
   $selection = 'new';
 }

function user_messaging_block_view_alter(&$data, $block) {
  global $selection;
  if ($block->module == 'user_messaging' && $block->delta == 'messaging-body' && $selection == 'new') {
    $data['content'] = '<p>This is the new content</p>';
  }
}

This is the first time I’m using the hook_block_view_alter() function. Now I know that the function works because when I remove the last part in the if statement condition ‘ selection == ‘new’ ‘, the content changes. But I need the content to remain as the original and change only when the user clicks the link. Can anyone please direct me in the correct direction ? Thank you all very much in advance.

Drupal version: 


Source: https://www.drupal.org/taxonomy/term/4/feed