Overriding the user registration form in Drupal 6 - drupal

I want to be able to customise the user registration form in Drupal 6
I have found thousands of tutorials that show me how to override the structure in which you can output the form as a whole, but I want to move form elements around etc and I cant quite seem to see the best way to do this

To expand on Jeremy's answer, you're going to want to study Drupal's Form API and user_register(). In short, you build an associated array; each element in the array corresponds to one form element.
Each form element in the array is its own associated array. They can have a type: textfield, select menu, checkboxes, etc.: see the Form API reference for all the types.
Each form element can also have a weight: this is how you order elements around. Lower numbered weights show up before higher numbered weights in the form.
One of the element types available to you is fieldset: this is what will allow you to group elements together. When you use a fieldset, it creates a section of the form with its own weight values.
So, let's say you have a form with three fields: Name, Company, and E-mail address. The Name should show up first, Company second, E-mail address third. You could specify the form like so:
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#weight' => 1,
);
$form['company'] = array(
'#type' => 'textfield',
'#title' => t('Company'),
'#weight' => 2,
);
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('E-mail address'),
'#weight' => 3,
);
Note the #weight key. If you wanted Company to appear after E-mail address, you'd set $form['company']['#weight'] to something higher than 3.
Now let's say you wanted to group Name and Company into a fieldset called Personal Information. Your form would now look something like this:
$form['personal'] = array(
'#type' => 'fieldset',
'#title' => t('Personal information'),
'#weight' => 1,
);
$form['personal']['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#weight' => 1,
);
$form['personal']['company'] = array(
'#type' => 'textfield',
'#title' => t('Company'),
'#weight' => 2,
);
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('E-mail address'),
'#weight' => 3,
);
Note that Name and Company are now array elements of $form['personal'].
If you want to make Name show up after Company in the fieldset, set its #weight higher than 2. Because the Name is now part of a fieldset that has a lower #weight than the E-mail address field, even if you set $form['personal']['name']['#weight'] to 4, it wouldn't make the Name show up after E-mail address.
So what you're going to attempt to do is use hook_form_alter() to alter the user_register form to change the weights of certain form elements, create your own fieldsets, and move certain form elements into your newly created fieldsets.
There are ways to do this within your theme, but I prefer creating a custom module for this. Create your custom module, and implement hook_form_alter():
function test_form_alter(&$form, $form_state, $form_id) {
if ($form_id === 'user_register') { // Only modify the user registration form
// Before you can get down to business, you need to figure out the
// structure of the user registration form. Use var_dump or kpr to dump
// the $form array.
// Note: if you want to use kpr on the user registration form, give
// anonymous permission to see devel information.
// kpr($form);
// Move Name field to after E-Mail field
$form['name']['#weight'] = 2;
$form['mail']['#weight'] = 1;
// Group Name and E-mail together into a fieldset
$form['personal_info'] = array(
'#type' => 'fieldset',
'#title' => t('Personal information'),
);
$form['personal_info']['name'] = $form['name'];
$form['personal_info']['mail'] = $form['mail'];
// The last block only copied the elements: unset the old ones.
unset($form['name']);
unset($form['mail']);
}
}
In more complex forms, moving things from one fieldset to another might yield unexpected results when submitting the form. This is because $form['name'] isn't the same as $form['group']['name'], which isn't the same as $form['other_group']['name']. You don't have to worry about that on the user_register form for the most part, but check out the handbook page on #tree and #parents for more information about this.
This covers modifying existing fields in the user registration form: if you want to add new fields, I highly recommend using Content Profile. If you want to create custom fields on your own, it's going to get a lot more complex as you're going to have to implement your own validate and submit handlers. Content Profile handles this for you: check out its README to see how to activate it for registration forms.

Using hook_form_alter you can do whatever you want with a form.
For example changing the weight can change the position on the page.
If you try:
MYMODULE_form_user_profile_form_alter(&$form, $form_state) {
// do your processing here
var_dump($form);
}
replacing MYMODULE with the name of your module.
You will see the structure of the form, you can change values in there to alter, labels weights descriptions etc.

In a module, first use hook_theme() , now assuming the name of your module is 'd6_forms' :
function d6_forms_theme() {
return array(
'user_register' => array(
'template' => 'templates/user-register-form',
'arguments' => array('form' => NULL),
),
);
}
This will make the user_register form look for a template, in the specified folder.
So make sure that in your module folder, there is a folder called 'templates', with a file 'user-register-form.tpl.php'.
You notice that in the hook_theme() , the extenstion of the template file ( .tpl.php ) is not supplied. That's normal, you don't need to specify it there.
Do make sure however, that the template has that extension, and that it's not just named 'user-register-form.php' !
In that template file, you have access to the $form variable , so print it there to see what fields are in there.
The devel module is recommened, since it's able to print big Drupal arrays in a fancy way ( using dpm() ).
If you do not have Devel module, or don't want to use it, this also works : <?php print '<pre>' . print_r($form, 1) . '</pre>'; ?>.
To print a field, just use <?php print drupal_render($form[field_name]); ?>, this will print the field and make sure that it works as intended.
So for example, if you want to print the 'name' field in the $form array, just use <?php print drupal_render($form['name']); ?>.
You don't have to print every field ! Just print the fields that you want to move somewhere ( which, with a basic Drupal register form, are about 3 : name, email & submit ).
To print all the remaining fields, just end your template with <?php print drupal_render($form); ?>.
It is important that you don't forget this, since the $form var contains stuff that is absolutely needed for your form to work ( like a token, etc .. ).
So good standard behaviour when templating a form, is to print that piece of code first at the bottom of your template.
This is an entire example of a small register form template, with some basic html :
<?php
// What is in that $form var ? To check, uncomment next line
// print '<pre>' . print_r($form, 1) . '</pre>';
?>
<div style="background-color:#ddd;padding:10px;">
<?php print drupal_render($form['name']); ?>
<?php print drupal_render($form['mail']); ?>
</div>
<div>
<?php print drupal_render($form['submit']); ?>
</div>
<?php print drupal_render($form); ?>

maybe this will help:
http://drupal.org/node/44910

You just need to Enable the Profile Module which would give access to place more fields in sign up form.
Go through this Simple Video tutorial which would be very helpful for beginners in Drupal .
http://planetghost.com/add_more_fields_to_sign_up

Related

wp_get_archives link to that particular archive page

Setup an Archive list on the right of my page and the design has a "view" button after each item.
I'm trying to have that view button link to the archive month page.
Looking at doing something with the after and adding a href="" to the view button, However not sure what to reference to get that to happen.
My current code is as follows :
<?php
// Get Archives'
$args = array (
'type' => 'monthly',
'order' => 'DESC',
'after' => '<div class="pull-right"><a class="view" href="#">View</a></div>',
'limit' => '6'
);
$archives = wp_get_archives( $args );
?>
As you can see the 'after' parameter in the array is where I am trying to add the href.
Hope that makes sense.
Thank You!
Few things about wp_get_archives:
It doesn't return anything except if you force the echo parameter to 0 - otherwise calling the function will result of the archive links to be printed.
The after parameter is dependant of the format parameter - it is only used when using the "html" (default - list mode) or "custom" format. What it does is to display the value you pass as parameter after the link. So you don't need to reference the link inside. Use it in combination with the before parameter to achieve what you want to do here.
You don't really need to set the type as monthly, as it is the default value for this parameter. Same for the order parameter, which default is allready DESC.
So a valid call would be:
wp_get_archives(array(
'format' => 'custom',
'before' => '<div class="pull-right">',
'after' => '</div>',
'limit' => 6
));
You probably notice that it doesn't output exactly what you are trying to do, as it miss the class view on your links. You need to add a filter on get_archives_link in order to achieve this (this go in your theme functions.php):
add_filter('get_archives_link', 'get_archive_links_css_class' );
function get_archive_links_css_class($link) {
return str_replace('href', 'class="view" href', $link);
}
This will add the class before the href attribute.

Drupal fields: Passing a variable to view

I have a custom field plugin. It's widget looks somewhat like this:
function mymodule_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
...
$main_widget = $element + array(
'#test' => 'test',
'#type' => 'textarea',
'#attributes' => array('item_capacity' => 3),
'#theme'=>'mymodule_theme'
);
...
}
I need to get a value of another field, attached to current node, and pass it to this widget's theme file. Can i somehow get at least an id of a node that contains current field from hook_field_widget_form()?
You can use the module Devel to print the value of the $form array which will contain the form information you are looking for.
Example:
dpm($form);
You will be able to easily see the content of the form and access it with php in array form.

Create content types on the fly in Drupal

One of our website requirements is to have a content type that let's you decide on the total amount of content types on the fly.
For instance, if I specify a number 10, then it ought to generate the content types consecutively, one of type 'textarea' and another of type 'radio' are created 10 times.
essentially to break it programmatically, it will create:
<?php
for(i=0;i<10;i++)
{
echo "<input type = 'textarea'></input>";
echo "<select><option>1</option><option>2</option></select>";
}
?>
This is pretty straightforward if I was dabbling with simple PHP files, but with Drupal 7's content types (CCK), it is posing a bigger challenge than what it ought to be IMHO. I have tried exploring modules that let you create content types on the fly and considered creating a custom content type programmatically which seems like another challenge altogether.
I am curious if anybody has an alternative for this and has dabbled with this challenge before. Your answers are most appreciated.
Thanks guys
To create content dynamic types in drupal 7 you will need to follow the below process:
Updated *
1) Create a menu path using hook_menu() which uses drupal_get_form(). This will allow you to gather all data for your users input for the dynamic content creation.
Example:
$items['newpost'] = array(
'title' => 'Create Post',
'description' => 'The main noticeboard',
'page callback' => 'drupal_get_form',
'page arguments' => array('customvishal_create_content'),
'access callback' => TRUE,
);
return $items;
2) Then use:
function customvishal_create_content($form, &$form_submit) // To create your form on that page
function customvishal_create_content_validate($form, &$form_state) // for any kind of validation
function customvishal_create_content_submit($form, &$form_state)
In this function you can submit the values into your new content type.
Here is where you will call the below functions.
3) Create an array which will hold the meta data about your content type.
// Define the node type.
$mystuff = array(
'type' => 'mystuff',
'name' => $t('my new Stuff'),
'base' => 'node_content',
'description' => $t('This is an example node type.'),
'body_label' => $t('Content')
);
// Set defaults.
$content_type = node_type_set_defaults($mystuff);
4) use node_type_save() to save/declare your content type.
node_type_save($content_type);
5) Create fields and then attach to your content type.
foreach (_mystuff_installed_fields() as $field) {
field_create_field($field);
}
// Create instances of fields.
foreach (_mystuff_installed_instances() as $instance) {
$instance['entity_type'] = 'node';
$instance['bundle'] = $mystuff['type'];
field_create_instance($instance);
}

Wordpress: How to integrate the media uploader with a custom meta box field?

I'm writing a plugin which creates a custom post_type called "dictionary_entry" which has several custom meta boxes and fields. I'd like to add an addition field which allows the custom post author to upload an audio clip.
I've done some digging and tried the code offered here but I can't get it to work.
I think one possible answer to my question would be the "type" parameter for fields. I've seen "text", "textarea", "time", "color", "radio", etc. but I haven't been able to find a list of all the possibilities. Is it wishful thinking that there might be a field type: "file" or "upload"?
I'm going to skip the code for adding the custom post_type, but here's an example of my code for adding the meta boxes (in case somebody else is trying to use this, remember to use your custom post_type in the 'pages' parameter):
//meta box code
$meta_boxes = array();
$meta_boxes[] = array(
'id' => 'examples', // meta box id, unique per meta box
'title' => 'Examples', // meta box title
'pages' => array('dictionary_entry'), // post types, accept custom post types as well, default is array('post'); optional
'context' => 'normal', // where the meta box appear: normal (default), advanced, side; optional
'priority' => 'high', // order of meta box: high (default), low; optional
'fields' => array( // list of meta fields
array(
'name' => 'Example 1', // field name
'desc' => 'Use it in a sentence? EX: Kanien\'kéha kahrónkha.', // field description, optional
'id' => $prefix . 'example1', // field id, i.e. the meta key
'type' => 'text', // text box
'std' => '', // default value, optional
'validate_func' => 'check_apos' // validate function, created below, inside RW_Meta_Box_Validate class
),
array(
'name' => 'Translation 1', // field name
'desc' => 'What does the sentence mean? EX: I speak Mohawk.', // field description, optional
'id' => $prefix . 'ex_translation1', // field id, i.e. the meta key
'type' => 'text', // text box
'std' => '', // default value, optional
'validate_func' => 'check_apos' // validate function, created below, inside RW_Meta_Box_Validate class
)
)
);
foreach ($meta_boxes as $metabox) {
add_meta_box... //see the codex for add_meta_box()
}
I figured out how to do this by digging into the code found here. If you take a look, you'll recognize parts of my code quoted above. I was originally using this class, but didn't fully realize it. It's a custom class which can be called to add various meta boxes / fields.
It turns out that the "type" parameter I was wondering about actually belongs to this Class (as opposed to the Wordpress API) and that it does allow for a type: 'file' which brings up a default file picker window (not the built-in media uploader). For my purposes this is okay, because I don't need all the slick options.
If you're reading this, you've probably already googled this question and seen a wide variety of posts that partially explain how to do this. For what it's worth, I found this to be the easiest way to add this functionality that ALSO works for custom post_types (without a fair amount of hacking). I hope this is useful to someone else.

How does hook_theme() work?

I am having a hard time understanding what hook_theme() does.
My understanding is that it has something to do with making it possible to override templates.
I was looking at:
$theme_hooks = array(
'poll_vote' => array(
'template' => 'poll-vote',
'render element' => 'form',
),
'poll_choices' => array(
'render element' => 'form',
),
'poll_results' => array(
'template' => 'poll-results',
'variables' => array('raw_title' => NULL, 'results' => NULL, 'votes' => NULL, 'raw_links' => NULL, 'block' => NULL, 'nid' => NULL, 'vote' => NULL),
),
'poll_bar' => array(
'template' => 'poll-bar',
'variables' => array('title' => NULL, 'votes' => NULL, 'total_votes' => NULL, 'vote' => NULL, 'block' => NULL),
),
);
Could you provide an example of how it works?
It provides a place for a module to define its themes, which can then be overridden by any other module/theme. It will also provide the opportunity for any module to use a hook such as mymodule_preprocess_theme_name to change the variables passed to the eventual theme function or template file.
There are basically two ways to initialise a theme function:
theme('poll_results', array('raw_title' => 'title', 'results' => $results, etc...));
and
$build = array(
'#theme' => 'poll_results',
'#raw_title' => 'title',
'#results' => $results,
etc...
); // Note the '#' at the beginning of the argument name, this tells Drupal's `render` function that this is an argument, not a child element that needs to be rendered.
$content = render($build); // Exact equivalent of calling the previous example now that you have a render array.
Please keep in mind, you should avoid calling theme() directly (per the documentation in theme.inc) since it:
Circumvents caching.
Circumvents defaults of types defined in hook_element_info(), including attached assets
Circumvents the pre_render and post_render stages.
Circumvents JavaScript states information.
In Drupal 8, theme() is a private function, _theme(). For more detail, please see www.drupal.org/node/2173655.
When you compare the two of these to the poll_results element in the example you give above you can probably work out what's happening...since PHP is not a strongly typed language Drupal is providing 'named arguments' through either a keyed array passed to the theme function, or as hashed keys in a render array.
As far as 'render element' is concerned, this basically tells the theme system that this theme function will be called using a render array, with one named argument (in this case form). The code would look something like this:
$build = array(
'#theme' => 'poll_choices',
'#form' => $form
);
This will pass whatever's in the $form variable to the theme function as it's sole argument.
Regarding the template key:
'poll_vote' => array(
'template' => 'poll-vote',
'render element' => 'form',
)
defines a theme called poll_vote which uses a template file (hence the template key) with a name of 'poll-vote.tpl.php' (this is by convention). The path to that template file will be found by using the path to the module that implements it (e.g. modules/poll/poll-vote.tpl.php), so it's fine to put template files in sub-folders of the main module folder.
There are two ways to actually return the output for a theme function, by implementing the physical function name (in this case it would be theme_poll_vote) or by using a template file. If the template key is empty Drupal will assume you've implemented a physical function and will try to call it.
Template files are preferable if you have a fair bit of HTML to output for a theme, or you simply don't like writing HTML in strings inside PHP (personally I don't). In either case though, the variables passed when you call the theme (either using theme() or a render array as described above) are themselves passed through to the template file or theme function. So:
function theme_poll_results(&$vars) {
$raw_title = $vars['raw_title'];
$results = $vars['results'];
// etc...
}
If you were using a template file instead for the same method the variables would be available as $raw_title, $results, etc, as Drupal runs extract on the $vars before parsing the template file.
I'm sure there's a lot I've missed out here but if you have any more specific questions ask away and I'll try to help out.
Drupal 6
I was stuck all day with this and now successfully implemented, so sharing my finding here, may it will help understand hook_theme.
There are 3 steps involved:
hook_theme
function YOURMODULENAME_theme() {
return array(
'xxx_xxx' => array(
'template' => 'xxx-xxx', // define xxx-xxx.tpl.php inside module
'arguments' => array('xxx' => null), //define $xxx so it will available in your xxx-xxx.tpl.php
),
);
}
echo/return the theme in your .tpl or any .module
$output = theme('xxx_xxx', $xxx);
Now variable are magically available in you xxx-xxx.tpl.php.
<?php echo $xxx ?>
Note: you can pass $xxx as array,object or anything :)
There is yet another way: (can be found in Bartik theme)
The scenario here is that we have created our own module and want to override the default output for let's say a node with title 'zzz' only.We don't know and don't really care how the default output is generated. All we need is to tell Drupal to use our own custom template file (node--custom--name.tpl.php) to render that specific node.
These are the steps:
Tell Drupal where our template file lives. (Keep in mind that this function will take effect only once and after clearing Drupal's cache):
// Implements hook_theme()
function mymodulename_theme() {
$theme = array();
$theme['node__custom__name'] = array(
'render element' => 'node',
'template' => 'path_from_mymodule_root/node__custom__name',
);
return $theme;
}
Tell Drupal where and when to use it
//Implements hook_preprocess_node()
function mymodulename_preprocess_node($vars) {
if($vars['node']->title == 'zzzz') {
$vars['theme_hook_suggestions'][] = 'node__custom__name';
... your other code ...
}
}
Now Drupal will use our template file for that specific case only, provided that 'node--custom--name.tpl.php' file is in the declared path, otherwise it will keep searching according to the suggestions naming conventions for a fallback template.

Resources