Now that #1551228: AJAX-ify dependent fields when changing a project on issues is done, I noticed that we have a bug in how the 'Assigned' field works on issue nodes during a node edit.

We've got a custom selector plugin that knows how to get the allowed values for assigned based on a project. However, it only knows how to do that based on the project the issue is actually saved as (or that's in the URL on node/add/project-issue/[project-machinename]). When you change the project entity reference field and AJAX triggers a rebuild of the form, this custom selector plugin is still loading the issue entity and finding the allowed values based on that, not based on the project in the form values.

So, I think we need to modify this plugin (if possible!) to take the currently active form values instead of what's in the loaded entity.

Alternatively, we need the AJAX magic from #1551228 to somehow clobber the project field in the entity used in all these entity forms.

Or something. ;)

Comments

dww’s picture

Issue tags: +12hr

We don't really know how this is going to work. Since we have to guess, we're going to call it 1/2 day to figure out what to do, and a day to implement it. We hope. ;)

amateescu’s picture

Assigned: Unassigned » amateescu

I'll take a stab at this.

dww’s picture

Assigned: amateescu » Unassigned
Status: Active » Needs work
StatusFileSize
new1.48 KB

Note: based on a lot of looking around in entityreference and a few IRC chats, my current theory is to do something horrible like the attached patch. :/ I'm not sure what other options we have. However, in IRC amateescu (one of the entityreference maintainers) said he'd take a look, so hopefully he'll have a better approach.

dww’s picture

Argh, sorry for the x-post. ;)

amateescu’s picture

Assigned: Unassigned » amateescu

Yeah.. I really hope we can do something better than that :)

dww’s picture

Me, too... :)

Thanks!
-Derek

amateescu’s picture

Status: Needs work » Needs review
StatusFileSize
new4.59 KB

So.. since we cannot really do anything worthwhile in the custom handler, we just need pass it an updated entity which contains the new value for the project field.

While I was there, I couln't resist cleaning and speeding up the code a bit :/

amateescu’s picture

For the record, only this piece was actually needed. And it didn't take 12h :P

+++ b/project_issue.module
@@ -287,6 +288,7 @@ function project_issue_form_alter(&$form, &$form_state, $form_id) {
     'callback' => 'project_issue_form_change_project',
     'wrapper' => 'project-issue-ajax-form',
   );
+  $form['field_project'][LANGUAGE_NONE][0]['target_id']['#element_validate'][] = 'project_issue_field_project_validate';
 }
 
 /**
+ * Validation callback for the field_project field element.
+ */
+function project_issue_field_project_validate($element, &$form_state) {
+  // Always update the node entity saved in form_state in order to allow other
+  // fields to be populated with correct values (e.g. 'field_issue_assigned').
+  $form_state['node']->field_project[LANGUAGE_NONE][0]['target_id'] = $form_state['values']['field_project'][LANGUAGE_NONE][0]['target_id'];
dww’s picture

Status: Needs review » Fixed

Love it! Patch review is totally clean and the code now works great. I (and the folks I had talked to in IRC) didn't realize that entityreference used the node in $form_state like that. That's exactly the kind of inside info we needed. Thanks! Pushed to 7.x-2.x:

http://drupalcode.org/project/project_issue.git/commit/d3156b0

Thanks again,
-Derek

p.s. The hour estimates on all these upgrade issues were intentionally pessimistic in general, and as I wrote when I added this one, I wasn't sure how we were even going to fix this at the time I had to guess. :) Glad it was easy.

p.p.s. If you're interested in working on any other upgrade issues, we'd love your help. ;)

Automatically closed -- issue fixed for 2 weeks with no activity.