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

How to install a library in a custom plugin?

Solved by Ole Vik View solution

Started by M 8 years ago · 9 replies · 1674 views
8 years ago

Hello,

i'd like to develop a plugin that reads an iCalendar/ics file and provide the events for further processing/displaying.

As a first step I have created the plugin skeleton as described in the tutorial here: https://learn.getgrav.org/plugins/plugin-tutorial

But I have absolutely no idea how I can add existing libraries to my new plugin as I only have very little experience with PHP and even less with things like composer.

I'd like to use either https://github.com/OzzyCzech/icalparser or https://github.com/u01jmg3/ics-parser. I already got the first one running in a simple PHP script:

PHP
<?php
    require ('src/IcalParser.php');
    $cal = new \om\IcalParser();
    $results = $cal->parseFile('events.ics');

    foreach ($cal->getSortedEvents() as $r) {
        echo sprintf('<li>%s - %s</li>'.PHP_EOL, $r['DTSTART']->format('j.n.Y'), $r['SUMMARY']);
    }

But how do I properly embed it in my plugin?

Best regards,
Michael

8 years ago

Use composer, and autoload the libraries (example). Then, in the events which you do the parsing, employ the libraries (example). Composer is just a library-manager, which generates a composer.json, and populates a vendor-folder.

8 years ago

Thank you very much for your reply.

I copied the whole folder of the library manually into /vendor in my plugin and added the line

require DIR . '/vendor/autoload.php';

at the top of my plugins PHP file (event-calendar.php).

When I reload any site now, I get the following error:

TXT
**Fatal error** : require(): Failed opening required 'xxx/user/plugins/event-calendar/vendor/autoload.php' (include_path='.:/usr/local/php72/share/php72') in **/xxx/user/plugins/event-calendar/event-calendar.php** on line **4**
8 years ago

Can you confirm that there is a autoload.php in /pluginname/vendor?

8 years ago

No there is no autoload.php. Where do I get that or where does it come from?

8 years ago

When you install with Composer, it creates the autoload.php and requisite folders beside it.

8 years ago

I tried to, but to be honest I have no idea how to do that correctly. I'm coming from programming in C and Python and therefore I have no idea how to use the composer.

Could you help me with the correct command line command? If I type

composer

in the command line it shows me

bash: composer: command not found

Shall I use the composer.phar in /bin or must it be installed by my hosting company? How do I tell it which package it shall install and where it shall be installed.

last edited 11/11/18 by M
8 years ago Solution

Since we have Composer available in Grav, in /bin as you note, you can use it from there. Say you're on the terminal, and in your plugin's folder (eg. cd /user/grav/user/plugins/pluginname).

You can write php /user/grav/bin/composer.phar require om/icalparser to install it to this folder. That will add it to composer.json, and the contents of the library to /user/grav/user/plugins/pluginname/vendor.

In most environments, you want it installed globally like PHP, but if the host makes that difficult just using composer.phar is sufficient.

👍 1
8 years ago

Thank you very much! That helped me and now the library was installed and seems to work as expected. I'll proceed now and come back if I have another issue.

7 years ago

Hi myscha,
I'm currently thinking about doing the same as you described here:
writing a Plugin to display Events from ics File(s) 😀 .
so, short question: is your Plugin finished/working ?
and, if so, available e.g. on GitHub ?

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 43 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