0

This is actually related to a previous question ( Appending markup to privatemsg message body ) but as it's a slightly different issue I figured I'd start a new question.

In my original question, tenken's suggestion is to create a new module implementing hook_field_info(), then use field_create_field() and field_create_instance() on hook_enable() to attach the custom field to the privatemsg bundle.

I have sort of figured out the gist of defining a custom field and attaching it to a bundle, but I'm stuck with a few things despite extensive googling, searching drupal.org and documentation as well as searching for video tutorials on YouTube. Specifically:

  • when populating arrays to use with field_create_field() and field_create_instance(), I'm not sure what I should specify for the entity_type value. I know typical examples for these functions usually refer to 'user' and 'node', but what should it be for privatemsg?
  • I don't want the field to be visible on any forms as it's only intended to be populated "behind the scenes". I understand that I need to define a custom field formatter for that. How do I do that?

I think those are my only obstacles at the moment. Sorry if I appear lazy at all, but I only have very limited experience with Drupal module development, much less the field API, and this is my first D7 module. Needless to say I have searched for specific answers to the above but they continue to elude me!

Thanks

1

There is no need to define your own field type, just use a simple text field. You are storing a string after all, no need to re-invent the wheel.

The entity_type of privatemsg is privatemg_message. Note that you are not required to specify this property as the purpose of it is to limit the field for certain entity types. Leaving it empty means that instances of it can be attached to any entity.

To hide the field, you could use a module like Field permissions which gives you fine-tuned permission-based control over all fields. What that module actually does is provide a user interface for hook_field_access(), so you can alternatively implement that hook yourself and simply return FALSE for the field you created based on the field name.

|improve this answer|||||
  • OK, that clears things up a bit, thank you. :) I tried using the Field permissions module before but despite disabling all write/edit permissions for the field, it still appeared on the Compose Message form. However it just occurred to me that the admin role might override it. I logged in as a registered user and sure enough the field is private, so that's that part sorted. I just have one more question (asking here as it's sort if in the same vain): how can I manipulate the field in the presave hook? Would it be automatically available as $message->field_name? – Riari Apr 14 '12 at 13:14
  • Have a look a drupal.org/node/1500308, that's quite a nice description on how to access fields. It's focused on field language, but private messages are always LANGUAGE_NONE. – Berdir Apr 14 '12 at 14:24
  • Awesome, thanks! It's working, but there are a few issues with this approach. I didn't realise it auto appends (attaches?) fields to $message_body, so I had to change the default tpl line "print $message_body" to "print render($message->content['body'])" and print the new field separately to avoid filter issues. Also, the Field permissions module is working, but if I remove create/edit perms for users, the field gets suppressed. As soon as I enabled those perms, the field started displaying on messages sent by affected users. Back to the drawing board with that then...! – Riari Apr 14 '12 at 16:11
  • OK, it seems to work well using the 'Field Hidden' module in place of the permissions one. One minor point that I completely overlooked: the field data is stored independently for each site, so the string was only being displayed in the message if it was sent via the site you're viewing it from. I moved the 'field_data' and 'field_revision' tables for the new field to my shared DB, updated my settings.php files, and all is well. – Riari Apr 14 '12 at 16:48

Your Answer

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

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