4

In Drupal 7 I want to add couple of custom operations in the Drupal Commerce's orders list (see picture below) after "delete" e.g. "Shipping List" and "Invoice" which would open external URLs in a new window. How can I achieve this? I tried to modify some files under sites\all\modules\commerce\modules\order\includes\views but I didn't see any change in the view. Why?

Order operations

3 Answers 3

5

The proper way to add new operations is to define them as menu items. Here is a snippet from the payment module, responsible for the payment operation listed in the screenshot:

  // Payment tab on orders.
  $items['admin/commerce/orders/%commerce_order/payment'] = array(
    'title' => 'Payment',
    'page callback' => 'commerce_payment_ui_order_tab',
    'page arguments' => array(3),
    'access callback' => 'commerce_payment_transaction_order_access',
    'access arguments' => array('view', 3),
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
    'file' => 'includes/commerce_payment_ui.admin.inc',
  );

The key part being this line right here:

'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,

In fact, in most (maybe all?) places where you see this list of operations or tasks in Drupal Commerce 1.x, they will be defined this way. Another example of this being the transaction listing screen. Payment modules can add new contextual menu items for things like credits, captures, etc:

enter image description here

2
  • You beat me by 30 minutes :) I did some digging after @PekkaK response to my answer and came up with the same thing.
    – argiepiano
    Jan 4, 2016 at 1:27
  • Great, I finally got it! Although I had to change context to code MENU_CONTEXT_INLINE code only. Thanks guys!
    – PekkaK
    Jan 4, 2016 at 11:35
2

To expand @nvahalik's answer a bit, you should implement hook_menu() in a custom module and include something like this:

function your_module_menu(){
  $items['admin/commerce/orders/%commerce_order/your_new_operation'] = array(
    'title' => 'Link title',
    'page callback' => 'your_callback_for_the_operation',
    'page arguments' => array(3),
    'access callback' => TRUE,
    'type' => MENU_LOCAL_TASK,
    'weight' => 5,
    'context' => MENU_CONTEXT_INLINE,
);

  return $items;
}

You can add some custom 'access callback' to limit access to this link. If you do, the link will not be shown for users who don't fulfill the access callback.

0

You should look into implementing hook_views_pre_render() in a custom module.

This hook is called right before the render process. The query has been executed, and the pre_render() phase has already happened for handlers, so all data should be available.

Adding output to the view can be accomplished by placing text on $view->attachment_before and $view->attachment_after. Altering the content can be achieved by editing the items of $view->result.

There are many examples of this hook online.

By the way, you should avoid changing the Drupal core (the first maxim you learn in Drupal is "do not hack core"). One of the problem of hacking the core is that any Drupal or module update will delete your hack.

2
  • I managed to make changes to the commerce_orders view without a custom module with template.php: code function seven_views_pre_render(&$view) { switch ($view->name) { case 'commerce_orders' code But I can't figure out (or find online) how to add new link after "delete" link, because it is not a field but part of Operations which comes from order module's code as far as I understood how the view is designed. Can you give me another hint? :)
    – PekkaK
    Jan 3, 2016 at 20:05
  • You are right. Please see nvahalik's response, which is the correct answer.
    – argiepiano
    Jan 4, 2016 at 1:28

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.