media_save_pre
Filter the media form data before saving it to the database.
You need to have a working knowledge of Hooks before you get started.
Parameters
| Name | Type | Description |
|---|---|---|
$data |
parameter |
(array) form data |
$params |
parameter |
(array) associative array with contextual data |
Boilerplate Code
Use the boilerplate code to start using the filter, and add your own logic to modify the first argument and return it.
Clickfwd\Hook\Filter::add('media_save_pre', function($data, $params)
{
// Uncomment line below to view available data passed into hook
// fwd_dd($data, $params);
return $data;
});
Examples
Set First Photo Upload as Cover
Using the knowledge that the first photo is also automatically set as the main media, we can check if the uploaded media has been set as the main media and then also set it as cover.
Clickfwd\Hook\Filter::add('media_save_pre', function($data, $params)
{
if ( $params['is_new'] && $data['Media']['main_media'] == 1 )
{
$media['Media']['media_function'] = 'cover';
}
return $media;
}, 10);
Set First Photo Upload as Logo
Using the knowledge that the first photo is also automatically set as the main media, we can check if the uploaded media has been set as the main media and then also set it as logo.
Clickfwd\Hook\Filter::add('media_save_pre', function($data, $params)
{
if ( $params['is_new'] && $data['Media']['main_media'] == 1 )
{
$media['Media']['media_function'] = 'logo';
}
return $media;
}, 10);
Source Files
/models/media.php