I'm trying to get a Drupal 7 Field working programmatically and it's not going well.
I simply want to create a module that creates a Field with a checkbox. The module is working, the field is working but the checkbox will not save.
Here is my hook_field_widget_form:
<?php
function add_to_basket_field_widget_form(&$form,&$form_state,$field,$instance,$langcode,$items,$delta,$element)
{
$element += array(
'#type' => 'checkbox',
'#title' => t('Add to basket?'),
'#default_value' => isset($item['add_to_basket_cfield']) ? $item['add_to_basket_cfield'] : '',
);
return $element;
}
?>
When I run it as part of my module it works but wont save a tick or no tick.
I've tried everything I can think of to get this working but after 3 days with nothing to show it's time to ask the experts
Thanks
hope this helpful for you
$form[$group]['ex_account'] = array(
'#type' => 'checkbox',
'#title' => t('your title'),
'#default_value' => variable_get('ex_account', 1) ? 1 : 0,
'#description' => t('desc.'),
);
return system_settings_form($form); or return $form;
Try the example module and look at the field_example portion. Make sure that your custom field has a schema described in the .install file so that the value you are putting in your custom field actually saves.
Related
I have created a custom Drupal module/form that when submitted the action takes you to another page and posts data with it. Pretty standard stuff.
Randomly, the form action changes at random (not set) intervals during the day that are not correlating to anything immediately obvious - for example cron runs. It only happens maybe once or twice a day, so if anyone has any idea what might cause this or point me in the right direction.
Anything anyone feels needs adding, let me know.
The is the custom module code:
/**
* Test form declaration
*/
function test_form($form, &$form_state){
$form['#attributes'] = array('id' => "test-form");
$form['search-field'] = array(
'#type' => 'textfield',
'#title' => t('<span class="highlighted">Test</span>'),
'#attributes' => array(
'class' => array('form-control form-text'),
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Search',
'#attributes' => array(
'class' => array('btn btn-default text-hide'),
),
);
return $form;
}
/**
* search form submit
*/
function test_form_submit($form, &$form_state){
$searchQuery = $form_state['values']['search-field'];
$query = array();
if($search <> ''){
$query = array(
'field_geofield_distance[distance]' => '100',
'field_geofield_distance[unit]' => '3959',
'field_geofield_distance[origin]' => $search
);
}
drupal_goto('test-page', array('query' => $query));
}
If your form creates data (let's say a node) possibly you have a redirection on rules when creating a node that with specific values redirects to another page.
Look for rules that are activated when content creation occur or when submission occur.
Also you could have some redirection magic in the top of Drupal, like a wrong configuration of an .htacess file. If your form does the same thing always it should behave the same always. And why it's redirecting to page of old site?
Finally, if the new site have code from the old site the check submission, creation and form altering hooks.
Hope that helps.
I have been searching all day but unable to find any answers - I am sure I am doing it right as worked fine in Drupal 6 and should work fine in Drupal 7.
I want to give a custom theme function to my select element in my form
$form['field_name'] = array(
'#type' => 'select',
'#title' => t('Title Here'),
'#theme' => 'custom_select',
'#options' => $values,
);
I have the theme hook right to declare the new custom theme function but my problem is when using that custom theme function as above I get an empty $variables array which just reads
Array([element] => null)
can anyone see what I may be doing wrong? cleared cache, done everything I can think of - any ideas why Drupal is not passing the element data to the theme function? thanks
well finally figured this one out incase any one else has the problem - make sure you set render element in hook_theme and not variables!
before
function hook_theme(){
return array(
'select_currency' => array(
'variables' => array('element' => null),
'file' => 'module_name.theme.inc',
));
}
after
function hook_theme(){
return array(
'select_currency' => array(
'render element' => 'element',
'file' => 'module_name.theme.inc',
));
}
I was pulling my hair out until I remembered the render element!
I need to add a programmatic form to a node in Drupal 7. How to attach the form to the node?
function addtabexample_form($node, &$form_state) {
$type = node_type_get_type($node);
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#default_value' => !empty($node->title) ? $node->title : '',
'#required' => TRUE,
'#weight' => -5,
);
$form['field1'] = array(
'#type' => 'textfield',
'#title' => t('Custom field'),
'#default_value' => $node->field1,
'#maxlength' => 127,
);
return $form;
}
You can follow this code sample, using hook_node_view()
function [YOUR_MODULE]_node_view($node, $view_mode, $langcode)
{
$my_form = drupal_get_form('addtabexample_form', $node);
$node->content['my_form_attached'] = array(
'#markup' => drupal_render($my_form),
'#weight' => 10,
);
}
Your code has some problems that would require some rewriting...
First, I'd suggest reading Form API Quickstart, which is a decent source to get the job done.
I'm not sure how you get the $node object to this. You have $node in the function parameters and $form as a return value...
See http://drupal.org/node/197122 for an example (I added the D7 part) of a form that could be embeded in a node.
But doing that is ultra bad - you will face function redeclare problems, indexing problems, and a whole lot of troubles.
I know this is not an actual answer but I don't know how to write this in 500 chars.
I have created a custom module and have used 'hook_form' to create a custom form. I have then created a new page using menu callback and inserted this form on it
This form basically is to create nodes from the frontend. I have all the same fields as in the backend.
Form code:
function input_simple_form($form, &$form_submit) {
$form['title'] = array(
'#type' => 'textfield',
'#default_value' => '',
'#required' => TRUE,
'#title' => 'Title',
'#maxlength' => 1024,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
How do I save this, or how do I tell the hook_form that this is for a specific content type? I know how to use node_save but not sure how I would implement this with the hook_form.
How do I go about achieving what I want?
Thanks
Robert
Use in your callback function:
$my_form = drupal_get_form('input_simple_form');
echo $my_form;
if your are using hook_theme to theme your page parce $my_form as variable and print it in your template/theme file.
Can anyone advise me on customising the Add Block form? (/admin/build/block/add)
I want to hide the "User specific visibility settings" and "Role specific visibility settings" from users. This is what i've got so far, but obviously it's not right and I can't figure out what the array is. Anyone got the experience on this?
function theme_add_block_form($form) {
$form['roles']['#prefix'] = '<div class="hidden">';
$form['roles']['#suffix'] = '</div>';
return drupal_render($form);
}
Thanks,
H
EDIT - perhaps I wasn't clear - I'm comforable using the various form hooks from the API, but my problem in this case is that I can't identify the array elements to use in my function. The devel module doesn't seem to act on the blocks page, and the themer popup block thing is less than clear.
In modules/block/block.admin.inc, function block_admin_configure:
$form['user_vis_settings'] = array(
'#type' => 'fieldset',
'#title' => t('User specific visibility settings'),
'#collapsible' => TRUE,
);
(...)
$form['role_vis_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Role specific visibility settings'),
'#collapsible' => TRUE,
);
Just try to hide $form['user_vis_settings'] and $form['role_vis_settings'].
EDIT:
Don't touch modules/block/block.admin.inc!! (I only was pointing where I found the form fields' names ). Hide the fields in your theme_add_block_form. Instead of wrapping the fields inside a div, you can write $form['user_vis_settings']['#access'] = false;
http://api.drupal.org/api/function/hook_form_alter/6
This is the way to go. Using the http://api.drupal.org/api/function/hook_form_alter/6 as say in an other answer. You need to write this code in a costum module.
<?php
function module_name_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'block_admin_configure') {
$form['user_vis_settings'] = array(
'#type' => 'fieldset',
'#title' => t('User specific visibility settings'),
'#collapsible' => TRUE,
'#access' = FALSE,
);
$form['role_vis_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Role specific visibility settings'),
'#collapsible' => TRUE,
'#access' = FALSE,
);
}
}