I'm building a shop where users can add their products.
I don't use a product content type, nor a product reference field.
I allow authenticated users to add (and edit their own) products.

The problem is: after adding a product, they get redirected to the administrative product overview which is only accessible for users with 'manage products' rights, which they don't have, and thus: arriving on an access denied page.

My workaround is a custom module which form-alters the redirects (if the logged-in user doesn't have 'manage products' rights) to a page they can access.

But I was hoping there is a more elegant solution ... any hints?

Comments

rszrama’s picture

Status: Active » Needs review

Hmm... I think what you're doing is the simplest solution. The other option would be to instantiate the product add form at a different URL than the Product UI module does using a similar method... define a form ID using hook_forms() that displays the same base form and then use an alter hook on the custom form ID to add a submit handler for the redirect. This would let you place the add form in the IA closer to the listing page they actually have access to, too.

What do you think?

pieterdc’s picture

You're right.

But I was thinking the simplest / most flexible solution would be if the the product-add form could remember its referrer (even throughout the 'add another' button).

But that still leaves me with the problem an (almost totally) inaccessible breadcrumb trail on top of that form, which can possibly be hidden by a small amount of custom code.

What do you think? ;-)

But you can't rely on a browser to fetch the referrer, it would have to be an explicit url argument or so... hmm.

rszrama’s picture

Yeah, making it function based on the referer isn't very reliable, especially since with the admin menu or bookmarks you could get to the add form from virtually anywhere. The idea behind separating the forms from the menu items the way we are between the API and UI modules was so other modules can "instantiate" the forms in their own user interfaces as necessary... unfortunately you just came up with a use case for it before we could get it all documented. : P

pieterdc’s picture

Component: Product » Documentation
Category: support » task
Status: Needs review » Needs work

The workaround is doable, and things got clearer now, so now we have to document it somewhere.

rszrama’s picture

Status: Needs work » Fixed

Documented: http://www.drupalcommerce.org/node/221 : )

I'll tidy this up with more explanation in an article soon... it's been on the to-do list for some time now after all!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Weaver’s picture

I have the same scenario, PieterDC would it be possible to get your module code for your workaround? Thanks.

tommy kaneko’s picture

I have the same problem. It's a bit hacky, but the code I used is:

<?php
function mymodule_form_alter(&$form, &$form_state, $form_id) {
   // Change the redirect URI for Product Add forms.
  if ($form_id == 'commerce_product_ui_product_form') {
		// completely override the function
    $form['actions']['submit']['#submit'] = array('mymodule_product_form_submit');
  }

}

/**
 * Submit callback for commerce_product_ui_product_form().
 */
function mymodule_product_form_submit($form, &$form_state) {
   // Set the redirect based on the button clicked.
  if ($form_state['triggering_element']['#parents'][0] == 'save_continue') {
    $form_state['redirect'] = 'admin/commerce/products/add/' . strtr($form_state['commerce_product']->type, array('_' => '-'));
  }
  elseif (arg(2) == 'products' && arg(3) == 'add' && !user_access('administer commerce_product entities')) {
    // Redirect to product view page.
    $product_id = $form_state['commerce_product']->product_id;
		if (empty($product_id)) {
			$form_state['redirect'] = 'admin/commerce/products/' . $product_id;
		} else {
			$form_state['redirect'] = 'admin/user';
		}
  }
  elseif (arg(2) == 'products' && arg(3) == 'add') {
    $form_state['redirect'] = 'admin/commerce/products';
  }

}

?>
leevh’s picture

Same problem as OP, but I can't seem to get any form_alter stuff to work, including the code shown above. Is there any reason the code above would not work anymore? Thanks for any help!

drasgardian’s picture

I think the first function from #8 should be more like this

function mymodule_form_alter(&$form, &$form_state, $form_id) {
   // Change the redirect URI for Product Add forms.
  if ($form_id == 'commerce_product_ui_product_form') {
        // add another submit callback
    $form['actions']['submit']['#submit'][] = 'mymodule_product_form_submit';
  }

}

otherwise it performs the redirect but never actually saves the product.