I have built a draggable table using this tutorial (halfway down page is the Drupal 8 code):
https://www.drupal.org/node/1876710
In the submit function I would like to look through all the rows and create an array for each row. I am using the following code:
foreach ($form_state->getValues(['mytable','rows']) as $tablerow) {
And then running db_update on each item using this:
$tablerow['label']
But this returns an error "undefined index". Is there something wrong with the foreach line?
UPDATE: My table is generated using this code:
foreach ($entities as $id => $entity) {
// TableDrag: Mark the table row as draggable.
$form['mytable'][$id]['#attributes']['class'][] = 'draggable';
// TableDrag: Sort the table row according to its existing/configured weight.
$form['mytable'][$id]['#weight'] = $entity->get('weight');
// Some table columns containing raw markup.
$form['mytable'][$id]['label'] = array(
'#plain_text' => $entity->label(),
);
$form['mytable'][$id]['id'] = array(
'#plain_text' => $entity->id(),
);
// TableDrag: Weight column element.
$form['mytable'][$id]['weight'] = array(
'#type' => 'weight',
'#title' => t('Weight for @title', array('@title' => $entity->label())),
'#title_display' => 'invisible',
'#default_value' => $entity->get('weight'),
// Classify the weight element for #tabledrag.
'#attributes' => array('class' => array('mytable-order-weight')),
);
// Operations (dropbutton) column.
$form['mytable'][$id]['operations'] = array(
'#type' => 'operations',
'#links' => array(),
);
$form['mytable'][$id]['operations']['#links']['edit'] = array(
'title' => t('Edit'),
'url' => Url::fromRoute('mymodule.manage_edit', array('id' => $id)),
);
$form['mytable'][$id]['operations']['#links']['delete'] = array(
'title' => t('Delete'),
'url' => Url::fromRoute('mymodule.manage_delete', array('id' => $id)),
);
}
I have run dpm($form_state->getValue(['mytable','2']));
on submit for each of the rows, but only the operations and weight column values appear in $form_state. How do I get the id
and label
columns to appear?