1

I am trying to apply an ID to table rows in a view using a field as the ID value.

I've copied views-view-table.tpl.php template to my theme template folder and renamed it based on the view I am working with.

I'm using Commerce Kickstart2, and I would like to id the table rows in this view by the SKU of each product.

I added SKU as a field in my view and then render it as a class for the Views Row using replacement patterns for the row class of a table view. (See screen capture).

The view I am working with

In my template this is the line that writes the row class:

<tr <?php if ($row_classes[$row_count]) { print 'class="' . implode(' ', $row_classes[$row_count]) .'"';  } ?>>

I would like to add to that the field SKU as a row ID: id="SKU Value Here".

I have tried a number of ways without success.

after running print_r($view->result); at the top of the template I get this:

Array ( [0] => stdClass Object ( [product_id] => 1 [commerce_product_sku] => sample1 [field_data_title_field_commerce_product_entity_type] => commerce_product [field_data_commerce_price_commerce_product_entity_type] => commerce_product [_field_data] => Array ( [product_id] => Array ( [entity_type] => commerce_product [entity] => stdClass Object ( [revision_id] => 1 [sku] => sample1 [title] => Product 1 [revision_uid] => 1 [status] => 1 [log] => [revision_timestamp] => 1423407240 [data] => [product_id] => 1 [type] => product [language] => und [uid] => 1 [created] => 1418869347 [changed] => 1423407240 [commerce_price] => Array ( [und] => Array ( [0] => Array ( [amount] => 1600 [currency_code] => USD [data] => Array ( [components] => Array ( ) ) ) ) ) [field_images] => Array ( [und] => Array ( [0] => Array ( [fid] => 1 [uid] => 1 [filename] => tote-1v1.jpg [uri] => public://tote-1v1.jpg [filemime] => image/jpeg [filesize] => 48075 [status] => 1 [timestamp] => 1418869347 [alt] => [title] => [width] => 860 [height] => 842 ) 1 => Array ( [fid] => 2 [uid] => 1 [filename] => tote-1v2.jpg [uri] => public://tote-1v2.jpg [filemime] => image/jpeg [filesize] => 52861 [status] => 1 [timestamp] => 1418869347 [alt] => [title] => [width] => 860 [height] => 1251 ) [2] => Array ( [fid] => 3 [uid] => 1 [filename] => tote-1v3.jpg [uri] => public://tote-1v3.jpg [filemime] => image/jpeg [filesize] => 96645 [status] => 1 [timestamp] => 1418869347 [alt] => [title] => [width] => 860 [height] => 1100 ) ) ) [title_field] => Array ( [und] => Array ( [0] => Array ( [value] => Product 1 [format] => [safe_value] => Product 1 ) ) ) [field_associated_product_diagram] => Array ( [und] => Array ( [0] => Array ( [tid] => 272 ) ) ) [title_original] => Product 1 ) ) ) [field_title_field] => Array ( [0] => Array ( [rendered] =>

If anyone can lend any insight into how to accomplish this, I would appreciate it.

0

Try this to add an id attribute with your SKU based on the $view->result output:

 <tr <?php if ($row_classes[$row_count]) { print 'class="' . implode(' ', $row_classes[$row_count]) .'"';  } ?> id="<?php print $view->result[0]->commerce_product_sku; ?>">
  • Thank you - that worked except I needed it for every row in the table, that printed the sku for the first result as an ID in every row. This is very helpful though - thank you. – Scott859 Feb 12 '15 at 19:53
0

I did this with nodes in the view:

Added nid as a field.

altered the row class code to this:

<tr <?php if ($row_classes[$row_count]) { print 'id="' . $row['nid'] . '" '; print 'class="' . implode(' ', $row_classes[$row_count]) .'"';  } ?>>

Just use sku where I used nid

  • Thank you - that is exactly what I needed in this case - works perfectly. Thanks again for your help on this - much appreciated. – Scott859 Feb 12 '15 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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