old_friend

size_tables

Jul 24th, 2021
918
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function mymodule_preprocess_commerce_product(&$variables)
  2. {
  3.     $product = $variables['elements']['#commerce_product'];
  4.  
  5.     // get products taxonomy terms ids    
  6.     $pr_brand_id = $product->get('field_brand')->target_id;
  7.     $pr_category_id = $product->get('field_product_categories')->target_id;
  8.  
  9.     // get nid of size table (depends on category and brand)
  10.     $query = \Drupal::entityQuery('node')
  11.         ->condition('type', 'size_table')
  12.         ->condition('field_brands', $pr_brand_id)
  13.         ->condition('field_categories', $pr_category_id);
  14.     $nids = $query->execute();
  15.  
  16.     // set variable with table's render
  17.     if (count($nids)) {
  18.         $nid = reset($nids);
  19.         $node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
  20.         $view = $node->body->view('default');
  21.         $variables['size_table'] = render($view);
  22.     }
  23. }
RAW Paste Data