1

I create filenames using some ID and timestamps. I don't want to add additional table to "remember" associations, as filename keeps them pretty well:

$filename = get_symbol_by_id($id) . ' ' . get_modified_by_id($id) . '.xml';

Also, I don't care if they will be managed or not. And I don't care if they get deleted - I can recreate them on demand. They are small, so leaving older ones behind does not create any cost worth taking care of. What is the proper course of action?

"Unmanaged" files seems to be generally discouraged in 7, but using them is pretty straightforward with tools like:

With managed files, I'm afraid I would be forced to use

So what's the proper course of action, if I want to avoid both deprecated functionality and overly complicated code?

1 Answer 1

5

I'd definitely go with EntityFieldQuery, not a really complicated solution:

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'file')
      ->propertyCondition('filename', '<filename>%', 'LIKE');
$result = $query->execute();
if (!empty($result['file'])) {
  $files = entity_load('file', array_keys($result['file']));
}

Use of conditions is not really recommended on any load multiple function anymore, the method above would be more standard and "future proof" as EntityQuery is the way to go in Drupal 8.

Installing file entity module would add much more value on what you can do regarding managed files.

1
  • 1
    Small improvement would be to use db_like() to prepare the string, but yeah EFQ is the way forward
    – Clive
    Commented Jun 5, 2014 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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