Hi,

I want to override a function in core 'user' module. Can someone please give me the clean steps how to achieve this??

Thanks for your help in advance.

Regards
Deepti

Comments

deeptitiwari’s picture

Can someone please help, m stuck :( !!!!

bojanz’s picture

1) What function do you want to override
And more importantly:
2) What are you trying to achieve?

deeptitiwari’s picture

There is a function 'user_login(&$form)' in user.module(core). I want to override this function to redirect the user to another legacy application which returns the 'userID'. In the reverse process, this 'userID' is used to query another server that gets the username and Email ID for the user and stores the user info into the drupal database.

Regards
Deepti

tamanna-freelancer’s picture

You can write form_alter hook

function your_module_name_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'user-login-form') {
$form['#redirect']=Your-path;

}
}

vbose’s picture

Hi Tamanna,

Thanks for giving the way to override the module in Drupal.

As I am new in drupal, so would like to know where I can create this function in our custom module folder or per define module folder.

One more thing I tried it in my custom module file and give the redirect path name but it doesn't connect or redirect in the same page.

Thanks

bojanz’s picture

You can use hook_form_alter() to alter the login form and add your validation handler (which queries your app, gets the userID, writes it into the DB..).

See these Drupal 6 migration resources for more info: 1 and 2

deeptitiwari’s picture

Thanks. This solves my problem.

Regards
Deepti

deeptitiwari’s picture

While I did this, a new problem came up.

I am actually running multisites on one drupal instance. The user module which we were discussing above includes a 'constants.php' file. This file defines the constants for each drupal site.I want the user module to pick up the contants.php file according to the site that is running.

Is this possible and how??

I was trying the following in user.module:

include(drupal_get_path('theme', 'themename'). '/constants.php')

but again 'themename' should be according to the drupal site running. How can that be achieved??

Please help.

Thanks
Deepti

deeptitiwari’s picture

Hey, I am waiting for a reply ..

Please help..

Regards
Deepti

qcode’s picture

Due to some reasons I need to override md5 encryption and use encryption of mysql instead for login,register and forgot password sections. Can anybody help me with best suggestions?

punkrack’s picture

Hi!

Related to this topic, I need to alter the user_admin_account() function in user.admin.inc.

I hacked core temporarely for my needs, but I would like to find a way to do it without hacking core.

Putting aside what needs to be done for the moment, what I really want to know is how can I overwrite this function by using my function and / or in a general way, how to overwrite any core function with my own.

Thanks!

jaypan’s picture

You can't override functions - functions can only be created once. But depending on what you are trying to do, there are other ways to override functionality. What are you trying to do?

Contact me to contract me for D7 -> D10/11 migrations.

punkrack’s picture

In this particular function, I needed to change $header and change $query: add a column to the users table and get a field created by fields. Finally, sort that colunm also... Later on, we add to $options :

$options[$account->uid] = array(
...
'full_name' => $account->field_nom_value . ", " . $account->field_pr_nom_value,
..

I found a way to do what I want without hacking core but it feels awfully "wrong".

-create customfunctions_admin_account() in customfunctions.module (a module I created)
-copy in it, the function user_admin_account() and do my alterations.
-customfunctions_menu_alter() to take my function instead of the core one...

Works well, only thing though is that other modules (such has role_delgation) alter user_admin_account(), which is a form btw.

Other modules might has well so I had to copy and rename in my case from role_delegation module the function : role_delegation_form_user_admin_account_alter() to customfunctions_form_customfunctions_admin_account_alter().

Not to mention had to copy the validate and submit functions from user_admin_account() -> customfunctions_admin_account_validate and customfunctions_admin_account_submit().

After done all this, works fine but it would seem that hacking core would be simpler if other modules might alter this core function...

Any suggestions ?

jaypan’s picture

This one is tricky, insofar as they don't offer a lot of options for alteration throughout that function. The only spot I can see at which you can alter the table of output itself is if you implement hook_page_alter(). This hook allows you to alter the render array of the entire page, right before it's rendered. So you will make your changes to the $page['content'] element. You can add to the header here, and you can insert your own columns, (though it may be a little tricky).

The big problem is your wanting to be able to sort on the column that you wish to add to the table. As far as I can tell, if you really need to have this column sortable, you will need to do something along the lines of what you have already done (overriding the whole function, rather than altering the parts you want to change). The reason for this is double:
1) The query in the function doesn't add a tag, which means you don't have the ability to alter that query in hook_query_alter().
2) The $header is used for the tablesort, so you would need to change $header before the query is called, which means, overriding the whole query.

I think you've already figured out #2, which is why you've done it the way you did.

I doubt it helps, but maybe you could add a filter to the users by implementing hook_user_operations(). But I don't think you want a filter, you want a sorting method, so that probably won't do anything for you.

So if you need the sorting functionality, you are going in the right direction. If you are willing to give up sorting functionality, but still show the column in the table, use hook_page_alter().

Contact me to contract me for D7 -> D10/11 migrations.

punkrack’s picture

Yeah basically like I said you either hack core or do it in a module like I did...

But anyways, I will tell my client that if they really want the sort, it has consequences and it's best just to put all this information without the sort. It's a good compromise IMO.

Thanks for your input btw!

chintukarthi’s picture

Hi, 

I am using Drupal 8.

In drupal form I am displaying some error messages. The changes i need is, to display the error message according to my customer needs. Whenever a required field is missed, drupal lists the error messages as,

2 error has been found:(Error message 1 here) (Error message 2 here).

But what my customer need is, 

Errors:

Error message 1

Error message 2

But the problem is, i need to make changes in the core module,

PATH : (core/modules/inline_form_errors/src/FormErrorHandler.php),  Where the "1 error has been found" and "@count error has been found" is mentioned. (@count represents the number of errors that was caught).

I need to edit and remove the "Error has been found " text from the core module. So without making changes in core, is there any other way to implement this ?

Thanks.

amit.drupal’s picture

You can't override core modeul functions -
you can overright only theme function or implement hook function in any core module.
When overriding theme functions, e.g., in template.php for PHPTemplate based themes, there is the option to use PHPTEMPLATE_function() or THEME_NAME_function() to name the function. Functionally, both produce the same result.

chanderbhushan’s picture

You ca overrite the core module function through this hook hooK_function_alter

for exmple hook_form_altre(), hook_node_view_alter(&$build) and role_delegation_form_user_admin_account_alter() etc ..

ZR_basetis’s picture

Hi!

I have an issue related to this topic, I hope someone can help!

I need to have the aggregated js and css in a certain path and the functions drupal_build_css_cache() and drupal_build_js_cache() put them into the public:// filesystem. As the path is hardcoded I don't know how to change it. 

An option could be to modifie the core functions with a drupal_alter() function. I'd put it right after in the drupal_build_css_cache() the variable $path is defined and it would be something like drupal_alter('function_name', $path). And then when I want to change the path from an external module I could use:

my_module_name_function_name_alter(&$path) {

$path = 'new_path';

}

But I don't think changing drupal core is a good practice. Is there another way to do it?

Here is the drupal_build_css_cache() function: API page: https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_...

Thanks in advance, 

jaypan’s picture

You shouldn't ever need to do anything with this. What exactly are you trying to do?

Contact me to contract me for D7 -> D10/11 migrations.

ZR_basetis’s picture

I want to store them in Amazon's S3 so I can access them from different servers.

 

jaypan’s picture

The generated CSS is dynamic, and will differ depending on the user and the page, and potentially other factors. The aggregated CSS would not be usable on other sites.

Contact me to contract me for D7 -> D10/11 migrations.