Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Plugins

Store and read data in grav

Solved by Andy Miller View solution

Started by Michael Gollmer 9 years ago · 7 replies · 6464 views
9 years ago

Hi there,

can anybody give an example of how to use "Grav\Common\Data". I want to store some information, but I can't find any example of how to use it. Can somebody help?

Regards,
Michael

9 years ago

I don't use Grav\Common\Data but this is a sample code I use in my plugins to store things in data folder:

PHP
            $data = 'This is my data';
            $filename = 'my-file.txt';
            $dirname = 'my-directory';

            $locator = $this->grav['locator'];
            $path = $locator->findResource('user://data', true);
            $dir = $path . DS . $dirname;
            $fullFileName = $dir. DS . $filename;

            $file = File::instance($fullFileName);
            $file->save($data);
9 years ago

Thanks for your response. But the way you are doing it is just writing to a file, or not? . I thought there is some kind of standard way to store the data and some sort of standard format(Json) that is provided by the API. :)

9 years ago

Of course it writes to a file in user/data folder. You can use pure PHP instead as long as it works for you.

As I know there is no standards format, it is up to you for what format is used. For example the code below stores a YAML file.

PHP
        $filename = 'my-file' . YAML_EXT;
        $dirname = 'my-directory';

        $myArray = ['key1' => 'value1', 'key2' => 'value2'];
        $data = new Data\Data($myArray);
        $data->set('key3', 'value3');

        $file = CompiledYamlFile::instance($this->grav['locator']->findResource("user://data/" . $dirname . "/" . $filename));
        $data->file($file);
        $data->save();

The content of user/data/my-directory/my-file.yaml should be:

YAML
key1: value1
key2: value2
key3: value3

You can store the files in JSON, Markdown, Yaml by using the classes in system/src/Grav/Common/File/ folder. I haven't used these classes as I only needed to store log files and CSV files.

👍 3
9 years ago

I do not really know that much about Grav code base other than it is very extensible. But I was also hoping for a "standard way" to write things like that. But I can see you are right, and you know what you are doing.

Could maybe you take a quick look at: https://github.com/bleutzinn/grav-plugin-add-page-by-form/blob/507be4be7ad2b94a4b403c070b99f7c03566a8b8/add-page-by-form.php#L302
From this post: /forum/forms-blueprints/get-form-data-select-input-and-add-it-to-page-frontmatter-t7216

I guess it is an easy fix for some one who understands the logic.

Thanks

👍 1

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 42 1 week ago
Plugins · by Xavier, 4 weeks ago
2 53 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1179 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 47 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 71 2 months ago