How do I transfer data from a plugin to the server, where it will be added to a database?
I have a checkbox on the discussion editor, and when I click "Post Discussion" it should call my DiscussionWillBeSaved
listener. From there, I want to be able to access whether the checkbox is checked or not. How should I do this?
Currently, here's what I got on my javascript file on the client-side:
extend(DiscussionComposer.prototype, 'data', function(data) {
data.relationships = data.relationships || {};
data.relationships.emailNotification = true;
});
Note: for testing purposes, I purposely set emailNotification
to true.
Here's my PHP:
$events->listen(DiscussionWillBeSaved::class, function(DiscussionWillBeSaved $event) {
print_r($event);
flush();
});
Unfortunately, the javascript code won't even execute, throwing an error:
Uncaught TypeError: Cannot read property 'type' of undefined
So after a lot of debugging, reading the tags
extension source code, and a lot of messy code writing, here's my updated javascript file:
extend(DiscussionComposer.prototype, 'data', function(data) {
data.relationships = data.relationships || {};
data.relationships.emailNotification = [
{
data: {
type: "emailNotification",
sendEmail: true
}
}
];
});
which makes everything work - ish.
The output of the PHP file is:
...
[data] => Array
(
[type] => discussions
[attributes] => Array
(
[title] => test
[content] => test
)
[relationships] => Array
(
[emailNotification] => Array
(
[data] => Array
(
[0] => Array
(
[type] => emailNotification
)
)
)
[tags] => Array
(
[data] => Array
(
[0] => Array
(
[type] => tags
[id] => 1
)
)
)
)
)
...
As you can see, it gives the type of the emailNotification but doesn't give the true/false value.
How do I get this to work? What I want is similar to something like the tags
array, where it has a key of emailNotification
and a value of true/false
.
PS: Is there any documentation on this? I read the Extension/Plugin documentation on the Flarum homepage, but it doesn't seem to cover this... 🙁
EDIT: Never mind... I need to read the JSON-API docs now... 🙂