1

I have a file field which can have unlimited number of values. I am using File Entity module. As default, if users want to add more files, they must edit the node.

I'm looking for a solution to give the user the possibility to add another file directly from the node page where the files are displayed/listed. Just like the edit/delete functions from file entity, which the user can edit or delete an existing file directly from the node display using the contextual links hovering the file.

Is there any setting, API function or any hook that I can implement to provide this feature? Any advice will be very helpful.

UPDATE:

I don't want editable fields (Edit or Editable Fields as suggested as an answer. They may provide this functionality, but their main purpose is making fields editable, which is much more than I need and I already have the editing and deleting features through the File Entity module, and I only need this functionality for file fields.

I hope I was clear enough, otherwise I could expand my question and provide some screenshots.

0

You can able to achieve it using Edit module which supports D7 and file Entities...

Project page says

In-place editing for fields on any entity. Including WYSIWYG editing powered by CKEditor.

In-place editing through forms will work for any field. For textual fields, "direct" editing is used, powered by contentEditable.

Here is the sample screenshot which shows inline editing of Multiple File Field.. Here is how i can able to achieve it..

  1. Install like any other Drupal module.
  2. Download Underscore 1.5.x and Backbone 1.x from http://underscorejs.org/ and http://backbonejs.org/, respectively and put them in sites/all/libraries, in a "underscore" and a "backbone" subdirectory, respectively. Either the development or production version will work. Edit module will automatically use the production (minified) version if it's available, because it's more efficient.
  3. Grant the 'Access in-place editing' permission to relevant roles.
  4. Contextual links on nodes will now have a 'Quick edit' link. Clicking it will start in-place editing of that node!

enter image description here

Seems like editablefields module also provides same functionality..

  • I know of Edit module, but it is a bit overkill for our project, since we have already tons of modules for diverse functionalities. We don't need inline editing. What we only need is to be able to add a new file to a file field without editing the whole node. I'd rather do it with some custom code but I'm not sure where to start. – Елин Й. Apr 8 '14 at 5:47
  • What you are trying to achieve is made generic and provided to community already as a contributed module.. Even if you start writing custom code to achieve above you will end up with some what similar to contributed module and might be bugs in the module... So , I would suggest go ahead and use one of the module mentioned above.. I am afraid with out editing a node you cannot update field of a node in a Drupal way unless you do direct db change which is not suggested.. – Anil Sagar Apr 8 '14 at 5:54
  • I don't need the functionality of the above modules. I already told, I only need to be able to add a new item to an existing multi-valued file field. All other functionality like editing and deleting the field item without editing the whole node is already there (provided by File Entity module). So the above module don't bring anything new for me other than making all fields editable which I definitely don't need. – Елин Й. Apr 8 '14 at 6:47
  • I would use javascript to popup a window when clicking on a link and then pass on the variable to PHP and add a new field value. Don't have the time to provide an example. – user1973842 Apr 8 '14 at 7:07
0

This would require a bunch of custom code to accomplish this from scratch, but here is one route you may want to explore.

First, assuming that you are using the Field API's default file field display, override theme_file_link() in your themes template.php

function THEME_file_link($variables) {
  $file = $variables['file'];
  $icon_directory = $variables['icon_directory'];

  $url = file_create_url($file->uri);
  $icon = theme('file_icon', array('file' => $file, 'icon_directory' => $icon_directory));

  // Set options as per anchor format described at
  // http://microformats.org/wiki/file-format-examples
  $options = array(
      'attributes' => array(
          'type' => $file->filemime . '; length=' . $file->filesize,
      ),
  );

  // Use the description as the link text if available.
  if (empty($file->description)) {
    $link_text = $file->filename;
  }
  else {
    $link_text = $file->description;
    $options['attributes']['title'] = check_plain($file->filename);
  }

  return '<span class="file">' . $icon . ' ' . l($link_text, $url, $options) . '</span>';
}

Using Drupals Form API you could create a form that upload's and attaches a file to the current node without having to visit 'node/NID/edit'.

You may also want to consider adding some AJAX so that the page doesn't need to be constantly refreshed while managing files.

0

With field collections you automatically get edit, delete and add buttons on the node page.

0

I can't add an answer with examples sorry, don't have time. What you need to do is create a form which has an upload field. There are numerous examples for building forms in Drupal. Basically you're going to need 3 functions, one to build the form, one to validate it and one to submit it. Then you're going to to put this form into the page somehow, there are various ways of doing this also (I'd suggest putting the form into a block using a custom block or as a pseudo field using hook_field_extra_fields).

In your form builder function load the node object of the current node, maybe using menu_get_item(). Store the node id in a custom property on the form like #nid so that you can access it in the submit function. Build the form with your file upload field, add the #submit and #validate properties to add your custom validate and submit functions (google 'drupal fapi').

In your validate function, validate the file upload however you see fit. Then the submit function will run where you can load the node, add the file to it and save it. I'm not sure how to add the file to the node but it will involve some drupal api functions to save the file to the disc, which will return a file address, which you can add onto the multivalue field along with the other properties required for the field.

I don't think it's going to be quick but if this is what you need to do, this is how you would do it. If you have experience, this sort of thing might take an hour.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.