Add 3 custom menu links to the navigation menu - drupal-7

I am using Drupal 7 and I want to add 3 menu links to the navigation menu. The links are based on the current logged in users 'uid' but I can't get it to work. I have checked this site out and ever example uses a custom module to implement. I am trying to add this to my template.php page.
This is currently what I have.
function mytheme_menu() {
$items = array();
$items['user/%uid/following'] = array(
'title' => 'My Following Content',
'description' => 'View the item',
'type' => MENU_NORMAL_ITEM,
'menu_name' => 'navigation',
'access callback' => 'user_is_logged_in', // TRUE, 'user_is_logged_in' or user_is_anonymous to check if logged in
'expanded' => TRUE,
);
$items['user/%uid/created'] = array(
'title' => 'My Created Content',
'description' => 'Item members',
'type' => MENU_NORMAL_ITEM,
'menu_name' => 'navigation',
'page callback' => views_page('individual_item', 'item_members'),
'access callback' => 'user_is_logged_in', // TRUE, 'user_is_logged_in' or user_is_anonymous to check if logged in
'expanded' => TRUE,
);
$items['user/%uid/interacted'] = array(
'title' => 'My Interacted Content',
'description' => 'All the Content for this item',
'menu_name' => 'navigation',
'type' => MENU_NORMAL_ITEM,
'page callback' => views_page('individual_item', 'item_content'),
'access callback' => 'user_is_logged_in', // TRUE, 'user_is_logged_in' or user_is_anonymous to check if logged in
'expanded' => TRUE,
);
return $items;
}

You're right there Brad, you can only implement hook_menu() in a custom module.
You can follow something like this to create a simple custom module and put your custom_module_menu() function it the .module file.
https://www.drupal.org/node/778734

Related

How to create an image button that behaves like a button in drupal 7?

How I want it to work:
I have list of buttons(type button) that trigger an Ajax call that add the that item to a list. This works as exactly as I want it except for the button looks ugly .
The problem:
When I try to replace the "button" with an "image button" the form is submitted, which is not what I want. Is there a way to add an image to a button that do not trigger a submit? Can I disable the submit for "image button"? Or shall I add the image using css on the button?
What is the difference between "button", "image button" and "submit"?
To make the type image_button act like an button is looked in system.module and found "'#executes_submit_callback' => TRUE", so change this to false will prevent submit function to be called on your image_button.
Code from system.module:
$types['button'] = array(
'#input' => TRUE,
'#name' => 'op',
'#button_type' => 'submit',
'#executes_submit_callback' => FALSE,
'#limit_validation_errors' => FALSE,
'#process' => array('ajax_process_form'),
'#theme_wrappers' => array('button'),
);
$types['image_button'] = array(
'#input' => TRUE,
'#button_type' => 'submit',
'#executes_submit_callback' => TRUE, // <--- This is why submit is triggered
'#limit_validation_errors' => FALSE,
'#process' => array('ajax_process_form'),
'#return_value' => TRUE,
'#has_garbage_value' => TRUE,
'#src' => NULL,
'#theme_wrappers' => array('image_button'),
);
Another thing that you might encounter is the validation error, to remove it just add the "'#validate' => array()" or if you want to run validation but ignore errors use "#limit_validation_errors' => array()". This also apply for button.
Here is an example where you can experiment the above things and see when the validation and submit callback is triggered. Also a checkbox is included to show when validation errors occurs.
function button_menu()
{
$items = array();
$items['button'] = array(
'title' => 'Button',
'page callback' => 'drupal_get_form',
'page arguments' => array('button_form'),
'access callback' => array(TRUE),
'type' => MENU_CALLBACK,
);
return $items;
}
function button_form($form, &$form_state)
{
// Add to prove the point of how validation is working
$form['checkbox'] = array(
'#type' => 'checkbox',
'#title' => t('checkbox'),
'#default_value' => FALSE,
);
$form['button'] = array(
'#id' => 'button_1',
'#type' => 'button',
'#name' => 'test1',
'#value' => 'test1',
//'#validate' => array(), // This line will remove validation completely
'#limit_validation_errors' => array(), // This line will run validation but ignore any errors
'#ajax' => array(
'callback' => 'button_test_callback',
'wrapper' => 'wrapper',
'method' => 'replace',
'effect' => 'fade',
),
);
$form['image_button'] = array(
'#id' => 'image_button_1',
'#type' => 'image_button',
'#src' => '/themes/bartik/logo.png', // hope you still have bartik theme
'#executes_submit_callback' => FALSE, // This line removes the submit callback
//'#validate' => array(), // This line will remove validation completely
'#limit_validation_errors' => array(), // This line will run validation but ignore any errors
'#ajax' => array(
'callback' => 'button_test_callback',
'wrapper' => 'wrapper',
'method' => 'replace',
'effect' => 'fade',
),
);
// Just some code to show what button was pressed
if (array_key_exists('triggering_element', $form_state) &&
($form_state['triggering_element']['#id'] == 'button_1' || $form_state['triggering_element']['#id'] == 'image_button_1'))
{
$form['markup'] = array(
'#type' => 'markup',
'#markup' => '<div id="wrapper"><p>'. $form_state['triggering_element']['#id'] .'</p></div>',
);
}
else {
$form['markup'] = array(
'#type' => 'markup',
'#markup' => '<div id="wrapper"><p>nothing</p></div>',
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
function button_test_callback($form, $form_state)
{
return $form['markup'];
}
function button_form_validate($form, &$form_state)
{
// To display when validation is triggered
drupal_set_message('button_form_validate');
// Only when submit button is pressed we care about this error.
if ( $form_state['values']['checkbox'] == 0) {
form_set_error('checkbox', 'checkbox not checked');
}
}
function button_form_submit($form, &$form_state)
{
// To display when submit is triggered
drupal_set_message('button_form_submit');
}

Not able to make submit button work in form theme in drupal 7

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); ?>

Drupal Submit handler is not called properly in custom module

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()

Wordpress preview broken on custom post types

I can not preview a post when it is in a custom post type. If I am on the page where all the post are listed and press the preview button, that works fine. But wheni go into a post and click the "preview changes" button, I get a 404 error page.
I have updated and saved the permalinks. I set the post type to publicly_queryable.
If I disable Javascript in my browser, then the preview button works, but this would mean that I would have to change the Wordpress core file which I don't want to do.
Here is my custom post type structure:
function my_custom_post_attq() {
$labels = array(
'name' => _x( 'Product' ),
'singular_name' => _x( 'product' ),
'add_new' => _x( 'New product' ),
'add_new_item' => __( 'add product' ),
'edit_item' => __( 'edit product' ),
'new_item' => __( 'new product' ),
'all_items' => __( 'all products' ),
'view_item' => __( 'view product' ),
'search_items' => __( 'search products' ),
'not_found' => __( 'product not found' ),
'not_found_in_trash' => __( 'product not found in trash' ),
'parent_item_colon' => '',
'menu_name' => 'product'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'attq' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 8,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'revisions', 'custom-fields', 'post-formats' ),
'taxonomies' => array( 'category', 'post_tag')
);
register_post_type( 'attq', $args );
}
add_action( 'init', 'my_custom_post_attq', 'flush_rewrite_rules' );
My permalinks structure is
/%year%/%monthnum%/%day%/%postname%/
Does anyone have any suggestions?
I took the 'post-formats' out of the args array. This fixed the problem. Wordpress was looking for post formats and I didn't have any of them created in my custom post types.

yii booster checkbox doesn't show boxes to check

I add the checkbox functionality from the yii-booster. But the widget renders the model view without the needed boxes. What's wrong?
Widjet code in view
<?php
$this->widget('bootstrap.widgets.TbExtendedGridView',array(
'id'=>'docs-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'type'=>'bordered condensed',
'template' => "{items}",
'bulkActions' => array(
'actionButtons' => array(
array(
'buttonType' => 'button',
'type' => 'primary',
'size' => 'small',
'label' => 'Choose',
'click' => 'js:function(values){console.log(values);}'
)
),
// if grid doesn't have a checkbox column type, it will attach
// one and this configuration will be part of it
'checkBoxColumnConfig' => array(
'name' => 'id'
),
),
));
If you are using bulkActions, you have to use 'columns' to list out the columns you want to display instead of using 'template'.
'columns' => array(
'id',
'title',
...
),
the problem has been in the wrong hierarchy from the example:
The 'checkBoxColumnConfig' attribute must be outside of the 'actionButtons' attribute:
'bulkActions' => array(
'actionButtons' => array(
/*array(
'buttonType' => 'button',
'type' => 'primary',
'size' => 'small',
'label' => 'Выбрать отмеченные',
'click' => 'js:function(values){console.log(values);}'
)
),*/
// if grid doesn't have a checkbox column type, it will attach
// one and this configuration will be part of it
),
'checkBoxColumnConfig' => array(
'name' => 'id'
),
...
));
but now the widget doesn't work when i uncomment the array part inside 'actionButtons':
array(
'buttonType' => 'button',
'type' => 'primary',
'size' => 'small',
'label' => 'Выбрать отмеченные',
'click' => 'js:function(values){console.log(values);}'
)
what might be a cause?

Resources