0

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?

  • You stated a lot of facts, but you didn't actually ask any question. – Mołot Jan 5 '16 at 23:25
  • sorry!! editing now – matt9292 Jan 5 '16 at 23:41
  • Please, can someone help with this or at least point me to an example of a tabledrag submit function? I can't find any for Drupal 8 – matt9292 Jan 8 '16 at 21:32
0

The answer is quite simple - markup doesn't make it into $form_state. Place the item id ($entity->ref in the below code) in a textfield or number field instead and use the following to hide and disable it (not sure if disabling is necessary):

    $form['mytable'][$id]['label'] = array(
    //'#plain_text' => $entity->ref,
    '#type' => 'number',
    '#title' => "",
    '#default_value' => $entity->ref,
    '#disabled' => TRUE,
    '#access' => FALSE,

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.