0

I have a custom module which previously working fine before adding its view part. form_example.module

   <?php
function form_example_permission() {
  return array(
    'submit form_example' => array(
      'title' => t('Submit form_example'),
      'description' => t('Submit the form_example form'),
    ),
    'access form_example submissions' => array(
      'title' => t('Access form_example submissions'),
      'description' => t('Access the form_example submissions'),
    ),
  );
}
function form_example_menu() {
  $items = array();

  $items['form-example'] = array( //this creates a URL that will call this form at "examples/form-example"
    'title' => 'Example Form', //page title
    'description' => 'A form to mess around with.',
    'type' => MENU_NORMAL_ITEM,
    'access arguments' => array('submit form_example'),
    'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed.  for a form, use drupal_get_form
    'page arguments' => array('form_example_form'),

      );

 $item['form-submissions'] = array(
    'title' => t('List'),
    'page callback' => 'form_example_submissions',
    'access callback' => TRUE,
    'access arguments' => array('access form_example submissions'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function form_example_form($form, &$form_state) {

$form['empcode'] = array(
  '#weight' => '0',
  '#required' => '1',
  '#type' => 'textfield',
  '#title' => t('Employee Code'),
);
$form['empname'] = array(
  '#weight' => '1',
  '#required' => '1',
  '#type' => 'textfield',
  '#title' => t('Employee Name'),
);
$form['file'] = array(
  '#weight' => '2',
  '#type' => 'file',
  '#size' => '30',
  '#title' => t('Employee Photo'),
);
 $form['submit'] = array(
     '#weight' => '3',
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

return $form;

}

function form_example_form_validate($form, &$form_state) {
  /*if (!($form_state['values']['price'] > 0)){
    form_set_error('price', t('Price must be a positive number.'));
  }*/
 // print('<pre>'.print_r($form_state['values'],1).'</pre>');
  //die();
 $file = file_save_upload('file', array(
    'file_validate_is_image' => array(),
    'file_validate_extensions' => array('png gif jpg jpeg'),
  ));
  if ($file) {
    if ($file = file_move($file, 'public://')) {
      $form_state['values']['file'] = $file;
    }
    else {
      form_set_error('file', t('Failed to write the uploaded file the site\'s file folder.'));
    }
  }
  else {
    form_set_error('file', t('No file was uploaded.'));
  }
}

function form_example_form_submit($form, &$form_state) {
   $file=$form_state['values']['file'];
  unset($form_state['values']['file']);
  $file->status = FILE_STATUS_PERMANENT;
  file_save($file);
  drupal_set_message(t('The form has been submitted and the image has been saved, filename: @filename.', array('@filename' => $file->filename)));


 $fe_id = db_insert('form_example')
        ->fields(array(

          'empcode' => $form_state['values']['empcode'],
          'empname' => $form_state['values']['empname'],
          'empphoto' => $file->filename,
        ))
        ->execute();
        drupal_set_message(t('Successfully added'));

}
/*function form_example_list(){
    //$form_example =drupal_get_form('form_example_search_form');
    // If wants to implement searching.
    //$content = '<div class="block" >'.drupal_render($form_example).'</div>';
    //$content .= '<div style="clear:both" ></div>';
    $header = array(
        array('data' => t('empcode'), 'field' => 'empcode', 'sort' => 'asc'),
        array('data' => t('empname'), 'field' => 'empname'),
        //array('data' => t('empphoto'), 'field' => 'empphoto'),
    );

    $query = db_select('form_example', 'pd');
    if(isset($_GET['empcode']) && $_GET['empcode'] != ''){
        $query->condition('pd.empcode', $_GET['empcode'],'=');
    }
    if(!empty($_GET['empname'])){
        $query->condition('pd.empname', $_GET['empname'],'=');
    }
    $query->fields('pd');

    $table_sort = $query->extend('TableSort')->orderByHeader($header);
    $pager = $table_sort->extend('PagerDefault')->limit(10);

    $result = $pager->execute();
    $rows = array();

    foreach($result as $res){
      $rows[] = array($res->empcode, $res->empname);
    }
    $content .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'sort-table')));
    $content .= theme('pager');
    return $content;
}*/

/* view the form submissions*/
function form_example_submissions()
{
  $results =  db_query("Select * from {form_example}");
  $header = array(t('ID'),t('Empcode'));
  $rows = array();
  foreach($results as $result)
  {
    $rows[] = array(
        $result->fe_id,
        $result->empcode,
      );

  }
  return theme('table',array('header=>$header','rows' => $rows));
}
/*function form_example_list(){
    //$form_example =drupal_get_form('form_example_search_form');
    // If wants to implement searching.
    //$content = '<div class="block" >'.drupal_render($form_example).'</div>';
    //$content .= '<div style="clear:both" ></div>';
    $header = array(
        array('data' => t('empcode'), 'field' => 'empcode', 'sort' => 'asc'),
        array('data' => t('empname'), 'field' => 'empname'),
        //array('data' => t('empphoto'), 'field' => 'empphoto'),
    );

    $query = db_select('form_example', 'pd');
    if(isset($_GET['empcode']) && $_GET['empcode'] != ''){
        $query->condition('pd.empcode', $_GET['empcode'],'=');
    }
    if(!empty($_GET['empname'])){
        $query->condition('pd.empname', $_GET['empname'],'=');
    }
    $query->fields('pd');

    $table_sort = $query->extend('TableSort')->orderByHeader($header);
    $pager = $table_sort->extend('PagerDefault')->limit(10);

    $result = $pager->execute();
    $rows = array();

    foreach($result as $res){
      $rows[] = array($res->empcode, $res->empname);
    }
    $content .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'sort-table')));
    $content .= theme('pager');
    return $content;
}*/


?>

2 Answers 2

1

Remove 'access arguments' => array('administer users'), and add 'access callback' => TRUE,

$items['form-submissions'] instated of this $item['form-submissions'] need to write. (missing "s").

Clear the cache before check.

3
  • please check my edited question. now form-example is working fine its show form and insert the data but when i type in form-submissions in url its give me error page not found
    – user67248
    Sep 21, 2016 at 7:01
  • updated my answer please check.
    – DRUPWAY
    Sep 21, 2016 at 7:08
  • You always welcome :)
    – DRUPWAY
    Sep 21, 2016 at 7:25
1

I think this should be the error 'acccess callback' => TRUE, , you have typo which should

'access callback' => TRUE,

Clear the cache and check. Change $item['form-submissions'] to $items['form-submissions'] and clear the cache.

1
  • please check my edited question. now form-example is working fine its show form and insert the data but when i type in form-submissions in url its give me error page not found
    – user67248
    Sep 21, 2016 at 7:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy