0

I built a simple module to find the files in a folder and display them. However no "content" is rendered and I get warnings from a core-include file. Here is the module file:

<?php
function composer_example_menu() {
    $items = array();

    $items['composerExample/composer_example'] = array(
        'title' => 'Composer Demo',
        'page callback' => 'get_current_files', // 'call_back_function_name',
        'access arguments' => array('access content'),
        'type' => MENU_CALLBACK,
    );

    return $items;
}

/**
 * A utility function to return the array of current SplFileInfo objects
 */
function get_current_files()
{

  require __DIR__.'/../../../../../vendor/autoload.php';

    // the "files" directory
    $dir = drupal_realpath(file_default_scheme() . '://');

    $finder = new \Symfony\Component\Finder\Finder();
    $finder->in($dir)
        ->name('*.txt')
    ;
    $files = array();
    foreach ($finder as $file) {
        $files[] = $file->getFilename();
    }
  return $files;

The Warnings I get:

Warning: Invalid argument supplied for foreach() in element_children() (line 6583 of /Applications/MAMP/htdocs/drupal-7.51/includes/common.inc). Warning: Illegal string offset '#children' in drupal_render() (line 6040 of /Applications/MAMP/htdocs/drupal-7.51/includes/common.inc). Warning: Illegal string offset '#children' in drupal_render() (line 6050 of /Applications/MAMP/htdocs/drupal-7.51/includes/common.inc). Warning: Illegal string offset '#children' in drupal_render() (line 6088 of /Applications/MAMP/htdocs/drupal-7.51/includes/common.inc). Warning: Illegal string offset '#printed' in drupal_render() (line 6095 of /Applications/MAMP/htdocs/drupal-7.51/includes/common.inc).

1
  • 1
    Replace foreach loop with foreach ($finder as $key=>$file) { $files[$key]['#markup'] = $file->getFilename(); } Commented Dec 1, 2016 at 5:47

1 Answer 1

2

The value returned from the page callback will be passed into the function stated in delivery callback.

The default value of delivery callback is drupal_deliver_html_page().

"delivery callback": The function to call to package the result of the page callback function and send it to the browser. Defaults to drupal_deliver_html_page() unless a value is inherited from a parent menu item. Note that this function is called even if the access checks fail, so any custom delivery callback function should take that into account. See drupal_deliver_html_page() for an example.

So you cannot output the $files directly. You have to return a render array if you want to keep using the default delivery callback.

1
  • Excellent. Thank you for the very detailed explanation.
    – Mike
    Commented Dec 1, 2016 at 13:39

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.