When it comes to customizing your website, people tend to search for the right piece of code in source file, and begin to do modification from there.
It’s one way to do it, only if you are not planning to do any updates with your framework. That’s because in most cases, you would be modifying the source code which has the chanced to be updated/modified in the next version. This rule applies to almost all framework that’s out there.
Some of the framework may offer a place for you to insert your own functions that will not go away because of updates. However, if you want to customize some core functions in regards to the framework you may run into some conflicts during the process. For instance, you only want to hide a field in a function, but you may end up with copying the entire core function to override with. In addition, sometimes you may not know which one runs first.
This is where Drupal Hooks comes in. A way to get the right part of the function you need, and you only need to do minimum modification. From as big as a full page to as small as a submit button. Technically, everything can be altered.
Create your custom module first, then begin the fun.
Let’s see an example here. For all the function to alter with, “form” would be the most common one. Either for validation purpose or information purpose.
function mymodule_form_alter(&$form, &$form_state, $form_id) {
// your code here
// you can get form_id by just do a var_dump($form_id)
// here we want to modify views_form_commerce_cart_form_
if (strpos($form_id, 'views_form_commerce_cart_form_') === 0) {
/* you may need to continue copy some parts of the code from
* the source code, depends on which part do you want to modify
* but this is the general idea about how to get to the place where
* you want to do your modifications
*/
// in my case, I want to have some special action while click on submit
$form['actions']['submit']['#submit'] = array_merge($form['#submit'], array('mymodule_commerce_cart_line_item_views_form_submit'));
}
}
From here, I have defined my own function “mymodule_commerce_cart_line_item_views_form_submit” that will be called when I click on submit on “views_form_commerce_cart_form_”. All I have to do is create this function and do whatever I want in it.
function mymodule_commerce_cart_line_item_views_form_submit($form, &$form_state) {
// your code here
}
Here are some Q/A:
1. How do I set the parameter to use for each alter function?
A: You can either find them in drupal module or drupal documentation.
2. Can I alter all the functions?
A: Not all function can be modified, but they should always have a hook that you can do your modification with. It may not be that easy to find the right hook, but it’s there you just need to find it!
3. Will there be conflicts?
A: For any alter function, the original function will always run first. Then your altered version will run after it. So there will not be any conflicts.
Since all those modification are inside your custom module, they can be switched off easily without hurting anything else. The most important, they can survive core updates! (Unless major versions change)
