Is on the cards? (IE: To replace the file field widget with an embedded file entity form similar to Field Collection)

Comments

dave reid’s picture

Priority: Major » Normal
Status: Active » Postponed

This is not really in the plan as of yet. If you want to work on it, feel free to assign and move out of postponed with a patch.

alan d.’s picture

For this project I moved on to Field Collection as we have a 3 week deadline and the project hasn't even been spec'd out yet! It would be an awesome addition to the module (not the biggest fan of the field collection workflow)

weri’s picture

Status: Needs review » Needs work

It would be great, when the fields of a file entity would added to the widget. The following code adds the fields to every widget with a file field.

/**
 * Implements hook_element_info_alter().
 *
 * Add an additional process and validation callback to control the added file entity fields.
 */
function file_entity_element_info_alter(&$type) {
  // Define an array of element names to proceed
  $file_element_names = array('managed_file', 'mfw_managed_file');

  foreach ($file_element_names as $name) {
    $type[$name]['#process'][] = 'file_entity_field_widget_process';
    $type[$name]['#element_validate'][] = 'file_entity_field_widget_validate';
  }
}

/**
 * An element #process callback for the file entity. Adds the additional file entity fields to the widget.
 */
function file_entity_field_widget_process($element, &$form_state, $form) {

  // Attach the file entity fields to saved files
  if ($element['fid']['#value']) {

    // Save the entity information because the field_attach_form() will change this
    $entity_type = $element['#entity_type'];
    $bundle = $element['#bundle'];

    $file = file_load($element['fid']['#value']);
    field_attach_form('file', $file, $element, $form_state);

    // Reset the entity information
    $element['#entity_type'] = $entity_type;
    $element['#bundle'] = $bundle;
  }

  return $element;
}

/**
 * An element #element_validate callback for the file entity. Saves the file entity field values.
 */
function file_entity_field_widget_validate($element, &$form_state, $complete_form) {
  // Only if the form is being submitted, save the field values
  if ($form_state['submitted'] && !form_get_errors() && $element['fid']['#value']) {

    $file = file_load($element['fid']['#value']);
    field_attach_submit('file', $file, $element, $form_state);
    file_entity_file_update($file);
  }
}

This works also with multiple fields. Values correctly submitted and saved. Maybe there are some side effects with other file widget add-ons. Thats the reason why I not submit a patch yet.

weri’s picture

Status: Postponed » Needs review
dave reid’s picture

Yeah the problem is that you also have to handle submit. It's not automatic just by calling field_attach_form() - that will only add the form elements and not handle any validation nor submission.

weri’s picture

Status: Needs work » Needs review
StatusFileSize
new1.94 KB

I implemented the functionality to add the entity fields to file- and image-field-widgets and save the submitted values. The implementation is added as a patch.

Status: Needs review » Needs work

The last submitted patch, file_entity-embedded_file_entity_forms-1422632-6.patch, failed testing.

Letharion’s picture

Status: Needs work » Needs review
StatusFileSize
new2.42 KB

Re-rolled a patch that hopefully applies, and also complies with coding standards for comments. Let's see what the bot says.

Letharion’s picture

Nice.

@weri, you still deserve the credit, so you may wanna do the re-roll yourself. :)

weri’s picture

Thank you Letharion for cleaning up the patch! It's not necessary that I re-roll the patch. :-)

Shadlington’s picture

This patch doesn't seem to have had any visible effect on my install - I assumed from the description that it would add fields that I have added to the file entity to the edit form of a node with a file field attached to it. This does not seem to be the case - the fields on the file entity continue to go unseen.

alan d.’s picture

From first glance, this will not handle images, but is that worthy of a separate issue? Or should a more universal approach be looked at?

burgs’s picture

At another first glance - this seems to only work with files uploaded after you implement the patch. Old uploads are still only editable in the files list section.

bojanz’s picture

Both Subform and Inline Entity Form attempt to solve this generically, and might be useful at least as a reference.

I used #1 and helped write #2, ping me on IRC if there's info / help I can provide.

mrfelton’s picture

Looks like Alan D has created a File Attributes module for this in his sandbox . Alan, can you provide some commentry on your module? Are you recommending that this be used? Or, is this functionality likely to get rolled into File Entity?

alan d.’s picture

Regarding, File Attributes status.

I'm sitting on the fence (in relation to publishing) while waiting for an act of god to free up some developer time to integrate File Entity as the best and only solution for sticking stuff onto files.

However, initial tests have show that the File Attributes sandbox can append these fields to Media widgets (after a module defines these as attachable), and I expose these fields as optional additions of the Media display, as insert module style tokens and the token system itself. So take what you want from that. I do not foresee myself using anything else in the next couple of months.

nitebreed’s picture

Patch from #8 works great, also for images. Can this be included in the next release?

quicksketch’s picture

I'm working on incorporating @weri's approach from #8 into a fully configurable module over in a sandbox at http://drupal.org/sandbox/quicksketch/1797528. It's not even usable yet, but I'm planning on fleshing it out over the next few weeks.

ParisLiakos’s picture

devin carlson’s picture

aaron’s picture

I am having difficulty determining where this patch applies to. I cannot find the form elements anywhere. I have looked under the default file widget, which is where I expect it to appear, but it does not actually appear there. Could someone please explain more what this patch does, or give a screenshot? Thanks.

gbonillo’s picture

I had to add this (field_attach_presave) to the code from post #3 to make it work with a taxonomy term reference (with an autocomplete widget) :

/**
 * An element #element_validate callback for the file entity. Saves the file entity field values.
 */
function file_entity_field_widget_validate($element, &$form_state, $complete_form) {
  // Only if the form is being submitted, save the field values
  if ($form_state['submitted'] && !form_get_errors() && $element['fid']['#value']) {

    $file = file_load($element['fid']['#value']);
	 
    field_attach_submit('file', $file, $element, $form_state);
    field_attach_presave('file', $file, $element, $form_state);
    file_entity_file_update($file);
  }
}
aaron’s picture

Status: Needs review » Needs work

The last submitted patch, file_entity-embedded_file_entity_forms-1422632-8.patch, failed testing.

devin carlson’s picture

klokie’s picture

I was able to use File Entity Inline for this purpose.

honza pobořil’s picture

Status: Needs work » Fixed

So how about to close this issue?

hefox’s picture

Status: Fixed » Needs work

This seems like someone best closed by maintainer, as some may still rather incorporate it into their project. (hm, I don't remember why I was subscribed to this issue)

shevgeny’s picture

#26 — the File Entity Inline module is dead, it works very poorly (no longer works, than works). Did anyone have any thoughts after 8 years? :)

shevgeny’s picture

#6 & #8 — partially solve the problem, but I see an error if I choose not the usual file upload field, but a media browser:

#entity_type in function file_entity_field_widget_process() (line 541 in file /sites/all/modules/file_entity/file_entity.field.inc).
Notice: Undefined index: #bundle in function file_entity_field_widget_process() (line 542 in file /sites/all/modules/file_entity/file_entity.field.inc).

joseph.olstad’s picture

see the issue queue for that module https://www.drupal.org/project/issues/file_entity_inline?categories=All
might help.

otherwise you could use a form alter and add the form or make a copy of this form and modify the copy.
see hook_form_alter