Original Post:
Not 100% sure this is a Entity Reference request; I apologize if not.
I am the administrator of a site and only I can make accounts. When I do that, I have a select field where I select which branch the employee works at.
I'd like to create a content type that references that field when the user creates that content. So, if they add "Event," there will be a field that automatically fills in "North Branch" (because that's what is in their profile).
Is this possible?
Update by bmarcotte
I've added a bunch of changes to get both the select and autocomplete widgets to work in the "Simple (with optional filter by Bundle)" mode. This is where you want to sort by either a property in the base table or a field attached to the entity.
The source code was using 'label's and ->getLabel calls. That does not work for much.
I also removed the entity_id from showing up the user's screen.
I removed a couple of check_plain calls because I need '&' characters to work i.e. Bar&Grill
| Comment | File | Size | Author |
|---|---|---|---|
| #9 | entityreference_module.patch | 4.72 KB | bmarcotte |
| #9 | EntityReference_SelectionHandler_Generic_class.patch | 3.09 KB | bmarcotte |
Comments
Comment #1
alexmc commentedI'm kind of worried that no one has been able to answer this relatively simple question. I fear that we are lacking some vital documentation. If I knew who to talk to I would write it!
I am guessing that to do this you need to write a custom module for it - but that is where my knowledge in this area ends...
Comment #2
mbouchard58 commentedI am working on the same issue. You don't need a custom module but you will have to write some custom code, glue code if you will. You can try using hook_form_alter implementing it as, say, your_mod_form_alter. In that function, you'll have to grab the logged in user's profile, get the Branch, then amend the drop down list to include only that Branch (i.e., delete every branch but the user's branch). Your select list now has just one choice. That's one approach. I am also investigating hook_query_entityreference_alter and possibly writing my own selection handler (see http://drupal.org/node/1568214)
I'll report back when I have something that works.
Maury
Comment #3
mbouchard58 commentedI was able to get this (i.e., getting the logged-in user's profile, finding an entity reference id, and then putting it in the proper field on node create) to work using a combination of hook_form_FORM_ID_alter, page manager, and a block display view. Where you are working with Branches (as in banks or libraries) I'm working with Organizations but otherwise it's the same application. I created a profile 2 type for Supervisors. Supervisors are assigned to a particular Organization (custom content type). When a Supervisor creates an Internship (custom content type), the Org ID from the profile has to find its way automatically to the Internship. I don't want the Supervisor to have to choose.
Using the Page Manager module (part of Chaos Tool Suite), I enabled the node/%node/edit page and then added a variant.
The variant is only used by Supervisor's on node type Internship and contains only the fields I want a supervisor to see.
I created a module called ims
I created a function called ims_form_internship_node_form_alter implementing hook_form_FORM_ID_alter which populates the default values of various fields in the internship form with data from the user's profile.(the form ID is internship_node_form. you can use module X-Ray or Devel to find formID)
I also created a block display view for profile type Supervisor which uses the logged in user as a contextual filter and a relationship to the Organization. I display a bunch of Organization fields in the view (which can only display at most a single row) and I use the view in a pane within page manager (Add Content|View). When a Supervisor adds an Internship, all the pertinent information from her Organization is shown, but she cannot change it.
HTH
Maury
Comment #4
lsolesen commentedWhy do you want to fill in a field automatically. The information is already there, when the user gets referenced to the node, and you should be able to get the information using views?
Comment #5
bmarcotte commentedI believe I'm having the problem referenced in the beginning (or similar too).
While creating a node, I have fields in a profile2 entity that I would like to access via an autocomplete (or at least a select). Neither autocomplete nor select works for this.
If I have fields in a first node_type and try to reference them in a second node_type it works fine. Doing the exact same thing trying to reference a profile2 field in a node_type does not work. The autocomplete or dropdown options get filled with the user readable version of the profile name; not the desired field.
My profile2 is called Entertainer and the field name is entertainer_name. My Node is called Event. In creating an event node_type, I want to reference 1 (or more) entertainer_names. Users having an entertainer role, have a profile_type called entertainer.
In the Event node_type, I have a field called event_performer for storing the entertainer's stage name (which is different from the user's username). The field settings specify:
When I do a node/add/event, the form loads, the Event performer select box is loaded with -None- and Entertainer, Entertainer, Entertainer... There is one Entertainer option for each user having an entertainer profile. So, rather than getting the field from the entertainer profile, I am only getting the label from the profile. Autocomplete likewise only returns Entertainer.
If the exact same field settings are used to reference another node (A Target Type of Node instead of a Target type of Profile) the settings work.
On maybe a related note, I would like to be able to use a token to default the reference to the current user's entertainer name.
Is anyone else trying to do this? Can a view do this?
Thanks,
Bob
Comment #6
bmarcotte commentedI stand corrected,
In the node case, you always get the title of the node being referenced; not the sort field from the "field attached of this entity"
So both nodes and profiles do not work with the above field settings.
I'm in the midst of routing this down... The problem appears to be in getReferencableEntities of EntityReference_SelectionHandler_Generic and its call to getLabel...
Comment #7
bmarcotte commentedI've added a bunch of changes to get both the select and autocomplete widgets to work in the "Simple (with optional filter by Bundle)" mode. This is where you want to sort by either a property in the base table or a field attached to the entity.
The source code was using 'label's and ->getLabel calls. That does not work for much.
I also removed the entity_id from showing up the user's screen.
I removed a couple of check_plain calls because I need '&' characters to work i.e. Bar&Grill
Comment #8
bmarcotte commentedI've added some changes to get both the select and autocomplete widgets to work in the "Simple (with optional filter by Bundle)" mode. This is where you want to sort by either a property in the base table or a field attached to the entity.
Comment #9
bmarcotte commented