I could not find any documentation on how to use Multiple Files for a Module's _views_data and _views_default_views functions.

My module has close to 20 Views that are defined in module_name.views.inc and module_name.views_default.inc. These files are now pushing 4000 lines and are very hard to maintain. I would like to split these files into two files for each View so they are easier to maintain.

The only method I've found for this is to use something like this in module_name.views_default.inc:

function module_name_views_default_views() {
  $files = file_scan_directory(drupal_get_path('module', 'module_name'). '/views/default', '.inc');
  foreach ($files as $filepath => $file) {
    require $filepath;
    if (isset($view)) {
      $views[$view->name] = $view;
    }
  }
  return $views;
}

I cannot find an example for module_name.views.inc, but something like this might work:

function module_name_views_data() {
  $files = file_scan_directory(drupal_get_path('module', 'module_name'). '/views/data', '.inc');
  foreach ($files as $filepath => $file) {
    require $filepath;
  }
  return $data;
}

Is this the standard/best method to split these large files into smaller ones? If so, adding this to Advanced Help would be useful.

Comments

dawehner’s picture

There is a good reason to split views default, because later is it much easier to update a single one.
But i don't see this advantage on views data, but here is a code snippet which would do the same

function module_name_views_data() {
  $files = file_scan_directory(drupal_get_path('module', 'module_name'). '/views/data', '.inc');
  foreach ($files as $filepath => $file) {
    require $filepath;
    if (isset($data)) {
      $datas += $data;
    }
    $data = NULL;
  }
  return $datas;
}

I'm not sure whether this works, but i hope so :)

merlinofchaos’s picture

Yes, og and advanced forum use a method like this. Patches to the advanced help to document this would be welcome.

bendiy’s picture

Thanks for the feedback. I'll look into option #1 and browser OG and Advanced Forum's code. I'll try and put together a patch to Advanced Help if time permits.

dawehner’s picture

Category: feature » task
Issue tags: +Documentation

Cool!

esmerel’s picture

Status: Active » Postponed

I'm not quite willing to close this yet, but..

esmerel’s picture

Status: Postponed » Active
merlinofchaos’s picture

Status: Active » Closed (won't fix)

Just won't fixing. THis issue is 2 years old.