Require external files from module.install
Hello, I am working on a module with a rather large install file. This module is more like a cross between an installation profile and a set of features than it is a normal module. One of its functions is to create numerous content types and fields at activation. The arrays that are needed to handle this are pretty large and I'd like to store them as multiple external files (one for each content type). The bundle_copy and features modules are being used to create the arrays and import code from bundle_copy is used to process the arrays and create the entities from a master $data array in each file. The code looks like this: /** * Implements hook_enable(). */ function baseline_enable() { foreach(glob(drupal_get_path('module', 'baseline') . '/includes/install/{*.entities}', GLOB_BRACE) as $file) { require_once DRUPAL_ROOT . '/' . $file; /* IMPORT CODE */ } } When the module is enabled via drush the contents of each file are printed to the screen and the $data variable is not populated. When enabled from the UI, there is no output or errors and the module is enabled, but no content types or fields are generated. I thought there might be an issue with glob, so I changed the code to read the directory to this: $handle = opendir(drupal_get_path('module', 'baseline') . '/includes/install/'); while (false !== ($file = readdir($handle))) { if (strpos($file, '.entities')) { require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'baseline') . '/includes/install/' . $file; } } This produces the same results. Can someone help me understand what's going on here? Thanks, Cameron
On 2012-03-07 10:31, Cameron B. Prince wrote:
When the module is enabled via drush the contents of each file are printed to the screen
Have you made sure your included files begin with '<?php'? It sounds as though your included code is printed instead of executed, which happens when there are no enclosing PHP tags. The reason you're not seeing code in the UI is that the module is enabled (and the code is printed) while handling the form submission (POST) which silently redirects you to the form page without displaying any output. A few tips aside from this: - It sounds as though the included files directly work on a $data variable in the file scope. It might be better to define functions in the include files that return these arrays, and then call the functions from module.install after including. - Scanning a folder might not be the best approach. If the files you need to include are always the same, then hard-coding the file names is faster; if users can extend them, then you could provide a hook for other modules instead of users having to copy files into this folder. Regards, Christoph
Sorry, didn't read fully. Of course, file_scan_directory is no good for this. Sent from my iPhone On 7 Mar 2012, at 11:28, Christoph Burschka <christoph@burschka.de> wrote:
On 2012-03-07 10:31, Cameron B. Prince wrote:
When the module is enabled via drush the contents of each file are printed to the screen
Have you made sure your included files begin with '<?php'? It sounds as though your included code is printed instead of executed, which happens when there are no enclosing PHP tags.
The reason you're not seeing code in the UI is that the module is enabled (and the code is printed) while handling the form submission (POST) which silently redirects you to the form page without displaying any output.
A few tips aside from this: - It sounds as though the included files directly work on a $data variable in the file scope. It might be better to define functions in the include files that return these arrays, and then call the functions from module.install after including. - Scanning a folder might not be the best approach. If the files you need to include are always the same, then hard-coding the file names is faster; if users can extend them, then you could provide a hook for other modules instead of users having to copy files into this folder.
Regards, Christoph
Hi Christoph,
When the module is enabled via drush the contents of each file are printed to the screen
Have you made sure your included files begin with '<?php'? It sounds as though your included code is printed instead of executed, which happens when there are no enclosing PHP tags.
This was it! I should have caught it, but I guess this goes to prove brain cramps happen when coding at 3AM. Thanks for your help, Cameron
participants (3)
-
Cameron B. Prince -
Christoph Burschka -
Richard Burford