2

For a Joomla 4 project I wrote a Content Plugin to grab video thumbs from Video-URLs (Vimeo) the user adds into Custom form field of a frontend form. The following code successfully gets the video thumbnail onContentBeforeSave(). But the manipulated $data object which holds all the form data, seems not get saved to the database. How to make sure the manipulated $data object gets saved properly?

class PlgContentVideothumbgrabber extends CMSPlugin
{
    public function onContentBeforeSave($context, $article, $isNew, $data)
    { 
        if(!in_array($context, array('com_content.form')))
        {
            return true;
        } 
        
        $vimeo = $data['com_fields']['pa-video-vimeo'];
    
        $thumbnail = $this->_grab_vimeo_thumbnail($vimeo, 'HIGH');
    
        // The $thumbnail holds video thumbnail correctly here.
        // How to save the manipulated $data object to database?
        $data['com_fields']['pa-video-screenshot'] = $thumbnail;
        
        return true;
    }

    public static function _grab_vimeo_thumbnail($vimeo_url, $thumbNamilQuality = '')
    {
         ...
    }
...
}

1 Answer 1

2

onContentBeforeSave is used to determine whether the item can be saved. The plugin should return a boolean value. The data, as you've noticed, is an array and is not passed by reference. So changes you make to it will not be reflected outside your plugin. You should consider using another event.

onContentAfterSave can be used to save additional data after the item has been successfully saved.

onContentPrepareForm can be used to alter form data. However, it can get tricky. Sometimes data can be an array and sometimes an object. Sometimes it may not be available at all and needs to be read from request data.

Since J! 3.8.7 there is a new onContentNormaliseRequestData event which solves the above issues. The data is always an object and can be modified.

There is also an older onUserBeforeDataValidation event (renamed to onContentBeforeValidateData in 4.0) which is fired almost right after onContentNormaliseRequestData. Data is cast to array but it is passed by reference so it can still be modified.

Additionally, you need to make sure the pa-video-screenshot field is part of the form definition. So there should be a dedicated custom field for it.

Finally, you need to consider whether your approach is the best way to go. There are other, potentially more reliable ways to do what you're trying to do. For example, writing a custom field type plugin instead of a content plugin. It could handle such functionality without having to hardcode specific field names inside and it could also handle the rendering of the video in frontend.

2
  • Is the onContentBeforeValidateData still there in Joomla 4.3? I cannot find it in the documentation?
    – cappu
    Commented Sep 10, 2023 at 14:44
  • 1
    Yes, it's there. There is an error in documentation, it should be onContentBeforeValidateData, not onContentValidateData.
    – Sharky
    Commented Sep 10, 2023 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.