Drupal 7 adding custom menus to pages - drupal

I have been developing with Drupal 7 for the past 4 months now, and I can't seem to find a straight answer as to how to add more menus on my pages. I understand the whole system_main_menu and system_secondary_menu, but how in the world can I add more menus to my page if I make a custom menu, let's say I have a footer_social_menu? I just love dynamic menus.
Here's what I am working with right now
function cornmaze_links($variables){
$html = '<ul>';
foreach($variables['links'] as $link){
$html .= '<li>'. l($link['title'], $link['href'], $link).'</li>';
}
$html .= '</ul>';
return $html;
}
I tried using the THEME_links($vars) function, but that affects ALL of the menus, what if I wanted to add a certain ID to a custom menu? or change the custom menu to use all divs? That's what I don't get. I can't necessarily loop through the menus using the THEME_links() function?
I don't want to put them in a block either, if I don't have to, just to avoid any extra markup that I don't need. I just want to be able to control menus, whether they be system or custom.
Any help, or light shed would be awesome! Thank you in advance!

Try menu block module. It creates your menus as blocks and highly configurable.
Here's the documentation link.

Please Check , this may be help ful for you,
function formuserentry_menu() {
$items = array();
$items['formuserentry'] = array( //this creates a URL that will call this form at "examples/form-example"
'title' => 'Entry Page',
'page callback' => array('formuserentry_view'),
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM
);
$items['formuserentry/application'] = array( //this creates a URL that will call this form at "examples/form-example"
'title' => 'Entry Application Forms', //page title
'description' => 'A form to mess around with.',
'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed. for a form, use drupal_get_form
'page arguments' => array('formuserentry_application' , 2), //put the name of the form here
'access arguments' => array('administer your module'),
);
return $items;
}
/********** front page view ***********/
function formuserentry_view() {
$result = 'My Sub Menu URL was hit';
$header = array('Entry Id','Name', 'DOB', 'Year', 'Image' );
$rows = array();
$no_yes = array('No', 'Yes');
$results = db_query("SELECT * FROM userentryform ORDER BY userentryId DESC");
foreach ($results as $node) {
$rows[] = array(
l($node->firstname, 'formuserentry/application/'. $node->userentryId ),
array('data' => $node->firstname, 'class' => 'title'),
array('data' => $node->lastname, 'class' => 'type'),
array('data' => $node->birthyear, 'class' => 'type'),
array('data' => '<img src="sample.jpg">dff', 'class' => 'image'),
);
}
return theme('table', array('header' => $header, 'rows' => $rows));
}
/********************** add form ***************************/
function formuserentry_application($form, &$form_state, $candidateId) {
$firstname = '';
$lastname = '';
$birthyear = '';
/****** query fetch ******/
if(isset($candidateId)){
$fetchquery = db_select('userentryform', 'n')
->fields('n', array('firstname','lastname', 'birthyear'))
->fields('n')
->condition('userentryId',$candidateId)
->execute()
->fetchAll();
$firstname = $fetchquery[0]->firstname;
$lastname = $fetchquery[0]->lastname;
$birthyear = $fetchquery[0]->birthyear;
}
/**************************/
//print($fetchquery);
//drupal_set_message('<pre>'. print_r($fetchquery[0]->firstname, TRUE) .'</pre>');
$form['name'] = array(
'#type' => 'fieldset',
'#title' => t('Name'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['name']['first'] = array(
'#type' => 'textfield',
'#title' => t('First Name'),
'#required' => TRUE,
'#default_value' => $firstname,
'#description' => "Please enter your first name.",
'#size' => 20,
'#maxlength' => 20,
);
$form['name']['canid'] = array(
'#type' => 'hidden',
'#required' => FALSE,
'#default_value' => $candidateId,
'#description' => "Please enter your first name.",
'#size' => 20,
'#maxlength' => 20,
);
$form['name']['last'] = array(
'#type' => 'textfield',
'#title' => t('Last name'),
'#value' => $lastname,
'#required' => TRUE,
);
$form['name']['year_of_birth'] = array(
'#type' => 'textfield',
'#title' => "Year of birth",
'#description' => 'Format is "YYYY"',
'#value' => $birthyear,
'#required' => TRUE,
);
// Image upload field.
$form['name']['image'] = array(
'#type' => 'managed_file',
'#title' => 'File',
'#upload_location' => 'public://my-files/',
'#process' => array('formuserentry_my_file_element_process'),
"#upload_validators" => array('file_validate_is_image' => array())
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
/********* for validation ************/
> function formuserentry_my_file_element_process($element, &$form_state,
> $form) { $element = file_managed_file_process($element, $form_state,
> $form); $element['upload_button']['#access'] = FALSE; return
> $element; }
>
> function formuserentry_application_validate($form, &$form_state) {
> $year_of_birth = $form_state['values']['year_of_birth'];
> if ($year_of_birth && ($year_of_birth < 1900 || $year_of_birth > 2000)) {
> form_set_error('year_of_birth', 'Enter a year between 1900 and 2000.');
> } }
/********** form submission ***************/
function formuserentry_application_submit($form, &$form_state) {
$first_name = $form_state['values']['first'];
$last_name = $form_state['values']['last'];
$year_of_birth = $form_state['values']['year_of_birth'];
$profileimage = $form_state['values']['image'];
$canid = $form_state['values']['canid'];
if($canid == '') {
$nid = db_insert('userentryform')
->fields(array(
'firstname' => $form_state['values']['first'],
'lastname' => $form_state['values']['last'],
'birthyear' => $form_state['values']['year_of_birth'],
'profileimage' => $form_state['values']['profileimage'],
))
->execute();
drupal_set_message(t('The form has been Added your last insert ID is => '.$nid));
} else {
$nid = db_update('userentryform')
->fields(array(
'firstname' => $form_state['values']['first'],
'lastname' => $form_state['values']['last'],
'birthyear' => $form_state['values']['year_of_birth']
))
->condition('userentryId',$canid)
->execute();
drupal_set_message(t('The form has been Updated your last insert ID is => '.$nid));
}
drupal_goto("/formuserentry/");
}

Related

Drupal form submission

I have created a form in drupal. I don't know how to handle the submission. I want to do some selection from database with the values i get from form. Here is my code to create form
function q_search_form() {
$form['qsearch']['category'] = array(
'#type' => 'select',
'#options' => array(0 => 'Any', 1 => 'Automotive', 2 => 'Real Estate'),
'#attributes' => array('class' => 'drop-box'),
'#prefix' => '<table width="470"><tr><td width="170">Select Category</td><td width="300">',
'#suffix' => '</td></tr>'
);
$form['qsearch']['city'] = array(
'#type' => 'select',
'#options' => array(0 => 'Any', 1 => 'Calicut', 2 => 'Kochi'),
'#attributes' => array('class' => 'drop-box'),
'#prefix' => '<tr><td width="170">City</td><td width="300">',
'#suffix' => '</td></tr>'
);
$form['qsearch']['property'] = array(
'#type' => 'select',
'#options' => array(0 => 'Any', 1 => 'House', 2 => 'Land'),
'#attributes' => array('class' => 'drop-box'),
'#prefix' => '<tr><td width="170">Property</td><td width="300">',
'#suffix' => '</td></tr>'
);
$form['qsearch']['wanto'] = array(
'#type' => 'select',
'#options' => array(0 => 'Any', 1 => 'Sell', 2 => 'Buy'),
'#attributes' => array('class' => 'drop-box'),
'#prefix' => '<tr><td width="170">Want to</td><td width="300">',
'#suffix' => '</td></tr>'
);
$form['qsearch']['submit'] = array(
'#type' => 'submit',
'#value' => t('Search'),
'#attributes' => array('id' => 'Search', 'class' => 'srch-button'),
'#prefix' => '<tr><td><a class="adv-srch" href="#">Advance Search</a></td><td>',
'#suffix' => '</td></tr></table>'
);
return $form;
}
You can write following submit function,
function q_search_form_submit($form, &$form_state) {
// To get selected values from form
$values = $form_state['values'];
// do print_r($values); exit; to check the values
}
This is pretty simple. You just have to implement the form_submit function. From the function above, I assume the name of your module to be q_search. So, here is what the submit function would look like:
function q_search_form_submit($form, &$form_state) {
// you can get the values submitted by the users using
// `$form_state['values']`, and use the database functions
// to implement your logic.
}
If you also want to validate the user inputs before the actual submit, you should add a validation function which would look like:
function q_search_form_validate($form, &$form_state) {
// validate here
}
You can use form_set_error, in the validate function if the validation fails.
You can do pretty much anything you want in the form_submit function.
function q_search_form_submit($form, &$form_state) {
$category = $form_state['values']['category'];
$city = $form_state['values']['city'];
//etc..
//use these values to query your database tables
$query = db_select($category, 'c');
//narrow results to those only in the selected city:
$query->join($city, 'cy', 'c.city = cy.cid');
//narrow results further with more ->join statements
$results = $query
->fields('c', array(fields you want from the categories table))
//fields from other tables...
->execute();
//do whatever with the $results (print on a different page, print in a table, etc).

I want to place 2 forms in a single custom page in Drupal 7

I am completely new to Drupal am trying to create a single page that contains 2 forms (that come from a custom module). I also want this page to be a dedicated file in my theme directory.
In the custom module called "vocabt" I have vocabt.module that contains 3 needed functions:
function vocabt_menu() {
$items['scores/admin'] = array(
'title' => 'Check Your Student\'s Scores',
'page callback' => 'drupal_get_form',
'page arguments' => array('vocabt_admin_login_form'),
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
$items['scores/student'] = array(
'title' => 'Check My Scores',
'page callback' => 'drupal_get_form',
'page arguments' => array('vocabt_student_login_form'),
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
}
function vocabt_admin_login_form($form, &$form_state) {
if(!empty($form_state['values'])) {
$values = $form_state['values'];
} else {
$values = array();
}
$schools = vocabt_get_school_options();
$form['school'] = array(
'#type' => 'select',
'#title' => 'Select Your School',
'#required' => TRUE,
'#options' => $schools,
'#default_value' => !empty($values['school']) ? $values['school'] : '',
'#empty_option' => 'Select',
);
$form['username'] = array(
'#type' => 'textfield',
'#title' => 'User Name',
'#required' => TRUE,
);
$form['password'] = array(
'#type' => 'password',
'#title' => 'Password',
'#required' => TRUE,
);
$form['actions'] = array();
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => 'Log In',
'#attributes' => array('class' => array('button blue')),
);
return $form;
}
function vocabt_student_login_form($form, &$form_state) {
if(!empty($form_state['values'])) {
$values = $form_state['values'];
} elseif(isset($_GET['id']) && $info = vocabt_decode_id($_GET['id'])) {
$values = $info;
} else {
$values = array();
}
$form['left'] = array(
'#prefix' => '<div class="lookup">',
'#suffix' => '</div>',
);
$form['right'] = array(
'#prefix' => '<div class="results">',
'#suffix' => '</div>',
);
$schools = vocabt_get_school_options();
$form['left']['school'] = array(
'#type' => 'select',
'#title' => 'Select Your School',
'#required' => TRUE,
'#options' => $schools,
'#default_value' => !empty($values['school']) ? $values['school'] : '',
'#empty_value' => 'Select',
);
$form['left']['id'] = array(
'#type' => 'textfield',
'#title' => 'Enter your student ID',
'#required' => TRUE,
'#default_value' => !empty($values['id']) ? $values['id'] : '',
);
$form['left']['submit'] = array(
'#type' => 'submit',
'#value' => 'Check',
'#attributes' => array('class' => array('button blue')),
);
if(!empty($form_state['storage']['error'])) {
$results = '<div class="blackbox">';
$results .= '<div style="padding: 20px 0px; text-align: center;">'.$form_state['storage']['error'].'</div>';
$results .= '</div>';
} elseif(empty($values['id'])) {
$results = '<div class="blackbox">';
$results .= '<div class="instructions">Your score will appear here once you’ve entered your information on the left.</div>';
$results .= '</div>';
} else {
$results = vocabt_get_student_results($values['school'], $values['id']);
}
$form['right']['results'] = array('#markup' => $results);
return $form;
}
My question is I want to hit a URL such as "/scores/admin" that NOT ONLY contains the form within the function of "vocabt_admin_login_form()" but ALSO contains the form within the above function "vocabt_student_login_form()" ....how can I do this?
I also want both forms to appear in page that uses a dedicated PHP file (I can already create custom pages using dedicated PHP files via creating a new page in the CMS), but I do not know how to tie this dedicated PHP file with the 2 forms above. Please let me know! Thank you
It's not clear what you mean by "Dedicated PHP files". In Drupal, you define menu router items using hook_menu(), and then return their data in the specific page callback function. This function can be in any .module file or any included file in normal PHP scope.
Add a new item in your hook_menu() implementation like this:
$items['scores/check'] = array(
'title' => 'Check Scores',
'page callback' => 'vocabt_scores_page', // Note this is the PAGE CALLBACK!
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
)
Now, lets create the page callback function, vocabt_scores_page, to return a render array so Drupal will build both forms in this page - not just the specified one.
function vocabt_scores_page() {
$output = array();
$output['admin'] = drupal_get_form('vocabt_admin_login_form');
$output['student'] = drupal_get_form('vocabt_student_login_form');
return $output;
}
Make sure you clear your site's caches before testing the code. Once saved, go to scores/check page and you will see both forms in one page!
You can move your page callback functions to a different file from the .module file. To do so, add 'files' => 'vocabt.pages.inc' to the menu item and move the functions to a new file named vocabt.pages.inc. Drupal will assume the file is in same folder as the .modulefile. This file name can be anything but we mostly use module.pages.inc, module.admin.inc, likewise.
You could call a template page using the hook_theme function. You could place your html contents in this page.And just call the theme('page_name', $link);.Place your newly created theme file in your corresponding module.
function modulename_theme() {
return array(
'page_name' => array(
'template' => 'page_name',
'arguments' => array('link' => NULL),
),
);
}
You could find more information here
http://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_theme/7
Hope this helps you... :)

Drupal 7 Custom Field Widget not saving user input

I'm building a custom field module to work with our streaming video provider. As part of this, I need to grab a video based on a selected category from the provider's API. The field will need to store a number of pieces - the selected category, the selected video, and some additional details (such as a caption) yet to be added.
I can get this form to appear on the node edit page (after attaching the field to a content type), but it doesn't appear to be saving the user's input. Here's the form in place on the edit node screen: Field on node editing screen. Workflow for the user is they select a category, which populates the list of videos to select. When they select a video, they're shown a preview in $element['teacherstv']['teacherstv_video_details']. This is working right up to the point where the user saves the form. At that point, the selected values are lost.
I've looked through the Field API and Forms API docs for D7, but can't see any clear instructions for how to set up this kind of field widget and have it save user input.
Here's the code for hook_field_widget_form()
function teacherstv_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
// $value = $isset($items[$delta['teacherstv']) ? $items[$delta]['teacherstv'] : '';
$widget['#delta'] = $delta;
//lets get an array of our categories from core
$coreapi = new coreapi();
$APIsettings = variable_get('teacherstv_API_settings');
$coreapi->apiuser = $APIsettings['username'];
$coreapi->apipwd = $APIsettings['password'];
$coreapi->apiurl = $APIsettings['api_url'];
$coreapi->sortcriteria = "alpha";
//$categoriesavailable = $coreapi->get_categories();
$categories = coreapi_get_categories();
$defaults = $field['settings'];
$settings = $instance['settings'];
$category = !is_null($form_state['values'][$field['field_name']]['und'][$delta]['teacherstv']['teacherstv_category']) ? $form_state['values'][$field['field_name']]['und'][$delta]['teacherstv']['teacherstv_category'] : $defaults['teacherstv']['defaultcategory'];
switch ($instance['widget']['type']) {
case 'teacherstv':
$element['teacherstv'] = array(
'#tree' => TRUE,
'#type' => 'fieldset',
'#title' => t('TeachersTV Video'),
'#description' => '<p>' . t('Select a video from the TeachersTV service to embed.') . '</p>',
'#delta' => $delta,
);
$element['teacherstv']['teacherstv_category'] = array(
'#type' => 'select',
'#field_parents' => 'teacherstv',
'#title' => t('Video Categories'),
'#options' => $categories,
'#description' => t('Select a video category for a list of videos'),
'#default_value' => !is_null($settings['teacherstv_category']) ? array($settings['teacherstv_category']) : array($category),
'#ajax' => array(
'callback' => 'teacherstv_ajax_videolist',
'wrapper' => 'teacherstv-videolist-' . $delta . '-div',
'method' => 'replace',
),
'#delta' => $delta,
);
$videos = coreapi_list_videos($category);
$videos[0] = "--Please select a video--";
asort($videos);
$element['teacherstv']['teacherstv_video'] = array(
'#type' => 'select',
'#title' => t('Select a video'),
'#field_parents' => 'teacherstv',
'#prefix' => '<div id="teacherstv-videolist-' . $delta . '-div">',
'#suffix' => '</div>',
'#options' => array(0 => 'Video 1'),
'#default_value' =>
isset($form_state['values'][$field['field_name']]['und'][$delta]['teacherstv']['teacherstv_video']) ? $form_state['values'][$field['field_name']]['und'][$delta]['teacherstv']['teacherstv_video'] : NULL,
'#description' => t('Select a video.'),
'#options' => $videos,
'#ajax' => array(
'callback' => 'teacherstv_ajax_videoselect',
'wrapper' => 'teacherstv-videodetails-' . $delta . '-div',
),
'#delta' => $delta,
);
$video_keys = array_keys($videos);
$selected_video = isset($form_state['values'][$field['field_name']]['und'][$delta]['teacherstv_category']) ? $form_state['values'][$field['field_name']]['und'][$delta]['teacherstv_category'] : NULL;
$element['teacherstv']['teacherstv_video_details'] = array(
'#type' => 'markup',
'#field_parents' => 'teacherstv',
'#title' => t('Video details'),
'#prefix' => '<div id="teacherstv-videodetails-' . $delta . '-div">',
'#suffix' => '</div>',
'#description' => t('Details about the video.'),
'#markup' => teacherstv_ajax_render_video($selected_video),
'#delta' => $delta,
);
break;
}
return $element;
}
Its probably the AJAX callback , in your AJAX callback, use
$form_state['rebuild'] = TRUE;
or assign the ajax value to a new element to have it in the $form_state array..
use devel and dpm($form_state). I bet your value is ""

How to add checkbox in sorted table in drupal 7?

I'm writing a module in drupal 7 and i want to make a table like table in content management.
I can make a sorted table like this but how can I add checkbox to header and each row?
Here's my code:
$header = array(
array('data' => 'Title', 'field' => 'title'),
array('data' => 'Created', 'field' => 'created','sort' => 'desc'),
array('data' => 'Published', 'field' => 'status'),
array('data' => 'Action'),
);
$result = db_select('news','n')->extend('PagerDefault')
->fields('n')
->limit(10) //This is we can change the number of rows
->extend('TableSort') // Sorting Extender
->orderByHeader($header)// Field to sort on is picked from $header
->execute()->fetchAll();
$path = drupal_get_path("module","tuan_nguyen");
foreach($result as $row){
$img = $path."/del.png";
if($row->status == 1){
$img = $path."/check.png";
}
$date = format_date($row->created,'medium','','Asia/Ho_Chi_Minh');
$rows[$row->id] = array(
l($row->title,'admin/tuan_nguyen/news/edit/'.$row->id),
$date,
"<img width='30px' height='30px' src='".$img."'/>",
l('Edit','admin/tuan_nguyen/news/edit/'.$row->id).' / '.l('Del','admin/tuan_nguyen/news/del/'.$row->id),
);
}
//Create a render array ($build) which will be themed as a table with a pager
$build['tuan_table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' =>t('Table is empty'),
);
//attach the pager theme
$build['tuan_pager'] = array('#theme' => 'pager');
return $build;
I did it, the problem is the table must be initialized like a form.
The code in hook_menu() like this:
$items['admin/tuan_nguyen/news'] = array(
'title' => 'Management news',
'page callback' => 'drupal_get_form', // <----I missed here
'page arguments' => array("tuan_nguyen_manage_news"),
);
And the table in the function "tuan_nguyen_namage_news" like this:
$form['tuan_table'] = array(
'#type' => 'tableselect', // <--'#type', not '#theme'
'#header' => $header,
'#options' => $options, // <-- '#options' not '#rows'
'#empty' =>t('Table is empty'),
);
Any way. Thank you so much!!
You can use #type 'tableselect' to add a checkbox in each row including header.
Your code with following few changes may work out fine,
function your_form($form,$form_state){
$header = array(
array('data' => 'Title', 'field' => 'title'),
array('data' => 'Created', 'field' => 'created','sort' => 'desc'),
array('data' => 'Published', 'field' => 'status'),
array('data' => 'Action'),
);
$result = db_select('news','n')->extend('PagerDefault')
->fields('n')
->limit(10) //This is we can change the number of rows
->extend('TableSort') // Sorting Extender
->orderByHeader($header)// Field to sort on is picked from $header
->execute()->fetchAll();
$path = drupal_get_path("module","tuan_nguyen");
$options = array();
foreach($result as $row){
$img = $path."/del.png";
if($row->status == 1){
$img = $path."/check.png";
}
$date = format_date($row->created,'medium','','Asia/Ho_Chi_Minh');
$rows = array();
$rows[] = array(
l($row->title,'admin/tuan_nguyen/news/edit/'.$row->id),
$date,
"<img width='30px' height='30px' src='".$img."'/>", l('Edit','admin/tuan_nguyen/news/edit/'.$row->id).' / '.l('Del','admin/tuan_nguyen/news/del/'.$row->id),
);
$options[$row->id] = $rows;
}
//Create a render array ($build) which will be themed as a table with a pager
$form['tuan_table'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#empty' =>t('Table is empty'),
);
//attach the pager theme
$build['tuan_pager'] = array('#theme' => 'pager');
return $form;
}

Drupal 7, how to dynamically populate a field based on the summation of two other fields?

I have two fields
$form["field_num_males"]
$form["field_num_females"]
I need to dynamically populate the field $form["field_gender_total"] with the summation of them, before the submission (AJAX).
How can this be done with Drupal 7?
Thanks!
Yes it can be done with Ajax. The following code will automatic update the total when the text field is updated:
function gender_total_menu()
{
$items = array();
$items['test'] = array(
'title' => Gender total,
'page callback' => 'drupal_get_form',
'page arguments' => array('gender_total_form'),
'access callback' => array(TRUE),
'type' => MENU_CALLBACK,
);
return $items;
}
function gender_total_form($form, &$form_state)
{
$total = 0;
if (array_key_exists('values', $form_state) &&
array_key_exists('field_num_males', $form_state['values']) &&
array_key_exists('field_num_females', $form_state['values'])
) {
$total = $form_state['values']['field_num_males'] + $form_state['values']['field_num_females'];
}
$form = array();
$form["field_num_males"] = array(
'#type' => 'textfield',
'#title' => t("Number of males"),
'#default_value' => 0,
'#ajax' => array(
'callback' => 'ajax_update_callback',
'wrapper' => 'wrapper',
),
);
$form["field_num_females"] = array(
'#type' => 'textfield',
'#title' => t("Number of females"),
'#default_value' => 0,
'#ajax' => array(
'callback' => 'ajax_update_callback',
'wrapper' => 'wrapper',
),
);
$form['total'] = array(
'#markup' => '<p> Total: ' . $total . '</p>',
'#prefix' => '<div id="wrapper">',
'#suffix' => '</div>',
);
return $form;
}
function ajax_update_callback($form, $form_state)
{
return $form['total'];
}
Try the Computed Field module
Computed Field is a very powerful CCK field module that lets you add a
custom "computed fields" to your content types. These computed fields
are populated with values that you define via PHP code. You may draw
on anything available to Drupal, including other fields...

Resources