Drupal 7: how do I save module settings? - php

I am trying to create some settings for a module and am following the guide on Drupal.org. I am also comparing my work to existing modules.
The config menu appears in the correct place with the correct fields but when I hit save no input is saved. (I have run cache clear and registry rebuild.)
Would anyone know what I am doing wrong here? I cannot see how my stuff differs.
On my .admin.inc file I have set the form as such:
function contact_page_settings() {
$form = array();
$config = contact_page_default_settings();
$form['#tree'] = TRUE;
$form['contact_page_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Top Section'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'top-title' => array(
'#type' => 'textfield',
'#title' => t('Top title'),
'#default_value' => !empty($config['top-title']) ? $config['top-title'] : '',
),
'top-left' => array(
'#type' => 'text_format',
'#title' => t('Top left'),
'#default_value' => !empty($config['top-left']) ? $config['top-left'] : '',
),
'top-right' => array(
'#type' => 'text_format',
'#title' => t('Top right'),
'#default_value' => !empty($config['top-right']) ? $config['top-right'] : '',
),
);
return system_settings_form($form);
}
On my .module file I have:
function contact_page_menu() {
$items = array();
$items['admin/settings/contact-page'] = array(
'title' => 'Contact Page content',
'page callback' => 'drupal_get_form',
'access arguments' => array('administer site configuration'),
'page arguments' => array('contact_page_settings'),
'type' => MENU_NORMAL_ITEM,
'file' => '/admin/contact_page.admin.inc',
);
return $items;
}
function contact_page_default_settings() {
$defaults = array(
'top-tile' => 'Top title',
'top-left' => 'Top left',
'top-right' => 'Top right',
);
$config = variable_get('contact_page_settings', array());
return array_merge($defaults, $config);
}

OK, so I found out the values were saving - just they were not appearing in my text areas when I reloaded the config page.
The problem was that I was using text_format fields which save data as an array. I needed to get the value property of that array as my default value.
So, I just added ['value'] to the default_value ternary expression:
'#default_value' => !empty($config['top-right']['value']) ? $config['top-right']['value'] : '',

Related

Drupal Form module development

I am developing a custom module by name 'form' in drupal 7.
I have placed my module folder in sites\all\modules\form.
And i have 2 files in 'form' folder. 'form.info' and 'form.module'.
'form.info'
name = Form
description = Form creation.
core = 7.x
And 'form.module' contains
<?php
function form_menu()
{
$items['form/examples'] = array
(
'title' => 'Form API Examples',
'description' => 'Examples of using the Form API',
'page callback' => 'drupal_get_form',
'page arguments' => array('form_simple_form'),
'access callback' => TRUE
);
return $items;
}
function form_simple_form($form, &$form_submit)
{
$form['color'] = array
(
'#title' => t('Favorite Color'),
'#type' => 'textfield',
'#required' => TRUE,
'#description' => t('What is the favorite color?'),
);
$form['submit'] = array
(
'#type' => 'submit',
'#value' => 'Submit',
)
return $form;
}
I am getting only blank white page when i click on the link.
Thanks in advance.
I works for me try this code for mymodule.module
<?php
// $Id$
function form_menu()
{
$items['form/examples'] = array
(
'title' => 'Form API Examples',
'description' => 'Examples of using the Form API',
'page callback' => 'drupal_get_form',
'page arguments' => array('form_simple_form'),
'access callback' => TRUE
);
return $items;
}
function form_simple_form($form, &$form_submit)
{
$form['color'] = array
(
'#title' => t('Favorite Color'),
'#type' => 'textfield',
'#required' => TRUE,
'#description' => t('What is the favorite color?'),
);
$form['submit'] = array
(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
When you create mymodule.info file
name = Form
description = Form creation.
core = 7.x
package = Form
Add package to it so you have your module in his own box. It is easier to find it betwen all of those modules.
Rename your module to form1, as it is conflicting with Drupal internal form module, located in /includes.
You forgot a semicolon here:
$form['submit'] = array
(
'#type' => 'submit',
'#value' => 'Submit',
); //<- semicolon missing

Drupal: Edit/delete is not working on the custom module

I am a newbie to drupal. I am working to create a custom module in the name "course". I have read about the list of hooks to create a custom module. So, what's my problem is that i cant able to see the edit form for the module configure but i can do add management and its working fine.
I've used Existing contact modules as a reference.
Below are my code:
In course.module
function course_menu() {
$items['admin/structure/course'] = array(
'title' => 'Academy\'s courses',
'description' => 'Create a system contact form and set up categories for the form to use.',
'page callback' => 'course_list',
'access arguments' => array('administer contact forms'),
'file' => 'course.admin.inc',
);
$items['admin/structure/course/add'] = array(
'title' => 'Add Courses',
'page callback' => 'drupal_get_form',
'page arguments' => array('course_edit_form'),
'access arguments' => array('administer contact forms'),
'type' => MENU_LOCAL_ACTION,
'weight' => 1,
'file' => 'course.admin.inc',
);
$items['admin/structure/course/edit/%course'] = array(
'title' => 'Edit Courses',
'page callback' => 'drupal_get_form',
'page arguments' => array('course_edit_form',4),
'access arguments' => array('administer contact forms'),
'file' => 'course.admin.inc',
);
return $items;
}
and in course.admin.inc:
function course_edit_form($form, &$form_state, array $course = array()) {
// If this is a new course, add the default values.
$course += array(
'name' => '',
'course_id' => NULL,
);
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Course Name'),
'#maxlength' => 255,
'#default_value' => $course['name'],
'#description' => t("Example: 'Available Course Names'."),
'#required' => TRUE,
);
$form['course_id'] = array(
'#type' => 'value',
'#value' => $course['course_id'],
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
function course_edit_form_submit($form, &$form_state) {
if (empty($form_state['values']['course_id'])) {
db_insert('courses') // Table name no longer needs {}
->fields(array(
'name' => $form_state['values']['name'],
'course_url' => $form_state['values']['name']
))
->execute();
//drupal_write_record('courses', $form_state['values']);
}
else {
db_insert('courses') // Table name no longer needs {}
->fields(array(
'name' => $form_state['values']['name'],
'course_url' => $form_state['values']['name']
))
->condition('course_id', $form_state['values']['course_id'])
->execute();
//drupal_write_record('courses', $form_state['values'], array('course_id'));
}
drupal_set_message(t('Course %name has been saved.', array('%name' => $form_state['values']['name'])));
watchdog('contact', 'Course %name has been saved.', array('%course' => $form_state['values']['name']), WATCHDOG_NOTICE, l(t('Edit'), 'admin/structure/course/edit/' . $form_state['values']['course_id']));
$form_state['redirect'] = 'admin/structure/course';
}
I can't able to find the exact problem, why add is working but not edit & delete? Help me on this.
Below image tells you what exactly i am asking for(Red mark).
If anything additional needed in this please let me know.
At A guess I'd say the edit route is not matching because of an issue with the course load function (not listed). Try dumping the result of that method. Is the course entity defined in your module? If so, then the convention is to prefix the variable name with the module name, and implement the appropriately names hook.
There's a better description of how this should work here: https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_menu/7
Jump to the "Auto-Loader Wildcards" section.
Try to clear cache /admin/config/development/performance

Drupal 6 page and form custom module

I'd like to make a drupal page with a form. Something like the following, which doesn't render:
function score_table_menu() {
$items['table'] = array(
'title' => t('name'),
'page arguments' => array('table_page'),
'page arguments' => array('table_form'),
'description' => t('score table'),
'type' => MENU_CALLBACK,
);
return $items;
}
function table_page(){
$output .= t('Complicated Hello');
$header = stuff;
$rows = stuff;
$output .= theme_table($header, $rows);
return $output;
}
function table_form(){
$stuff_array = array (values);
$form['choice']= array(
'#type' => 'select',
'#title' => t('Select Stuff'),
'#options' => $stuff_array,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
function table_form_validate(){}
function table_form_submit(){
drupal_set_message(t('Submitted'));
}
Is it possible to have half the module render a page and the other half a form? I've written out two page arguments. I don't see any difference between page argument and callback. Also might need to use drupal_get_form() to render the form on the page.
page callback
is the function that you want this URI to trigger.
page arguments
are any arguments that you want to sent to that function when the callback is called.
Your menu item should look like this:
$items['table'] = array(
'title' => t('name'),
'page callback' => 'table_page',
'description' => t('score table'),
'type' => MENU_CALLBACK,
);
Or this:
$items['table'] = array(
'title' => t('name'),
'page callback' => 'drupal_get_form',
'page arguments' => array('table_form'),
'description' => t('score table'),
'type' => MENU_CALLBACK,
);
In your table_form, you can use other form types to display markup,
$form['table'] = array(
'#type' => 'markup',
'#markup' => theme_table($header, $rows),
);
Take a look in the API for a better understanding of hook_menu: http://api.drupal.org/api/drupal/developer%21hooks%21core.php/function/hook_menu/6

Drupal custom form remembering textfield data

I am setting up a configuration page for a new module and I am having trouble getting my textfield to remember data I previously entered.
I am setting #default_value by calling variable_get but the value never gets reset in the textfield. It is however correctly stored as a variable and visible on the variable editor page.
Below is a code fragment:
$form = array();
$form['username'] = array(
'#type' => 'textfield',
'#title' => t('Username'),
'#default_value' => variable_get('username', '' ),
'#description' => t('supply the username'),
'#maxlength' => 30,
'#required' => TRUE,
'#weight' => -1,
);
return system_settings_form($form);
How can I fix this?
Updated code
The submit function is as follows:
function mymodule_settings_submit($form, &$form_state){
drupal_set_message('form submit responding : ' . $form_state['values']['username'] );
variable_set( 'username', $form_state['values']['username'] );
}
Updated code #2
The full code:
function my_module_init(){
}
function my_module_menu(){
$items = array();
$items['admin/settings/helper'] = array(
'title' => 'helper',
'page callback' => 'drupal_get_form',
'page arguments' => array('helper_setup'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function helper_setup(){
$node_types = node_get_types('names');
$form = array();
$form['username'] = array(
'#type' => 'textfield',
'#title' => t('Username'),
'#default_value' => variable_get('username', '' ),
'#description' => t('supply the username'),
'#maxlength' => 30,
'#required' => TRUE,
'#weight' => -1,
);
$form['#submit'][] = 'my_module_settings_submit';
return system_settings_form($form);
}
function my_module_validate($form, &$form_state){
die('validation area responding!');
}
function my_module_settings_submit($form, &$form_state){
drupal_set_message('form submit responding : ' . $form_state['values']['username'] );
variable_set( 'username', $form_state['values']['username'] );
}
in your form submit function you should insert variable_set('variableName' , 'value') variables is not inserted automatically..
UPDATE:
$form['username'] = array(
'#type' => 'textfield',
'#title' => t('Username'),
'#default_value' => variable_get('username', '' ),
'#description' => t('supply the username'),
'#maxlength' => 30,
'#required' => TRUE,
'#weight' => -1,
);
$form['#submit'][] = '_myform_submit_function' ;
return system_settings_form($form);
now lets build your submit function
function _myform_submit_function($form,&$form_state ) {
variable_set('username', $form_state['values']['username']) ;
drupal_set_message('form submit responding : ' . $form_state['values']['username'] );
}
now clear the cache and tell me if its working or not

Theming a form in Drupal which has to be a part of an Entity's Content

I've a form in my module (mymodule)...
function mymodule_form()
{
$form['mytext'] = array(
'#title' => 'Password',
'#type' => 'password',
'#size' => 10,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Cool!',
);
$form['cool_submit'] = array(
'#type' => 'submit',
'#value' => 'Cool Submit!',
);
return $form;
}
I've used the hook_entity_view hook to display this form under all drupal entities that are displayed.
function mymodule_entity_view($entity, $type, $view_mode, $langcode) {
$entity->content['myadditionalfield'] = mymodule_form();
}
When showing this form, drupal adds a DIV tag to the mytext (password field) by itself. I want to override this and provide my own DIV tags and theme to this form. How do I do it?
Thanks :-)
I worked further on this question to solve it. And the problem got solved. I changed the above code...
function mymodule_entity_view($entity, $type, $view_mode, $langcode) {
$element = array(
'start' => array(
'#type' => 'button',
'#name' => 'start',
'#button_type' => 'submit',
),
'my_text' => array(
'#type' => 'textfield',
'#size' => 30,
'#maxlength' => 50,
),
'my_submit' => array(
'#type' => 'button',
'#name' => 'Submit Discussion',
'#button_type' => 'submit',
),
);
$entity->content['disc_bar'] = $element;
}
And the problem kept creeping up whenever a textfield or a password field was rendered. I checked the type array in systems elements info function (a elements_info hook) and found that textfield and password fields come with a default value for the #theme-wrapper. I tried to override it by this way...
'my_text' => array(
'#type' => 'textfield',
'#size' => 30,
'#maxlength' => 50,
'#theme-wrapper' => '',
),
and It worked. Its not generating any additional division tags... :-)

Categories