I have a nodetype containing both a multi-valued filefield and a multi-valued emvideo field.
And I have a media-mover configuration that:
(a) harvests all the files in the multi-valued filefield
(b) uploads the files to youtube
(c) saves the url's to the newly-created youtube videos in the emvideo field.

My question is about deleting files. If the user deletes a file from the multi-valued filefield, then I also need to: (a) delete the corresponding emvideo field and (b) delete the youtube video itself.

I am currently planning to do this in hook_nodeapi. But I was wondering ... does media_mover have a better way to keep the emfield in synch with its corresponding filefield, so that when the filefield is deleted, the corresponding emfield is deleted as well?

Comments

arthurf’s picture

Media Mover does support this kind of deleting, however it sounds like you might need to throw in a bit more (and hopefully contribute it back!). I will say that the deletion handling is better in 6.2.x but that is probably not helpful.

There is some code in media_mover_api_node_update() (called on node update) that could be adapated for use by cck and/or emfield. It's not great code, but it would do the trick.

As per node delete, media_mover_api_node_files_delete($node) gets called on node delete. This will call back to the delete functions in hook_media_mover.

mm_emfield does have a function mm_emfield_node_file_delete() but this is not implemented correctly from the looks of it. It is only removing the data from the field rather than from youtube.

So what needs to happen is pretty much what you had planned- catch the file deletion with nodeapi by verifing that all the MM files that exist for this node still exist on this node- media_mover_api_node_update() is an example of how to do this. You could probably contact the maintainer of mm_youtube and ask them to add the delete function there, or just add it to emfield.

I'd be thrilled if media_mover_api_node_update() could be abstracted to handle drupal attached files and cck files.

As a side node, this is way easier on D7 because there are things like hook_file_delete()

kobnim’s picture

Hi Arthur,
You mention:

I will say that the deletion handling is better in 6.2.x

When do you expect to have an alpha release of 6.2.x? I'm wondering if it might be better to wait, and make my code changes to the 6.2 version.

kobnim’s picture

Hi again,
If I am understanding the code correctly, it looks like the function media_mover_api_node_update($node) invokes
media_mover_api_file_delete_call()
when a node attachment has been deleted, but not when a filefield has been deleted.

function media_mover_api_node_update($node) {
  // we only do this if there are media mover files that we need to delete
  // because they are linked to files on a node
  // we do not handle creation as other modules are responsible for that
  if ($node->media_mover) {
    // build a list of all the media mover files associated with this node
    foreach ($node->media_mover as $cid => $mm_files) {
      if ($mm_files) {
        foreach ($mm_files as $mm_file) {
          // load the configuration for this file
          $config = media_mover_api_get_configuration($mm_file['cid']);
          // check and see if this media mover file comes from a drupal file AND the
          // drupal file will be removed  AND the configuration removes files
            if ($node->files[$mm_file['fid']]->remove && $config->settings->mma_node_item_delete ) {
            media_mover_api_file_delete_call($mm_file);
          }
        }
      }
    }
    // now reset the nodes media mover cache
    cache_clear_all('media_mover_files_node_'. $node->nid, 'cache_media_mover');
  }
}

In particular, when a filefield has just been deleted:
(a) $node->media_mover is not set.
(b) $node->files[$mm_file['fid']]->remove is not set

I can see how to fix (a), by an explicit call to media_mover_api_node_files_fetch($node->nid)
But I am at a loss for how to address (b), i.e. how to tell in hook_nodeapi that a filefield has just been deleted. Any suggestions?

kobnim’s picture

Never mind. I think I figured out how to handle deleted filefields, using hook_file_references.

kobnim’s picture

StatusFileSize
new819 bytes
new3.68 KB

I modified media_mover_api.module and mm_emfield.module, so that when a filefield is deleted, the corresponding emfield is deleted as well.

The changes are in the attached patch files, and were created using "diff". Will that work for you?

Also, if you would like an explanation of the changes, just let me know.

kobnim’s picture

P.S. I submitted a feature request to media_youtube, to add a 'delete' clause to its media_mover hook.
See http://drupal.org/node/981058

arthurf’s picture

I'm shooting for a beta release by the first of the year. I'm not sure this is possible, but it seems enough people are filing bug reports that a somewhat usable release should be possible.

arthurf’s picture

@kobnim - as per your patch (yes diff is the way to do it, using cvs diff is even better because then it's tracked against the version you're using) - most of this looks fine. A questions about moving the functionality from $op = 'update' to $op = 'presave' I'm not totally sure if this is the right thing to do. My main concern when I chose to do it with 'update' was the case when a module might get called after media mover- in this case media mover would never know when to delete the file- well maybe on a secondary save. Perhaps I'm wrong here, but that was my thinking anyway.

Otherwise this looks good and I'll commit it. It'd be great to look at mm_node on the 6.2.x branch and implement this there as well.

kobnim’s picture

A questions about moving the functionality from $op = 'update' to $op = 'presave' I'm not totally sure if this is the right thing to do. My main concern when I chose to do it with 'update' was the case when a module might get called after media mover- in this case media mover would never know when to delete the file- well maybe on a secondary save. Perhaps I'm wrong here, but that was my thinking anyway.

The reason for switching from 'update' to 'presave' is that I was not able to modify the node from within $op = 'update'. After struggling with this for a while, I went back to the hook_nodeapi documentation and found this:

# "presave": The node passed validation and is about to be saved. Modules may use this to make changes to the node before it is saved to the database.
# "update": The node has just been updated in the database.

So it seems node_save() is called before $op = "update".

Before switching to $op = "presave", I tried making an explicit call to node_save() from within $op = "update". But that caused an error: "node was modified by another user and could not be saved"

Do you have any suggestions for an alternate approach?

zuriwest’s picture

Title: mm_emfield: deleting files » a bit off topic question

@kobnim,

Would you mind to share how you got Media Mover and Save to Youtube working with multi-value filefield and embedded video field?
Please describe how you configured filefield(s) and embedded video field(s) for your content type and how you setup the Media Mover Configuration.

Your help is much appreciated!

zuriwest’s picture

Title: a bit off topic question » mm_emfield: deleting files
kobnim’s picture

@zuriwest
I created a node type containing:
- multi-valued filefield
- multi-valued emvideo field

Then a created a media-mover configuration:
- harvest: harvest files from this filefield
- process: upload the harvested files to youtube
- complete: save result (link to youtube video) in an emfield

When cron runs, media-mover will harvest all of the as-yet-unharvested videos from this filefield, upload them to youtube, and then save the youtube links to the emfield.

kobnim’s picture

Hi Arthur,
Just checking back to see how you would like to handle the proposed patch. (See my comment #9 above.)
Thanks

kobnim’s picture

Version: 6.x-1.0-beta7 » 6.x-1.0-beta9

See here for a patch to media_mover_api_node_update() that does not require any changes to hook_nodeapi or mm_emfield.module: http://drupal.org/node/1478246

kobnim’s picture

StatusFileSize
new137 bytes

Here is a patch for what I believe is a different bug in mm_emfield. The function mm_emfield_node_file_delete() hard-codes the verb to 'complete', rather than looking up the verb in $configuration.

The patch replaces:
if ($url = $file['complete_file']) {
With:

$verb = $configuration['verb'];
if ($url = $file[$verb . '_file']) {
kobnim’s picture

Category: support » bug

Forgot to change category to "bug report" in previous comment.

arthurf’s picture

Version: 6.x-1.0-beta9 » 6.x-1.x-dev

Committed to dev branch