Drupal preprocess a commerce function - drupal

I need to preprocess a function from the module "commerce pricing attributes".
Here is the function :
function commerce_pricing_attributes_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {...}
I don't know how to preprocess this (if it's possible).
This function create some element in the back-office and the thing I want to do is to give a color to these elements in function of the type of the option the element is. If it's an insurance option there is a color, if it's a room option another color.
I try to do this with an alter like this :
function my_module_field_widget_commerce_pricing_attributes_custom_widget_form_alter(&$element, &$form_state, $context) {...}
But I can't have all the information I need (the type of the option).
Is there any way to preprocess the function so I can use all the values they use in their module ?

I think you need to use this hook : hook_field_widget_form_alter
It allow you to override (or add) widget applied to a field
function my_module_field_widget_form_alter(&$element, &$form_state, $context) {
if ($context['field']['type'] == 'mytype') { // you can use another condition on field name or whatever
// Loop through the element children (there will always be at least one).
foreach (element_children($element) as $key => $child) {
// Add the new process function to the element
$element[$key]['#process'][] = 'my_custom_callback_field_widget_process';
}
}
}
function my_custom_callback_field_widget_process($element, &$form_state, $form){
// do your stuff
return $element;
}
NB : dump variables to target exactly you want if you don't know structre of them

Related

Is there a way to override user_admin_account?

I need to override the user_admin_account function (which list users in backoffice).
Is there a way to do that without modify the core? I don't see any hook to do that.
That function is a form so you can implement hook_form_alter() in a custom module (or even a theme as it's an alter hook).
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'user_admin_account') {
$form['something']['#default_value'] = 'something else';
}
}

Drupal hook_form_alter for Taxonomy admin

I created a module to do all my form altering called "form_mods". It's working for most situations but not for the Taxonomy page.
I'm targeting the form id of "taxonomy_overview_vocabularies". I'm trying to hide the link "edit vocabulary" for roles of "webmaster" and "dj".
My code is unsetting the $form array correctly, but Drupal is still displaying the "edit vocabulary" link.
function form_mods_form_alter($form, $form_state, $form_id) {
if($form_id == 'taxonomy_overview_vocabularies'){
global $user;
$hide=0;
$hideArray = array('webmaster', 'dj');
foreach($user->roles AS $key => $value){
if(in_array($value, $hideArray)){
$hide++;
}
}
if($hide){
foreach($form AS $vocab){
//print_r($vocab);
if(isset($vocab['edit']['#value'])){
unset($vocab['edit']['#value']);
}
}
}
}
}
Very small PHP mistake,
when you want to change array members in a for each statement you have to pass them by reference & foreach($form AS &$vocab) otherwise the $vocab would be just a copy of the array
foreach($form AS &$vocab){
//print_r($vocab);
if(isset($vocab['edit']['#value'])){
unset($vocab['edit']['#value']);
}
}
In addition to Amjad's answer, if you don't like using references, I would suggest another alternative:
foreach ($form as $key => $vocab) {
unset($form[$key]['edit']['#value']);
}
This way you avoid using references, and potential issues they may lead to.
Also note I removed the if statement, which is not useful (PHP can figure it out).
An array_map could also be considered.

Double output within Drupal function

I'm going crazy
function module_form_alter(&$form, $form_state, $form_id) {
// Nothing here
$var = 'bla-bla';
print_r($var);
// Nothing here
}
I see on the screen bla-blabla-bla
WHY?
You probably have two forms on the page. Try printing (better yet, install devel and use dpm) $form_id instead of $var and see which forms are involved.
hook_form_alter works on every form. You probably have the search form in this page, so it prints the text twice (one for every form).
In order to add changes to only one form, use the $form_id argument like this:
function module_form_alter(&$form, $form_state, $form_id) {
if($form_id == 'YOURFORMID') {
$var = 'bla-bla';
print_r($var);
}
}
change YOURFORMID to your form_id.
You can find the form_id by looking on the HTML of the form output and search the value of the input that his name is 'form_id'.

How can I get the title of a form element in Drupal?

For example, in the registration form, there is "Username" and the text field for it which has the input type="text" name="name" ....
I need to know how can I get the title from the input field's name.
I'm expecting a function like:
$title = get_title_for_element('name');
Result:
assert($title == 'Username'); // is true
Is there something like this in Drupal?
Thanks.
You have the form and the form state variables available to your validation function. You should use form_set_error() to set the error.
There is no function that I am aware of which will map from the values array to the form array. But it is not dificult to work it out. Understanding the form data structure is one of the key skills you need when building drupal.
In this case the form in question is generated (in a roundabout way) by user_edit_form, you can see the data structure in there.
$form['account']['name'] is the username field. and the array key for the title is '#title' as it will be in most cases for form elements.
You can do it in two different ways as I see it. Let's create a module called mycustomvalidation.module (remember to create the mycustomvalidation.info file also).
Note: The code below has not been tested, so you might have to do some minor adjustments. This is Drupal 6.x code by the way.
1) Using hook_user()
What you need is a custom module containing your own implementation of hook_user() http://api.drupal.org/api/function/hook_user/6.
<?php
function mycustomvalidation_user($op, &$edit, &$account, $category = NULL) {
if ($op == 'validate') {
// Checking for an empty 'profile_fullname' field here, but you should adjust it to your needs.
if ($edit['profile_fullname'] != '') {
form_set_error('profile_fullname', t("Field 'Fullname' must not be empty."));
}
}
}
?>
2) Using form_alter() and a custom validation function
Personally, I would go for this option because I find it cleaner and more "correct". We're adding a custom validation function to our profile field here.
<?php
function mycustomvalidation_form_alter(&$form, $form_state, $form_id) {
// Check if we are loading 'user_register' or 'user_edit' forms.
if ($form_id == 'user_register' || $form_id == 'user_edit') {
// Add a custom validation function to the element.
$form['User information']['profile_fullname']['#element_validate'] = array('mycustomvalidation_profile_fullname_validate');
}
}
function mycustomvalidation_profile_fullname_validate($field) {
// Checking for an empty 'profile_fullname' field here, but you should adjust it to your needs.
if ($field['#value'] != '') {
form_set_error('profile_fullname', t("Field %title must not be empty.", array('%title' => $field['#title']));
}
}
?>

theme form on drupal 6

I have a form 'pub_form' which has function $form['#theme'] = 'publication_order';
so in module file, i defined function theme_publication_order($element), but the function
was not called.
i have search the google, looks like i have to use hook_theme() to make theme_publication_order() to work.
how can i override hook_theme() to make it work?
To make a theme function work, you must first define it in an implementation of hook_theme. You need this, to let drupal know the function exists. This would look like this:
function mymodule_theme() {
$items = array();
$items['publication_order'] = array(
'arguments' => array('element' => NULL),
);
return $items;
}
Then drupal will know about your theme function:
function theme_publication_order($element) {
// do your stuff here
}

Resources