Elijah Manor/Code Snippet ( ASP.NET)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CodeSnippet.aspx.cs" Inherits="Playground.CodeSnippet" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="ctlHead" runat="server">
<title>Playground</title>
<script src="jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="jquery.blockUI.js" type="text/javascript"></script>
</head>
<body>
<form id="ctlForm" runat="server">
<asp:Button id="ctlAddContact" runat="server" Text="Add Contact" />
<div id="ctlAddContactModal" style="display: none;">
<h3>Add Contact</h3>
<dl>
<dt><asp:Label ID="lblFirstNameCaption" runat="server" AssociatedControlID="txtFirstName" Text="First Name" /></dt>
<dd><asp:TextBox ID="txtFirstName" runat="server" /></dd>
<dt><asp:Label ID="lblLastNameCaption" runat="server" AssociatedControlID="txtLastName" Text="Last Name" /></dt>
<dd><asp:TextBox ID="txtLastName" runat="server" /></dd>
</dl>
<div class="buttons">
<asp:Button ID="btnCancel" runat="server" Text="Cancel" />
<asp:LinkButton ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />
</div>
</div>
<script type="text/javascript">
$(function() {
$('#<%= ctlAddContact.ClientID %>').click(function(e) {
e.preventDefault();
$.blockUI({
message: $('#ctlAddContactModal'),
});
});
$('#<%= btnCancel.ClientID %>').click(function(e) {
$.unblockUI();
});
var btnSaveId = '<%= btnSave.ClientID %>';
$('#' + btnSaveId).click(function(e) {
e.preventDefault();
$.unblockUI({
onUnblock: function() {
eval($('#' + btnSaveId).attr('href'));
}
});
});
});
</script>
</form>
</body>
</html>
Code Snippet
cwd/select all checkboxes on drupal permissions page ( JavaScript)
var a=$(".form-checkbox");
for(var i=0;i<a.length;++i)
a[i].checked=true;
quick snippet to select all the check boxes on a drupal permissions page. helpful when setting up a role for Admin
sennesaelium/Drupal Form manipulation ( PHP)
RENDER ONLY 1 FIELD <?php print drupal_render($form['group_company']['field_street']['0']['value']); ?> RENDER A GROUP OF FIELDS You don't need to render fields in group one by one, just enter this code to render all fields in a group: <?php print drupal_render($form['group_company']); ?> RENDER A SELECT LIST Almost same like render a TEXTFIELD, but avoid ['0']['value'] at the end <?php print drupal_render($form['group_company']['field_region']); ?> REMOVE AN INPUT FIELD You may want to disable an input form, usually you need to remove TITLE as shown below: <?php unset($form['title']); ?> HIDE AN INPUT FIELD (HIDE vs REMOVE!) You may still want to enable an input form but need to prevent it: <?php $form['title']['#access'] = FALSE; ?> SHOW ALL VARIABLES OF FORM You may want to know what variables available for you: <?php print_r($form); ?> REORDER Use ['#weight'] to reorder $form['buttons']['#weight'] = -50; // buttons at the top PRINT BUTTONS <?php print drupal_render($form['buttons']); ?> RENAMING BUTTON What is "Submit"? You may need to write it as "Save now!", don't you? $form['buttons']['submit']['#value'] = 'Save to Database'; HIDE GROUP FIELD-SET $form['group_general']['#access'] = FALSE; HIDE BUTTON $form['buttons']['submit']['#access']= FALSE;
picxelplay/Drupal alter comment form ( PHP)
/**
* Implementation of HOOK_theme().
*/
function THEMENAME_theme() {
return array(
'comment_form' => array(
'arguments' => array('form' => NULL),
),
);
}
// theme the comment form
function THEMENAME_comment_form($form) {
// Make the text-area smaller.
$form['comment_filter']['comment']['#rows'] = 5;
// Change the text-area title
$form['comment_filter']['comment']['#title'] = t('Your message');
// Remove input formats information.
$form['comment_filter']['format'] = NULL;
// Removes homepage field
unset($form['homepage']);
// Removes preview button
unset($form['preview']);
return drupal_render($form);
}
jonathanpglick/Form validate email address ( PHP)
// form element
$form['email'] = array(
'#type' => 'textfield',
'#element_validate' => array('_valid_email'),
'#required' => TRUE,
'#default_value' => 'Enter your email address here',
);
// validation function
function _valid_email($element)
{
if ( !valid_email_address($element['#value']) ) {
form_error($element, t('Please enter a valid email address'));
}
}
picxelplay/Make Drupal form radio buttons horizontally inline ( CSS)
.form-radios .form-item {display:inline;}
Drupal forms have their radio button options vertical (one on each line) by default. This will put them horizontally inline.
focal55/Drupal 6 hook_form_alter ( PHP)
function mymodule_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'story_node_form') {
drupal_set_message(t('Editing a story node!'));
}
}
jonathanpglick/Load node form from menu callback ( PHP)
module_load_include('inc', 'node', 'node.pages');
// Mock the node type
$node = new stdClass();
$node->type = '{node type}';
$form = drupal_get_form('{node type}_node_form', $node);
return $form;
discipolo/usable drupal contact form ( CSS)
#contact-mail-page {
background: #777;
padding: 20px 40px;
}
#contact-mail-page, #user-login, #user-pass, #user-register {
background: #777;
padding: 20px 50px 20px 30px!important;
}
.box h2, .bodyr .body.bdysrch .box h2 {
background: #181818;
padding: 12px;
font-size: 18px;
color: #717171;
font-weight: normal;
}
.box h2 {
background: #555;
color: #000;
}
#contact-mail-page {
height: 1%;
overflow: hidden;
}
#contact-mail-page .form-item {
padding: 5px 0;
clear: both;
}
#contact-mail-page label, #user-login label, #user-pass label, #node-form label, #user-register label, #user-profile-form label {
font-size: 13px;
color: #DDD;
padding: 3px 0;
display: block;
}
#contact-mail-page .form-text, #contact-mail-page .form-textarea, #user-login .form-text, #user-pass .form-text, #user-register .form-text, #user-profile-form .form-text {
width: 100%;
padding: 10px;
background: #999;
border: none;
}
/* #comment-form #edit-submit, #comment-form #edit-preview, #contact-mail-page #edit-submit, .bdysrch #edit-submit, .node-form #edit-preview, .node-form #edit-submit, #user-login #edit-submit, #user-pass #edit-submit, .view-issues #edit-submit, #user-register #edit-submit */
#edit-submit, #edit-delete, #edit-preview, #edit-submit-1, #edit-delete-1, #edit-preview-1 {
background:#777 url(images/comment_btn.gif) no-repeat 0 0;
border: none;
width: 120px;
height: 24px;
cursor: pointer;
margin: 10px 0;
color: #AAA;
}
#node-form .form-text {
padding: 10px;
border: none;
width: 93%;
}
#edit-submit:hover, #edit-delete:hover, #edit-preview:hover {
color: #DDD;
}
#contact-mail-page .form-text:hover, #contact-mail-page .form-textarea:hover, #user-login .form-text:hover, #user-pass .form-text:hover, #user-register .form-text:hover, #user-profile-form .form-text:hover {
background: #BBB;
}
#contact-mail-page .form-text:focus, #contact-mail-page .form-textarea:focus, #user-login .form-text:focus, #user-pass .form-text:focus, #user-register .form-text:focus, #user-profile-form .form-text:focus {
background: #FFF;
}
#contact-mail-page .form-textarea {
width: 103%;
font-size: 12px;
line-height: 20px;
font-family:"Lucida Grande", Calibri, Helvetica, Arial;
height: 80px;
}
#contact-mail-page .form-textarea {
width: 105%!important;
}
highlights on hover and active states
brianyang/scaffold snippet ( HTML)
<style>
code {
border: 1px dotted #f00
}
button {
display: block
}
#mod_wrap {
}
</style>
<div class="mod_wrap">
<form>
<textarea>
</textarea>
<button>
submit
</button>
</form>
</div>
<code>
code{}
</code>
elijahmanor/Code Snippet ( HTML)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CodeSnippet.aspx.cs" Inherits="Playground.CodeSnippet" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="ctlHead" runat="server">
<title>Playground</title>
<script src="jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="jquery.blockUI.js" type="text/javascript"></script>
</head>
<body>
<form id="ctlForm" runat="server">
<asp:Button id="ctlAddContact" runat="server" Text="Add Contact" />
<div id="ctlAddContactModal" style="display: none;">
<h3>Add Contact</h3>
<dl>
<dt><asp:Label ID="lblFirstNameCaption" runat="server" AssociatedControlID="txtFirstName" Text="First Name" /></dt>
<dd><asp:TextBox ID="txtFirstName" runat="server" /></dd>
<dt><asp:Label ID="lblLastNameCaption" runat="server" AssociatedControlID="txtLastName" Text="Last Name" /></dt>
<dd><asp:TextBox ID="txtLastName" runat="server" /></dd>
</dl>
<div class="buttons">
<asp:Button ID="btnCancel" runat="server" Text="Cancel" />
<asp:LinkButton ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />
</div>
</div>
<script type="text/javascript">
$(function() {
$('#<%= ctlAddContact.ClientID %>').click(function(e) {
e.preventDefault();
$.blockUI({
message: $('#ctlAddContactModal'),
});
});
$('#<%= btnCancel.ClientID %>').click(function(e) {
$.unblockUI();
});
var btnSaveId = '<%= btnSave.ClientID %>';
$('#' + btnSaveId).click(function(e) {
e.preventDefault();
$.unblockUI({
onUnblock: function() {
eval($('#' + btnSaveId).attr('href'));
}
});
});
});
</script>
</form>
</body>
</html>
ReeceMarsland/Node Paging ( PHP)
<?php
// Previous/Next node paging by title
// See http://2bits.com for details.
// Add this function to the end of the template.php of your theme
function phptemplate_prev_next($current_node = NULL, $op = 'p') {
// Node types to include in paging
$node_types = array('blog');
if ($op == 'p') {
$sql_op = '<';
$order = 'DESC';
}
elseif ($op == 'n') {
$sql_op = '>';
$order = 'ASC';
}
else {
return NULL;
}
$output = NULL;
foreach($node_types as $type) {
$quoted_types[] = "'" . $type . "'";
}
$sql = "SELECT nid, title FROM {node} n
WHERE nid $sql_op %d
AND type IN (" . implode(',', $quoted_types) . ")
AND status = 1
ORDER BY nid $order
LIMIT 1";
$result = db_query($sql, $current_node->nid, $type);
$data = db_fetch_object($result);
if (!isset($data->nid) || !$data->nid) {
return NULL;
}
return l($data->title, "node/$data->nid", array('html' => TRUE));
}
?>
<!-- Node paging start. Add this to your node.tpl.php and/or node-TYPE.tpl.php -->
<?php if (!$teaser) : ?>
<div class="navpn">
<div class="pnlaquo">&laquo;</div>
<div id="pnprev"> <?php print phptemplate_prev_next($node, 'p') ?> </div>
<div id="pnmain"> | <a href="<?php print base_path() ?>">Home</a> | </div>
<div id="pnnext"> <?php print phptemplate_prev_next($node, 'n') ?> </div>
<div class="pnraquo">&raquo;</div>
</div>
<?php endif; ?>
<!-- Node paging end -->
/* Add this to your style.css file */
.navpn {
width: 500px;
margin: 0 auto 0 auto;
clear: both;
display: block;
text-align: center;
line-height: 1.5em;
}
#pnprev {
display: block;
float: left;
text-align: right;
width: 200px;
white-space: normal;
word-wrap: break-word;
padding: 0;
}
.navpn #pnmain {
float: left;
text-align: center;
display: inline;
padding: 0 1em 0 1em;
}
#pnnext {
display: block;
float: left;
text-align: left;
width: 200px;
white-space: normal;
word-wrap: break-word;
padding: 0;
}
.pnlaquo {
float: left;
width: 10px;
}
.pnraquo {
float: right;
width: 10px;
}
simple node paging snippet
Onfire60/Using drupal_get_destination() in a link item "l()" ( PHP)
global $base_url;
l("Member Login", $base_url."/user/login?".drupal_get_destination()),);
When adding a link to a form say for instance a login form you can use drupalgetdestination() to return the user to the page they were on when they finish loging in. This show you how to do this programatically in a link l(); function.
Peter Campbell/Interact with controls on another form ( C#)
((DevExpress.XtraTab.XtraTabPage)(this.Parent)).TabControl.TabPages.RemoveAt(((DevExpress.XtraTab.XtraTabPage)(this.Parent)).TabControl.SelectedTabPageIndex);
Code snippet that shows the interaction with a control on another form within the same project
sdxxx/Drupal 7 : Creating Horizontal Login Bar Without Module ( PHP)
/** Function 1 : create a $form array for our new login form
*/
<?php
function horizontal_login_block($form) {
$form['#action'] = url($_GET['q'], array('query' => drupal_get_destination()));
$form['#id'] = 'horizontal-login-block';
$form['#validate'] = user_login_default_validators();
$form['#submit'][] = 'user_login_submit';
$form['#prefix'] = '
';
$form['#suffix'] = '
';
$form['name'] = array(
'#type' => 'textfield',
'#prefix' => '
',
'#suffix' => '
',
'#maxlength' => USERNAME_MAX_LENGTH,
'#size' => 15,
'#required' => TRUE,
'#default_value' => 'Username',
'#attributes' => array('onblur' => "if (this.value == '') {this.value = 'Username';}", 'onfocus' => "if (this.value == 'Username') {this.value = '';}" ),
);
$form['pass'] = array(
'#type' => 'password',
'#maxlength' => 60,
'#size' => 15,
'#required' => TRUE,
'#prefix' => '
',
'#suffix' => '
',
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array('#type' => 'submit', '#value' => '');
return $form;
}
?>
/**
This function will create a div wrapper for the login form and div wrapper for the login textfield and password textfield. Also the function will embed a javascript function to display "Username" in the username textfield which will be changed when the textfield is clicked. This is important because the function didn't have any title field to let user differentiate between username field and password field.
Next we will need to create the actual function for building the form array and output it as html tag
Function 2 : Determine if user is not logged in -> show the login form but if user is logged in -> display greeting message instead
*/
<?php
function login_bar() {
global $user;
if ($user->uid == 0) {
$form = drupal_get_form('horizontal_login_block');
return render($form);
} else {
// you can also integrate other module such as private message to show unread / read messages here
return '
' . t('Welcome back ') . ucwords($user->name) . '
';
}
}
?>
/** This function will switch between anonymous user and logged in user. You will need to call this function in the area that you wish for the login bar to appears.
Last thing is the css, you will need to create a new css / use the theme css to add these simple css code :
*/
<?php
/** Login Bar **/
.usericon, .passicon {
float: left;
width: 180px;
padding-left: 36px; /** create space for small 24px x 24px icon **/
height: 24px;
}
#loginbar {width: auto; float: right;}
#loginbar .form-actions {display: none;}
#loginbar p { color: #fff; font-size: 1.1em; font-weight: bold;}
?>
/**
If you notice, the functions still build the login submit button, without this the form will not submit thus no login process will ever happen. To tackle this, we just need to hide the login submit button via css
To see this code in action, you can visit : http://zenith.victheme.com
*/
Do you know that you can create a new form directly from theme template.php without the need of creating a module in Drupal 7?
Armed with that knowledge, we will build a new login form and transform it to become a horizontal login bar.
You might asked, why create a new login form while you can form alter the standard login block form (userloginblock) ? The reason for this is very simple, the standard login block form will be altered by other contribution module such as captcha module and it would not be possible to display the captcha in a tight spaced horizontal bar. Not to mentioned other standard module such as openid will also altered the module and adds its own html tag.
So by creating a new login form we will be able to create a clean and slim horizontal login bar without the worry about other contribution module messing the layout of our horizontal login bar.
To do this we need to add a few functions to the theme template.php.