Score:1

Can I populate a select form element with options in separate file?

sy flag

I have a text file which is a list of 100s of rows. Which I am trying to turn in to a select list.

I have created a module, in mymodule/src/form/mymoduleform.php. I am trying to load the items from the file and turn the file content into an array which can be used as options for the form element.

The form element is created by the following code.

$form['mymodule_select'] = [
  '#type' => 'select',
  '#title' => $this->t('Select element'),
  '#options' =>  selectlist_options(),
];

selectlist_options() is implemented using the following code.

function selectlist_options() {
  $options = [];
  $result = file('options.txt');

  foreach ($result as $key => $value) {
    $options[$key] = $options['name'];
  }

  return $options;
}

Is this the correct approach?

id flag
I deleted my comments on reflection that this code actually could work. There is no way to answer the question however. We don’t know what is correct or what you should do because we don’t know the requirements.
David avatar
sy flag
@cilefen, thanks for commenting. All I want is to put a text file anywhere in my Drupal install, and be able to use that text file to populate the options in my select list. I was thinking something like mymodule/library/options.txt ? or would it be mymodule/inc/options.txt ?
id flag
What you are doing isn’t quite standard so this is a matter of opinion. Sorry, that’s not something this site is set up for.
Score:1
us flag

Is this the correct approach?

The code shown in the question needs to be first fixed: In the foreach() loop, it does not set $options[$key] to any value read from the file because it does not use $value.

Supposing that the options.txt file is contained in the same directory containing the .module file, the code to get the file path is the one shown by @4uk4.

The code you should use for selectlist_options() becomes the following one.

function selectlist_options() {
  $options = [];
  $result = file('options.txt');

  foreach ($result as $key => $value) {
    $options[$key] = $value;
  }

  return $options;
}

On Drupal, strings used in the user interface must be translatable, while strings read from a file are not translatable. Furthermore, the files in the directory containing modules cannot be altered by the web server nor PHP. This means that, if the options.txt file is in the directory containing the module, its content is probably changed when the module is updated.
Instead of using a text file that is updated when the module is updated, it would be better to set directly the options with the code that initializes $form['mymodule_select']. In this case, the options would be translatable too.

Score:0
cn flag

To read the file mymodule/options.txt in PHP use:

Drupal 8 + 9:

$file_path = drupal_get_path('module', 'mymodule') . '/options.txt';
$result = file($file_path);

Drupal 10 (works since Drupal 9.3):

$file_path = \Drupal::service('extension.list.module')->getPath('mymodule') . '/options.txt';
$result = file($file_path);

See change record https://www.drupal.org/node/2940438.

id flag
I think what the question is doing is technically identical to this answer but with less code.
4uk4 avatar
cn flag
The OP tried to put the file anywhere so that `file('options.txt')` is working, which is not possible. You don't need to move the file, you need more code to get it working and then the position of the file within the module doesn't matter, you can read any file.
apaderno avatar
us flag
Since the question is about a module and it does not explicitly say where the file is located, we can assume the file is part of the module. In this case, this answer is correct. If then the file is not in the same directory containing the .module file, but still part of the module files, the OP just need to appropriately change `'/options.txt'`.
I sit in a Tesla and translated this thread with Ai:

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.