public function onDetectMediaType(string $fileJModel $model) : string

Fires while detecting the media type of a file.


Description

Trigger hook to allow external plugins to register their own media types or to extend the existing ones.

Take a look at the onCheckFileAllowed hook for a complete list with all the supported media types (also mentioned as categories).

This hook is useful when supporting new file types/categories without injecting them within the $model->allowedFiles property.

Let's take the following example.

public function onCheckFileAllowed($file, $model)
{
    if (preg_match("/\.(php|css|html|js)$/i", $file))
    {
        return true;
    }
}

Here the system is going to support the upload of 4 new file types (PHP, HTML, CSS and JS) without registering them within the list of supported files. At this point, we could use this hook to categorize them under the coding group.

public function onDetectMediaType($file, $model)
{
    if (preg_match("/\.(php|css|html|js)$/i", $file))
    {
        return 'coding';
    }
}

In case the media type is extracted from the array, this hook won't be triggered.

When it is not possible to detect a media type, the default "binary" one will be used.


Parameters

$file

(string)  The file path/name to check.

$model

(JModel)  The model instance that handles the saving process.

Return Value

String. The assigned media type.


Example

/**
 * Trigger hook to allow external plugins to register their own media types
 * or to extend the existing ones.
 *
 * @param   string  $file   The file name/path to check.
 * @param   JModel  $model  The model instance.
 *
 * @return  string  The detected file type.
 */
public function onDetectMediaType($file, $model)
{
    $type = null;

    if (preg_match("/\.web$/i", $file))
    {
        $type = 'image';
    }
    else if (preg_match("/\.avi$/i", $file))
    {
        $type = 'video';
    }

    return $type;
}

Changelog

VersionDescription
1.7 Introduced.
Last Update: 2021-10-08 13:51
Helpful?
This site uses cookies. By continuing to browse you accept their use. Further information