-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[4.1] Add some events when fetching media data #35396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
I have tested this item ✅ successfully on e5dc8dd This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/35396. |
|
I have tested this item ✅ successfully on 40c3b81 This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/35396. |
1 similar comment
|
I have tested this item ✅ successfully on 40c3b81 This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/35396. |
|
@laoneo I have thought about that thing with result for The event class, that have defined URL get/set: namespace Joomla\Component\Media\Administrator\Event;
class MediaFileUrlEvent extends AbstractEvent
{
public function __construct($name, array $arguments = array())
{
parent::__construct($name, $arguments);
// Check for required arguments
if (empty($arguments['url']) || !is_string($arguments['url']))
{
throw new BadMethodCallException("Argument 'url' of event $name is empty or not of the expected type");
}
}
public function getUrl(): string
{
return parent::getArgument('url');
}
public function setUrl(string $url)
{
parent::setArgument('url');
}
}Triggering the Event: $url = $this->getAdapter($adapter)->getUrl($path);
$event = new MediaFileUrlEvent('onFetchMediaFileUrl', ['url' => $url]);
Factory::getApplication()->getDispatcher()->dispatch(
$event->getName(),
$event
);
$newUrl = $event->getUrl();Plugin listener: public function onFetchMediaFileUrl(MediaFileUrlEvent $event)
{
$event->setUrl('blabla/my/url.jpg');
}As you see it more straightforward ;) @nibra @HLeithner please confirm, or maybe I wrong |
|
Regarding 'get result from the event instead of dispatcher': Events are usually just emitted without expecting (or waiting for) results. Other parts of the software or even other systems may or may not react on those events. Hooks are a technique to inject code - they get some input data and have a return value. Thus they can alter values or provide new data. In Joomla, we have no distinction between those two approaches. We just have plugins that might or might not return values. In Joomla, we're using events to communicate with the plugins. If a plugin returns a value, it is expected that the value is returned by the In the present case, both @laneo's and @Fedik's approaches are valid an can be considered 'clean', while @laoneo's is more 'Joomla like'. |
|
@nibra thanks for clarification |
f2699fe to
21be8a4
Compare
|
So boys, I hope now everybody is happy and we can get it merged. |
|
@laoneo I made PR to your branch, |
|
Merged |
* Class per event * Argument validations * global
|
Event calls are now the same across all methods. Can be tested now again. |
|
The event set functions need validators, Like is_array at least for setFiles and is_string for setUrls like in the constructor. Also the parameters must be Immutable (which is already done in the events). |
|
There are no set functions in the new events. |
in this case it makes no sense to use |
|
Re reading your opening post you say
doesn't that mean the plugins should/can change the event attributes? |
|
You can change by setArgument() which is a public function in the event. It is intended to give the plugin full control over the returned data. Better would be to make a proposal, instead of getting into an argument. All I want is to have these events in MM without over engineering the whole thing. |
|
@laoneo I think @HLeithner means something like there: joomla-cms/libraries/src/Event/Table/AbstractEvent.php Lines 44 to 61 in 3a5984d
It does not have So we have validation in both cases, in |
It's not about over engineering it's that the new event system should be clean and safe, that's the reason for validation. Fedir already wrote it. Adding would fit the needs (at least for the one plugin). |
|
@HLeithner there a caveat, public function setUrl(string $url)
{
if (empty(url))
{
throw new BadMethodCallException("Argument 'url' is empty");
}
parent::setArgument('url');
}This will lead to infinity loop ;) |
|
Events have been renamed. @HLeithner can you please fix your part please. |
|
@HLeithner can you please have a look on the errors? |
|
I have tested this item ✅ successfully on c42a582 This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/35396. |
|
All seems works now, @HLeithner please confirm when you will have a min. |
|
thanks @laoneo for fixing it your self and simplify the validation. thanks @Fedik for testing. |
|
I have tested this item ✅ successfully on c42a582 This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/35396. |
|
RTC This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/35396. |
|
I'm pretty sure, this should go into 4.1. |
|
Initially the changes have been small so it made sense at that time to go into 4.0. Now it makes more sense for 4.1. Rebased. |
|
If you allow maintainer to change your code, I could trigger the sync with the base branch and merge. So for you you have to do it @laoneo , could you please do it? |
|
@bembelimen , the maintainer checkbox works only for personal repos |
|
Ah ok, thx. |
|
Did already |
|
Thx |
Summary of Changes
In media manager we have events for the CUD operations but not for the read one. This pr adds the missing events.
During all events the objects can be manipulated, except the getUrl one. There it is possible for subscribers to provide a new url.
@wilsonge code review as there is nothing to test when all system tests are passing.
Testing Instructions
Open the media manager in the back end.
Actual result BEFORE applying this Pull Request
All is working as expected.
Expected result AFTER applying this Pull Request
All is working as expected.