You are here

function _module_builder_write_file in Module Builder 5

Same name and namespace in other branches
  1. 6.2 module_builder.module \_module_builder_write_file()
  2. 7 module_builder.module \_module_builder_write_file()

Helper function to write files saves moving this code while mucking about with different formsAPI approaches & quicker to shortcircuit

2 calls to _module_builder_write_file()
module_builder_write_buttons in ./module_builder.module
Form after build callback. If update button was clicked, update hooks documentation. Rest of form is not submitted. Cribbed from node_form_add_preview()
Xmodule_builder_page_write in ./module_builder.module
Module form: 'write' step

File

./module_builder.module, line 696
Builds scaffolding for custom modules.

Code

function _module_builder_write_file($basename, $extension, $content) {
  if (strlen($content) == 0) {
    return;
  }

  // Build the directory name.
  $directory = variable_get('file_directory_path', 'files') . '/' . variable_get('module_builder_write_directory', 'modules') . '/' . $basename;

  // Create the directory if it doesn't exist.
  _module_builder_check_directory($directory, TRUE);
  $file_name = $basename . $extension;
  $created_file = file_save_data($content, "{$directory}/{$file_name}", FILE_EXISTS_REPLACE);
  if ($created_file) {
    drupal_set_message(t("File @file has been written.", array(
      '@file' => $created_file,
    )));
  }
  else {
    drupal_set_message(t("There was a problem writing the file @file.", array(
      '@file' => "{$directory}/{$file_name}",
    )), 'error');
  }
}