I am using an entity reference behavior plugin to add an extra column to the entity reference field schema, and am trying to figure out if there is a clean/cleanish way to get this working with this widget.

inline_entity_form_field_attach_submit seems to be where it builds the array parent element for saving:

foreach ($values['entities'] as $item) {
  if ($item['needs_save']) {
    $controller->save($item['entity'], $context);
  }
  list($entity_id) = entity_extract_ids($entity_type, $item['entity']);
  $entity_ids[] = array($values['settings']['column'] => $entity_id);
}
// Set the list of ids as the field value.
$parent_entity->{$field_name}[$langcode] = $entity_ids;

So it seems pretty determined to just use the single column. Any suggestions for how I might support multiple values here?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Andre-B’s picture

can you share the code you currently have? did you find anything further on this topic?

andrew.fulton’s picture

The workaround I have right now is fairly horrible, what I am doing is adding an extra setting to the inline entity form called 'extra_columns' which looks to the field columns and gets the ones that aren't target_id:

function inline_entity_form_settings($field, $instance) {
  ...
  elseif ($field['type'] == 'entityreference') {
     ...
     $settings['extra_columns'] = array_filter( array_keys($field['columns']), function($item){ return $item != 'target_id'; });
  }
  ...
}

Then in inline_entity_form_field_attach_submit, it looks back to the extra columns settings and pulls out the values accordingly:

foreach ($values['entities'] as $item) {
        if ($item['needs_save']) {
          $controller->save($item['entity'], $context);
        }
        list($entity_id) = entity_extract_ids($entity_type, $item['entity']);
     
        $field_array = array(
          $values['settings']['column'] => $entity_id
        );
        foreach ($values['settings']['extra_columns'] as $extra_column){
          $field_array[$extra_column] = $item[$extra_column];
        }
        $entity_ids[] = $field_array;
      }
      // Set the list of ids as the field value.
      $parent_entity->{$field_name}[$langcode] = $entity_ids;

Given that we can add extra columns to the entityreference field through behaviors/plugins, it seems like there should be a more generalized way to extend it through the ief? (I'm pretty sure the above isn't it, though)
Also I'm not really sure how common a case it is to be adding extra columns - I may be the only one.

lslinnet’s picture

Category: Support request » Bug report

I have been trying to add an extra database field through a behavior field on an entityreference field, basically trying to get it to store an view mode associated to the referenced entity.

I have run into the same problem as described above, where it throws away additional data on the referenced entities.

If need be I can work towards sharing my current progress with the behavior?

I have changed this to a bug, as it breaks the default functionality of entityreference field (allowing it to be extended with behaviors)

lslinnet’s picture

I have pushed an initial take on a module which uses an behavior to extend an entity reference field with an additional column.

The goal is to allow the author / editor a choice regarding how the referenced entity should be presented. We do this by extending the entity reference field with an behavior, which adds an additional column to the database where it stores the view mode information.
The behavior plugin system do not by default supply a way to extend the form element for the item (which perhaps would have been quite appropriate), so I am using the hook_field_widget_form_alter to modify the widget and add the select form element for the view mode.

I have been trying to do the same for the inline entity form widget but... here it gets quite a lot more tricky to archive the same result, I actually have to modify the module it self to get it somewhere near working.

I have attached what I have so far, but the act of editing an existing entity reference do not saves it's changed view_mode information correctly.

lslinnet’s picture

In order to test the patch from above I have used the following patched together code, it is no where near production ready and do not actually handle any rendering of the field with the additional column data.

Chris Matthews’s picture

Version: 7.x-1.3 » 7.x-1.x-dev
Status: Active » Needs work
Issue tags: +Needs reroll

The 4 year old patch in #4 to inline_entity_form.module does not apply to the latest inline_entity_form 7.x-1.x-dev and if still applicable needs a reroll.

Checking patch inline_entity_form.api.php...
Checking patch inline_entity_form.module...
Hunk #1 succeeded at 473 (offset -1 lines).
Hunk #2 succeeded at 525 (offset 4 lines).
Hunk #3 succeeded at 568 (offset 6 lines).
Hunk #4 succeeded at 1039 (offset 49 lines).
error: while searching for:
      // Go through the IEF data and assemble a list of ids.
      $entity_ids = array();
      $need_reset = false;
      foreach ($values['entities'] as $item) {
        if ($item['needs_save']) {
          $controller->save($item['entity'], $context);
          $need_reset = true;
        }

        list($entity_id) = entity_extract_ids($entity_type, $item['entity']);
        $entity_ids[] = array($values['settings']['column'] => $entity_id);
      }

      // Prevent the entity from showing up in subsequent add forms.

error: patch failed: inline_entity_form.module:1495
error: inline_entity_form.module: patch does not apply