0

I am working on a project where I first was only designing the theme. However, I now need to create 5 already made custom pages or blocks in a page, which will be available as soon as theme is enabled.

Each page should hold its own code for grabbing information from external database. Now I am having a huge dilemma on deciding how to create those pages. Since there are DB calls made on each of them, I was suggested to create a custom module, which would display a block with the printed content from database. I tried creating "pvpstats.module" and "pvpstats.info" as normal for custom modules, and displaying it in sidebar_first. After I enable the module, nothing happens. The block is just not visible. I used the code below.

function pvpstats_block_info() {
  $blocks['pvpblock'] = array(
    'info' => t('Example Block'),
    'status' => TRUE,
    'region' => 'sidebar_first',
    'weight' => 0,
    'visibility' => 0,
    'pages' => 'admin*
coder*
node/add*
node/*/edit
node/*/devel
',
  );

  return $blocks;
}

/**
  * Implements hook_block_view().
  */
function pvpstats_block_view($delta = '') {
  $block = array();

  switch ($delta) {
    case 'pvpblock':
      $block['subject'] = t('Block Name');
      $block['content'] = theblock_contents();
      break;
  }

  return $block;
}

function theblock_contents() {
  return '
    $dbserver="serveradress"; 
    $dblogin="username";       
    $dbpass="dbpassword";     
    $dbname="databaseserver";    

                mysql_connect($dbserver, $dblogin, $dbpass);
                mysql_select_db($dbname);
                mysql_query("SET NAMES 'utf8'");

                $vypis =  mysql_query("SELECT * FROM characters WHERE (accesslevel < '1') AND (pvpkills > '0') order by pvpkills desc LIMIT 20"); 
                echo '<table><tr><td><center>Nick</center></td><td></td><td>Kills</td></tr>';
         while($row = mysql_fetch_array($vypis))
                    {
                echo '<tr><td><font color="green">';
                echo $row["char_name"];
                echo '</font></td><td>&nbsp;&nbsp;</td><td><font color="blue">';
                echo $row["pvpkills"];
                echo '</font></td></tr>';

                    }
    echo '</table>';
    ?> 

The main point is that I need those blocks later included in my theme so that they are visible immediately after enabling the theme, if that is somehow possible.

  • 1
    As side note, you need to rewrite the code of theblock_contents(), since that is not Drupal code. You don't call any mysql_ function in a module. – kiamlaluno Sep 5 '13 at 9:43
1

In theblock_contents you are trying to return php code. Don't. Do not echo or print in it. If anything, build a string. It should look about like that:

/**
* custom html block
* @return string
*/
function theblock_contents() {
  $dbserver="serveradress"; 
  $dblogin="username";       
  $dbpass="dbpassword";     
  $dbname="databaseserver";    

  $result = '';

  mysql_connect($dbserver, $dblogin, $dbpass);
                mysql_select_db($dbname);
                mysql_query("SET NAMES 'utf8'");

  $vypis =  mysql_query("SELECT * FROM characters WHERE (accesslevel < '1') AND (pvpkills > '0') order by pvpkills desc LIMIT 20"); 
  $result .= '<table><tr><td><center>Nick</center></td><td></td><td>Kills</td></tr>';
  while($row = mysql_fetch_array($vypis))
  {
    $result .= '<tr><td><font color="green">';
    $result .= $row["char_name"];
    $result .= '</font></td><td>&nbsp;&nbsp;</td><td><font color="blue">';
    $result .= $row["pvpkills"];
    $result .= '</font></td></tr>';
  }
  $result .= '</table>';
  return $result;
}

It's worth to mention that your code was plainly invalid PHP, no matter if it was supposed to run in Drupal or not. You can't simply return PHP code as string and hope it'll be executed. Only the part about not using echo in hooks is really Drupal dependent.

Important note: if you want your modules to work stable, please don't use PHP SQL functions directly. Use db_select to obtain data. Connecting to multiple databases is pretty well documented too.

| improve this answer | |
  • Of course after this you may encounter other bugs, but that will be another question. – Mołot Sep 5 '13 at 9:39
  • Actually your answer resolved my issue without adding any to it. The PvP stats are now correctly displayed in the assigned region. Thank you very much! – risha riss Sep 5 '13 at 9:48
0

You can associate a block automaticly with a theme's region. (I've done it in a module.install file.) By default a block in not visible; you have to choose manually a theme's region, or you can use code similar to the following one to add it programmatically.

  $default_theme = variable_get('theme_default', 'bartik');
  $values = array(
    array(
      'module' => 'block',
      'delta' => 'pvpblock',
      'theme' => $default_theme,
      'status' => 1,
      'weight' => 1,
      'region' => 'sidebar_first',
      'pages' => '',
      'cache' => -1,
    ),
  );

  $query = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'pages', 'cache'));
  foreach ($values as $record) {
    $query->values($record);
  }
  $query->execute();
| improve this answer | |
  • May i ask where should i include your code to make block visible? Is it in themes template.php or in pvpblock.module.php? Thank you – risha riss Sep 5 '13 at 8:54
  • have you tried to set the block visible by the UI ? I mean on this page : "admin/structure/block/list/yourthemename" – pico34 Sep 5 '13 at 8:59
  • The custom block is not visible for me to assing a region to it, under structure/blocks, after enabling the custom module. So i guess something went wrong in module code? – risha riss Sep 5 '13 at 9:05
  • clear the cache :) – pico34 Sep 5 '13 at 9:10
  • That was the first thing i tried but it didn't help. – risha riss Sep 5 '13 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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