Hi,

I'm a newby with Drupal, but I have written my own module in Drupal 7.
The module is written for private use and you can insert a URL with the logo of that webpage. It works fine :)

Now i need a 'edit' page zo I make in mymodule.module the following code in the function mymodule_menu():

  $items['admin/config/media/mymodule/bewerken/%'] = array(
	'title' => 'Bewerk mymodule',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('mymodule_admin_edit_load', '5'),
    'access arguments' => array('administer news feeds'),
    'file' => 'mymodule.admin.inc',
  );

In mymodule.admin.inc at the function 'mymodule_admin_edit_load' I have the following code:

function mymodule_admin_edit_load($adv_id) {

$vara = $adv_id;
$uid = 23;
$result = db_query('SELECT *
FROM {mymodule} 
WHERE ssqd = :uid', array(':uid' => $uid));
	
	foreach ($result as $feed) {
		$url= $feed->url;
	}
	
	$form['#attributes'] = array('enctype' => "multipart/form-data");
  $form['name'] = array('#type' => 'textfield',
    '#title' => t('URL'),
    '#value' => isset($url) ? $url: '',
    '#maxlength' => 255,
    '#description' => t('URL of the webpage'),
    '#required' => FALSE,
  );
   $form['image'] = array('#type' => 'file',
    '#title' => t('Afbeelding'),
    '#maxlength' => 255,
    '#required' => FALSE,
  );
  $form['submit'] = array(
	'#type' => 'submit',
	'#value' => t('Submit'),
  );

	return $form;
 }

Now I see the URL '23' because I have set de :uid = $uid.
When I edit that to $vara or $adv_id I've got the following error:

Incorrect syntax near '='.: SELECT * FROM {mymodule} WHERE ssqd = ; Array ( ) in mymodule_admin_edit_load()

I have try a lot of things, but I don't know how I can fix me.
Does anybody a idea how I can fix this?

Thanks in advance!

Comments

gff-adrian’s picture

Hi, did you try to remove the single quotes in the page arguments?

    'page arguments' => array('mymodule_admin_edit_load', 5),

Actually you could also use

  $items['admin/config/media/mymodule/bewerken/%mymodule_admin_edit'] = array(
    ...
  );

This way, Drupal will automatically call mymodule_admin_edit_load(23) and then use the result for the page callback.

    'page arguments' => array(5),

Of course, then you will build the form in another function that is only the page callback, and in your mymodule_admin_edit_load() you will only load the data.

Best regards
AS

pv91’s picture

Thanks for your comment AS.

When I try your answer It wouldn't work.

I use the following code:

$items['admin/config/media/mymodule/bewerken/%mymodule_admin_edit'] = array(
	'title' => 'Bewerk advertentie',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('mymodule_admin_edit_load', 5),
    'access arguments' => array('administer news feeds'),
    'file' => 'mymodule.admin.inc',
  );

At that moment, I don't see the edit form.
When I remove 'mymodule_admin_edit' at $items, I see the form, I can send it (it goes wrong but okay).
The only thing is that the variable after /bewerken/ not work.

BUT I found another thing what te problem is I guess.

In

function mymodule_admin_edit_load($adv_id) { 
...
}

I set print_r($adv_id) ;

The output is Array ( ) .

Maybe is that 'the' problem??

Best regards,
PV

gff-adrian’s picture

Change

    'page arguments' => array('mymodule_admin_edit_load', 5),

to

    'page arguments' => array('mymodule_admin_edit', 5),

and then split the function mymodule_admin_edit_load into two functions.

The first one will only load the object that has to be edited and returns it.

function mymodule_admin_edit_load($adv_id) {
  $vara = $adv_id;
  $uid = 23;
  $result = db_query('SELECT *
  FROM {mymodule}
  WHERE ssqd = :uid', array(':uid' => $uid));
  foreach ($result as $feed) {
    $url= $feed->url;
  }
  return $url;
}

The second one will define the form.

function mymodule_admin_edit($form, &$form_state, $edit = array()) {
  $form['#attributes'] = array('enctype' => "multipart/form-data");
  $form['name'] = array('#type' => 'textfield',
    '#title' => t('URL'),
    '#value' => isset($url) ? $url: '',
    '#maxlength' => 255,
    '#description' => t('URL of the webpage'),
    '#required' => FALSE,
  );
   $form['image'] = array('#type' => 'file',
    '#title' => t('Afbeelding'),
    '#maxlength' => 255,
    '#required' => FALSE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form;
}

Best regards
AS

pv91’s picture

Thanks for your answer,

I have insert your source in mine (mymodule.admin.inc) and I don't have an error but I don't see a URL...

Well I see the input fields and a they are standing in a <form>..</form> tag. :D

Also 1 thing,

$vara = $adv_id;
  $uid = 23;
  $result = db_query('SELECT *
  FROM {mymodule}
  WHERE ssqd = :uid', array(':uid' => $uid));

$adv_id is the id who where given in the % wildcard.
I have edit the $uid in de query to $adv_id but it wouldn't work....

I guess a small thing?

gff-adrian’s picture

Do you know Watchdog? This is a function you can call in your code to save information in a log manner.
The logs can be watched via /admin/reports/dblog.

Load

So try the following in your load-function:

function mymodule_admin_edit_load($uid) {
  $result = db_query('SELECT *
  FROM {mymodule}
  WHERE ssqd = :uid', array(':uid' => $uid));
  foreach ($result as $feed) {
    $url= $feed->url;
  }
  watchdog('mymodule', '$url = %url', array('%url' => $url));
  return $url;
}

and check what there is in the log. Of course you have to visit the page with the form :-).

Form

Then try the "same" thing on your form-function:

function mymodule_admin_edit($form, &$form_state, $edit = array()) {
  watchdog('mymodule', '$edit = %edit', array('%edit' => print_r($edit, true)));

  $form['#attributes'] = array('enctype' => "multipart/form-data");
  ...

What would there be in your $edit variable? Usually, there should be the output of the load-function.

Best regards
AS

pv91’s picture

I have done a watchdog with your code and here is the log:

Type mymodule
Date 04-03-2011 09:20
User admin
Location http://localhost/admin/config/media/mymodule/bewerken/26?render=overlay
Referrer http://localhost/
Message $edit = 26
Severity notice
Hostname ::1
Operations
pv91’s picture

Can you help me?

Vj’s picture

The callback is proper. your url must be sitename/admin/config/media/mymodule/bewerken/integer if ssqd is an integer.

so when you go to link sitename/admin/config/media/mymodule/bewerken/20

then $uid is 20

$items['admin/config/media/mymodule/bewerken/%'] = array(
    'title' => 'Bewerk mymodule',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('mymodule_admin_edit_load', '5'),
    'access arguments' => array('administer news feeds'),
    'file' => 'mymodule.admin.inc',
  );

At the function some changes and do clear cache whenever change in callback.

function mymodule_admin_edit_load(&$form_state, $adv_id) {

$uid = $adv_id;
$result = db_query('SELECT * FROM {mymodule} WHERE ssqd = :uid ', array(':uid' => $uid));
	foreach ($result as $feed) {
		$url= $feed->url;
	}
	
  $form['#attributes'] = array('enctype' => "multipart/form-data");
  $form['name'] = array('#type' => 'textfield',
    '#title' => t('URL'),
    '#value' => isset($url) ? $url: '',
    '#maxlength' => 255,
    '#description' => t('URL of the webpage'),
    '#required' => FALSE,
  );
   $form['image'] = array('#type' => 'file',
    '#title' => t('Afbeelding'),
    '#maxlength' => 255,
    '#required' => FALSE,
  );
  $form['submit'] = array(
	'#type' => 'submit',
	'#value' => t('Submit'),
  );
  return $form;
}

also instead of doing
$result = db_query('SELECT * FROM {mymodule} WHERE ssqd = :uid ', array(':uid' => $uid));
foreach ($result as $feed) {
$url= $feed->url;
}

you can use

$url = db_result(db_query('SELECT field_url FROM {mymodule} WHERE ssqd = :uid ', array(':uid' => $uid)))

pv91’s picture

I don´t see a form.

Vj’s picture

'access arguments' => array('administer news feeds'),

replace with

'access callback' => TRUE,

clear cache and try

pv91’s picture

This is my source now:
mymodule.module:

  $items['admin/config/media/mymodule/bewerken/%'] = array(
	'title' => 'Bewerk mymodule',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('mymodule_admin_edit_load', 5),
    'access arguments' => array('administer news feeds'),
	'access callback' => TRUE,
    'file' => 'mymodule.admin.inc',
  );

mymodule.admin.inc:

function mymodule_admin_edit_load(&$form_state, $adv_id) {

$uid = $adv_id;
$result = db_query('SELECT * FROM {mymodule} WHERE ssid = :uid ', array(':uid' => $uid));
foreach ($result as $feed) {
$url= $feed->url;
}

  $form['#attributes'] = array('enctype' => "multipart/form-data");
  $form['name'] = array('#type' => 'textfield',
    '#title' => t('URL'),
    '#value' => isset($url) ? $url: '',
    '#maxlength' => 255,
    '#description' => t('URL of the webpage'),
    '#required' => FALSE,
  );
   $form['image'] = array('#type' => 'file',
    '#title' => t('Afbeelding'),
    '#maxlength' => 255,
    '#required' => FALSE,
  );
  $form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
  );
  return $form;
}

When I visit '/admin/config/media/mymodule/bewerken/20' I only see the message who I have set in mymodule.module in the function mymodule_help($path, $arg)....

Vj’s picture

some changes for drupal 7, I am not sure but this code worked for me hope it will work to you also

$items['admin/config/media/mymodule/bewerken/%'] = array(
    'title' => 'Bewerk mymodule',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('mymodule_admin_edit_load', 5),
    'access callback' => TRUE, // So any user can access it.
    'file' => 'mymodule.admin.inc',
  );

mymodule.admin.inc:

function mymodule_admin_edit_load(&$form_state, $adv_id) {
$uid = $adv_id['build_info']['args'][0];     //please add this line for drupal 7
$result = db_query('SELECT * FROM {mymodule} WHERE ssid = :uid ', array(':uid' => $uid));
foreach ($result as $feed) {
$url= $feed->url;
}

  $form['#attributes'] = array('enctype' => "multipart/form-data");
  $form['name'] = array('#type' => 'textfield',
    '#title' => t('URL'),
    '#value' => isset($url) ? $url: '',
    '#maxlength' => 255,
    '#description' => t('URL of the webpage'),
    '#required' => FALSE,
  );
   $form['image'] = array('#type' => 'file',
    '#title' => t('Afbeelding'),
    '#maxlength' => 255,
    '#required' => FALSE,
  );
  $form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
  );
  return $form;
}

clear cache and try
previously i was trying with drupal 6 this one is for drupal 7

pv91’s picture

It doesn't work....

I only see the message who I have set in mymodule.module in the function mymodule_help($path, $arg)....

Vj’s picture

this is mymodule.module

<?php

/**
 * @file
 * create a form.
 */

function multipart_menu(){
  $items = array();
  $items['admin/config/media/mymodule/bewerken/%'] = array(
    'title' => 'Bewerk mymodule',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('mymodule_admin_edit_load', 5),
    'access callback' => TRUE, // So any user can access it.
  );
  return $items;
}

function multipart_pager_display_nodes(&$form_state, $adv_id) {
  $uid = $adv_id['build_info']['args'][0];     //please add this line for drupal 7
  $result = db_query('SELECT * FROM {user} WHERE uid = :uid ', array(':uid' => $uid));
  foreach ($result as $feed) {
    $url= $feed->mail;
  }
  
  $form['#attributes'] = array('enctype' => "multipart/form-data");
  $form['name'] = array('#type' => 'textfield',
    '#title' => t('URL'),
    '#value' => isset($url) ? $url: '',
    '#maxlength' => 255,
    '#description' => t('URL of the webpage'),
    '#required' => FALSE,
  );
   $form['image'] = array('#type' => 'file',
    '#title' => t('Afbeelding'),
    '#maxlength' => 255,
    '#required' => FALSE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form;  
}

take a backup and remove all other stuff
clear cache
put just this
go to http://localhost/sitename/admin/config/media/mymodule/bewerken/2
it gives form filled with url = email id of user having uid 2

pv91’s picture

* Notice: Undefined index: mymodule_admin_edit_load in drupal_retrieve_form() (line 736 of C:\Users\*******\includes\form.inc).
* Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'mymodule_admin_edit_load' not found or invalid function name in drupal_retrieve_form() (line 771 of C:\Users\*******\includes\form.inc).

Vj’s picture

sorry working on two things at a time messed up the code

'page arguments' => array('mymodule_admin_edit_load', 5),

replace with

'page arguments' => array('multipart_admin_edit_load', 5),

also function name
function multipart_pager_display_nodes(&$form_state, $adv_id) {

with

function multipart_admin_edit_load(&$form_state, $node) {

clear cache and try. it should work now

pv91’s picture

<?php

/**
* @file
* create a form.
*/

function multipart_menu(){
  $items = array();
  $items['admin/config/media/mymodule/bewerken/%'] = array(
    'title' => 'Bewerk advertentie',
    'page callback' => 'drupal_get_form',
	'page arguments' => array('multipart_admin_edit_load', 5),
    'access callback' => TRUE, // So any user can access it.
  );
  return $items;
}

function multipart_admin_edit_load(&$form_state, $node) {
  $uid = $adv_id['build_info']['args'][0];     //please add this line for drupal 7
  $result = db_query('SELECT * FROM {user} WHERE uid = :uid ', array(':uid' => $uid));
  foreach ($result as $feed) {
    $url= $feed->mail;
  }
 
  $form['#attributes'] = array('enctype' => "multipart/form-data");
  $form['name'] = array('#type' => 'textfield',
    '#title' => t('URL'),
    '#value' => isset($url) ? $url: '',
    '#maxlength' => 255,
    '#description' => t('URL of the webpage'),
    '#required' => FALSE,
  );
   $form['image'] = array('#type' => 'file',
    '#title' => t('Afbeelding'),
    '#maxlength' => 255,
    '#required' => FALSE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form; 
}

I don't see a form. What I see is this location: admin/config/media

pv91’s picture

Anyone?

pv91’s picture

Nobody?!?!?!?!?!?!?!?!?!