Score:-3

How to fill a form field of type fieldset with values from an array

cn flag

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

Score:0
us flag

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),
  /** ... **/
];
Steffen  avatar
cn flag
This works fine, but it created vor every value an own block, i want to list all tables in one form???
us flag
I'm assuming you mean one fieldset. If that's the case, I've updated the answer.
Score:0
us flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.