Define default path [type_field]/[content_type]/[node:nid] in the settings field

Public

You can use also imagefield_tokens, filefield_paths.

</> CopyGet raw version
php
  1. /**
  2.  * Implements hook_form_FORM_ID_alter().
  3.  */
  4.  
  5. function YOUR_MODULE_form_field_ui_field_edit_form_alter (&$form, &$form_state, $form_id) {
  6.  
  7. $type_field = $form['#field']['module'];
  8. // Check if type field is image or file.
  9. if (in_array($type_field, array('image', 'file'))) {
  10. $default_directory = $form['instance']['settings']['file_directory']['#default_value'];
  11. if (empty($default_directory)) {
  12. $content_type = $form['instance']['bundle']['#value'];
  13. // Seo optimization for url.
  14. $default_directory = str_replace('_', '-', $type_field . 's/' . $content_type);
  15. if (module_exists('token')) {
  16. $default_directory .= '/' . '[node:nid]';
  17. }
  18. }
  19. $form['instance']['settings']['file_directory']['#default_value'] = $default_directory;
  20. }
  21.  
  22. }