Im programming my own module in Drupal 6 and create a form, but I need something to select data from a list. I am trying to use multiselect module. I can show the field with options, but on submit I cant get the selected options.
I made the following:
$element = array(
'#type' => 'field_multi_select',
'#title' => 'Field name',
'#description' => 'description',
'#multiple' => true,
'required' => true,
'#field_name' => 'field_select',
'#columns' => array('value'),
'#value' => array(array('value' => null)),
'#process' => array('multiselect_select_process'),
'#enable'=>true,
'module' => 'module name' // set here you current module name
);
$form['#field_info']['field_multi_select'] = $element;
$element = multiselect_select_process($element, NULL, $form_state, $form);
Related
I have checked lot and tried many things, but I am still not getting the product multiselect selected values label on product view page.
I have product attribute called package which is multiselect,
Code that create the product attribute
$this->addAttribute(
'catalog_product',
'package',
array(
'group' => 'Package',
'backend' => 'eav/entity_attribute_backend_array',
'frontend' => '',
'class' => '',
'default' => '',
'label' => 'Package',
'input' => 'multiselect',
'type' => 'text',
'source' => 'npm_recurrex/package_source',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'is_visible' => 1,
'required' => 0,
'searchable' => 0,
'filterable' => 0,
'unique' => 0,
'comparable' => 0,
'visible_on_front' => 0,
'user_defined' => 1,
)
);
this works fine, I am successfully saving the product.
But in frontend product view page when I say
Mage::log(print_r($_product->getData('package'), true));
Its prints the result as 1,2
But I wanted to display option labels of multiselect not option id's.
So I tried with this code
Mage::log(print_r($_product->getAttributeText('package'), true));
It prints nothing, just blank space :(.
I have checked this link but no use.
I am confused with this, Where I am wrong? and what is the wrong thing?
Can anybody explain me what is happening in my case?
If you need to show Drop Down or Multiple select value of an attribute product you should do something like this:
$attributes = $_product->getAttributes();
$customAttributeValue = $attributes['custom_attribute']->getFrontend()->getValue($_product);
Mage::log($customAttributeValue);
I just want the standard Excerpt box - not a metabox of my own creation, added to a Custom Post. The box shows up in Posts but not in Custom Posts. I've tried both of these older solutions but neither of them worked (maybe it's a WP 3.9 problem):
The custom post type name is "Scoop"
I added this to the register_post_type_scoop() $labels = array
'supports' => array('title','thumbnail','excerpt')
but it didn't work - neither did this:
add_post_type_support('Scoop', 'title');
add_post_type_support('Scoop', array('title', 'thumbnail', 'excerpt') );
Add a index value excerpt to the supports object. Below the example is:
add_action( 'init', 'create_testimonial_posttype' );
function create_testimonial_posttype(){
register_post_type( 'testimonials',
array(
'labels' => array(
'name' => __( 'Testimonials' ),
'singular_name' => __( 'Testimonial' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'clients'),
'supports' => array('title','thumbnail','editor','page-attributes','excerpt'),
)
);
}
Being a newbie and altering the site-wide form and fields using hook_form_Form_ID_alter() was a big accomplishment but now I'm beginning to run into walls involving validation of some of the custom fields I've added. Following is the code from my custom_form.module file.
<?php
/**
* Implementation of hook_form_Form_ID_alter().
* #see http://api.drupal.org/api/function/hook_form_Form_ID_alter
* Targets only request catalog form and adds a few custom fields to the default Drupal contact form and removes some fields.
*/
function states_list() {
return array(
'- Select -',
'AL' => 'Alabama',
//rest of states....,
);
}
function request_catalog_form_local_contact_page_alter(&$form, $form_state) {
$form['parts_catalog'] = array(
'#title' => t('Retail Parts Catalog'),
'#type' => 'checkbox',
);
$form['sprayer_catalog'] = array(
'#title' => t('Sprayer Parts Catalog'),
'#type' => 'checkbox',
);
$form['address'] = array(
'#title' => t('Address'),
'#type' => 'textfield',
'#required' => true,
);
$form['city'] = array(
'#title' => t('City'),
'#type' => 'textfield',
'#size' => 30,
'#required' => true,
);
$form['state'] = array(
'#title' => t('State'),
'#type' => 'select',
'#options' => states_list(),
'#required' => true,
);
$form['country'] = array(
'#title' => t('Country'),
'#type' => 'select',
'#options' => array(
'US' => t('United States'),
'Canada' => t('Canada'),
),
'#required' => true,
);
$form['zip'] = array(
'#title' => t('Zip'),
'#type' => 'textfield',
'#size' => 6,
'#maxlength' => 6,
'#required' => true,
);
$form['phone'] = array(
'#title' => t('Phone (xxx-xxx-xxxx)'),
'#type' => 'textfield',
'#size' => 12,
'#maxlength' => 12,
'#required' => true,
);
//Removes the textfield Message and the dropdown Subject choice.
$form['message']['#access']=false;
$form['subject']['#access']=false;
//Remove the Send Email submit button text by setting it to nothing and changing it.
$form['submit']['#title'] = '';
$form['submit']['#value'] = 'Submit Your Request';
// reorder the elements in the form to include the elements being inserted
$order = array('parts_catalog', 'sprayer_catalog', 'name', 'address', 'city', 'state', 'country', 'zip', 'phone', 'mail', 'copy', 'submit');
foreach($order as $key => $field) {
$form[$field]['#weight'] = $key;
}
}
// The custom email message sent using the data gathered in the form
function request_catalog_mail_alter(&$message) {
if ($message['id'] == 'contact_page_mail') {
$message['body'][1] = $message['params']['name'].' '.'has requested the following catalogs:';
$message['body'][2] = 'Parts Catalog: ' . $message['params']['parts_catalog']. ' ' . 'Sprayer Parts Catalog: ' .$message['params']['sprayer_catalog'];
$message['body'][4] = 'Send to: ' . $message['params']['address'].', '. $message['params']['city'].', ' . $message['params']['state'].' ' . $message['params']['zip']. ' ' . $message['params']['country'];
}
}
At this point I am most interested in how to show " - Select - " in the US States drop-down field BUT not let the user choose that as a choice. At this point they can leave -Select- in that required field and Drupal allows the form to submit. The data sent via email for that particular field = 0. I want to force the user to actually select a state.
I have tried all of the following methods.
http://www.chromaticsites.com/blog/drupal-tutorial-form-overrides-and-element-specific-validations/
Drupal form validation not working for me
http://befused.com/drupal/additional-validation-function
I have tried multiple code trial-an-error variations of these and nothing has worked as advertized...
Your questions for clarification or your suggestions are greatly appreciated. Remember to be specific and simple in your explanations and define terms please - presently for me most of php is like reading shakespeare.
Thanks so much.
I slept on this problem and then retried one of the links I had tried before. Drupal form validation not working for me
I was able to make it work to my satisfaction: I used this within
function request_catalog_form_local_contact_page_alter(&$form, $form_state) right after setting the $order value.
// reorder the elements in the form to include the elements being inserted
$order = array('parts_catalog', 'sprayer_catalog', 'name', 'address', 'city', 'state', 'country', 'zip', 'phone', 'mail', 'copy', 'submit');
foreach($order as $key => $field) {
$form[$field]['#weight'] = $key;
}
$form['#validate'][] = 'request_catalog_local_contact_page_validate';
function request_catalog_local_contact_page_validate(&$form, &$form_state) {
if ($form_state['values']['state'] == '0') {
form_set_error('state', t('Please Select a State'));
};
if ($form_state['values']['parts_catalog'] == '0' and $form_state['values']['sprayer_catalog'] =='0') {
form_set_error('parts_catalog' and 'sprayer_catalog', t('Please Select a Catalog'));
};
}
// The custom email message sent using the data gathered in the form
function request_catalog_mail_alter(&$message) {
if ($message['id'] == 'contact_page_mail') {
$message['body'][1] = $message['params']['name'].' '.'has requested the following catalogs:';
$message['body'][2] = 'Parts Catalog: ' . $message['params']['parts_catalog']. ' ' . 'Sprayer Parts Catalog: ' .$message['params']['sprayer_catalog'];
$message['body'][4] = 'Send to: ' . $message['params']['address'].', '. $message['params']['city'].', ' . $message['params']['state'].' ' . $message['params']['zip']. ' ' . $message['params']['country'];
}
}
}
Works for what I need now.
There is another way you can avoid this.
Just assign NULL value as ket to the first option.
Ex:
function states_list() {
return array(
'' =>'- Select -',
'AL' => 'Alabama',
//rest of states....,
);
}
This will do.
I developing magneto module, In this module i want to make a ajax request in admin panel.I don't understand were to add script and controllers.please help me.
My requirement:
When change the select box(On change) i want add some field in the form.
Mycode:
/app/code/local/<Namespace>/<Module>/Block/Adminhtml/<Module>/Edit/Tab/Form.php
$fieldset->addField('type', 'select', array(
'label' => Mage::helper('<Module>')->__('Article Type'),
'name' => 'status',
'values' => array(
array(
'value' => '',
'label' => Mage::helper('<Module>')->__('Choose'),
),
array(
'value' => 1,
'label' => Mage::helper('<Module>')->__('Normal'),
),
array(
'value' => 2,
'label' => Mage::helper('<Module>')->__('video'),
),
),
'required' => true
));
I am creating fields using this.
How can i add another field on change.
Where should i add script
Where should i add controller
I created this module using this instructions
Let this field always be on the form. But make it invisible and show when you need it.
In the same template where the form is.
Place controller in your module. On stackoverflow you can find many questions about creating your own magento module and controller.
I've added a custom order attribute and updated the onepage checkout page.
Now I'm trying to add this attribute to the new order form in the admin.
I'm trying to extend Mage_Adminhtml_Block_Sales_Order_Create_Form_Account and add a new field in the _prepareForm() method similar to the way the Group and Email fields are added.
How do I get the order attribute?
I've tried several ways but nothing works.
This is how I'm creating the custom order attribute in the mysql-install file:
$attribute = array(
'type' => 'int',
'label' => 'myattr',
'visible' => false,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
);
$installer->addAttribute('order', 'myattr', $attribute);
and this is how I'm trying to get the attribute:
$res = Mage::getSingleton('core/resource');
$eav = Mage::getModel('eav/config');
$attr = $eav->getAttribute('sales/order', 'myattr');
or with this:
$entityType = Mage::getModel('eav/config')->getEntityType('order');
$entityTypeId = $entityType->getEntityTypeId();
$attribute = Mage::getResourceModel('eav/entity_attribute_collection')
->setCodeFilter('myattr')
->setEntityTypeFilter($entityTypeId)
->getFirstItem();
or this:
$order = Mage::getResourceSingleton('sales/order');
$myAttr = $order->getAttribute('myattr');
None of them work.
Have you verified that the attribute is being added to eav_attribute table in the database with the correct entity_type_id? (I think sales_order is 11 by default, but don't assume that)
At first glance, it looks like you should be using
$installer->addAttribute('sales/order', 'myattr', $attribute);
HTH,
JD
Sales/Order used to use the EAV Model which supports attributes, that was before 1.4.0 or so not sure.
I think now you should do:
$installer->getConnection()->addColumn($installer->getTable('sales/order'), 'my_column', 'decimal(12,4)');
you can still add the attribute as static field
$installer->addAttribute('order', 'my_column', array('type'=>'static'));
What I noticed after much trial-and-error was that the new attribute must have a default (non-null) value in order to work. Somehow the attribute is not writable if it has a 'NULL' value in the database. So using this attribute options array worked for me:
$attribute = array(
'type' => 'int',
'label' => 'myattr',
'default' => 0,
'visible' => false,
'required' => false,
'user_defined' => true,
'searchable' => false,
'filterable' => false,
'comparable' => false );