A site sells printed issues of a magazine. When all the issues are sold they want to keep all the information about the magazine issue but want to remove everything related to selling (not just "Add to cart" button but the price info too).

The current solution to that is an inline entity form multiple value product reference field. But because we need to reference only one product entity this solution feels a little bit hacky and would be better to use a single value inline entity reference which makes possible to remove its value when it is not needed any more.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Anonymous’s picture

I went ahead with single value inline entity reference and added extra code via hook_form_alter() to remove single value inline entity reference when their fields are empty... it works for my client's site now but I do not feel that it is optimal and wonder if there is a better way of doing this or there are plans for single value inline entity reference to cater for these use case?

What I have done so far:

1. Content-type created with optional single value inline entity reference field.
2. Added extra form submit handlers with hook_form_alter():

function MYMODULE_form_MY_FORM_ID_form_alter(&$form, &$form_state, $form_id) {
  array_unshift($form['actions']['submit']['#submit'], 'MYMODULE_form_submit');
  $form['actions']['submit']['#submit'][] = 'MYMODULE_form_submit_delete_reference';
}

3. I added 2 submit handlers as the first one calls before anything else and deletes the referenced entity, and the second one actually removes the reference to the entity.

function MYMODULE_form_submit(&$form, &$form_state) {
$node_wrapper = entity_metadata_wrapper('node', $form['#node']);
$condition = SOME_CONDITION_TO_DETECT_IF_THE_FIELD_IS_BLANK;
  if ($condition) {
    // Remove any product reference when editing a node.
    if ($form['#node']->nid) {
      $ief_id = $form['field_product_ref']['und']['#ief_id'];
      $product_id = $node_wrapper->field_product_ref->product_id->value();
      $form_state['inline_entity_form'][$ief_id]['delete'][] = $product_id;
      foreach (element_children($form['#node']->field_product_ref['und']) as $delta) {
        unset($form_state['inline_entity_form'][$ief_id]['entities'][$delta]);
      }
      // Also mark the reference for deletion.
      $form_state['temporary']['MYMODULE']['remove_product_reference'] = TRUE;
    }
    // Prevent reference from being created when this is for a new node.
    else {
      unset($form['field_product_ref']);
      unset($form_state['values']['field_product_ref']);
    }
  }
}

function MYMODULE_form_submit_delete_reference(&$form, &$form_state) {
  if ($form_state['temporary']['MYMODULE']['remove_product_reference']) {
    $node = node_load($form['#node']->nid);
    $node->field_product_ref = array();
    node_save($node);
  }
}

I would think that the second submit hook is less than optimal, as it is potentially writing to the database a second time?

DamienMcKenna’s picture

Category: Feature request » Bug report

I'd say this is a bug, or at least the field settings should indicate that it won't be editable.

bojanz’s picture

Category: Bug report » Support request
Status: Active » Fixed

The single widget only works for required fields, which means that there must always be a value.

Optional fields can use the normal, multiple widget.

DamienMcKenna’s picture

Component: Code » User interface
Category: Support request » Feature request
Status: Fixed » Active

@bojanz: In that case it's a UI problem as this is not made clear.

bojanz’s picture

What's your suggestion? Adding a warning above widget settings? The widget label doesn't leave a lot of room to communicate intent.

DamienMcKenna’s picture

Something like this? The widget label change might honestly be enough, but this also adds descriptions to the widget form.

Chris Matthews’s picture

Status: Needs review » Reviewed & tested by the community

The 4 year old patch in #6 to inline_entity_form.module applied cleanly to the latest inline_entity_form 7.x-1.x-dev and is a very reasonable fix for this issue so changing the status to RTBC.

  • ram4nd committed 0c42ac1 on 7.x-1.x
    Issue #2348915 by DamienMcKenna, Chris Matthews: Make possible to remove...
ram4nd’s picture

Status: Reviewed & tested by the community » Fixed
dww’s picture

Status: Fixed » Patch (to be ported)

Thanks for this improvement!

Is there a D9+ version of this? Seems weird to be adding new features for D7 that don’t have parity in D9+. It’d become a regression for folks that rely on this in D7 when they try to upgrade.