I created in php multidimensional array example $table[$row_id][$col_id];

HOw can i set it as tablefield value.

$node->field_tablefield_name['und'][0]['value'] = tablefield_function($table);

So i mean what fuction will return my $table in tablefield value format??

Comments

carlovdb’s picture

Have you succeeded this?

nimek’s picture

Yes

All you must do is properly indexed array:

$yourtable = array(
'col_0_0' => $value0_0,
'col_0_1' => $value0_0
);

where col_0_X index will be used as header.
then

$node->field_tablefield_name['und'][0]['value'] = serialize($yourtable);

and node_save($node) of course
All the best :)

carlovdb’s picture

Hmm this is not directly what I need I think.

This is my problem.. https://drupal.org/node/2235751

kalabro’s picture

Approach in #2 looks quite dirty. I also have to add 'rebuild' key to get rid of PHP Notices.
Why not add Entity API's 'property_callbacks' so we can set value simply:

$node_wrapper = entity_metadata_wrapper('node', $node);
$node_wrapper->field_tablefield = array(
    "Heading 1", "Heading 2",
    "Value 1", "Value 2",
);

I'm ready to provide a patch if the idea someone find it interesting.

oenie’s picture

I think this oatch would be very welcome.
I can't imagine the worold without entity_metadat_wrapper, but it's a bit silly having to access one of the fields the 'old' way with array indexing.

mchelen’s picture

@kalabro
A patch to add a callback would be great! Entity metadata wrapper seems to work with most other field types, but not Tablefield.

Here is a full working example using the approach from #2, note that "col" is supposed to be "cell":

  $yourtable = array(
    'cell_0_0' => "colA",
    'cell_0_1' => "colB",
    'cell_1_0' => "rowA",
    'cell_1_1' => "rowB",
    'rebuild' =>
      array(
        'count_cols' => '2',
        'count_rows' => '2',
        'rebuild' => 'Rebuild Table',
      ),
    'import' =>
      array (
      'tablefield_csv_field_mytablefield_0' => '',
      'rebuild_field_mytablefield_0' => 'Upload CSV',
      ),
  );
   
  $n = node_load(array('nid' => 1));
  $foo = serialize($yourtable);
  $n->field_mytablefield['und'][0]['value'] = $foo;
  node_save($n);

In this case I am editing field_mytablefield in node 1.

I still get a warning: array_flip(): Can only flip STRING and INTEGER values!
But the node seems to be working ok.

lolandese’s picture

Category: Feature request » Support request
Status: Active » Fixed

This seems to be answered sufficiently. Feel free to reopen if not.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

xelat’s picture

I'm getting this error using the code in #2 or 6.

EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids() (line 7929 of C:\Users\ssaata\Documents\websites\docgen\includes\common.inc).

The error is coming from node_save();.

I'm using Table field 7.x-3.1 and core 7.56. Is there any code I need to update to get that version to work?

EDIT on 24/10/2017 2:57pm

I updated this line and the error is gone

$n = node_load(1);

EDIT on 25/10/2017 4:54pm

This is the syntax for tablefield 7.x-3.1

$yourtable = array(
  'tabledata'=>array(
    'row_0'=>array(
      'col_0'=>'row1 col1',
      'col_1'=>'row1 col2',
      ),
    'row_1'=>array(
      'col_0'=>'row2 col1',
      'col_1'=>'row2 col2',
      ),
  )
);

$n = node_load(1);
$n->field_mytablefield['und'][0]['value'] = serialize($yourtable);
node_save($n);