3.4 3.3
Jump to table of contents

Extend a Map

Maps are used to convert data objects to another format. The most common use of maps in the application is to convert data objects to associative arrays, so that they can be compiled into JSON and returned in REST API results.

Plugins can extend a map to add data to the mapped output. This allows a plugin to integrate deeply with parts of the application like the REST API. For example, a plugin may want to add the number of days since an announcement was posted to the response data of the Announcements endpoint of the REST API.

Use the app('maps') helper to extend the Schema map of the Announcement class.

use PKP\announcement\Announcement;
use PKP\announcement\maps\Schema;

app('maps')->extend(
    Schema::class,
    function($output, Announcement $item, Schema $map) {
        $then = new DateTime($item->getData('datePosted'));
        $now = new DateTime();
        $output['daysSince'] = (int) $now->diff($then)->format('%a');

        return $output;
    }
);

REST API responses usually use a schema map. For example, the map for submissions is located at APP\submission\maps\Schema. Most entities use a similar pattern.


View more examples.