3

When "themeing" modules, especially core modules, as far as I know it is a good idea to take the module specific tpl.php files, duplicate the ones you need, and modify them once they're in the relevant theme directory.

However, what if I want to modify a .inc file? Is it the same process as with tpl.php files, or is there a better workflow? Basically what I would like to do is change some of the user status messages found in user.admin.inc in the core Drupal 7 User module. I'm not sure if editing this file directly is a good idea. Thoughts?

0

3 Answers 3

4

You can't override an .inc file by copying it to your theme, but you can override most of the function with your template.php; for example, any form can be altered using hook_form_alter(), or any function whose name starts with "theme" can be override by using YOURTHEME_functionaname() in your theme template.php.

12
  • This is exactly what I was asking, thanks Serjas. Although based on what you said it now seems easier to just patch the core module as I'm not very familiar with php functions. Commented Aug 13, 2012 at 10:35
  • which message you want to edit in user.admin.inc? give me one example , and i ll show you how to do it without changing core
    – Serjas
    Commented Aug 13, 2012 at 10:41
  • Hi Serjas, apologies for taking so long. I was trying to find the exact line where the message is set. The message is basically: "The name $ is not allowed", $ being the username. It appears when entering an invalid username at the Create new account page, and I wanted to change it to something like: "Spaces and one letter names are not allowed". But I can't find that line any more. When I do a 'file and contents" search in explorer for "is not allowed" I get user.admin.inc as a result, but when I do a ctrl/cmd+F find in the actual file it says it can't find "is not allowed"! How strange. Commented Aug 13, 2012 at 13:57
  • its in lin 834 (user.admin.inc) .. anyway as far i know, drupal_set_message cant be override, so best use this stringoverrides module. it wont affect much (or for last resort you can edit core module since its just texts, but dont do this for any other cases, always look for overrides)
    – Serjas
    Commented Aug 14, 2012 at 5:25
  • 1
    So edit the title then, @kiam.
    – Shog9
    Commented Aug 18, 2012 at 15:58
4

Take a look at String Overrides module, which allows to overwrite everything passed through t(). So, it works for “the strings in the module that's already installed”.

There are other ways to alter data in Drupal, but I’m sure in your case overwriting needed.

String Overrides just uses core drupal overwrite behavior, but adds nice UI to control it. Let me point you again to core comments for string overwriting:

First, check for an array of customized strings. If present, use the array instead of database lookups. This is a high performance way to provide a handful of string replacements. See settings.php for examples. Cache the $custom_strings variable to improve performance.

6
  • Yeah this module was mentioned by Drupality above, and I agree it would be the easiest solution without patching the core module. I had read in a few separate articles though that using a large number of overrides with this module can slow things down to a degree. Since each time the string is being used/viewed etc the override has to be triggered looked up and then the string has to be overriden, and if there are several such overrides on a single page my guess is that it is slower than only reading the strings from the module and printing them. Your thoughts on this? Commented Aug 13, 2012 at 10:44
  • Updated my answer. Maybe it will be faster just to create HTML site without PHP overhead.
    – kalabro
    Commented Aug 13, 2012 at 11:25
  • Consider that the String Overrides module replaces any occurrence of the string, whatever page contains it. If it is required to change the string in a specific page, then a different approach should be adopted.
    – avpaderno
    Commented Aug 13, 2012 at 12:00
  • Thank you for all the helpful advice guys, much appreciated. I suppose for kalabro's answer, the real question is: Which provides better performance - String Overrides or a patched User core module? By patched I mean going into the module files and rewriting the relevant strings myself to what I want them to be. Commented Aug 13, 2012 at 14:06
  • @DrupalDesigner You don't hack core, if you are not able to maintain your hacked version as a new fork. Remember that Drupal 7 comes with a suite of tests, which could not work anymore with your hacked version. If you have issues with a third-party module because your changes to Drupal code, you are alone. Nobody is probably giving you support to find how to resolve the issue.
    – avpaderno
    Commented Aug 16, 2012 at 6:45
0

In a comment you said you wanted to replace the string for the error message "The name [username] is not allowed." That error message is returned only from Drupal 6 core, or from a Drupal 7 module; the function that in Drupal 6 shown that error has been removed from Drupal 7, together the functionality of allowing/not allowing some usernames. Drupal 7 can disallow some IPs from being used when accessing a Drupal site.

In Drupal 6, the function showing that error message is user_admin_access_check_submit(), which contains the following code.

  switch ($form_state['values']['type']) {
    case 'user':
      if (drupal_is_denied('user', $form_state['values']['test'])) {
        drupal_set_message(t('The username %name is not allowed.', array('%name' => $form_state['values']['test'])));
      }
      else {
        drupal_set_message(t('The username %name is allowed.', array('%name' => $form_state['values']['test'])));
      }
      break;
      // …
 }

The same message is shown at least from the User restrictions module, which is the third-party module that implements the functionality that has been removed from Drupal 6. I am one of the maintainers of that module, even though I didn't touch its code from months.

To change that message I would implement hook_form_alter() to add a new validation handler that alters the error message, if the error message is shown during the validation of a form, or to add a new form submission handler, if the error message is shown during the submission of a form.

In both the cases:

  • The messages set with drupal_set_message() are saved in $_SESSION['messages'][$type], where $type is status, error, or warning. Keep in mind you don't see the string 'The username %name is not allowed.' but the string where %name is already replaced with the username. You would need a regular expression to find that message, and replace it with the string you prefer.

  • The error messages set with form_set_error() are returned from $errors = &drupal_static('form_set_error, array());. The array follows the format$error[$field_name] = $error_message`. Also in this case, the string you are looking for contains the username, and you need to user a regular expression to find the error message. You need to alter this array if the error message is set in a form validation handler.

Keep in mind that:

  • The error message, in Drupal 6, and the User restrictions module, doesn't mean the username contains spaces when it should not. It means there is a rule that doesn't allow a user to use a specific username; for example, there could be a rule that disallow usernames matching fun%boy (it's a regular expression used from MySQL, and other database engines), and the error message would appear when the username is fun boy, fun-boy, fund boy, or funxxxxxxxboy.

  • In Drupal 7, spaces in usernames are allowed; they are not allowed at the beginning of the username, at the end of the username, or when there are 2 or more spaces in row.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.