2

I am trying to use the drupal_get_form funtion outside of a menu and am not having any luck finding examples of how this is done.

An overview of what I am trying to achieve is: 1) Pass control from a script using an href html tag passing to another script that will then read what has been passed to the new script and build a form based on the action that has been passed in. I am using the drupal_get_form function just as you would any php function. 2) I am also getting a message that says drupal_get_form is an unknown function.

This script is the initial calling script that contains an html href link to the called processing script.

function view_payment_methods() {
  try {$result = db_select('tls_connect_cashier_payment_methods', 'q')
    ->fields('q', array('id', 'payment_method', 'payment_desc', 'logically_deleted', 'system_or_user'))
    ->orderBy('payment_desc')
    ->execute();
  } catch(Exception $e) {
    watchdog_exception(
      'view_payment_methods',
      $e,
      'Select from tls_connect_cashier_payment_methods has failed.',
      null,
      'Alert'
    );

    return false;
  }
  $payment_methods = $result->fetchAll(PDO::FETCH_ASSOC);

  $col1_label           = t('Payment Method');
  $col2_label           = t('Payment Description');
  $col3_label           = t('Disabled');
  $col4_label           = t('Created By');
  $col5_label           = t('Action');
  $edit_label           = '(' . t('Edit') . ')';
  $delete_label         = '(' . t('Delete') . ')';

  $markup  = "<h1>" . t('Payment Methods') . "</h1>";
  $markup .= "<table>";
  $line    = "<tr>";
  $line   .= "<td>" . $col1_label . "</td>";
  $line   .= "<td>" . $col2_label . "</td>";
  $line   .= "<td>" . $col3_label . "</td>";
  $line   .= "<td>" . $col4_label . "</td>";
  $line   .= "<td>" . $col5_label . "</td>";
  $line   .= "</tr>";
  $markup .= $line;
  if (isset($payment_methods[0])) {
    foreach ($payment_methods as $entry) {
      $disabled          = '';
      $created_by        = '';
      $id                = $entry['id'];
      $payment_method    = $entry['payment_method'];
      $payment_desc      = $entry['payment_desc'];
      $logically_deleted = $entry['logically_deleted'];
      $system_or_user    = $entry['system_or_user'];
      $line              = "<tr>";
      $line             .= "<td>" . t($payment_method) . "</td>";
      $line             .= "<td>" . t($payment_desc) . "</td>";
      if ($disabled == 0) {
        $disabled      = 'No';
      } else {
        $disabled      = 'Yes';
      }
      $line             .= "<td>" . t($disabled) . "</td>";
      if ($system_or_user == 'S') {
        $created_by    = 'System';
      } else {
        $created_by    = 'User';
      }
      $line             .= "<td>" . t($created_by) . "</td>";
      $line             .= "<td><a href = '/TLSConnect3App/sites/all/modules/custom/TLSConnectPackage/cashier/PaymentMethodEditDelete.php?action=edit&id=" . $id . "'>" . $edit_label . "</a>
                        <a href = '/TLSConnect3App/sites/all/modules/custom/TLSConnectPackage/cashier/PaymentMethodEditDelete.php?action=delete&id=" . $id . "'>" . $delete_label . "</a></td>";
      $markup           .= $line;
    }
  }
  $markup .= "</table>";

  return $markup;
}

This script is the called script accessed by way of the html href link for the edit link.

if (isset($_GET['action'])) {
  $id = $_GET['id'];
  switch ($_GET['action']) {
    case 'edit':
      $form_id = 'edit_form';
      return drupal_get_form($form_id);
      break;
    case 'delete':
      break;
  }
}

function edit_form($form, &$form_state) {
    global $id;
    die('chuck');
    $form = array();
}
0

As I understand it, you want to link to a page that displays a form and accepts certain parameters that will influence the form.

2) I am also getting a message that says drupal_get_form is an unknown function.

That is because drupal_get_form() is not known when Drupal hasn't been formally loaded (bootstrapped). When you call PaymentMethodEditDelete.php directly, then Drupal doesn't know about it, and PHP doesn't know about any Drupal API functions. In order to have all Drupal functionality available, the point of entry must be the index.php in the webroot and not a custom script somewhere.

In order to achieve what I think you want, you need to define a so called menu callback. This is simply a url that is associated to a callback function. You can find more on the menu system in the docs to hook_menu().

In order to use this menu callback with drupal_get_form you would define the menu item like this:

function mymodulde_menu() {
  $items = array();
  ...
  $items['my/custum/url/to/form'] = array(
    'title' => 'My form title',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('mymodule_custom_form_id'),
  );
  ...
  return $items;
}

This is the bare minimum to define a page that will render as a form.

Now in order to handle arguments, you can either pass them in as ordinary $_GET parameters that you can access normally inside your form builder function.

Or you alter the newly defined menu so that it will accept additional arguments and passes them into the form builder, as arguments to drupal_get_form (see Wildcards in Paths). Also have a look at this question for more information: https://drupal.org/node/2054083.

Some more reading I can recommend:

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.