Drupal 7: insert a photo in a drupal form - drupal-forms

Is it possible in drupal7 to insert a photo in fieldset form?
I have a start interface that contains two buttons leading to two athors interfaces and I would like to insert a photo above the two buttons
function my_module_start_form($form, &$form_state) {
$form['start']['image'] = array(
'#type' => 'fieldset',
'#title' => t('image'),
// is it possible some how to insert a photo in this form?
);
$form['start']['next'] = array(
'#type' => 'submit',
'#value' => t('Create charts')
);
$form['start']['examples'] = array(
'#type' => 'submit',
'#value' => t('See charts examples')
);
return $form;
}

I would simply nest a "markup" type form element with the image (assuming that it's a static image that doesn't change with user input): https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#markup
Something like this should work:
function my_module_start_form($form, &$form_state) {
$image_options = array(
'path' => 'path/to/img.jpg',
'alt' => 'Test alt',
'title' => 'Test title',
'width' => '50%',
'height' => '50%',
'attributes' => array('class' => 'some-img', 'id' => 'my-img'),
);
$image = theme('image', $image_options);
$form['start']['image'] = array(
'#markup' => $image,
);
ETC...
I'm assuming the ['start'] form element is one fieldset, and you'll have another fieldset for the other grouping that includes an image and some form elements.

Related

Drupal AJAX button on user profile

I've been trying to add AJAX buttons for a while. I am able to do it on forms, like this:
function hook_form_alter(&$form, &$form_state, $form_id) {
$form['suspend'] = array(
'#type' => 'button',
'#name' => 'foo',
'#value' => t('bar'),
'#ajax' => array('callback' => '_foo_bar'),
);
return $form;
}
working fine. However I cannot get it to work on user profiles or non-forms, like this:
function hook_user_view_alter(&$build) {
$build['suspend'] = array(
'#type' => 'button',
'#name' => 'foo',
'#value' => t('bar'),
'#ajax' => array('callback' => '_foo_bar'),
);
return $build;
}
Are there simple ways of doing this? I use blocks & views on this site and would rather not have to install Panels if possible (:
Thanks!
If it's not inside a form, wrap it into a form:
function example_suspend_form($form, &$form_state) {
$form['suspend'] = array(
'#type' => 'button',
'#name' => 'foo',
'#value' => t('bar'),
'#ajax' => array('callback' => '_foo_bar'),
);
return $form;
}
function example_user_view_alter(&$build) {
$build['example_suspend_form'] = drupal_get_form('example_suspend_form');
}

How to select multiple check box in edit grid section of magento

http://magento.localhost.com/index.php/arithmetic/adminhtml_arithmetic/edit/id/5/key/c03c12d4c338a2e4cdbb93c3d9e511a93401d19b21a13ea77cffda20cac94577/
This is what my link looks like. I am getting all values by the ID, in the edit grid page
there is a section for multiple check boxes. How can I select all the check boxes according to the result array
$fieldset-> addField('st_user_interest', 'checkboxes', array(
'label' => Mage::helper('arithmetic')->__('Interest'),
'required' => true,
'name' => 'st_user_interest[]',
'values' => array(
array(
'label' => Mage::helper('arithmetic')->__('Education'),
'value' => 'education',
'class' => 'required-one',
),
array(
'label' => Mage::helper('arithmetic')->__('Business'),
'value' => 'business',
'class' => 'required-one',
),
array(
'label' => Mage::helper('arithmetic')->__('Marketing'),
'value' => 'marketing',
'class' => 'required-one',
),
array(
'value' => 'investment',
'label' => Mage::helper('arithmetic')->__('Investment'),
'class' => 'required-one',
)
),
));
Thanks
Hi at the time of storing array field value we are storing as string after converting array to string,
So at the time of setValues() Magento looking for that same input field value as array to check the check boxes
Trick is that convert that stored string value into array and assign to that column field that will work
Package_Arithmetic_Block_Adminhtml_Arithmetic_Edit_Tab_Form
protected function _prepareForm()
{
$id = $this->htmlEscape(Mage::registry('arithmetic_data')->getIn_user_id());
$model = Mage::getModel("arithmeti/newuser")->load($id);
/* here is the stuff witch converting the stored string to array and set in st_user_interest */
$interest = $model->getSt_user_interest();
$model->setSt_user_interest(explode(',',$interest));
$form = new Varien_Data_Form();
$this->setForm($form);
$fieldset = $form->addFieldset('arithmetic_form', array('legend'=>Mage::helper('arithmetic')->__('User information')));
$fieldset-> addField('st_user_interest', 'checkboxes', array(
'label' => Mage::helper('arithmetic')->__('Interest'),
'required' => true,
'name' => 'st_user_interest[]',
'values' => array(
array(
'label' => Mage::helper('arithmetic')->__('Education'),
'value' => 'education',
),
array(
'label' => Mage::helper('arithmetic')->__('Business'),
'value' => 'business',
),
array(
'label' => Mage::helper('arithmetic')->__('Marketing'),
'value' => 'marketing',
),
array(
'value' => 'investment',
'label' => Mage::helper('arithmetic')->__('Investment'),
)
),
));
if ( Mage::getSingleton('adminhtml/session')->getArithmeticData() )
{
$form->setValues(Mage::getSingleton('adminhtml/session')->getArithmeticData());
Mage::getSingleton('adminhtml/session')->setArithmeticData(null);
} elseif ( $model->getData() ) {
//Mage::registry('arithmetic_data')->getData(); /* removing this line and adding $model->getData() inslde the $form->setValues() */
$form->setValues($model->getData());
}
return parent::_prepareForm();
}

How to save a custom form image to a custom user field

I have a custom form that creates a new user and fills in a number of custom fields for that user. One of these fields is a custom image (not the system avatar image).
I can get the image uploaded to the server through the form, but can't get it into the appropriate field. Here is my (custom module) code so-far.
function newacc_freebusiness_form($form, &$form_state) {
$form['bussimage'] = array(
'#title' => t('Upload an image that shows off your business.'),
'#type' => 'managed_file',
'#description' => t('Max size of 3Mb and filetype of jpg jpeg or png'),
'#upload_location' => 'public://bussimages/',
'#upload_validators' => array(
'file_validate_extensions' => array('png jpg jpeg'),
'file_validate_size' => array(3*1024*1024),
),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
$form['#validate'][] = 'newacc_freebusiness_validate';
return $form;
}
function newacc_freebusiness_validate($form, &$form_state) {
$bussimage = $form_state['values']['bussimage'];
$file = file_load($bussimage);
$bussimage = image_load($file -> uri);
image_save($bussimage);
$bussimage = image_load($file -> uri);
$edit = array(
'name' => 'name',
'mail' => 'mail#mail.com',
'status' => 0,
'language' => 'en',
'init' => 'mail#mail.com',
'roles' => array(8 => 'Promoter'),
'field_business_image' => array(
'und' => array(
0 => array(
'value' => $bussimage,
),
),
),
);
user_save(NULL, $edit);
}
This is throwing the error message:
Notice: Undefined index: fid in file_field_presave() (line 219 of /var/www/drupal_site/modules/file/file.field.inc).
I have tried so many tricks now and googled so long that I can't even explain what I have and haven't tried anymore!
Any help please.
OK - solved this. The clue was in the error message and the solution was very simple! Here is the code:
function newacc_freebusiness_form($form, &$form_state) {
$form['bussimage'] = array(
'#title' => t('Upload an image that shows off your business.'),
'#type' => 'managed_file',
'#description' => t('Max size of 3Mb and filetype of jpg jpeg or png'),
'#upload_location' => 'public://bussimages/',
'#upload_validators' => array(
'file_validate_extensions' => array('png jpg jpeg'),
'file_validate_size' => array(3*1024*1024),
),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
$form['#validate'][] = 'newacc_freebusiness_validate';
return $form;
}
function newacc_freebusiness_validate($form, &$form_state) {
$bussimage = $form_state['values']['bussimage'];
$file = file_load($bussimage);
$edit = array(
'name' => 'name',
'mail' => 'mail#mail.com',
'status' => 0,
'language' => 'en',
'init' => 'mail#mail.com',
'roles' => array(8 => 'Promoter'),
'field_business_image' => array(
'und' => array(
0 => array(
'fid' => $file -> fid,
),
),
),
);
user_save(NULL, $edit);
}
All Drupal was looking for was the file fid in the array. Don't know why I initially assumed it had to be more complicated than this.

Drupal Form APi Checkbox states

So I have create my checkbox in my form
$form['existing_customer'] = array(
'#type' => 'checkbox',
'#title' => t('Are you an existing customer'),
'#ajax' => array(
'callback' => 'checkbox_selected',
'wrapper' => 'subject',
),);
This calls my function and changes the values in my checkbox
The problem is I cannot get it to switch back if it is unchecked
function checkbox_selected(&$form, &$form_state) {
if ($form_state['values']['existing_customer'] == 1) {
$my_options = array( 'select' => t('Select'), 'mr' => t('Mr'), 'mrs' => t('Mrs'), 'miss' => t('Miss'), 'ms' =>t('Ms'), 'sir' =>t('Sir'), 'dr' => t('Dr'), 'prof' => t('Prof') );
}
elseif ($form_state['values']['existing_customer'] == 0){
$my_options = array( 'seconfZ' => t('jimmy'), 'mr' => t('Mr'), );
}
$form['subject'] = array(
'#type' => 'select',
'#title' => t('Subject'),
'#options' => $my_options//$form['subject_options']['#value']
);
return $form['subject'];
}
I thought I could do a switch on the checkbox value or state but no joy?
Oftentimes, when you use the Form API #ajax system, the wrapper that you specify is actually replaced with another element AFTER drupal_html_id() has been called again on the element wrapper. So I would check the markup of the "subject" element in Firebug/Web Inspector after your AJAX stuff happens--I'm betting that the wrapper div is now something like "subject--1".
To fix this, you need to manually set a wrapper div on the item you are replacing--one that won't change when the form is rebuilt. For example, in your form builder:
$form['existing_customer'] = array(
'#type' => 'checkbox',
'#title' => t('Are you an existing customer'),
'#ajax' => array(
'callback' => 'checkbox_selected',
'wrapper' => 'subject-wrapper',
),
);
$form['subject'] = array(
'#prefix' => '<div id="subject-wrapper">',
...
`#suffix' => '</div>',
);
Hope that helps!

Drupal: full cycle form with parameters and response page

I am trying to make a full cycle form with parameters and response page. Form is working OK, but response page is coming up black. Anyone have a suggestion or model example.
function module99_menu(){
$items = array();
// inital form
$items['module-99'] = array(
'title' => t('Export'), // Page title
'page callback' => 'fn_module99', // function to call when this page is called
'access arguments' => array('access content'), // An array of arguments to pass to the access callback function.
'description' => t('Export'),
'type' => MENU_CALLBACK,
);
// response page
$items['my_module-99-response/%/%'] = array(
'title' => t('Response Page'), // Page title
'page callback' => 'fn_module99_response', // function to call when this page is called
'page arguments' => array(0,1), // pass with arg(0) and arg(1)
'access arguments' => array('access content'),
'description' => t('Export - response form'),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
function fn_module99() {
return drupal_get_form('module99_my_form');
}
function module99_my_form_validate($form, &$form_state) {
// do some validation
}
function module99_my_form_submit($form, &$form_state) {
// do some stuff
drupal_set_message(t('The form has been submitted.'));
$parms = "p1=" . "A" . "&p2=" . "B" ;
$form_state['redirect'] = array('my_module-99-response', $parms);
}
function fn_module99_response($parm1,$parm2) {
$output = $parm1 . $parm2;
return $output;
}
function module99_my_form($form_state){
$form = array();
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('E-mail address') ,
'#size' => 64,
'#maxlength' => 64,
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
You should change the redirect a bit:
$form_state['redirect'] = array("my_module-99-response/$param_a/$param_b");
Also in your hook_menu you want to change the page arguments:
$items['my_module-99-response/%/%'] = array(
'page arguments' => array(1,2),
);
This will match the two % in your url, as 0 is 'my_module-99-response'.
I don't know if it will help, but the standard method is to use drupal_get_form on the hook menu with the form id of the form as a parameter. I'm not sure what you are trying to do with the arguments?
$items['my_module-99-response/'] = array(
'title' => t('Response Page'), // Page title
'page callback' => 'drupal_get_form',
'page arguments' => array('fn_module99_response'),
'access arguments' => array('access content'),
'description' => t('Export - response form'),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
You should also specify a submit handler in the form using the '#submit' property (make sure you pass an array). Do validation in the same way while you are at it.
function module99_my_form($form_state){
$form = array();
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('E-mail address') ,
'#size' => 64,
'#maxlength' => 64,
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
$form['#submit'] = array('module99_my_form_submit') ;
$form['#validate'] = array('module99_my_form_validate') ;
return $form;
}
$form_state['redirect'] = array("my_module-99-response/$param_a/$param_b");
this works great except drupal mangles the slashes with encoding

Resources