Last active
December 25, 2015 16:19
-
-
Save jonhattan/7004650 to your computer and use it in GitHub Desktop.
Extends "edit line item" (see https://drupal.org/node/1211278) functionality to allow editing within the product display node, in the same way as when add-to-cart in the first instance. Sadly it needs overriding the views handler to set our own link.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_menu(). | |
*/ | |
function MODULENAME_menu() { | |
$items['node/%node/edit-line-item/%commerce_line_item'] = array( | |
'title' => 'Edit line item in the cart', | |
'page callback' => 'node_page_view', | |
'page arguments' => array(1), | |
'access callback' => 'MODULE_edit_line_item_edit_access', | |
'access arguments' => array(1, 3, 'access checkout'), | |
); | |
return $items; | |
} | |
/** | |
* Implements hook_views_data_alter(). | |
* | |
* Overrides cart_line_item_link_edit handler. | |
*/ | |
function MODULENAME_views_data_alter(&$data) { | |
$data['commerce_line_item']['cart_line_item_link_edit']['field']['handler'] = 'MODULENAME_handler_field_cart_line_item_link_edit'; | |
} | |
/** | |
* Access callback for node/%node/edit-line-item/%commerce_line_item. | |
*/ | |
function MODULENAME_edit_line_item_edit_access($node, $line_item, $permission) { | |
$context = $line_item->data['context']; | |
if (($context['entity']['entity_type'] != 'node') || ($context['entity']['entity_id'] != $node->nid)) { | |
return false; | |
} | |
return commerce_cart_line_item_form_menu_item_access($line_item, $permission); | |
} | |
/** | |
* Implements hook_module_implements_alter(). | |
* | |
* Invoke our hook_field_attach_view_alter() before commerce_cart's one. | |
*/ | |
function MODULENAME_module_implements_alter(&$implementations, $hook) { | |
if ($hook == 'field_attach_view_alter') { | |
$key = 'MODULENAME'; | |
$group = $implementations[$key]; | |
unset($implementations[$key]); | |
$implementations = array($key => $group) + $implementations; | |
} | |
} | |
/** | |
* Implements hook_field_attach_view_alter(). | |
* | |
* Replace the line item argument to the add to cart form with the one being edited. | |
* | |
* @see commerce_cart_field_attach_view_alter(). | |
* @see MODULENAME_module_implements_alter(). | |
*/ | |
function MODULENAME_field_attach_view_alter(&$output, $context) { | |
if (arg(2) == 'edit-line-item') { | |
$line_item = menu_get_object('commerce_line_item', 3); | |
if ($line_item) { | |
$output[DISCOVER_PLAN_FIELD_PRODUCT_REFERENCE][0]['#arguments']['line_item'] = $line_item; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Field handler to present an edit link to a cart line item. | |
*/ | |
class MODULENAME_handler_field_cart_line_item_link_edit extends commerce_cart_handler_field_cart_line_item_link_edit { | |
function render($values) { | |
$text = !empty($this->options['text']) ? $this->options['text'] : t('edit'); | |
$line_item_id = $this->get_value($values, 'line_item_id'); | |
$line_item = entity_load_single('commerce_line_item', $line_item_id); | |
$display_path = $line_item->data['context']['display_path']; | |
$path = $display_path . '/edit-line-item/' . $line_item_id; | |
$menu_item = menu_get_item($path); | |
// Exit if no access to the menu path | |
if (empty($menu_item['access'])) { | |
return; | |
} | |
// Build query parameters | |
$query_params = drupal_get_destination(); | |
$query_params['show_quantity'] = $this->options['show_quantity']; | |
$query_params['token'] = drupal_get_token('commerce_line_item_edit:' . $line_item_id); | |
// render link | |
return l($text, $path, array('query' => $query_params)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment