I want to ship a default image in my custom module. I know I can reference images in the files directory with public://, but how can I properly reference my module's URL path?

Update: I need to pass this URI to image_style so a raw path to the module directory won't be enough:

  $render_image = [
    '#theme' => 'image_style',
    '#style_name' => 'blah',
    '#uri' => $uri,
| improve this question | | | | |
  • Get the relative path to your module using the module manager service. – Eyal Jun 6 '16 at 21:29

The \Drupal class has many useful static functions such as moduleHandler. I recommend (for future issues) to first search there, then in the core.services.yml & system.services.yml files for a service or a method that could help.

| improve this answer | | | | |

I think following is easy method to do this

Create private file path

$settings['file_private_path'] = 'give path to you /private-files'; \Drupal::service('file_system')->realpath("private://");

Now you can use this to upload your module related file also custom file for custom form.

Ref: https://www.drupal.org/docs/8/modules/skilling/installation/set-up-a-private-file-path

| improve this answer | | | | |

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.