1

We are just trying to gain a deeper understanding of Drupal forms and how to manipulate and render them as needed.

We have a module installed which allows uses to edit and create content through a form. The module renders the form on its own edit/create page. We would like to render the edit/create forms on a different page with other content.

Below are a few functions which we believe render the forms. (if not which function should we be looking for??)

/**
 * Page callback for creating a customer profile of a given type.
 */
function commerce_addressbook_profile_create($account, $profile_type) {
  // Add the breadcrumb for the form's location.
  commerce_addressbook_set_breadcrumb($account, $profile_type);

  $profile = commerce_customer_profile_new($profile_type, $account->uid);
  // The submit handler needs an indication that the profile was just added.
  // The regular is_new property gets unset after save is complete.
  $profile->_is_new = TRUE;
  module_load_include('inc', 'commerce_customer', 'includes/commerce_customer_profile.forms');
  return drupal_get_form('commerce_addressbook_customer_profile_form', $profile);
}

/**
 * Page callback for editing a customer profile.
 */
function commerce_addressbook_profile_options_edit($account, $customer_profile) {
  // Add the breadcrumb for the form's location.
  commerce_addressbook_set_breadcrumb($account, $customer_profile->type);

  // If the profile is referenced by an order, make sure it gets duplicated.
  $profile = clone($customer_profile);
  if (!commerce_customer_profile_can_delete($customer_profile)) {
    $profile->previous_id = $profile->profile_id;
    unset($profile->profile_id);
    unset($profile->revision_id);
    $profile->is_new = TRUE;
  }
  module_load_include('inc', 'commerce_customer', 'includes/commerce_customer_profile.forms');
  return drupal_get_form('commerce_addressbook_customer_profile_form', $profile);
}

What is the process for calling these functions (if they are the ones we are looking for) into a TPL page?




EASY TO READ COMMENTS: I have added the follow to template.php within my theme directory

function THEME_preprocess_page(&$variables){
    module_load_include('inc', 'commerce_addressbook', 'includes/commerce_addressbook.user');
    $variables['profile_form'] = commerce_addressbook_profile_options_edit($account, $customer_profile);
}

I have also included the following with a page tpl file.

<?php print render($profile_form); ?>

This results in the page returning a 500 error. (commenting out the variables line allows the page to load.)

1
  • 1
    You are not defining the arguments passed to the edit function. I would bet all my money the 500 error is complaining about that, because the first thing that function does is use those arguments. I've updated my answer because I think you are flying by some core requirements, namely what the function expects to be passed.
    – Kevin
    Commented Oct 5, 2017 at 17:30

1 Answer 1

1

You don't want to be putting that much PHP code into a tpl template file. Your menu item should have two parameters (user account, and profile type) and the page callback is specified as the desired function.

If you are doing this another way, you still need the user object and customer profile. In a preprocess function or in a custom page callback, you can create the form as a variable:

template.php:

// you can load a user account in various ways, ill assume this is behind a user path
$account = menu_get_object('user'); 
// you will need to verify this is the right function to use.
$customer_profile = commerce_customer_profile_load(profile_id_here);

$variables['profile_form'] = commerce_addressbook_profile_options_edit($account, $customer_profile);

in your tpl file:

<?php print render($profile_form); ?>

I am not familiar with the commerce_addressbook module, but there is a chance you'll need to include the file that defines those functions too, especially if they do not live in a .module file but live in a .inc file.

In which case, you'd need:

module_load_include('inc', 'commerce_addressbook', 'filepath/filename');
$variables['profile_form'] = commerce_addressbook_profile_options_edit($account, $customer_profile);
3
  • My comment will be appended to the question so it will be easier to read.
    – CleanCode
    Commented Oct 5, 2017 at 17:13
  • Thank you so much for re-outline your answer. It makes a lot more sense, I will have a go at this for a bit and see what I can come up with.
    – CleanCode
    Commented Oct 5, 2017 at 17:35
  • I am marking this question as answered, but I am still at a loss as to getting the form to print. Thank you for your time Kevin
    – CleanCode
    Commented Oct 6, 2017 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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