Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.61 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * @fle
  5.  * Module file for adding a tab to edit the node's product page.
  6.  */
  7.  
  8. /**
  9.  * Implements hook_menu().
  10.  */
  11. function commerce_edit_product_menu() {
  12.   $items = array();
  13.  
  14.   $items['node/%node/edit-product'] = array(
  15.     'title' => t('Edit Product'),
  16.     'access callback' => 'commerce_edit_product_callback',
  17.     'access arguments' => array(1),
  18.     'type' => MENU_LOCAL_TASK,
  19.     'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
  20.   );
  21.  
  22.   return $items;
  23. }
  24.  
  25. /**
  26.  * Implements hook_menu_local_tasks_alter().
  27.  *
  28.  * Chaging the tab href and not call to other function and then use drupl_goto.
  29.  */
  30. function commerce_edit_product_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  31.   foreach ($data['tabs'][0]['output'] as $key => $item) {
  32.     if ($item['#link']['path'] == "node/%/edit-product") {
  33.       $wrapper = entity_metadata_wrapper('node', $router_item['page_arguments'][0]);
  34.       $data['tabs'][0]['output'][$key]['#link']['href'] = 'admin/commerce/products/' . $wrapper->field_product->getIdentifier() . '/edit';
  35.     }
  36.   }
  37. }
  38.  
  39. /**
  40.  * Access callback.
  41.  *
  42.  *  @param $node
  43.  *    The node object.
  44.  *
  45.  *  @return
  46.  *    TRUE/FALSE if the user can edit the product.
  47.  */
  48. function commerce_edit_product_callback($node) {
  49.   global $user;
  50.  
  51.   if (!$user->uid || ($node->uid != $user->uid)) {
  52.     return;
  53.   }
  54.  
  55.   $edit_commerce_product = user_access('edit any commerce_product entity of bundle product');
  56.   $edit_any_commerce_product = user_access('edit own commerce_product entities of bundle product');
  57.  
  58.   return $edit_commerce_product | $edit_any_commerce_product;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement