<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Engwar &#187; oxygentank</title>
	<atom:link href="http://engwar.com/tags/oxygentank/feed" rel="self" type="application/rss+xml" />
	<link>http://engwar.com</link>
	<description>Chintana Wilamuna&#039;s weblog</description>
	<lastBuildDate>Wed, 30 Nov 2011 13:28:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Fiddling with drupal forms</title>
		<link>http://engwar.com/post/46?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=fiddling-with-drupal-forms</link>
		<comments>http://engwar.com/post/46#comments</comments>
		<pubDate>Thu, 21 Aug 2008 15:30:06 +0000</pubDate>
		<dc:creator>Chintana</dc:creator>
				<category><![CDATA[drupal]]></category>
		<category><![CDATA[oxygentank]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://engwar.com/?p=46</guid>
		<description><![CDATA[This will show some of the neat API calls Drupal provides which you could use to do almost anything. OxygenTank, the developer portal related to WSO2 products runs on Drupal. The code examples in this post are extracted from a &#8230; <a href="http://engwar.com/post/46">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This will show some of the neat API calls Drupal provides which you could use to do almost anything.  <a href="http://wso2.org/">OxygenTank</a>, the developer portal related to <a href="http://wso2.org/downloads">WSO2 products</a> runs on <a href="http://drupal.org/">Drupal</a>.  The code examples in this post are extracted from a soon to be implemented module which will alter the way you download products. Everything you need to know about the <a href="http://api.drupal.org/">Drupal API is nicely documented</a>.</p>
<p>Forms are defined as PHP arrays,</p>
<pre lang="php">
$form['noreg'] = array(
    '#type' => 'checkboxes',
    '#title' => "Reason not to register",
    '#options' => $reasons,
    '#default_value' => 0,
    '#validate' => array('fizzed_up_validator' => array())
);
</pre>
<p>When the above code fragment gets rendered into HTML it&#8217;ll be a list of checkboxes with the items in <code>$reasons</code>. Also note that we&#8217;re passing a custom validation function. Inside your custom validation function you could perform all the checking and if there&#8217;s an error condition you could say,</p>
<pre lang="php">
form_set_error($form, $message);
</pre>
<p>which will display an error message and redirects users back to the form. <code>$form</code> is a parameter your validation function takes. Forms API is well documented with examples.</p>
<p>There&#8217;s this need to add the user to a particular role depending on from where the user come to the download page. This should be done in the registration phase. That&#8217;s trivial to do with Drupal. You could get this &#8220;event&#8221; where the user submitted details has passed all the validation logic and about to be inserted into the database, at that time you have to fetch the role info from the DB and insert stuff to <code>users_roles</code> table. Oh, and the redirection part. When the form is successfully submitted (meaning user got registered) you could redirect the user to anywhere. All the API and the things you could do with it is extremely well thought out and nice to play with. To get this registration form, all I had to do was hide some attributes before the form gets rendered. For example look at the standard <a href="http://wso2.org/user/register">registration page</a>. Embedded registration form of the newly designed page had to contain only the mandatory fields. There was no need to display rest of the stuff. So, what I had to do was in <code><a href="http://api.drupal.org/api/function/hook_form_alter/6">hook_form_alter</a></code> unset the relevant <code>$form['elems']</code>. Had to define a session variable to detect we&#8217;re coming from the right page, otherwise the all the registration form instances will be changed. After that display the form,</p>
<pre lang="php">
drupal_get_form('user_register');
</pre>
<p>voila! Now you have a customized registration form. The cool thing is all the validation stuff defined for the registration form will work and you don&#8217;t have to do anything extra.</p>
<p>A slightly less documented API call that we use is <code><a href="http://api.drupal.org/api/function/user_external_load/6">user_external_load</a></code>. This&#8217;ll load a given user&#8217;s details into the current session so that the user will be logged into the system. To pull this off successfully there needs to be an entry in the <code>authmap</code> table as the function implementation shows. I&#8217;ve not been able to find a way one could insert a record to this table via the admin screens.</p>
]]></content:encoded>
			<wfw:commentRss>http://engwar.com/post/46/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

