I'm rather new to Drupal, and got lost in documentation,
so I turn to the forums to ask if anyone is willing to show me some of the basics.
I have a simple test site with a basic page called "about", dislpayed on the content area.
---
What I want to do:
create a button inside the "about" page.
when user clicks the button, it should print "hello world!" in the content area.
---
What I have tried:
first I enabled all the needed php options,
and put this in the page:
This is a test.
</p>
<p>
<?php echo "I'm still learning the basics"; ?>
</p>
<form action="ee/scripts/testscript.php" method="get">
<input type="submit" value="Run test!">
</form>
It works fine, I get a button labelled "Run test!"
and when I click it, it executes testcript.php,
whic is simply:
print "hello world!";
Well, now I guess that all of you more experienced users can say what
happens when I click the "Run test!" button in my "about" -page;
instead of getting "hello world!" in the content area,
the whole screen goes blank and has "hello world!" in it.
So, I kinda succeeded, but not quite.
From that on, I have tried to learn the proper way of doing it.
I created my first module;
function mytest_block_view($delta = '') {
$block_content = '';
switch($delta){
case 'mytest':
$block['subject'] = t('I'm trying to get the basics!');
$block_content .= t('First line of testing. <br>');
$block_content .= t('Does this work?');
$block_content .= '<form action="ee/scripts/testscript.php" method="get"> <input type="submit" value="Run test!"> </form>';
$block['content'] = $block_content;
}
return $block;
}
and it seems to work the same.
So obviosly I am missing some of the very basics. After extensive google
I failed to find a how-to or any example to clarify this out.
I hope some of you has enough patience and understanding to
point me the correct way of doing this.
Comments
In my humble opinion, this is
In my humble opinion, this is not the right way to get in Drupal.
1 - you need to ask yourself : what do I want ? In your case, I dont see any case where a website would do this ! But if yours is doing that, there MUST be a way with Drupal to achieve it. What I'm sure is that you dont need coding for this.
You need to fairly understand Drupal admin before thinking about coding. When you will have wandered a year or two around, maybe you could think that in some case coding in necessary. There is thousands of module, and each one can take you weeks to masterize.
In our case there is no mechanism to display a text this way in Drupal. You need to create a page, with a text in it and a url. And your 'button' probably is a link somewhere : in a menu, block etc...
2 - If you want to display a message, you can use drupal_set_message function. It displays a message on top of the page...
But it's not the way you build a page in Drupal!
3 - you'll never build a form this way in drupal. Not in admin, nor in coding. Everything is there, thought to build forms as user, or as a coder.
Also to masterize Drupal you need to learn Views module, Chaos Tools and specially Page Manager into this suite. With these two tools you can build almost any website.
Moreover, and I'm a coder too, Drupal is very long to understand from my coding point of view. And I was pretty experienced when I began.
If it can help you...
Artatum
www.ixxi-mobility.com
thank's for your reply! 1.
thank's for your reply!
1. Well, of course I don't want to have a website which displays "hello world!" when user clicks a button. But I just figured that this is a good training piece for me. To get to understand how it works.
1a. I think I already created a page with a text in it. It had even a button on it =)
1b. And my button is a link to a php script. But I suppose this is "counter-drupal"; maybe the button should be a link to a drupal module or block which then in turn prints "hello world!". If that is the case, I'd still like to see a simple code snippet to demonstrate this. Just to make it sure I got it right, instead of figuring out some dirty way which works but is a bad habit.
2. that might be pointing to the right direction, thanks! After I posted my question, I continued my search and managed to improve my custom module so that it creates a simple form with a text field and a submit button. And once the user clicks the button, it displays the conent of the text field wit drupal_set_message. Sure, this is not a way to make a professional web site. This is "trial-and-error-learning-by-doing" =)
3. Oh, I'll take a look at those tools. I just tought that "button printing hello world" is such a simple task that it could be done by a few lines of php. But, who am I to say, as this is a completely new continent for me to explore.
as a coder I'm just an old fart, and part of me still feels that coding means "commodore 64 assembler" =) and this was the way we learned things back then; making mistakes, trying out different things, asking others and slowly learning the correct ways.
Drupal can be hard to get
Drupal can be hard to get into, however it shouldn't have to be.
If you want to develope in Drupal you have to write your own module, this doesn't mean that there is no other way however this is the best way and this will make it so much easier for you in the long run.
here you can find a tutorial on how to create your module: http://drupal.org/node/1074362
if you done that you can go to http://api.drupal.org
here you can find hooks for editing node pages.
you probally need something along the lines of:
http://api.drupal.org/api/drupal/modules--node--node.api.php/function/ho...
this allowes you to alter the contents of one or multiple pages, just before the page will be displayed in your website.
Drupal Forms
If you want to create a form and control the page/text that appears after they submit it...you really should be creating a custom module that uses the Form API. This will help with making your form secure and it keeps PHP out of your node pages (which is advisable).
Below are a few options in how to implement this:
Option #1: Create a basic page in Drupal. Then create a block via a custom module, that display the form and implements hook_submit() to display text after the form is submitted.
Option #2: Create the whole page through a form in a custom module.
If you have not create a custom drupal module, I suggest going through this guide: http://drupal.org/node/1074360
I suggest going through the form api quickstart guide: http://drupal.org/node/751826
Here is the form api reference page: http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....
send file to user with drupal API?
Thank you guys!
I now have a module which creates a form and a button.
I'd like to make it so, that when user clicks the button, a "helloworld.png" file is
offered for him/her to download. I mean, that the user gets the browser dialog
where he/she can choose what to do with the file.
I have it like this:
function mytest_form_submit($form, &$form_state) {
$file = 'helloworld.png';
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$file.";");
header("Content-Length: ".filesize($file));
readfile($file);
exit;
}
and it works. Altough using "exit" seems a bit clumsy to me.
The question is, if there is a drupal API way of sending file to user?
In the reference page I see that there is "file_transfer($uri, $headers)"
but I'm not sure if it the right thing to do?
I would be very happy if someone can show me a little code snippet
as an example. Or, is the plain php way good enough?
I know there might be some ready modules like File Force Download.
But that is not the point. The point is that I'm trying to learn to do it myself,
in order to better understand how to work with drupal.
Good Question
Honestly, the way I have been doing it with the custom modules I created in the past is the same way you describe.
From my understanding, a return value from the form_submit() function is meant to be a string value that is displayed in the theme template. So any time you want other type of functionality (at least with Drupal 6 and Drupal 7), you need to exit the request.
If someone has a better way of doing something like this, please share. I also thought that this appeared to be the "lazy" way of doing it, but I think it is a limitation of drupal core (at this point). I think this type of thing is going to be fixed in Drupal 8 though....
IMHO the good way to do what
IMHO the good way to do what you're doing is to find a module that display a form. (User_login could do it) and have a look at the naming convention of the functions. That's the main point.
module_form_field_validate
module_form_submit
and also _add _edit _delete etc to interact with db
etc
Artatum
www.ixxi-mobility.com