2

I want to create a link on an order dropdown menu in commerce order that is attached to a view. So for example, when you are looking at the list of orders, there is a drop down menu which has Quick Edit, Payment, Delete, etc. I want to another element to this drop down menu which points to a view that I have created.

I have successfully added a link this dropdown menu using the following code, but it links to a form.

$items['admin/commerce/orders/%commerce_order/cancel'] = array(
    'title' => 'Cancel',
    'page callback' => 'bh_order_handling_ui_order_cancel_form_wrapper',
    'page arguments' => array(3),
    'access callback' => 'bh_order_handling_ui_cancel_access',
    'access arguments' => array(3),
    'type' => MENU_LOCAL_TASK,
    'weight' => 30,
    'context' => MENU_CONTEXT_INLINE,
);

What I want to do is link to a view that I have created at the path admin/commerce/orders/%/takehomeitems.

I have tried the following, using suggestions from http://zufelt.ca/blog/embedding-view-menu-hook-drupal

$items['admin/commerce/orders/%/takehomeitems'] = array(
        'title' => 'Take Home Items',
        'page callback' => 'views_embed_view',
        'page arguments' => array('bh_view_customer_donation_line_item','default',3),
        'access callback' => 'bh_donate_takehomeorder_access',
        'access arguments' => array(3),
        'type' => MENU_LOCAL_TASK,
        'weight' => 10,
        'context' => MENU_CONTEXT_INLINE,
        'file path' => drupal_get_path('module', 'views'),
        'file' => 'views.module',
);

1 Answer 1

1

I needed to call views_embed_view in my own page callback because I needed to pass the commerce order id for my contextual filter. Here is what I did:

I first defined the menu item. Note that the path has %commerce_order instead of just %. This is needed to access the order object which has the id.

When creating the view, I put in in a separate path, not the path I used for the $items. When I tried to use the same path, it didn't work. I think it is because the $items path is needed to create the listing in the dropdown menu.

I defined the path of my view as something that was not admin/commerce/orders/%commerce_order/takehomeitems It is important that your view have a contextual filter which takes the order id. My path was /admin/hubadmin/takehomeitems/%

See the bottom of my posted answer for the exported view example.

Then in hook_menu, i defined the following:

 $items['admin/commerce/orders/%commerce_order/takehomeitems'] = array(
   'title' => 'Take Home Items',
   'page callback' => '_bh_donate_takehome_lineitems',
   'page arguments' => array(3),
   'access callback' => 'bh_donate_takehomeorder_access',
   'access arguments' => array(3),
   'type' => MENU_LOCAL_TASK,
   'weight' => 10,
   'context' => MENU_CONTEXT_INLINE,
 );

I then defined the page callback which calls views_embed_view and by passing in the %commerce_order, I am able to get the order id

function _bh_donate_takehome_lineitems($order) {
  return views_embed_view('YOURVIEWNAME', 'YOURDISPLAYID', $order->order_id);
} //DISPLAYID is usually default. To find this out, choose to export your view and look at $handler = $view->new_display('default', 'Master', 'default'); The last parameter is the display id.

This creates a item in the dropdown menu of the order which links to a view. The breadcrumb path points to the order.

Here is my exported view that worked for an example.

$view = new view();
    $view->name = 'bh_view_customer_donation_line_item';
    $view->description = '';
    $view->tag = 'default';
    $view->base_table = 'commerce_order';
    $view->human_name = 'BH View: Customer Take Home Line Items';
    $view->core = 7;
    $view->api_version = '3.0';
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

    /* Display: Master */
    $handler = $view->new_display('default', 'Master', 'default');
    $handler->display->display_options['title'] = 'Customer Take Home Line Items';
    $handler->display->display_options['use_more_always'] = FALSE;
    $handler->display->display_options['access']['type'] = 'none';
    $handler->display->display_options['cache']['type'] = 'none';
    $handler->display->display_options['query']['type'] = 'views_query';
    $handler->display->display_options['exposed_form']['type'] = 'basic';
    $handler->display->display_options['pager']['type'] = 'full';
    $handler->display->display_options['pager']['options']['items_per_page'] = '10';
    $handler->display->display_options['style_plugin'] = 'table';
    $handler->display->display_options['style_options']['columns'] = array(
        'vendor_user_reference' => 'vendor_user_reference',
        'commerce_product' => 'commerce_product',
        'quantity' => 'quantity',
        'commerce_unit_price' => 'commerce_unit_price',
        'commerce_total' => 'commerce_total',
    );
    $handler->display->display_options['style_options']['default'] = '-1';
    $handler->display->display_options['style_options']['info'] = array(
        'vendor_user_reference' => array(
            'sortable' => 0,
            'default_sort_order' => 'asc',
            'align' => '',
            'separator' => '',
            'empty_column' => 0,
        ),
        'commerce_product' => array(
            'sortable' => 0,
            'default_sort_order' => 'asc',
            'align' => '',
            'separator' => '',
            'empty_column' => 0,
        ),
        'quantity' => array(
            'sortable' => 0,
            'default_sort_order' => 'asc',
            'align' => '',
            'separator' => '',
            'empty_column' => 0,
        ),
        'commerce_unit_price' => array(
            'sortable' => 0,
            'default_sort_order' => 'asc',
            'align' => '',
            'separator' => '',
            'empty_column' => 0,
        ),
        'commerce_total' => array(
            'sortable' => 0,
            'default_sort_order' => 'asc',
            'align' => '',
            'separator' => '',
            'empty_column' => 0,
        ),
    );
    $handler->display->display_options['style_options']['empty_table'] = TRUE;
    /* Relationship: Commerce Order: Referenced line items */
    $handler->display->display_options['relationships']['commerce_line_items_line_item_id']['id'] = 'commerce_line_items_line_item_id';
    $handler->display->display_options['relationships']['commerce_line_items_line_item_id']['table'] = 'field_data_commerce_line_items';
    $handler->display->display_options['relationships']['commerce_line_items_line_item_id']['field'] = 'commerce_line_items_line_item_id';
    /* Relationship: Commerce Line item: Referenced products */
    $handler->display->display_options['relationships']['commerce_product_product_id']['id'] = 'commerce_product_product_id';
    $handler->display->display_options['relationships']['commerce_product_product_id']['table'] = 'field_data_commerce_product';
    $handler->display->display_options['relationships']['commerce_product_product_id']['field'] = 'commerce_product_product_id';
    $handler->display->display_options['relationships']['commerce_product_product_id']['relationship'] = 'commerce_line_items_line_item_id';
    /* Field: Commerce Product: Vendor User Reference */
    $handler->display->display_options['fields']['vendor_user_reference']['id'] = 'vendor_user_reference';
    $handler->display->display_options['fields']['vendor_user_reference']['table'] = 'field_data_vendor_user_reference';
    $handler->display->display_options['fields']['vendor_user_reference']['field'] = 'vendor_user_reference';
    $handler->display->display_options['fields']['vendor_user_reference']['relationship'] = 'commerce_product_product_id';
    $handler->display->display_options['fields']['vendor_user_reference']['label'] = 'Vendor ';
    $handler->display->display_options['fields']['vendor_user_reference']['element_type'] = '0';
    $handler->display->display_options['fields']['vendor_user_reference']['element_label_colon'] = FALSE;
    $handler->display->display_options['fields']['vendor_user_reference']['element_wrapper_type'] = '0';
    $handler->display->display_options['fields']['vendor_user_reference']['element_default_classes'] = FALSE;
    $handler->display->display_options['fields']['vendor_user_reference']['field_api_classes'] = TRUE;
    /* Field: Commerce Line item: Product */
    $handler->display->display_options['fields']['commerce_product']['id'] = 'commerce_product';
    $handler->display->display_options['fields']['commerce_product']['table'] = 'field_data_commerce_product';
    $handler->display->display_options['fields']['commerce_product']['field'] = 'commerce_product';
    $handler->display->display_options['fields']['commerce_product']['relationship'] = 'commerce_line_items_line_item_id';
    $handler->display->display_options['fields']['commerce_product']['element_type'] = '0';
    $handler->display->display_options['fields']['commerce_product']['element_label_colon'] = FALSE;
    $handler->display->display_options['fields']['commerce_product']['element_wrapper_type'] = '0';
    $handler->display->display_options['fields']['commerce_product']['element_default_classes'] = FALSE;
    $handler->display->display_options['fields']['commerce_product']['type'] = 'commerce_product_reference_sku_link';
    $handler->display->display_options['fields']['commerce_product']['settings'] = array(
        'show_quantity' => 0,
        'default_quantity' => '1',
        'combine' => 1,
        'show_single_product_attributes' => 0,
        'line_item_type' => 'product',
    );
    $handler->display->display_options['fields']['commerce_product']['field_api_classes'] = TRUE;
    /* Field: Commerce Line Item: Quantity */
    $handler->display->display_options['fields']['quantity']['id'] = 'quantity';
    $handler->display->display_options['fields']['quantity']['table'] = 'commerce_line_item';
    $handler->display->display_options['fields']['quantity']['field'] = 'quantity';
    $handler->display->display_options['fields']['quantity']['relationship'] = 'commerce_line_items_line_item_id';
    /* Field: Commerce Line item: Unit price */
    $handler->display->display_options['fields']['commerce_unit_price']['id'] = 'commerce_unit_price';
    $handler->display->display_options['fields']['commerce_unit_price']['table'] = 'field_data_commerce_unit_price';
    $handler->display->display_options['fields']['commerce_unit_price']['field'] = 'commerce_unit_price';
    $handler->display->display_options['fields']['commerce_unit_price']['relationship'] = 'commerce_line_items_line_item_id';
    $handler->display->display_options['fields']['commerce_unit_price']['element_type'] = '0';
    $handler->display->display_options['fields']['commerce_unit_price']['element_label_colon'] = FALSE;
    $handler->display->display_options['fields']['commerce_unit_price']['element_wrapper_type'] = '0';
    $handler->display->display_options['fields']['commerce_unit_price']['element_default_classes'] = FALSE;
    $handler->display->display_options['fields']['commerce_unit_price']['click_sort_column'] = 'amount';
    $handler->display->display_options['fields']['commerce_unit_price']['settings'] = array(
        'calculation' => FALSE,
    );
    $handler->display->display_options['fields']['commerce_unit_price']['field_api_classes'] = TRUE;
    /* Field: Commerce Line item: Total */
    $handler->display->display_options['fields']['commerce_total']['id'] = 'commerce_total';
    $handler->display->display_options['fields']['commerce_total']['table'] = 'field_data_commerce_total';
    $handler->display->display_options['fields']['commerce_total']['field'] = 'commerce_total';
    $handler->display->display_options['fields']['commerce_total']['relationship'] = 'commerce_line_items_line_item_id';
    $handler->display->display_options['fields']['commerce_total']['element_type'] = '0';
    $handler->display->display_options['fields']['commerce_total']['element_wrapper_type'] = '0';
    $handler->display->display_options['fields']['commerce_total']['element_default_classes'] = FALSE;
    $handler->display->display_options['fields']['commerce_total']['click_sort_column'] = 'amount';
    $handler->display->display_options['fields']['commerce_total']['settings'] = array(
        'calculation' => FALSE,
    );
    $handler->display->display_options['fields']['commerce_total']['field_api_classes'] = TRUE;
    /* Contextual filter: Commerce Line Item: Order ID */
    $handler->display->display_options['arguments']['order_id']['id'] = 'order_id';
    $handler->display->display_options['arguments']['order_id']['table'] = 'commerce_line_item';
    $handler->display->display_options['arguments']['order_id']['field'] = 'order_id';
    $handler->display->display_options['arguments']['order_id']['relationship'] = 'commerce_line_items_line_item_id';
    $handler->display->display_options['arguments']['order_id']['default_action'] = 'empty';
    $handler->display->display_options['arguments']['order_id']['title_enable'] = TRUE;
    $handler->display->display_options['arguments']['order_id']['title'] = 'Order %1';
    $handler->display->display_options['arguments']['order_id']['default_argument_type'] = 'fixed';
    $handler->display->display_options['arguments']['order_id']['summary']['number_of_records'] = '0';
    $handler->display->display_options['arguments']['order_id']['summary']['format'] = 'default_summary';
    $handler->display->display_options['arguments']['order_id']['summary_options']['items_per_page'] = '25';
    /* Filter criterion: Commerce Product: Vendor User Reference (vendor_user_reference) */
    $handler->display->display_options['filters']['vendor_user_reference_uid']['id'] = 'vendor_user_reference_uid';
    $handler->display->display_options['filters']['vendor_user_reference_uid']['table'] = 'field_data_vendor_user_reference';
    $handler->display->display_options['filters']['vendor_user_reference_uid']['field'] = 'vendor_user_reference_uid';
    $handler->display->display_options['filters']['vendor_user_reference_uid']['relationship'] = 'commerce_product_product_id';
    $handler->display->display_options['filters']['vendor_user_reference_uid']['operator'] = 'not empty';
    /* Filter criterion: Commerce Line Item: Type */
    $handler->display->display_options['filters']['type']['id'] = 'type';
    $handler->display->display_options['filters']['type']['table'] = 'commerce_line_item';
    $handler->display->display_options['filters']['type']['field'] = 'type';
    $handler->display->display_options['filters']['type']['relationship'] = 'commerce_line_items_line_item_id';
    $handler->display->display_options['filters']['type']['value'] = array(
        'product' => 'product',
    );

    /* Display: Page */
    $handler = $view->new_display('page', 'Page', 'page');
    $handler->display->display_options['path'] = 'admin/hubadmin/takehome-items/%';

Your Answer

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

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