All, I have a problem with a new module development. Being a noob I need your kind help.
Info file
; $Id$
name="cities"
description="List of cities"
core=7.x
Module file
<?php
// $Id$
/**
*impliments hook_menu()
*/
/**
* #file
* cities module
.
*/
function cities_menu()
{
$items=array();
$items['cities']=array(
'title' => 'Cities listed',
'page callbacks' => 'cities_page',
'access arguments'=>array('view content')
);
return $items;
}
/**
*Build a from containing :
*(1)Space to input volume
*(2)A Submit ("Convert") button.
*/
function cities_form($form,&$form_state){
$form['cities']=array(
'#type'=>'select',
'#title' => t('cities'),
'#size'=>5,
'#default_value' => array('City list' => 'City list'),
'#options' => array('Kolkata' => 'Kolkata', 'Mumbai' => 'Mumbai','Cnennai' => 'Cnennai', 'Srilanka' => 'Srilanka','Delhi' => 'Delhi', 'Siliguri' => 'Siliguri'),
'#description'=>t('The number of Cities listed here'),
);
$form['submit']=array(
'#type'=>'submit',
'#value'=>t('Get Val'),
);
return $form;
}
function cities_page($form,&$form_state)
{
return cities_form('cities_form');
}
function cities_form_submit($form,&$form_state)
{
$output=(
$from_state['values']['default_value']);
set_drupal_message($output);
}
?>
But when I typed mysitename/cities in address bar it redirects me on localhost. Except these, I couldn't find any errors info in console.
Please help me to run my code.
Thanks.
First guess:
<?php
function cities_menu() {
$items['cities']=array(
'title' => 'Cities listed',
'page callbacks' => 'drupal_get_form',
'page arguments' => array('cities_form'),
'access arguments'=>array('view content'),
);
return $items;
}
}
You have to call drupal_get_form to render the form correctly (as I see you use hook_form in cities_form, but you do not call drupal_get_form from cities_page. You do not need it either, because drupal_get_form (what is have to be used to render the form itself) is callable via hook_menu too).
So, with this hook_menu, you can simply skip cities_page function completely.
The error free code for me
<?php
// $Id$
/**
*implements hook_menu()
*/
/**
* #file
* cities module
.
*/
function cities_menu() {
$items['cities']=array(
'title' => 'Cities listed',
'page callback' => 'cities_form',
'page arguments' => array('cities_form'),
'access arguments'=>array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function cities_form() {
return drupal_get_form('cities_test_form');
}
function cities_test_form($form,&$form_state){
$form['cities']=array(
'#type'=>'select',
'#title' => t('The number of Cities listed here'),
'#default_value' => array('City list' => 'City list'),
'#options' => array('Kolkata' => 'Kolkata', 'Mumbai' => 'Mumbai','Cnennai' => 'Cnennai', 'Srilanka' => 'Srilanka','Delhi' => 'Delhi', 'Siliguri' => 'Siliguri'),
);
$form['submit']=array(
'#type'=>'submit',
'#value'=>t('Get Val'),
);
return $form;
}
function cities_test_form_submit($form_state, $form_values)
{
$output = $form_values['values']['cities'];
drupal_set_message($output);
}
?>
Related
On direct URL hit it showing the result I want not to done it but when I give permission to it it should be work fine. Help me regarding this I will be very grateful to you.
This is my module provide me the code for that module:
<?php
// $Id: person.module
/**
* implements hook_menu()
*/
function person_menu(){
$items = array();
$items['person'] = array(
'title' => "Person",
'page callback' => "perso_personal_info", // after visit drupal6/person, person_personal_info() function is called
'access callback' => true, // must return true, otherwise it will not visible as menu item
'type' => MENU_NORMAL_ITEM, // drupal's default menu type
'weight' => '10', // we want to display person link below in our nav menu
);
return $items; // finally, do not forget to return $items array
}
function perso_personal_info(){
$output = 'Name: Gaurav</br>';
$output .= 'City: nanital </br>';
$output .= 'Country: india </br>';
return $output;
}
function person_permission(){
return array(
'administer my module' => array(
'title' => t('Administer my module'),
'description' => t('Perform administration tasks for my module.'),
),
); }
?>
Please provide me the the code needed; it should work fine when I set the permission for my module.
You need to update "access callback" in your hook_menu in which users permission will be checked as below:
/**
* implements hook_menu()
*/
function person_menu() {
$items = array();
$items['person'] = array(
'title' => "Person",
'page callback' => "demo_custom_personal_info", // after visit drupal6/person, person_personal_info() function is called
//'access callback' => true, // must return true, otherwise it will not visible as menu item
'access callback' => 'person_personal_info_check_access',
'type' => MENU_NORMAL_ITEM, // drupal's default menu type
'weight' => '10', // we want to display person link below in our nav menu
);
return $items; // finally, do not forget to return $items array
}
Now you will need to add below function in you module file (this function will check user's access permission which will assign from permission page)
/**
* To check user's permission
*/
function person_personal_info_check_access() {
if (user_access('administer my module')) {
return TRUE;
}
return FALSE;
}
How can I keep the same checkboxes checked after submit? All the other input fields on the form automatically keeps the values. I thought this would also go for checkboxes, but nope.
echo $this->Form->input('type_id', array(
'multiple' => 'checkbox',
'options' => array(
'1' => 'Til salgs',
'2' => 'Ønskes kjøpt',
'3' => 'Gis bort'
),
'div' => false,
'label' => false
));
I believe this can be done in the controller, but how?
Edit:
Since I posted this question I've changed to CakeDcs Search plugin, because I've gotten this to work with that before. Still... I can't get it to work this time.
Adding model and controller code:
AppController
public $components = array('DebugKit.Toolbar',
'Session',
'Auth' => array(
'loginAction' => '/',
'loginRedirect' => '/login',
'logoutRedirect' => '/',
'authError' => 'Du må logge inn for å vise denne siden.',
'authorize' => array('Controller'),
),
'Search.Prg'
);
public $presetVars = true; //Same as in model filterArgs(). For Search-plugin.
AdsController
public function view() {
$this->set('title_for_layout', 'Localtrade Norway');
$this->set('show_searchbar', true); //Shows searchbar div in view
$this->log($this->request->data, 'debug');
//Setting users home commune as default filter when the form is not submitted.
$default_filter = array(
'Ad.commune_id' => $this->Auth->user('User.commune_id')
);
$this->Prg->commonProcess(); //Search-plugin
$this->paginate = array(
'conditions' => array_merge($default_filter, $this->Ad->parseCriteria($this->passedArgs)), //If Ad.commune_id is empty in second array, then the first will be used.
'fields' => $this->Ad->setFields(),
'limit' => 3
);
$this->set('res', $this->paginate());
}
Model
public $actsAs = array('Search.Searchable');
public $filterArgs = array(
'search_field' => array('type' => 'query', 'method' => 'filterSearchField'),
'commune_id' => array('type' => 'value'),
'type_id' => array('type' => 'int')
);
public function filterSearchField($data) {
if (empty($data['search_field'])) {
return array();
}
$str_filter = '%' . $data['search_field'] . '%';
return array(
'OR' => array(
$this->alias . '.title LIKE' => $str_filter,
$this->alias . '.description LIKE' => $str_filter,
)
);
}
/**
* Sets the fields which will be returned by the search.
*
* #access public
* #return array Database table fields
* #author Morten Flydahl
*
*/
public function setFields() {
return array(
'Ad.id',
'Ad.title',
'Ad.description',
'Ad.price',
'Ad.modified',
'User.id',
'User.first_name',
'User.middle_name',
'User.last_name',
'User.link',
'User.picture_url',
'Commune.name',
'Type.id',
'Type.name'
);
}
You have to set manually the selected option of the input, as an array with "keys = values = intval(checkbox id)"
I cannot explain why this format, but this is the only way I get it to work.
Here is my code:
echo $this->Form->create('User');
// Read the submitted value
$selected = $this->Form->value('User.Albums');
// Formats the value
if (empty($selected)) {
$selected = array(); // avoid mess
} else {
$selected = array_map('intval', $selected);
$selected = array_combine ($selected, $selected);
}
// Renders the checkboxes
echo $this->Form->input('Albums',array(
'type' => 'select',
'multiple' => 'checkbox',
'options' => $albums, // array ( (int)id => string(label), ... )
'selected' => $selected, // array ( (int)id => (int)id, ... )
));
Hope this helps.
++
I'm building a Form in a module using the Form API. I've had a couple of dependent dropdowns that have been working just fine. The code is as follows:
$types = db_query('SELECT * FROM {touchpoints_metric_types}') -> fetchAllKeyed(0, 1);
$types = array('0' => '- Select -') + $types;
$selectedType = isset($form_state['values']['metrictype']) ? $form_state['values']['metrictype'] : 0;
$methods = _get_methods($selectedType);
$selectedMethod = isset($form_state['values']['measurementmethod']) ? $form_state['values']['measurementmethod'] : 0;
$form['metrictype'] = array(
'#type' => 'select',
'#title' => t('Metric Type'),
'#options' => $types,
'#default_value' => $selectedType,
'#ajax' => array(
'event' => 'change',
'wrapper' => 'method-wrapper',
'callback' => 'touchpoints_method_callback'
)
);
$form['measurementmethod'] = array(
'#type' => 'select',
'#title' => t('Measurement Method'),
'#prefix' => '<div id="method-wrapper">',
'#suffix' => '</div>',
'#options' => $methods,
'#default_value' => $selectedMethod,
);
Here are the _get_methods and touchpoints_method_callback functions:
function _get_methods($selected) {
if ($selected) {
$methods = db_query("SELECT * FROM {touchpoints_m_methods} WHERE mt_id=$selected") -> fetchAllKeyed(0, 2);
} else {
$methods = array();
}
$methods = array('0' => "- Select -") + $methods;
return $methods;
}
function touchpoints_method_callback($form, &$form_state) {
return $form['measurementmethod'];
}
This all worked fine until I added a file field to the form. Here is the code I used for that:
$form['metricfile'] = array(
'#type' => 'file',
'#title' => 'Attach a File',
);
Now that the file is added if I change the first dropdown it hangs with the 'Please wait' message next to it without ever loading the contents of the second dropdown. I also get the following error in my JavaScript console:
"Uncaught TypeError: Object function (a,b){return new p.fn.init(a,b,c)} has no method 'handleError'"
What am I doing wrong here?
I am trying to build my own node reference custom field - I know several projects out there already do this, I am building this in order to learn... :)
My problem is the autocomplete path, it's not being triggered, I have checked the noderefcreate project and implemented my solution based on that project. But still; nothing is being triggered when I check firebug.
Here is my code:
function nodereference_field_widget_info() {
return array(
'nodereference_nodereference_form' => array(
'label' => t('Node Reference Form'),
'field types' => array('nodereference'),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
'default value' => FIELD_BEHAVIOR_DEFAULT,
),
'settings' => array(
'autocomplete_match' => 'contains',
'autocomplete_path' => 'nodereference/autocomplete',
),
),
);
}
function nodereference_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
if ($instance['widget']['type'] == 'nodereference_nodereference_form') {
$widget = $instance['widget'];
$settings = $widget['settings'];
$element += array(
'#type' => 'textfield',
'#default_value' => isset($items[$delta]['nid']) ? $items[$delta]['nid'] : NULL,
'#autocomplete_path' => $instance['widget']['settings']['autocomplete_path'],
);
}
return array('nid' => $element);
}
You need to define method from where auto compete will take suggestions.
It can be done like this:
/**
* implements hook_menu
*/
function your_module_name_menu() {
$items['master_place/autocomplete'] = array(
'page callback' => '_your_module_name_autocomplete',
'access arguments' => array('access example autocomplete'),
'type' => MENU_CALLBACK
);
return $items;
}
/**
* Auto complete method implementation
* #param $string
*/
function _your_module_name_autocomplete($string) {
$matches = array();
// Some fantasy DB table which holds cities
$query = db_select('target_db_table', 'c');
// Select rows that match the string
$return = $query
->fields('c', array('target_column'))
->condition('c.target_column', '%' . db_like($string) . '%', 'LIKE')
->range(0, 10)
->execute();
// add matches to $matches
foreach ($return as $row) {
$matches[$row->city] = check_plain($row->city);
}
// return for JS
drupal_json_output($matches);
}
For some reason my allowed_values_function never gets called when showing a field on a user bundle. Code:
function get_business_units()
{
$options = entity_load('business_unit', FALSE, NULL, FALSE);
$opt = bu_to_list_values($options);
return $opt;
}
function MYMODULE_enable()
{
if (!field_info_field('field_user_business_unit')) {
$field = array(
'field_name' => 'field_user_business_unit',
'type' => 'text',
'settings' => array(
'allowed_values' => array(),
'allowed_values_function' => 'get_business_units',
)
);
field_create_field($field);
// Create the instance on the bundle.
$instance = array(
'field_name' => 'field_user_business_unit',
'entity_type' => 'user',
'label' => 'Business Unit',
'bundle' => 'user',
'required' => FALSE,
'settings' => array(
'user_register_form' => 1,
),
'widget' => array(
'type' => 'options_select',
),
);
field_create_instance($instance);
}
}
The field is created, and even displayed on the users "edit" page when editing their info. But the only value is "Select" or "None". My method is never called (I even placed a debug point). This is all in MYMODULE.install file.
The problem is: 'type' => 'text'.
You have to use: 'type' => 'list_text'.
Allowed values is meaningless for a text type.
Your get_business_units() function needs to be in the MYMODULE.module file; the .install files aren't included in a normal Drupal bootstrap.
Have you tried
drush features-revert MYMODULE ?