I am trying to use this with a field on a Profile2 type.

I can take the same field with the exact same settings (same field base) and place it on content type and it works no problem

Here is what happens.

  1. The profile2 edit form is shown
  2. Click "Add Items"
  3. Select items in the view.
  4. Close the window.
  5. All the content appears correct
  6. Save the Profile
  7. After Profile is saved no content appears selected.

When attached the same field the to a content type it everything is the same except that the values are saved. I have also tried with an Entityform Type and it works fine.

I also tried switching the field on Profile2 to checkboxes widget(not using this module at all). The values save correctly.
I then switched back to the widget provided by this module. At that point when I load the edit profile form it shows the correct content checked but no matter I do, uncheck all the selected content or add more when I save the profile it keeps whatever values where set before I switched back to this widget.

I have all this with 7.x-2.x-dev and 7.x-2.x-alpha2

Thanks for any help

Comments

bojanz’s picture

Profile2 embeds the fields one level below the main form.
This might be relevant, since the ids and form structures are different than expected.

tedbow’s picture

@bojanz thanks for the quick reply.

Yes I think that is the problem.

Don't know the solution but here is where the problem is

/**
 * Pushes input values to form state.
 */
function entityreference_view_widget_validate($element, &$form_state, $form) {
  array_pop($element['#parents']);
  $input = drupal_array_get_nested_value($form_state['input'], $element['#parents']);

$input is set to null because drupal_array_get_nested_value can't loop through the $element['#parents'] array to find the nested elements. After the first element in the parents array the 2nd isn't set(at least in my example).

tedbow’s picture

FYI I also tried the dev version of Profile2 and that didn't help.

tedbow’s picture

Status: Active » Needs review
StatusFileSize
new770 bytes

Ok I found the problem!

It is in entityreference_view_widget_field_widget_form.

  if (!empty($form['#parents'])) {
    $parents = $form['#parents'];
    $first_parent = array_shift($parents);
    $parents_path = $first_parent . '[' . implode('][', $parents) . '][' . $field['field_name'] . ']';
  }

If count($form['#parents']) == 1 then
implode('][', $parents)
will return an empty string
and parents_path will be something like this:
first_parent[][field_name]
Of course [] is a problem.

So this problem will not happen if $form['#parents'] is empty or if count($form['#parents']) > 1

Probably why it is only showing up with Profile2

So I am attaching patch that changes it to

if (!empty($form['#parents'])) {
    $parents = $form['#parents'];
    $first_parent = array_shift($parents);
    $parents_path = $first_parent;
    if (!empty($parents)) {
      $parents_path .= '[' . implode('][', $parents) . ']';
    }
    $parents_path .= '[' . $field['field_name'] . ']';
  }

This fixes it for me with Profile2 but also still works for nodes. Basically covers the $form['#parents'] only having 1 element.

tedbow’s picture

Title: Not working with Profile2 » Not working with Profile2 - forms elements with 1 parent

  • Commit e1781da on 7.x-2.x authored by tedbow, committed by jsacksick:
    Issue #2207761 by tedbow: Fix integration with Profile2.
    
jsacksick’s picture

Status: Needs review » Fixed

Thanks for your contribution tedbow! Committed (e1781da)

tedbow’s picture

jsacksick, no problem glad I could help out this awesome module!

Status: Fixed » Closed (fixed)

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