I know PHP/MySQL and I am learning Drupal. I like it very much, but there are some things which I have not found a way to do so I just code them myself in PHP.
Here are two examples to start:
1. A Contact Us page which returns the data submitted to the user. I mean that the page displays the email and message submitted.
2. A nested navigation UL based on a taxonomy vocabulary. What I mean is that I have an estore and the categories of products are stored as terms in a certain vocabulary. I want to display links to each category page as nested UL lists, so that Shoes is one link and inside of it is another UL with Men's Shoes and Womens' Shoes, each also a link to that category.
These are both things which I recently coded in PHP for sites. Just wondering if there is a better way to handle such things.
Thanks.
On Thursday 15 November 2007, Fred Jones wrote:
I know PHP/MySQL and I am learning Drupal. I like it very much, but there are some things which I have not found a way to do so I just code them myself in PHP.
Here are two examples to start:
- A Contact Us page which returns the data submitted to the user. I
mean that the page displays the email and message submitted.
Form-alter the contact form to add your own submit handler.
Mail-alter the message that is being sent and just store the value of the message to a variable with variable_set(). You'll probably want to key it per-user.
In the submit handler, redirect to a custom menu callback.
Add a custom menu callback, and in that callback pull the value from the variable you set earlier and display it as the body of the page. The delete the variable. If the variable is not set, just redirect to the contact form.
There may be an easier way, but that's the first that comes to mind.
- A nested navigation UL based on a taxonomy vocabulary. What I mean is
that I have an estore and the categories of products are stored as terms in a certain vocabulary. I want to display links to each category page as nested UL lists, so that Shoes is one link and inside of it is another UL with Men's Shoes and Womens' Shoes, each also a link to that category.
I am pretty sure there's a module that creates menu blocks out of taxonomy. Search for "site map", restricted to project nodes.
There may be an easier way, but that's the first that comes to mind.
My 20 lines of PHP code (mostly copied from other such pages) is indeed a lot easier. For me at least. :)
- A nested navigation UL based on a taxonomy vocabulary. What I mean is
that I have an estore and the categories of products are stored as terms in a certain vocabulary. I want to display links to each category page as nested UL lists, so that Shoes is one link and inside of it is another UL with Men's Shoes and Womens' Shoes, each also a link to that category.
I am pretty sure there's a module that creates menu blocks out of taxonomy. Search for "site map", restricted to project nodes.
Ah here go. Docs for http://drupal.org/project/site_map indeed say it can show:
"Any categories, i.e vocabulary, with all the terms expanded."
Thanks!
Fred Jones wrote:
My 20 lines of PHP code (mostly copied from other such pages) is indeed a lot easier. For me at least. :)
The usefulness of doing it the Drupal way comes from being able to hook into existing workflows, and to a certain extent the hook methodology of the API means development is sandboxed to just those hooks, and all the bits of your site can communicate at a later date more easily. I'm assuming you're happy creating an empty module:
1. If you want to be able to handle page requests to a certain URL, you use the menu hook i.e. a function called mymodule_menu(). You don't need this if you're hooking onto the existing contact form in some way.
2. If you want to hook into the existing workflow of a particular form, then every form built properly has an ID (an advantage of you building your forms properly). Using this, there are a number of points in the creation/validation/action workflow you can hook into:
By using a function called mymodule_form_alter() you can add the property $form['#submit'] = 'mymodule_contactform_submit_whatever' to the chunk of form API (an abstracted array representation of forms). That means that this callback gets activated during the post-validation process.
3. In your custom submission function, one possibility is to just send your own email and submit page content back to the user. This would mean your module would look like the below.
Larry's extra suggestions are refinements on this: they mean you don't necessarily have to assemble your own email completely; they also mean the acknowledgement page is one step away from the POSTed details i.e. refreshing it won't cause the email to be sent twice. There may be other security issues I'm not aware of, but I certainly bow to the right and proper way of doing things. But as a first attempt, something like:
<?php function mymodule_form_alter($form_id, &$form){ if ($form_id == "contact_mail_page") { $form['#submit'] = "mymodule_special_woot_submit"; } } function mymodule_special_woot_submit($form, $form_values) { // Send the email drupal_mail('mymodule_special_email', $form_values['email'], "Thank you for your submission to our site!", "Here's what you submitted... blah blah blah" ); // Write some content $content = "<p>". t('Thank you! You submitted blah blah blah') . "</p>"; print theme('page', $content);
// Disable any further form processing exit(); }
would probably do you well. I make that 20 lines, which I too have cut and paste a bit from other sources!
Cheers, J-P
Larry's extra suggestions are refinements on this: they mean you don't necessarily have to assemble your own email completely; they also mean the acknowledgement page is one step away from the POSTed details i.e. refreshing it won't cause the email to be sent twice. There may be other security issues I'm not aware of, but I certainly bow to the right and proper way of doing things. But as a first attempt, something like:
would probably do you well. I make that 20 lines, which I too have cut and paste a bit from other sources!
Very interesting. Thank you.
Freddie
- A nested navigation UL based on a taxonomy vocabulary. What I mean is
that I have an estore and the categories of products are stored as terms in a certain vocabulary. I want to display links to each category page as nested UL lists, so that Shoes is one link and inside of it is another UL with Men's Shoes and Womens' Shoes, each also a link to that category.
I am pretty sure there's a module that creates menu blocks out of taxonomy. Search for "site map", restricted to project nodes.
site_map and sitebrowser don't seem to create blocks. But taxonomy_menu does. I am using this now for a new site and it works almost as I want. Directions are here:
http://tela-web.com/drupal/how-to-make-a-list-of-the-taxonomy-terms-with-dru...
It shows the link for the parent category, which is not precisely what I want, but it's not bad. I may well have to write a bit of custom code to make it work as I need, but at least this is pretty close. :)
Thanks.