0

I've been writing a rather complicated module as of late. One of the steps I need to do is pre-populate one of the models with all of the data it needs to work right. This data is slowly replaced using a syncing system / rest calls from our database software, but it needs to be there initially so the end user doesn't think its 'all missing' before this sync happens.

I've been trying to use a csv file in my module directory. For the life of me, I can't figure out the combination of factories and methods to get that folder. I can't assume its going to be in ANY particular place though, since this module will end up on a CDN hosted platform.

I've tried injecting

\Magento\Framework\Module\Dir\ReaderFactory
\Magento\Framework\App\Filesystem\DirectoryListFactory

as well as direct php

__DIR__
dir()
dirname()
$_SERVER (ugh)

None of this can figure out

/var/www/app/code/mycompname/modulename/data.csv

while developing.. and

/var/www/vendor/mycompname/modulename/data.csv

when testing the composer distribution.

Any suggestions are welcome. I need to load the full path to use something like this....

$csv = file($pathThatWasFound);

EDIT: I read http://alanstorm.com/magento_base_directories <--- based on 1.x magento. I need 2.0 suggestions

EDIT: I may be bumping into constraints of FILE magic and xdebug (https://stackoverflow.com/questions/4924676/how-can-i-get-the-php-magic-constant-file-work-with-eclipse-and-pdt)

0

The magento2 sample data codebase has good examples of how this is done.

They define the install function in the model to populate the database.

This function is called inside the <vendor_name>\<module_name>\Installer class which is called inside the <vendor_name>\<module_name>\InstallData class.

  • I know how the installer works. Thanks though. I guess I should have mentioned i'm running this function from InstallData.php – CarComp Apr 28 '16 at 19:52
0

I figured out what I was doing wrong...

https://stackoverflow.com/questions/5497441/generic-path-to-a-module-in-magento

I need to use the module namespace as well as the module name on getModuleDir()

$readerFactory->getModuleDir('','YourNameSpace_YourExtension')

This gives me the root path to my module.

My final exact copy of this thing is as follows:

$csv = array_map('str_getcsv',file($readerFactory->getModuleDir('','Stti_Healthday') . '/assignments.csv'))
0

Depending on what you are doing there are other options

https://github.com/DominicWatts/CsvUpload/blob/446ba3c80fc7f9136e7bdf6bda859c0e5d327ed0/view/adminhtml/templates/index/submit.phtml#L2

<a href="<?= $block->escapeUrl($block->getViewFileUrl('Xigen_CsvUpload::template.csv'));?>" target="_blank"><?= $block->escapeHtml(__("CSV Template")); ?></a>

So file path is

vendor/module/view/adminhtml|frontend/web/template.csv

Example

https://github.com/DominicWatts/CsvUpload/blob/master/view/adminhtml/web/template.csv

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.