0
votes

I am a newbie to drupal. I am working to create a custom module in the name "course". I have read about the list of hooks to create a custom module. So, what's my problem is that i cant able to see the edit form for the module configure but i can do add management and its working fine. I've used Existing contact modules as a reference.

Below are my code: In course.module

function course_menu() { 
  $items['admin/structure/course'] = array(
    'title' => 'Academy\'s courses',
    'description' => 'Create a system contact form and set up categories for the form to use.',
    'page callback' => 'course_list',
    'access arguments' => array('administer contact forms'),
    'file' => 'course.admin.inc',
  );
  $items['admin/structure/course/add'] = array(
    'title' => 'Add Courses',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('course_edit_form'),
    'access arguments' => array('administer contact forms'),
    'type' => MENU_LOCAL_ACTION,
    'weight' => 1,
    'file' => 'course.admin.inc',
  );
  $items['admin/structure/course/edit/%course'] = array(
    'title' => 'Edit Courses',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('course_edit_form',4),
    'access arguments' => array('administer contact forms'),
    'file' => 'course.admin.inc',
  );

  return $items;
}

and in course.admin.inc:

function course_edit_form($form, &$form_state, array $course = array()) {
  // If this is a new course, add the default values.

  $course += array(
    'name' => '', 
    'course_id' => NULL,
  );

  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Course Name'),
    '#maxlength' => 255,
    '#default_value' => $course['name'],
    '#description' => t("Example: 'Available Course Names'."),
    '#required' => TRUE,
  );

  $form['course_id'] = array(
    '#type' => 'value',
    '#value' => $course['course_id'],
  );
  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );

  return $form;
}

function course_edit_form_submit($form, &$form_state) {

  if (empty($form_state['values']['course_id'])) {
        db_insert('courses') // Table name no longer needs {}
        ->fields(array(
          'name' => $form_state['values']['name'],
          'course_url' => $form_state['values']['name']
        ))
        ->execute();
    //drupal_write_record('courses', $form_state['values']);
  }
  else {
     db_insert('courses') // Table name no longer needs {}
        ->fields(array(
          'name' => $form_state['values']['name'],
          'course_url' => $form_state['values']['name']
        ))
        ->condition('course_id', $form_state['values']['course_id'])
        ->execute();
    //drupal_write_record('courses', $form_state['values'], array('course_id'));
  }

  drupal_set_message(t('Course %name has been saved.', array('%name' => $form_state['values']['name'])));
  watchdog('contact', 'Course %name has been saved.', array('%course' => $form_state['values']['name']), WATCHDOG_NOTICE, l(t('Edit'), 'admin/structure/course/edit/' . $form_state['values']['course_id']));
  $form_state['redirect'] = 'admin/structure/course';
}

I can't able to find the exact problem, why add is working but not edit & delete? Help me on this.

Below image tells you what exactly i am asking for(Red mark). enter image description here

If anything additional needed in this please let me know.

2

2 Answers

0
votes

At A guess I'd say the edit route is not matching because of an issue with the course load function (not listed). Try dumping the result of that method. Is the course entity defined in your module? If so, then the convention is to prefix the variable name with the module name, and implement the appropriately names hook.

There's a better description of how this should work here: https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_menu/7 Jump to the "Auto-Loader Wildcards" section.

0
votes

Try to clear cache /admin/config/development/performance