The User Import project has an array named $supported. It also provides the following hook to alter this array:

// hook to add functions to this array.
// Will call all modules implementing hook_field_user_import_supported_alter
drupal_alter('field_user_import_supported', $supported);

Implementing this hook in an altogether different project, e.g.

function mymodule_field_user_import_supported_alter(&$supported) {
  $supported['mymodule'] = array(
    'save' => 'mymodule_user_import_callback',

works as one expects.

The User Import project allows users to add support for new fields by implementing the required code in a *.inc-plugin and placing it in a subdirecory named supported.

Rather than adding this to a contributed project I do not own (there is very little chance that my patch will ever be accepted), I want to make use the plugin feature provided by User Import.

I've created file a file named fieldname.inc and inside fieldname.inc , I've implemented hook_user_import_form_field_match(). My implementation of this hook follows the same pattern as the generic implementation of the same hook that comes with the project (it is in a file named field.inc). What it does is to add the field with the name "fieldname" to the he pulldown-menu constructed by the project that lets the user assign a field from the imported CSV to a specific field in the Drupal database, and calls to custom custom functions to validate and save fields of type filename. This works fine, and fieldname field type is now supported by User Import on my site.

However, User Import is unaware of this, and when I upload a CSV-file containing fields of type fieldname is says:

Field fieldname is not supported

I want to get rid of this bogus warning.

To make this warning go away I need to alter the array named $supported to add fieldname to the list of supported fields.

I can do this by hacking the User Support project (i.e. changing this array directly). However, this is not a clean solution. Drupal developers are not supposed to hack code they do not own. So I want to have code inside my plugin that alter the array $supported belonging to User Support.

I've tried to implement hook_field_user_import_supported_alter inside fieldname.inc file as follows:

function fieldname_field_user_import_supported_alter(&$supported) {
  $supported['fieldname'] = array(
    'save' => 'fieldname_user_import_callback',

This makes the warning disappear, but the field is now being added to the pulldown menu twice. The latter is an undesired side-effect of implementing this hook.

So I want to add this add this element to the end of the array named $supported, but without having the field being added twice to the pulldown menu.

So the question is: How can make sure that User Import knows that fieldname is supported without having fieldname added twice to the pulldown menu?

  • 1
    drupal_alter is just used to take some data and give it to other modules so they can alter it. It doesn't do anything else (i.e. it doesn't execute the thing that caused you to want to alter the data in the first place). Unless you're doing something with $supported after your drupal_alter call example, nothing will happen – Clive May 30 at 8:58
  • To add to what @Clive said, it's not clear what drupal_alter('mymodule_field_user_import_supported', $supported); is trying to achieve. The first argument is a hook name (without hook_), so that line is invoking the hook_mymodule_field_user_import_supported() hook. If no module is implementing that hook, no code is executed. – kiamlaluno May 30 at 9:49
  • @Clive Thanks! That convinced me that I was on the wrong track. I've rewritten the question to get rid of my false assumption and expanded on what I try to do. – Free Radical May 30 at 11:03
  • @kiamlaluno I've expanded the question to make it clearer what I try to achieve. – Free Radical May 30 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.