2

I am quite new to module development although I have experience in modifying existing 6.x block modules developed by others. I have a need to develop a 7.x module and in spite of a lot of documentation reading seem to be missing a basic starting point. I have been looking at the documentation on drupal.org and other third party sites but through my own fault seem to be missing a basic concept.

The site I am working on has a content type called project. At present there are many project pages busing this content type. One of the field types is a User Reference field, called key_contact.

The idea is that I need to create a block that gets the key_contact's username (stored as an id number in the key_contact field) and then from that number perform an SQL query, on another table, to get the id's details and then display them in a block.

At present I have a View that can return the id and I have been asked to replace this view with a module that does the entire job.

Once I get the id for the key_contact I will have no trouble with the SQL query and formatting the output how I want it. I am familiar with creating a block module.

Where I am stuck is understanding how to get the key_contact value from the content type project page when it is loaded.

I would appreciate it if I could either be shown documentation or an example, or even an explanation, that will show me how to extract a field value from a content type like this.

  • Is there a reason to use a module rather than a view if the view is working? – Jeremy French Jul 25 '11 at 9:23
2

You will have to use Views for Drupal 7. First time you will have to do it manually.
Then you can convert to module using Features module. Features will give you an auto generated module that will product the same view block on the behalf of Views.

0

You should use Views for this. Add an argument for the user reference field, and then add the fields you want to display from your user profile. Then you can add a block display to the View and assign it to a region on the block configuration page.

  • I made a quick test for this. Arguments on Views 3 are called contextual filters. You need to add a contextual filter using the user reference field and set the 'When the filter value is NOT available' option to 'Hide view'. To have all the fields from the user available you will need to first add a Relationship using the user reference fields and then you can add your fields. Hope this helps. – ipwa Jul 25 '11 at 7:29
0

I agree with the others who say that Views is a good solution for this. If you insist on not using views, you can get the current node (assuming that you're viewing a node page) using menu_get_object(). The information you're looking for should be available in the loaded node object.

  • Thanks everyone for your answers suggesting using Views. As far as I have been able to tell Views will not give me the complete result I need. Using Views I have no trouble getting the Id for the key_contact and displaying the list of Id's in a list in a module. That is only half of the job though. What I need to do is take the Id returned from the View and to look inside another table, called profiles, and extract the Id's real name, phone number, email address and a URL that points to an image of them. – Richard Luckhurst Jul 26 '11 at 0:22
  • As far as I can see I can not direct the output of the View, which is just the Id, to some code, which runs the SQL query, to get the other stuff I need from the profiles table. As far as I can see the Views module returns just a formatted list in the block. If there is some way of taking the returned list of Id's and then pushing that into some code to get the extra data I need from the profiles table and then formatting it into the block I would very much like to know about it. In short I do not want to see the list of Id's, I want to see a list of the data returned from profiles – Richard Luckhurst Jul 26 '11 at 0:28
  • I don't know where the data in your profiles table comes from, but if it's regular Drupal content (eg. nodes), Views should be able to access it if you add the appropriate relationships to your view. If your profiles table is not native to Drupal, you could 1) write a custom module, exposing your data to Views, 2) migrate that data to Drupal fields attached to the user object, or 3) write a custom solution (which you were already looking for). – marcvangend Jul 26 '11 at 7:25
  • I explain how to make a relationship to the profile fields on my answer below. – ipwa Jul 27 '11 at 5:24

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.