0

I intend to create a slideshow in front page of my theme to show all images in custom directory, I try to use file_scan_directory and drupal_get_path in below codes to get any non static images path and display that images automatically without any need to calling each one of them separately:
Theme Name.theme:

function MyThemeName_preprocess_page(&$variables) {
$mask = '/\.(?:jp[e]?g|gif|png)$/i';
$files = file_scan_directory(drupal_get_path('theme', 'Theme Name') . '/assets/img/orbit', $mask);
ksort($files);
$variables['files'] = $files;
}

page.html.twig:

{% for file in files %}
<img src="{{ file.uri }}" width="100%" height="100%" alt="{{ file.name }}"/>
{% endfor %}

I would be happy to hear what is the problem with the codes and what is the right way to load that slideshowe pictures?

2
  • Do you have the above code in a preprocess function? Such as THEME_NAME_preprocess_page(&$variables)
    – timodwhit
    Commented Sep 29, 2016 at 0:19
  • 1
    WRT to the best way to do something. I think it depends on your use case. Typically, I wouldn't go about this by calling pictures in the theme unless they are never to be edited by the client in the CMS and there isn't a need for other slideshows on the site. An example of how this could be accomplished is to create a custom slideshow block with an image field that allows X amount of images. This would allow for all the content to be controlled in the CMS, and other slideshows could be created.
    – timodwhit
    Commented Sep 29, 2016 at 0:22

1 Answer 1

0

The path will be wrong, file_scan_directory() will return a relative path, you need to add the base_path(), you can set a variable in preprocess:

$variables['base_path'] = base_path();

So...

<img src="{{ base_path }}{{ file.uri }}" width="100%" height="100%" alt="{{ file.name }}"/>
2
  • Thank you so much for your attention, but can you help me and explain a little more, whatever I try I get error only. Commented Aug 8, 2016 at 2:25
  • I don't know what you need me to explain.
    – Jeff Burnz
    Commented Aug 8, 2016 at 4:38

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.