-3

How can I fill a formfield type fieldset with more than one markup value from an array?

foreach ($drugis_postgis_connection->tableNames() as $table) {
    $form['tables'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Tables'),
      '#markup' => $table, 
      '#collapsible' => TRUE, // Added
      '#collapsed' => FALSE,  // Added
  ];

I don't need more fields i want to markup all values from the array in the field. Actually there is only one value, from the array displayed.


Sorry if my question is not clear, i want something like this.

enter image description here

0

If you overwrite $form['tables'] on each iteration, you're only going to end up with one tables on the form. Each element on the $form needs to have a unique name. Try something like this:

foreach ($drugis_postgis_connection->tableNames() as $key => $table) {

  $form['table' . $key] = [
    /** ... **/
  ];

}

If you want only one fieldset, then trying something like this:

$tables = $drugis_postgis_connection->tableNames();
$form['tables'] = [
  /** ... **/
  '#markup' => implode('', $tables),
  /** ... **/
];
2
  • This works fine, but it created vor every value an own block, i want to list all tables in one form???
    – Steffen
    Oct 13 at 15:49
  • I'm assuming you mean one fieldset. If that's the case, I've updated the answer. Oct 13 at 18:59
0

The code shown in the question doesn't work for two reasons.

  • The foreach loop is overriding $form['tables'] each time, with the effect that $form['tables']['#markup'] contains only the last table name found from $drugis_postgis_connection->tableNames()
  • A fieldset element doesn't use #markup

You need to use code similar to the following one.

$tables = $drugis_postgis_connection->tableNames();

$form['tables'] = [
  '#type' => 'fieldset',
  '#title' => $this->t('Tables'),
  '#collapsible' => TRUE,
  '#collapsed' => FALSE,
];

$form['tables']['list'] = [
  '#markup' => implode(',', $tables),
];

I assume $drugis_postgis_connection->tableNames() returns an array of strings; otherwise, implode(',', $tables) needs to be changed.

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.