I have a module that adds a link to main menu. When I click on that link the requested page gets loaded (a .js and .html file).
My main menu looks like this:
My link
My code looks like this:
<?php
/**
* Implements hook_menu()
*/
function kl_menu(){
$items = array();
$items['simple_link'] = array(
'title' => t('my link'),
'page callback' => 'build_page',
'access arguments' => array('access content'),
'menu_name' => 'main-menu',
'type' => MENU_NORMAL_ITEM,
);
/*
* build_page
*/
function build_page() {
drupal_add_js(drupal_get_path('module', 'kl') . '/mypage.js', 'file');
return ( file_get_contents( drupal_get_path('module', 'kl').'/mypage.html') );
}
Now I would like add a submenu instead of a simple plain link so that my main menu would look like this:
My submenu
my sublink1
my sublink2
I would like that when I click on "my submenu" then this sub menu expands displaying more links. Then when I reclick on my submenu I would like it to collapse.
I am pretty new to drupal php etc.
How can I acheive that. I am using garland theme.
Thanks
baba
/**
* Implements hook_menu().
*/
function kl_menu() {
$items['simple_link'] = array(
'title' => t('my link'),
'page callback' => 'kl_build_page',
'access arguments' => array('access content'),
'menu_name' => 'main-menu',
'type' => MENU_NORMAL_ITEM,
);
$items['simple_link/my_sublink_1'] = array(
'title' => t('my sub link 1'),
'page callback' => 'mymodule_sub_page_1',
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
$items['simple_link/my_sublink_2'] = array(
'title' => t('my sub link 2'),
'page callback' => 'mymodule_sub_page_1',
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_theme().
*/
function kl_theme() {
$template_path = drupal_get_path('module', 'kl') . '/templates';
return array(
// File would be <module path>/templates/kl-build-page.tpl.php
'kl_build_page' => array(
'path' => $template_path,
'template' => 'kl-build-page')
),
// File would be <module path>/templates/kl-sub-page-1.tpl.php
'sub_page_1' => array(
'path' => $template_path,
'template' => 'kl-sub-page-1')
),
// File would be <module path>/templates/kl-sub-page-2.tpl.php
'sub_page_2' => array(
'path' => $template_path,
'template' => 'kl-sub-page-2')
),
);
}
/**
* Callback for main build page.
*/
function kl_build_page() {
drupal_add_js(drupal_get_path('module', 'kl') . '/mypage.js', 'file');
return theme('kl_build_page');
}
/**
* Page callback for sub page 1
*/
function kl_sub_page_1() {
return theme('kl_sub_page_1');
}
/**
* Page callback for sub page 2
*/
function kl_sub_page_2() {
return theme('kl_sub_page_2');
}
Related
I am creating a custom module for a Drupal 7 site and I need to create a form, using the form hook, and then include it in a template in the custom module.
I created a simple form in my .module file:
function my_login_form($form, &$form_state) {
$form['email_address'] = array(
'#type' => 'textfield',
'#attributes' => array('placeholder' => t('Email Address')),
);
$form['phone'] = array(
'#type' => 'password',
'#attributes' => array('placeholder' => t('Password')),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Login',
);
return $form;
}
In my hook_menu() I have a route created and the 'page callback' pointed to the function:
function my_login(){
return theme('my_login_template');
}
In my hook_theme() function I have it pointed to the .tpl:
function my_theme(){
return array(
'my_login_template' => array(
'template' => 'login',
),
);
}
How do I get the my_login_form into the login.tpl.php file?
Thank you for any help you can give or any place you can point me to.
You need to pass it to theme :
function my_login(){
$form = drupal_get_form('my_login_form');
return theme('my_login_template', array('form' => render($form)));
}
Into your tpl :
<?php print $form; ?>
Clear cache theme
I am trying to develop a module where in my form needs to be rendered in a tabular form. Everything is working fine except the submit button. It is neither calling default _validation or _submit functions nor _form_alter functions. I don't know where I am missing.
Here is the code for my .module file
<?php
function mytheme_menu(){
$items = array();
$items['enqform']=array(
'title' => 'Enquiry Form',
'page callback' => 'mytheme_function',
'access arguments'=>array('access content'),
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function mytheme_function(){
return theme('enquiry')
}
function mytheme_theme() {
return array(
'enquiry' => array(
'render element' => 'form',
'template' => 'custompage',
),
);
}
function enquiry_form($form, &$form_state){
$form['efname'] = array(
'#type' => 'textfield',
'#title' => 'First Name',
'#size' => 28,
'#maxlength' => 10,
'#required' => TRUE,
);
$form['elname'] = array(
'#type' => 'textfield',
'#title' => 'Last Name',
'#size' => 28,
'#maxlength' => 10,
'#required' => TRUE, //make this field required
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
function enquiry_form_validate($form, &$form_state){
}
function enquiry_form_submit($form, &$form_state) {
db_insert('form_example')->fields(array(
'efname'=>$form_state['values']['efname'],
'elname'=>$form_state['values']['elname'],
'active' => 1,
)
)->execute();
}
and my .tpl.php page looks like this
<?php$form = drupal_get_form('enquiry_form');?>
<h2>Please enter details here</h2>
<?php print render(drupal_render($form['efname']));?>
<?php print render(drupal_render($form['submit']));?>
<?php print render(drupal_render_children($form));?>
When I use 'drupal_get_form' as page callback in my hook menu, submit is working fine, but the purpose of template is of no use. It would be a day saver if someone can guide me where and what am I missing in this code.
I think that you should not render the drupal_render_children, you should just print drupal_render_children by itself.
<?php print drupal_render_children($form); ?>
I have this code for calling a form and than submit it..
<?php
// hook_menu
function pricepackages_menu()
{
$items = array();
$items['membership/packages'] = array(
'title' => t('Manage Membership Packages'),
'page callback' => 'drupal_get_form',
'page arguments' => array('pricepackages_form'),
//'access callback' => TRUE,
'access arguments' => array('access administration pages'),
);
return $items;
}
// FORM SHOW HOOK
function pricepackages_form($form, &$form_state)
{
$form = array();
$form['packagename'] = array(
'#type' => 'textfield',
'#title' => 'Package Name',
//'attribute' => array('class' => 'package'),
'#required' => TRUE,
);
$form['packageDescp'] = array(
'#type' => 'textfield',
'#title' => 'Package Short Description',
//'attribute' => array('class' => 'package'),
'#required' => FALSE,
);
$form['price'] = array(
'#type' => 'textfield',
'#title' => 'Package Price',
//'attribute' => array('class' => 'package'),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
$form['submit'][] = array('package_get_form'=> array());
return $form;
}
function package_get_form($form, &$form_state)
{
drupal_set_message('working');
?>
<script language="javascript">
alert("aaa");
</script>
<?php
return;
}
?>
but this one is not wokring proerly and form is not being submitted on the specific form...
neither its showing alert or message...
please help me...
This part is not correct:
$form['submit'][] = array('package_get_form'=> array());
To add a submit callback, simply write:
$form['submit'][] = 'package_get_form';
You don't even need this line since the form API provides a default callback appending "_submit" to the form id/callback. For your case:
pricepackages_form_submit()
Hopefully I'm missing something simple here. I'm using the CakeDC Search and Tags plugin for my cake (2.3.4) app.
Along with generic search by field functionality I want the user to be able to search by tags. I've almost got this working but the search will only display results if you search for a single tag not multiples. For example, if I add an article with the following tags - black, white, red. The article will only show in the search results if I search for a single tag (say, black) and not all 3, or even 2... What am I missing?
Heres my code:
Article.php model
class Article extends AppModel {
public $actsAs = array(
'Upload.Upload' => array(
'screenshot1' => array (
'fields' => array (
'dir' => 'dir'
),
'thumbnailMethod' => 'php',
'thumbnailSizes' => array(
'xvga' => '1024x768',
'vga' => '640x480',
'thumb' => '80x80'
),
),
),
'Search.Searchable',
'Tags.Taggable'
);
// Search plugin filters
public $filterArgs = array(
'title' => array('type' => 'like'),
'environment' => array('type' => 'like'),
'description' => array('type' => 'like'),
'error' => array('type' => 'like'),
'cause' => array('type' => 'like'),
'resolution' => array('type' => 'like'),
'live' => array('type' => 'value'),
'synced' => array('type' => 'value'),
'tags' => array('type' => 'subquery', 'method' => 'findByTags', 'field' => 'Article.id'),
array('name' => 'search', 'type' => 'query', 'method' => 'filterQuery'),
);
// This is the OR query that runs when the user uses the client side signle field search
public function filterQuery($data = array()) {
if(empty($data['search'])) { // search is the name of my search field
return array();
}
$query = '%'.$data['search'].'%';
return array(
'OR' => array(
'Article.title LIKE' => $query,
'Article.description LIKE' => $query,
'Article.error LIKE' => $query,
)
);
}
// Find by tags method
public function findByTags($data = array()) {
$this->Tagged->Behaviors->attach('Containable', array('autoFields' => false));
$this->Tagged->Behaviors->attach('Search.Searchable');
$query = $this->Tagged->getQuery('all', array(
'conditions' => array('Tag.name' => $data['tags']),
'fields' => array('foreign_key'),
'contain' => array('Tag')
));
return $query;
}
public $hasAndBelongsToMany = array(
'Category' => array(
'className' => 'Category',
'joinTable' => 'articles_categories',
'foreignKey' => 'article_id',
'associationForeignKey' => 'category_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
'Tag' => array('with' => 'Tagged')
);
Controller method
public function admin_advancedSearch() {
// Disable validation for this action
$this->Article->validate = array();
// For search plugin
$this->Prg->commonProcess();
// Set searched for details
$this->set('searchedFor', $this->passedArgs);
// If passed args are all empty
if ($this->passedArgs) {
if (empty($this->passedArgs['title']) AND
empty($this->passedArgs['environment']) AND
empty($this->passedArgs['description']) AND
empty($this->passedArgs['error']) AND
empty($this->passedArgs['cause']) AND
empty($this->passedArgs['resolution']) AND
empty($this->passedArgs['live']) AND
empty($this->passedArgs['synced']) AND
empty($this->passedArgs['tags'])) {
$this->Session->setFlash('Please enter at least one search criteria', 'flash_failure');
// Set this var for checks in view
$this->set('emptySeach', true);
} else {
$paginateConditions = $this->Article->parseCriteria($this->passedArgs);
$this->paginate = array('conditions' => array(
$paginateConditions),
'limit' => 10,
'order' => array('Article.modified' => 'DESC'));
$this->set('articles', $this->paginate());
// Count number of results
$count = 0;
foreach ($this->paginate() as $result) {
$count++;
}
$this->set('resultsCount', $count);
// Search was not empty - set flag for view
$this->set('emptySeach', false);
}
}
// Set layout
$this->layout = 'admin';
//debug($this->passedArgs);
}
All plugins are loaded successfully from bootstrap, searches work fine without tags. Searches with tags only work if one tag is entered....
*EDIT *
If I debug $query from the findByTags method I get this:
'SELECT `Tagged`.`foreign_key` FROM `knowledgebase`.`tagged` AS `Tagged` LEFT JOIN `knowledgebase`.`tags` AS `Tag` ON (`Tagged`.`tag_id` = `Tag`.`id`) WHERE `Tag`.`name` = 'kia, rio, red''
So it looks like its trying to find a single tag with all the searched for tags in its name. How can I make the WHERE part an IN?
For example:
WHERE `Tag`.`name` IN ('kia', 'rio', 'red')
Thanks
Use the "method" https://github.com/cakedc/search#behavior-and-model-configuration key her to pass the search args to a customized method.
The example here shows you exactly how this works https://github.com/cakedc/search#full-example-for-modelcontroller-configuration-with-overriding
public $filterArgs = array(
'some_related_table_id' => array('type' => 'value'),
'search'=> array('type' => 'like', 'encode' => true, 'before' => false, 'after' => false, 'field' => array('ThisModel.name', 'OtherModel.name')),
'name'=> array('type' => 'query', 'method' => 'searchNameCondition')
);
public function searchNameCondition($data = array()) {
$filter = $data['name'];
$cond = array(
'OR' => array(
$this->alias . '.name LIKE' => '' . $this->formatLike($filter) . '',
$this->alias . '.invoice_number LIKE' => '' . $this->formatLike($filter) . '',
));
return $cond;
}
Inside your custom method explode() the tags and make them an array so that CakePHP is using them as IN() or better (in can become slow) make it a chain of AND or OR.
I have a form set up where a users selects an item from a drop down. Once that item is selected another drop down is populated. Then depending on the value that is selected from the second drop down a fieldset may or may not be shown. If the field set is shown there is a field and a button. By clicking the button you add another copy of the same field. Once there is more than one a remove one button shows up as well. I got the basis for the code from here:
http://api.drupal.org/api/examples/ajax_example%21ajax_example_graceful_degradation.inc/function/ajax_example_add_more/7
The problem is when I click 'Add Another Survey Question' the first time it works fine and add a field. When I click it a second time or when I click remove now that there are two, I get this error: 'An illegal choice has been detected. Please contact the site administrator.'
What am I doing wrong?
Here's my code:
function touchpoints_addmetric_form($form, &$form_state, $tp_id) {
$selectedType = isset($form_state['values']['type']) ? $form_state['values']['type'] : FALSE;
$types = db_query('SELECT * FROM {touchpoints_metric_types}') -> fetchAllKeyed(0, 1);
$form['#tree'] = TRUE;
$form['type'] = array(
'#type' => 'select',
'#title' => t('Metric Type'),
'#options' => $types,
'#required'=>TRUE,
'#ajax' => array(
'event' => 'change',
'wrapper' => 'method-wrapper',
'callback' => 'touchpoints_method_callback'
)
);
$form['measurementmethod'] = array(
'#type' => 'select',
'#title' => t('Measurement Method'),
'#required'=>TRUE,
'#prefix' => '<div id="method-wrapper">',
'#suffix' => '</div>',
'#options' => _get_methods($selectedType)
);
$form['survey'] = array('
#type' => 'fieldset',
'#collapsible' => FALSE,
'#states' => array(
'visible' => array(
array(
array(':input[name="measurementmethod"]' => array('value' => '5')),
'xor',
array(':input[name="measurementmethod"]' => array('value' => '6')),
'xor',
array(':input[name="measurementmethod"]' => array('value' => '7'))
)
)
)
);
$form['survey']['contents'] = array(
'#type' => 'fieldset',
'#collapsible' => FALSE,
'#prefix' => '<div id="survey-div">',
'#suffix' => '</div>',
);
if (empty($form_state['num_surveys'])) {
$form_state['num_surveys'] = 1;
}
for ($i = 1; $i <= $form_state['num_surveys']; $i++) {
$form['survey']['contents']['survey_question'][$i] = array(
'#type' => 'textfield',
'#title' => t('Survey Question ' . $i),
'#size' => 70, '#maxlength' => 100,
);
}
$form['survey']['contents']['addsurvey'] = array(
'#type' => 'submit',
'#value' => t('Add Another Survey Question'),
'#submit' => array('touchpoints_metrics_survey_add_one'),
'#limit_validation_errors' => array(),
'#ajax' => array(
'callback' => 'touchpoints_metrics_survey_callback',
'wrapper' => 'survey-div',
),
);
if ($form_state['num_surveys'] > 1) {
$form['survey']['contents']['removesurvey'] = array(
'#type' => 'submit',
'#value' => t('Remove A Survey Question'),
'#submit' => array('touchpoints_metrics_survey_remove_one'),
'#limit_validation_errors' => array(),
'#ajax' => array(
'callback' => 'touchpoints_metrics_survey_callback',
'wrapper' => 'survey-div',
),
);
}
return $form;
}
function _get_methods($selected) {
if ($selected) {
$methods = db_query("SELECT * FROM {touchpoints_m_methods} WHERE mt_id=$selected") -> fetchAllKeyed(0, 2);
} else {
$methods = array();
}
return $methods;
}
function touchpoints_method_callback($form, &$form_state) {
return $form['measurementmethod'];
}
function touchpoints_metrics_survey_add_one($form, &$form_state) {
$form_state['num_surveys']++;
$form_state['rebuild'] = TRUE;
}
function touchpoints_metrics_survey_remove_one($form, &$form_state) {
if ($form_state['num_surveys'] > 1) {
$form_state['num_surveys']--;
}
$form_state['rebuild'] = TRUE;
}
function touchpoints_metrics_survey_callback($form, &$form_state) {
return $form['survey']['contents'];
}
I have encountered this error a few times, and this is what I found:
This is Drupal's way of saying "Hey, you tried to submit this form with a , checkbox or radio button option that wasn't included in the original form definition! That's not allowed." - See more at: http://proofgroup.com/blog/2008/jul/debugging_mysterious_illegal_choice_has_been_detected_please_contact_site_administrato#sthash.vDNmqslL.dpuf
Instead of custom code, maybe you should consider module Conditional Fields
Or a work around: '#validate' => TRUE source
Actually, it's '#validated' => TRUE.