1 file_example.module file_example_readwrite($form, &$form_state)

Form builder function.

A simple form that allows creation of a file, managed or unmanaged. It also allows reading/deleting a file and creation of a directory.

Related topics

File

modules/examples/file_example/file_example.module, line 74
Hook implementations for the File Example module.

Code

function file_example_readwrite($form, &$form_state) {
  if (empty($_SESSION['file_example_default_file'])) {
    $_SESSION['file_example_default_file'] = 'session://backdrop.txt';
  }
  $default_file = $_SESSION['file_example_default_file'];
  if (empty($_SESSION['file_example_default_directory'])) {
    $_SESSION['file_example_default_directory'] = 'session://directory1';
  }
  $default_directory = $_SESSION['file_example_default_directory'];

  $form['write_file'] = array(
    '#type' => 'fieldset',
    '#title' => t('Write to a file'),
  );
  $form['write_file']['write_contents'] = array(
    '#type' => 'textfield',
    '#title' => t('Enter something you would like to write to a file') . ' ' . date('m'),
    '#default_value' => t('Put some text here or just use this text'),
  );

  $form['write_file']['destination'] = array(
    '#type' => 'textfield',
    '#default_value' => $default_file,
    '#title' => t('Optional: Enter the streamwrapper saying where it should be written'),
    '#description' => t('This may be public://some_dir/test_file.txt or private://another_dir/some_file.txt, for example. If you include a directory, it must already exist. The default is "public://". Since this example supports session://, you can also use something like session://somefile.txt.'),
  );

  $form['write_file']['managed_submit'] = array(
    '#type' => 'submit',
    '#value' => t('Write managed file'),
    '#submit' => array('file_example_managed_write_submit'),
  );
  $form['write_file']['unmanaged_submit'] = array(
    '#type' => 'submit',
    '#value' => t('Write unmanaged file'),
    '#submit' => array('file_example_unmanaged_write_submit'),
  );
  $form['write_file']['unmanaged_php'] = array(
    '#type' => 'submit',
    '#value' => t('Unmanaged using PHP'),
    '#submit' => array('file_example_unmanaged_php_submit'),
  );

  $form['fileops'] = array(
    '#type' => 'fieldset',
    '#title' => t('Read from a file'),
  );
  $form['fileops']['fileops_file'] = array(
    '#type' => 'textfield',
    '#default_value' => $default_file,
    '#title' => t('Enter the URI of a file'),
    '#description' => t('This must be a stream-type description like public://some_file.txt or http://backdropcms.org or private://another_file.txt or (for this example) session://yet_another_file.txt.'),
  );
  $form['fileops']['read_submit'] = array(
    '#type' => 'submit',
    '#value' => t('Read the file and store it locally'),
    '#submit' => array('file_example_read_submit'),
  );
  $form['fileops']['delete_submit'] = array(
    '#type' => 'submit',
    '#value' => t('Delete file'),
    '#submit' => array('file_example_delete_submit'),
  );
  $form['fileops']['check_submit'] = array(
    '#type' => 'submit',
    '#value' => t('Check to see if file exists'),
    '#submit' => array('file_example_file_check_exists_submit'),
  );

  $form['directory'] = array(
    '#type' => 'fieldset',
    '#title' => t('Create or prepare a directory'),
  );

  $form['directory']['directory_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Directory to create/prepare/delete'),
    '#default_value' => $default_directory,
    '#description' => t('This is a directory as in public://some/directory or private://another/dir.'),
  );
  $form['directory']['create_directory'] = array(
    '#type' => 'submit',
    '#value' => t('Create directory'),
    '#submit' => array('file_example_create_directory_submit'),
  );
  $form['directory']['delete_directory'] = array(
    '#type' => 'submit',
    '#value' => t('Delete directory'),
    '#submit' => array('file_example_delete_directory_submit'),
  );
  $form['directory']['check_directory'] = array(
    '#type' => 'submit',
    '#value' => t('Check to see if directory exists'),
    '#submit' => array('file_example_check_directory_submit'),
  );

  $form['debug'] = array(
    '#type' => 'fieldset',
    '#title' => t('Debugging'),
  );
  $form['debug']['show_raw_session'] = array(
    '#type' => 'submit',
    '#value' => t('Show raw $_SESSION contents'),
    '#submit' => array('file_example_show_session_contents_submit'),
  );

  return $form;
}