Hello... I am new to Drupal..

Does anyone have a simple module that extracts some info from a db, and display it in a block?

I tried to do an example from online, and have come across an error as follows:

"Fatal error: Cannot use string offset as an array in {form.inc} on line 680"

Using the following function:

function mymodule_block() {

$block = array ('subject' => t('Title'),
'content' => "This is the content");

return $block;
}

There were other statements in the function, but stripped them off to isolate and fix the error... but even this basic function is causing the error.

This function is being called from a menu as a callback argument

Using Drupal 5.x and PHP 5.2.5

Any help would be appreciated...

Comments

nevets’s picture

If you are writing a module that provides a block there is no need for a menu callback, Also I suspect your callback is defined wrong but without seeing your menu hook can not be sure.

But give you want to provide a block, what you need is a block hook and while you have one it is defined incorrectly. See http://api.drupal.org/api/function/hook_block/5 for more information on the hook, At the end of the description is a link to an example of the hook. From your question you only need the cases where $op is 'list' or 'view'.

anilnair’s picture

Thank you for your quick response...

I added the menu call back for quick testing .. by clicking on the menu, rather than modifying the URL.. :)

What I have done is saved a sample module that I found on the net and trying to decipher it, and then modify it to what I need...

The module that i had downloaded was extracting the information for nodes that were created during a specific period, and displaying it.. pretty simple huh? This was not working.. so that is when I started modifying the code to track down the problem..

With the help that you provided, I added the $op... the module works when I have if ($op == 'view') { add stuff to block }.. Because, the $op's value is not 'view'.. it is ''. The module follows:

I added 'My Module's Data' to Primary Links and 'My Module Settings' to Administer

function mymodule_menu() {

$items = array();

$items[] = array(
'path' => 'admin/settings/mymodule',
'title' => t('My Module settings'),
'description' => t('Description of My Module settings control'),
'callback' => 'drupal_get_form',
'callback arguments' => 'mymodule_admin',
'access' => user_access('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);

$items[] = array(
'path' => 'admin/settings/mymodule_block',
'title' => t("My Module's Data"),
'description' => t('View Data'),
'callback' => 'drupal_get_form',
'callback arguments' => 'mymodule_block',
'access' => user_access('access mymodule content'),
'type' => MENU_NORMAL_ITEM,
);


return $items;
}

/**
* Display help and module information
* @param section which section of the site we're displaying help 
* @return help text for section
*/
function mymodule_help($section='') {

  $output = '';

  switch ($section) {
    case "admin/help#mymodule":
      $output = '<p>'.  t("Displays links to nodes created"). '</p>';
      break;
  }

  return $output;
} // function mymodule_help

/**
* Valid permissions for this module
* @return array An array of valid permissions for the mymodule module
*/
function mymodule_perm() {

return array('access mymodule content', 'administer mymodule');

} // function mymodule_perm



function newmodule_perm() {

return array('access newmodule', 'create newmodule', 'administer newmodule');

} // function newmodule_perm


/**
* Generate HTML for the mymodule block
* @param op the operation from the URL
* @param delta offset
* @returns block HTML 
*/

function mymodule_block($op='list', $delta=0) {

$block = array();

 if ($op == 'view') {
     $block = array ('subject' => t('My Module Data'), 'content' => 'Hello');
 }
 return $block;
}


function mymodule_admin() {

$form['mymodule_maxdisp'] = array(
'#type' => 'textfield',
'#title' => t('Maximum number of links'),
'#default_value' => variable_get('mymodule_maxdisp', 3),
'#size' => 2,
'#maxlength' => 2,
'#description' => t("The maximum number of links to display in the block.")
);

return system_settings_form($form);
}
nevets’s picture

A couple of things.

You can not call a hook_block from a menu callback (at least not easily).

The hook_block function also needs to handle $op == 'list' so it will show in the block admin page (otherwise you can not enable it).

Also regarding this menu entry

$items[] = array(
	'path' => 'admin/settings/mymodule_block',
	'title' => t("My Module's Data"),
	'description' => t('View Data'),
	'callback' => 'drupal_get_form',
	'callback arguments' => 'mymodule_block',
	'access' => user_access('access mymodule content'),
	'type' => MENU_NORMAL_ITEM,
);

You are trying to use 'drupal_get_form' as the callback but it expects a function that produces a form (not general a block) so that is where one of your errors from.

anilnair’s picture

Thank you nevets:

I changed my _block to a function that creates form fields and returns the form, and it is working...

.. now off to control/change the layout (look and feel) of the form... :)

nevets’s picture

You should be aware you are headed for trouble. Functions of the form {module_name}_block() are special, they are used to construct the output for the blocks you see. Though a block can produce a form, it should not do it through s menu callback.

anilnair’s picture

Thanks once again for that 'eye opening' tip..

I did put back the _block, and within the block, I updated the content with the return value of the function that sets up the form. When the block is enabled, I am able to see the form on my page...

Now I have to add insert/update/reset functionalities for the form.. As a start, I am just getting some data from db, and displaying it..

BTW.. this is my start of a bigger module, which will have google maps enabled, where you can click on the map and have the lat/long etc populated into these form fields ..

Thanks again..

tanoshimi’s picture

Have you tried looking at the gmap module (http://drupal.org/project/gmap)?

From memory, that already contains a similar feature (setting the lat/lon of a node by clicking on an embedded google map) and integrates with location.module ... you might want to check it out.

t.

anilnair’s picture

Thanks tanoshimi :

I looked at this module.. will need much overhauling to get it do what I want..

Also, I read in one of the threads that someone was suggesting this module be 'removed' because it does not have the proper functionalities..

Not sure if anyone has 'updated' and replaced that module with a better one...