OG version: 7.x-2.0-beta1

This does not work because in entityreference_prepopulate.module (entityreference_prepopulate_og_field_default_value) line 71 there is a check for $instance['field_mode'] that fails.
There is no such item in the $instance.

If you remove this check, it seems to work.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

amitaibu’s picture

> OG version: 7.x-2.0-beta1

Please test with the --dev version of OG and ER-prepopulate

liquidcms’s picture

i have same issue; this was working so i suspect my updating prepopulate must have broken as i have not updated OG.

can't update OG as i am not sure how... going from Organic groups 7.x-2.0-alpha3 to beta2 or -dev breaks site.

liquidcms’s picture

yes, commenting out line mentioned above fixes this for me as well... will have to do until an upgrade path from alpha to beta rel.

liquidcms’s picture

was able to get og to latest -dev as well as prepopulate to latest dev.. this didn't fix.

also, now i get different widget for selecting and this widget does not have the group node listed (perhaps if it was there the prepopulate would have worked); there is also now an autocomplete field here as well; which when i type in it i get an ajax error.

liquidcms’s picture

ok, got it.

group didnt show in list since admin was not explicitly part of the group; i guess in earlier versions (alpha2) this didn't matter for admin. once i added admin as a group member; the prepopulate works (since the value i was trying to populate with was now valid).

still not sure what the autocomplete is for; but still throws ajax errors if i try to use it.

AndreyMaximov’s picture

Version: 7.x-1.1 » 7.x-1.x-dev
Status: Active » Needs review
FileSize
778 bytes

It's broken cause of unknown field instance['field_mode']

Quick fix

AndreyMaximov’s picture

telematika’s picture

Simply commenting the lines out is bad practice.

I would recommend the following code:

function entityreference_prepopulate_og_field_default_value($entity_type, $entity, $field, $instance, $langcode) {
  if (isset($instance['field_mode']) && empty($instance['field_mode'])) {
    return;
  }
  ...

That works for me and the checking is still done the way the original code expects it to be.

skriptble’s picture

I'm using a clean install with all the newest version of og, entity reference, and entity reference preopopulate. No matter what I do, I can't get it to select the field automatically. I'm not quite sure what's going on.

ezra-g’s picture

Status: Needs review » Needs work

#6 proposes committing commenting out code. The code should either be refactored or removed, but not commented out.

m.stenta’s picture

Status: Needs work » Needs review
FileSize
704 bytes

Attached is a patch made against the current 7.x-1.x branch via Git, using the approach described in #8.

amitaibu’s picture

I don't understand the patch from #11 -- what's the difference from current code?

m.stenta’s picture

I guess the real question is: what is $instance['field_mode'] and why does that if statement return the function if it's empty (and therefore also if it isn't set)?

The patch adds a bit more differentiation to the if statement, so that if $instance['field_mode'] is not set in the first place, it will still work. But maybe that bypasses the original intention of that $instance['field_mode'] check?

What is that for?

telematika’s picture

But maybe that bypasses the original intention of that $instance['field_mode'] check?

It doesn't. ;-)
The function empty($foo['bar']); produces an error if $foo['bar'] does not exist. The isset($foo['bar']); makes sure that it exists.

See isset and empty manual pages for details. The usage of empty is to be preferred but sometime you just need isset.

I guess the real question is: what is $instance['field_mode'] and why does that if statement return the function if it's empty (and therefore also if it isn't set)?

Yes, that is definitely the question. :)

amitaibu’s picture

Do you have the widget set to "OG reference"?

telematika’s picture

My widget type is set to "Autocomplete".

If I change it to "OG reference" I get the following errors:

Notice: Undefined index: og_widget in og_get_mocked_instance() (line 186 of sites/all/modules/og/includes/og.field.inc).
Notice: Undefined index: module in og_get_mocked_instance() (line 191 of sites/all/modules/og/includes/og.field.inc).
Notice: Undefined index: settings in og_get_mocked_instance() (line 192 of sites/all/modules/og/includes/og.field.inc).
Warning: Invalid argument supplied for foreach() in drupal_array_merge_deep_array() (line 2064 of includes/bootstrap.inc).
Notice: Undefined index: og_widget in og_get_mocked_instance() (line 186 of sites/all/modules/og/includes/og.field.inc).
Notice: Undefined index: module in og_get_mocked_instance() (line 191 of sites/all/modules/og/includes/og.field.inc).
Notice: Undefined index: settings in og_get_mocked_instance() (line 192 of sites/all/modules/og/includes/og.field.inc).
Warning: Invalid argument supplied for foreach() in drupal_array_merge_deep_array() (line 2064 of includes/bootstrap.inc).
amitaibu’s picture

Category: bug » support
Status: Needs review » Fixed

For OG audience to work you need to create your field with "OG reference" widget.
To get rid of those errors are probably, you probably need to re-save manually the field settings via the UI.

telematika’s picture

Thanks, that worked.

However if I'm a site administrator I see two widgets is that intended behaviour?

amitaibu’s picture

> However if I'm a site administrator I see two widgets is that intended behaviour?

Yes, since you select ER field, I can't know before you set the "Selection mode" to OG, so I can't filter out other widgets.

amitaibu’s picture

@telematika,
+ How about a doc page explaining to users about the widget + the error troubleshooting?

telematika’s picture

Well, where should I put it? ;-)

amitaibu’s picture

A patch to OG's readme maybe, or the OG's docs.

arosboro’s picture

@Amitabu, "OG reference" widget isn't available in the widget selection of ERP, it's set as the widget of the field, but I have select list as the widget for ERP. Some fields work and some don't, when I disable the field and don't hide it, it shows the correct group selected, but when I save the content is orphaned. Any ideas? This only breaks when action is set to hide or disable the field.

arosboro’s picture

Status: Fixed » Needs work

Entity reference does not prepopulate when using #access = false or #disabled = true. I've tested by commenting the code that sets #access to false and the reference is populated. Is there an alternative way to hide the field and still use a default value function?

JvE’s picture

Updating OG from beta4 to rc1 caused this issue to re-occur. See #1876696: Warning message in line 69 of og.field.inc using Entity reference prepopulate module.
This code :

if (empty($instance['field_mode'])) {
  return;
}

is always wrong. The expected return-type is array and this returns NULL.

Re-saving the field settings does not fix this error.

I suspect og_update_7203 has something to do with the problem.

Edit:
Approach for a patch:
validating referenceable entities has to move from entityreference_prepopulate_get_values_from_url() to entityreference_prepopulate_get_values() so that og_context derived values are also validated.
Then the whole entityreference_prepopulate_og_field_default_value() function can be removed and entityreference_prepopulate_field_default_value() can be simplified.

This would break API compatibility. (other modules calling entityreference_prepopulate functions)

JvE’s picture

Status: Needs work » Needs review
FileSize
1.45 KB

A rather more simple patch attached.
I still do not know what the field_mode does.

amitaibu’s picture

As stated in #1876696: Warning message in line 69 of og.field.inc using Entity reference prepopulate module I can't reproduce, with latest -dev. I'll need exact steps/ db dump to reproduce.

JvE’s picture

Yes, the problem persists with the latest dev version.

When I create content with a group audience the function entityreference_prepopulate_og_field_default_value() is called three times.
The first time $instance has no 'field_mode', the second time it is 'default' and the third time 'admin'.

When there is no 'field_mode' the function returns NULL which causes a problem in og_field_widget_form() [og/includes/og.field.inc:69] which tries to do a foreach on it.

The first time this is the stacktrace:

15: entityreference_prepopulate_og_field_default_value() (Array, 2 elements)
14: entityreference_prepopulate_field_default_value() (Array, 2 elements)
13: field_get_default_value() (Array, 2 elements)
12: field_default_form() (Array, 2 elements)
11: _field_invoke() (Array, 2 elements)
10: _field_invoke_default() (Array, 2 elements)
 9: field_attach_form() (Array, 2 elements)
 8: node_form() (Array, 2 elements)
 7: call_user_func_array() (Array, 1 element)
 6: drupal_retrieve_form() (Array, 2 elements)
 5: drupal_build_form() (Array, 2 elements)
 4: drupal_get_form() (Array, 2 elements)
 3: node_add() (Array, 2 elements)
 2: call_user_func_array() (Array, 1 element)
 1: menu_execute_active_handler() (Array, 2 elements)
 0: main() (Array, 2 elements) 

The second and third time it looks like this:

19: entityreference_prepopulate_og_field_default_value() (Array, 2 elements)
18: entityreference_prepopulate_field_default_value() (Array, 2 elements)
17: field_get_default_value() (Array, 2 elements)
16: field_default_form() (Array, 2 elements)
15: ctools_field_invoke_field() (Array, 2 elements)
14: og_field_widget_form() (Array, 2 elements)
13: field_multiple_value_form() (Array, 2 elements)
12: field_default_form() (Array, 2 elements)
11: _field_invoke() (Array, 2 elements)
10: _field_invoke_default() (Array, 2 elements)
 9: field_attach_form() (Array, 2 elements)
 8: node_form() (Array, 2 elements)
 7: call_user_func_array() (Array, 1 element)
 6: drupal_retrieve_form() (Array, 2 elements)
 5: drupal_build_form() (Array, 2 elements)
 4: drupal_get_form() (Array, 2 elements)
 3: node_add() (Array, 2 elements)
 2: call_user_func_array() (Array, 1 element)
 1: menu_execute_active_handler() (Array, 2 elements)
 0: main() (Array, 2 elements) 

The og config, content types, etc are all in a Feature I cannot share yet. I'll have time to try reproduction on a clean install after the weekend.

amitaibu’s picture

> Yes, the problem persists with the latest dev version.

You downloaded the dev version of OG? I've committed 2 days ago a fix for the notice on OG:
* e7bc86d - (HEAD, origin/7.x-2.x, 7.x-2.x) Prevent notice on node creation in og_field_widget_form(). (2 days ago)

JvE’s picture

No, OG is not at fault here and should not be changed to hide bugs in other modules like this one.

My apologies if my statements come across as blunt or offensive, my intentions are not to criticize you or your modules or to be a know-it-all. I just want software to be the best it can be.

amitaibu’s picture

> my intentions are not to criticize you or your modules or to be a know-it-all. I just want software to be the best it can be.

Ok, but the patch is wrong:

-  if (empty($instance['field_mode'])) {
-    return;
-  }

This needs to be kept, otherwise you hit the DB for no reason.

> No, OG is not at fault here and should not be changed to hide bugs in other modules like this one.

Again, not sure about which bug you talk in ER-prepopulate - there was a bug in OG that was fixed in -dev. If I'm missing the point, please provide ways to reproduce or db dump.

amitaibu’s picture

Ok, I read you comment in #1876696: Warning message in line 69 of og.field.inc using Entity reference prepopulate module, then it seems the fix should be

if (empty($instance['field_mode'])) {
   return array();
}
amitaibu’s picture

Status: Needs review » Fixed

I've committed the code from #32. If you still see a problem, please re-open. (EDIT: I've also reverted the wrong fix from OG).

Status: Fixed » Closed (fixed)

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

BrightBold’s picture

Status: Closed (fixed) » Active

I seem to be having the same problem as @arosboro, but only on one content type. This is on a site that was upgraded from 7.1, so each content type has its own group reference field (og_node1, og_node2, etc. instead of all using og_group_ref). Three of my group content types can be used consistently to create group content with the appropriate context. On the fourth (og_node2), the prepopulate only seems to work when I have it set not to hide the field.

I am using OG rc3 and ERP 7.x-1.2 (both released well after the patches above were committed) and have my field widget set to OG Reference. All settings are identical to the ones on the three working content types. I've tried every combination I can think of of editing and resaving the field and the field widget, but it only seems to work when I turn off the "hide field" ERP setting. I'm completely stumped. For now I'm going to leave it on "do nothing" and hide the field with CSS. Any ideas what's going on here?

BrightBold’s picture

Hmm. I just found this issue #1502972: Issues with ajax file upload fields and wonder if it may explain what's going on with my site, because the difference between the content type that doesn't work and the ones that do is that the non-working one has a file upload field. For anyone else experiencing this problem, take a look at that issue. I'll re-close this if I determine that's the problem.

mrfelton’s picture

I also find that this does not work if the hide field option is used.

inno81’s picture

I have exactly the same issue as #35 and #37.
Disabling "hide field" as a workaround...

donquixote’s picture

Funny explanation:
- Edit the widget type of the og_group_ref field, set it to "select list".
-> field mode is gone, prepopulate stops working
- Switch the "Entity selection" mode for the og_group_ref to "Simple", Save, then switch back to "Organic groups".
-> field mode is restored, prepopulate works again!

So it seems to be a matter of in which order you save the field configuration forms.

Angry Dan’s picture

I see no reason why I should have to use the OG reference widget with an OG audience field - OG doesn't require it and it won't have any effect on the field storage which widget you use.
Using latest release, if I change the widget then this module breaks.

My solution is different to the one committed above:

if (module_exists('og') && og_is_group_audience_field($field_name) && $instance['widget']['type'] == 'og_complex') {

Simply react only when you are using the og_complex widget.

Angry Dan’s picture

Status: Active » Needs review
FileSize
1.24 KB

Don't use that last patch!

Status: Needs review » Needs work

The last submitted patch, entityreference_prepopulate-no_field_mode-1825256-41.patch, failed testing.