1

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?

3
  • 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.
    – mona lisa
    Commented Jul 28, 2023 at 22:09
  • @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 ?
    – David
    Commented Jul 28, 2023 at 22:27
  • 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.
    – mona lisa
    Commented Jul 29, 2023 at 2:41

2 Answers 2

1

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.

0

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.

3
  • I think what the question is doing is technically identical to this answer but with less code.
    – mona lisa
    Commented Jul 29, 2023 at 11:19
  • 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.
    – 4uk4
    Commented Jul 29, 2023 at 11:38
  • 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'.
    – avpaderno
    Commented Jul 30, 2023 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.