File widget custom upload location I am working on a custom module (based on Model project) with a file field attached to my custom entities, now I would like to save files in certain directories according to entity id eg:

public://files/my_custom_entity_ID/filename


I've tried with hook_file_insert but can't get the entity id in order to use it later in file_move.

Which hook (if any) should I use? Or I am missing the whole approach?
Assuming D8

If this is part of a "BaseFieldDefinition" of your own entity, you may set a lot of options like so:

// create photo
$fields['photo'] = BaseFieldDefinition::create('image')
    -&gt;setLabel("Photo")
    -&gt;setDescription('Ein Photo')
    -&gt;setSettings(array(
        "file_directory" =&gt; 'profilbilder/[date:custom:Y]-[date:custom:m]',
        "file_extensions" =&gt; 'png gif jpg jpeg',
        "max_filesize" =&gt; '600 KB',
        "max_resolution" =&gt; '600x800',
        "min_resolution" =&gt; '30x40',
        "alt_field" =&gt; false,
        "alt_field_required" =&gt; false,
        "title_field" =&gt; false,
        "title_field_required" =&gt; false,
    ))
    -&gt;setDisplayOptions('form', array(
        'type' =&gt; 'image_image',
        'weight' =&gt; 5,
    ))
    -&gt;setDisplayOptions('view', array(
        'type' =&gt; 'image',
        'weight' =&gt; 1,
        'label' =&gt; 'hidden',
        'settings' =&gt; array(
            'image_style' =&gt; 'thumbnail',
        ),
    ));


As you see, you can use tags in the file_directory setting.
If you craft your fields using the Drupal 8 field UI, you can also use tags in the UI field.

If it's D7, you should change to D8 :-)