Theming forms on alter with Drupal 7 - php

I'm having issues customizing a radio button for a specific form using hook_theme. Below is the piece of code I have on my module; see my comments elaborating the issue I encounter:
// Implementation of hook_form_alter().
function mymodule_form_alter(&$form, $form_state, $form_id){
// e.g form id: commerce_cart_add_to_cart_form_u6onPJSgS7pOgw0Tlo7zHy42LTQzbV913taANkYQKTo
if (strpos($form_id, 'commerce_cart_add_to_cart_form') !== FALSE) {
// Alter add to cart form
mymodule_commerce_cart_add_to_cart_form_alter($form, $form_state, $form_id);
}
}
function mymodule_commerce_cart_add_to_cart_form_alter(&$form, $form_state, $form_id) {
// Change the field type to radios.
$form['attributes']['field_artwork_ref']['#type'] = 'radios';
// Apply my custom theme for radios.
$form['attributes']['field_artwork_ref']['#theme'] = array('custom_radios');
}
// Implementation of hook_theme().
function mymodule_theme() {
return array(
'custom_radios' => array(
'variables' => array('element' => NULL),
),
);
}
function theme_custom_radios($variables) {
// Custom theme should go here.
// However, $variables are empty, print_r gives me "Array ( [element] => )."
// I am at least expecting to see my radio element here.
print_r($variables);
}

Themes for Drupal 7 form elements need to use the new render array key instead of variables in the theme definition:
function mymodule_theme() {
return array(
'custom_radios' => array(
'render element' => 'element',
),
);
}
Once you've made the change clear Drupal's caches and your code should work (I've just tested the above and it works fine).

Related

Drupal Form API : How to pass additional data back to the form after it is been submitted

I have created a form in Drupal using its API. The theme is overridden by a template. In the template, I want to show errors where the form exists, I do not want to show the error using drupal's form_set_error() because it shows the error in a fixed default area of the page.
This is my implementation of hook_form()
function newsletter_box($form, &$form_state)
{
$form = array();
$form["newsletter-email"] = array(
"#type" => "textfield",
"#maxlength" => 32,
"#title" => "Your Email"
);
$form['send-email'] = array(
"#type" => "submit",
"#value" => t("Join Our Newsletter!")
);
return $form;
}
And this is my implementation of hook_submit()
function newsletter_box_submit($form, &$form_state)
{
if( !filter_var($form_state['values']["newsletter-email"], FILTER_VALIDATE_EMAIL) )
{
$form_state['build_info']['args'] = ['form-error' => t("Email not formatted correctly.") ];
// NOTE: here is the place where the error is set, rather I use to set a new variable, but this variable is not available in my template. Why?
}
$form_state['rebuild'] = TRUE;
drupal_form_submit("newsletter_box", $form_state);
}
Suggest adding the variable by implementing a hook_preprocess, and add the error message into form rather than form_state.
Example:
// Set in newsletter_box_submit or newsletter_box_validate
$form['#form_error'] = t("Email not formatted correctly.");
/**
* Implements hook_preprocess_HOOK().
*/
function [module_name]_preprocess_newsletter_box(&$vars) {
$form = $vars['form'];
$error_message = $form['#form_error'];
// Use $error_message in template.
}
It feels like a hack though, strongly recommend theming the form the Drupal way.

content type not overriding in drupal7

I created a content type called custom content type..now I want to override the content type with the form id.I included bartik_theme in template.php and custom-content-type-node-form as my form id and I created custom-content-type-node-form.tpl.php in template in bartik..but am unable to override.I try to use dpm() function to print array structure that also not working.thank you in advance..
I included this in template.php
function bartik_theme() {
return array(
'custom_content_type_node_form' => array(
'arguments' => array('form' => NULL),
'template' => 'templates/custom-content-type-node-form',
'render element' => 'form'
),
);
}
I created custom-content-type-node-form.tpl.php in template folder
<?php
dpm($form);
hide($form['body']);
print drupal_render_children($form['field_custom_image']);
print drupal_render_children($form['title']);
print drupal_render_children($form);
?>
That's because your custom theme implementation is not in the theme hook suggestions array.
Use "template_preprocess_page" hook to add a suggestion to the the hook suggestions array :
function yourtheme_preprocess_page(&$vars) {
//your checks here ...
$vars['theme_hook_suggestions'][] = 'custom_content_type_node_form';
//...
}
However if you want to change form element I suggest to use "hook_form_alter" or "hook_form_FORM_ID_alter" hook.

Drupal 7 & PHP - display content if $form is any Content Type

I need help creating an if statement that will print out a cancel button only in forms for nodes. Without an if statement the cancel button prints out on all forms, including site search forms. I tried using a '$form_id !=' but adding every form ID where I don't want the cancel button to be doesn't seem very intuitive. Any help would be much appreciated.
<?php
/**
* Implements hook_form_alter().
*/
function cancel_button_form_alter(&$form, &$form_state, $form_id) {
// Here is where I'm having trouble. What variable can
// I put that targets ANY content type?
if ($form_id != 'search_block_form') {
// Add a cancel button.
$form['actions']['cancel'] = array(
'#type' => 'submit',
'#value' => t('Cancel'),
'#access' => TRUE,
'#weight' => 15,
'#submit' => array('cancel_button_form_cancel', 'node_form_submit_build_node'),
'#limit_validation_errors' => array(),
);
}
}
/**
* Custom cancel button callback.
*/
function cancel_button_form_cancel($form, &$form_state) {
$url = $_GET['destination'] ? $_GET['destination'] : '';
drupal_goto($url);
}
If you are in a node/content, the $form variable will have a node object. But if the form is not for/from node then it won't have the node object. You can check like this:
if(isset($form['#node'])) {
// Your code goes here
}
Actually, I have a bit confusion about $form['#node'] (:P). You can get it by debugging the $form or $form_state variable.

Magento : add custom button to an admin form

Building a custom adminhtml module. Im using a simple form. it looks like this :
<?php
class Namespace_Modulename_Block_Adminhtml_Modulename_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
$form = new Varien_Data_Form();
$this->setForm($form);
$fieldset = $form->addFieldset('modulename_form',array('legend'=>Mage::helper('modulename')->__('Module Data')));
$fieldset->addField('test', 'text', array(
'label' => Mage::helper('modulename')->__('Test'),
'name' => 'test',
));
// I want to add a custom button here. Say an action called "Add Option".
//Clicking this action adds input boxes or dropdowns to the form that are to
//to be included in the post when submitting the form (obviously).
I have been looking for a solution on Stack overflow and have not been able to find something that could help. I have then tried searching for something similar present in the Mage Core.
In the admin panel, if i go to [Catalog->Attributes->Manage Attributes] and click on a standard attribute like "Manufacturer" for example, on the second tab, called "Manage labels/Options" i see the following screen :
There is a button and action which allows me to add options (in the form of textboxes) to the form. Identifying this as something i am trying to replicate, i went into the core to try and figure out what to do. If i open the following files (Magento Enteprise 12.0.2) :
Mage_Adminhtml_Block_Catalog_Product_Attribute_Edit_Tab_Options
I see it is empty, but extends Mage_Eav_Block_Adminhtml_Attribute_Edit_Options_Abstract
and I have gone through this file, but little makes sense to me. Am I going down the wrong way? How can I achieve something similar, a button and action which adds fields to a admin form?
thanks
You can set custom template for your form, and add there any tags (like button) via html.
You can add renderer for you field
$customField = $fieldset->addField('test', 'text', array(
'label' => Mage::helper('modulename')->__('Test'),
'name' => 'test',
));
$customField->setRenderer($this->getLayout()->createBlock('yuormodule/adminhtml_yourform_edit_renderer_button'));
in Block Class
class Yournamespace_Yourmodule_Block_Adminhtml_Yourform_Edit_Renderer_Button extends Mage_Adminhtml_Block_Abstract implements Varien_Data_Form_Element_Renderer_Interface {
public function render(Varien_Data_Form_Element_Abstract $element) {
//You can write html for your button here
$html = '<button></button>';
return $html;
}
}
1- First you must create a container for your Grid like this
public function __construct() {
parent::__construct();
$this->setTemplate('markavip/dataflow/edit/form/mapping.phtml');
}
2- in the Same Container you will add your buttons like
public function _prepareLayout() {
$this->setChild('add_button', $this->getLayout()->createBlock('adminhtml/widget_button')
->setData(array(
'label' => Mage::helper('markavip_dataflow')->__('Add Option'),
'class' => 'add',
'id' => 'add_new_option_button'
)));
$this->setChild('delete_button', $this->getLayout()->createBlock('adminhtml/widget_button')
->setData(array(
'label' => Mage::helper('markavip_dataflow')->__('Delete'),
'class' => 'delete delete-option'
)));
return parent::_prepareLayout();
}
public function getAddNewButtonHtml() {
return $this->getChildHtml('add_button');
}
public function getDeleteButtonHtml() {
return $this->getChildHtml('delete_button');
}
3- and in the PHTML you will add thes buttons like this :
<?php echo $this->getAddNewButtonHtml() ?>
and after this you will play in JS functions for add and hide

Why am I getting a "skipping broken view error"?

This is Drupal 6.x and am having a nightmare of a time to modify a simple drupal form. This is a module file.
function modulename_menu() {
$items = array();
$items['school/registration'] = array(
'title' => 'Registration Form',
'page callback' =>'drupal_get_form',
'type' => MENU_CALLBACK
);
return $items;
}//end of the function
function modulename_school_form_alter(&$form, $form_state, $form_id)
{
// dsm($form_id);
if ($form_id == 'user_registration_form')
{
// modify the "#submit" form property by prepending another submit handler arra
$form['#submit'] = array_merge(
array('_modulename_registration_submit' => array()),
$form['#submit']
);
}
}
For what it's worth a few months afterwards - I just had the same problem. Check out
http://drupal.org/node/626834#comment-2393090
You probably have an install file with your custom module that doesn't contain all the necessary information.

Categories