Hi there.
I am new to development with drupal 8 so please bare with me.
I simply want to expose a list (js array) of .png files that are housed in directories in my default images folder.
I would normally just use something like

<script><?php
    $dir    = 'somedir/somesubdir/';
$files1 = scandir($dir);
echo "var theJsArray=".json_encode(array_slice($files1, 2)).";";?></script>

I need these arrays exposed for a custom control block.
I have also tried an ajax call though this results in a forbidden 403 error;
the directory called "trees" is in my default files.
I am trying these calls from a standard custom block. I have js files implemented through both a custom module and a customized version of business_responsive_theme.
I am simply not sure of the correct way to do this with Drupal8. EG should I try to expose these through the twig file for the block?
The code is to load a list of files for a random environment generator I created in php and javascript and I am now trying to migrate to drupal.
The project I am working on is a nature site designed at giving children and adults in the city knowledge of and experience of the Australian bush through artistic interactive art.
Any help would be appreciated
Thanks.

Comments

Jaypan’s picture

You can do this using the drupalSettings object. Here's an example (Note - I'm assuming the code you showed works):

$dir = 'somedir/somesubdir/';
$files1 = scandir($dir);
$files = array_slice($files1, 2);

$some_render_array = [
  ...
  '#attached' => [
    'library' => ['my_module/my_image_library'],
    'drupalSettings' => [
      'someUniqueKey' => ['files' => $files],
    ],
  ],
];

Your library in the above case will look like this:

my_image_library:
  js:
    path/to/myfile.js: {}
  dependencies:
    - core/jquery
    - core/drupalSettings

With the above library, in the file myfile.js, your files will be available as an array at drupalSettings.someUniqueKey.files.

Firehawk777’s picture

Thanks. Yes my code works and yes that was the answer I was looking for. Though as some bright spark has taken advantage of some security flaw in Drupal 8 on my site I am finding it hard to implement.
Thanks again.
I personally think drupal 7 was far better thought out than this crappy version. Its total garbage for its complexity and by the looks even less secure than its predecessor. I thought these guys that created and decided on the upgrades actually had a clue. Shame it appears I was wrong. Must be time to move to some other CMS if this is what we can expect.

Jaypan’s picture

I personally think drupal 7 was far better thought out than this crappy version. Its total garbage for its complexity and by the looks even less secure than its predecessor. I thought these guys that created and decided on the upgrades actually had a clue. Shame it appears I was wrong. Must be time to move to some other CMS if this is what we can expect.

I'm sorry to tell you this, but having worked extensively with both at a deep, code level, D8 is hands down a better system, and much more secure as well.
I can understand the frustration though, it's quite difficult to learn D8.