0

I am creating a table form using the table element. I do not push sub elements into #rows. I work as with children elements. But valueCallback does not map values right. I am looking inside. It looks very strange. Especially:

return is_array($input) ? array_combine($input, $input) : [];

$input always is array. So combining result is weird with "array" key https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21Element%21Table.php/function/Table%3A%3AvalueCallback/8.5.x

public static function valueCallback(&$element, $input, FormStateInterface $form_state) {

  // If #multiple is FALSE, the regular default value of radio buttons is used.
  if (!empty($element['#tableselect']) && !empty($element['#multiple'])) {

    // Contrary to #type 'checkboxes', the default value of checkboxes in a
    // table is built from the array keys (instead of array values) of the
    // #default_value property.
    // @todo D8: Remove this inconsistency.
    if ($input === FALSE) {
      $element += [
        '#default_value' => [],
      ];
      $value = array_keys(array_filter($element['#default_value']));
      return array_combine($value, $value);
    }
    else {
      return is_array($input) ? array_combine($input, $input) : [];
    }
  }
}

For me $input already is ready to move into #value. It does not need altering here. I hope it works well and I do something wrong. But not it seems weird for. Clarify it, please.

0

My solution is below. It is a weird solution because It requires small changes.

Define a table value callback.

    $form['links'] = [
      '#type' => 'table',
      ...
      '#value_callback' => array($this, 'valueCallback'),
    ];

Implement the callback.

 /**
   * {@inheritdoc}
   */
  public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
    return $input;
  }

The data has structure as it has been sent. So the standard data processing works badly for my case.

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.