Skip to content
This repository has been archived by the owner on Jul 6, 2020. It is now read-only.

How to write Plugins

Raydiation edited this page Sep 18, 2014 · 27 revisions

How to write plugins for the News app

You've got this cool idea that you'd like the News app to have but the developers are pricks and don't want to implement it? Create a plugin!

General plugin infos

A plugin is in essence a seperate app. You should first read the intro and tutorial and create the basic files.

In addition to the basic structure you also want to make sure that the News app is enabled. To do that open mynewsplugin/appinfo/app.php and add the following if:

	<?php 	
	namespace MyNewsPlugin\AppInfo;

	if (\OCP\App::isEnabled('news'))) {

		// your code here

	}

Serverside plugin

A serverside plugin is a plugin that uses the same infrastructure as the news app for its own purposes. An example would be a plugin that makes the starred entries of a user available via an interface or a bookmark app that that also shows starred articles as bookmarks.

Its very easy to interface with the News app. Because all Classes are registered in the news/app/application.php it takes almost no effort to use the same infrastructure.

Since you dont want to extend the app but use its resources, its advised that you dont inherit from the App class but rather include it in your own container in mynewsplugin/appinfo/application.php:

    <?php 
    namespace OCA\MyNewsPlugin\AppInfo;

    use \OCP\AppFramework\App;
    use \OCA\News\AppInfo\Application as News;

    class Application extends App {


        /**
         * Define your dependencies in here
         */
        public function __construct (array $urlParams=array()) {
            parent::__construct('mynewsplugin', $urlParams);

            $container = $this->getContainer();

            $container->registerService('NewsContainer', function($c) {
                return new News();
            });

            $container->registerService('YourController', function($c) {
                // use the feed service from the news app
                // you can use all defined classes but its recommended that you 
                // stick to the mapper and service classes since they are less
                // likely to change
                // also dont use controllers from the news app!
                return new YourController($c->query('NewsContainer')->query('FeedService'));
            });
        }

        
    }

Using this method you can basically access all the functionality of the news app in your own app.

Clientside plugin

A clientside plugin could either be a new interface for the news app or a script that enhances the current app.

Custom script

To add a simple script create the script in the my_news_plugin/js/script.js, then use this inside your my_news_plugin/appinfo/app.php:

	
	<?php 
	namespace MyNewsPlugin\AppInfo;

	if (\OCP\App::isEnabled('news')) {
		\OCP\Util::addScript('mynewsplugin', 'script.js'); // add a script from js/script.js
		\OCP\Util::addStyle('mynewsplugin', 'style.css'); // add a stylesheet from css/styles.css
	}

Inside your script you have to make sure that the News app is active. You can do that by using:

	
	(function ($, window, undefined) {
		'use strict';

		$(window.document).ready(function () {
			if ($('[ng-app="News"]').length > 0) {

				// your code here

			}
		});

	})(jQuery, window);
Clone this wiki locally