English (United Kingdom)
Knowledge Base  >  Vik Appointments  >  Hooks  >  Media  >  Check Allowed File

apply_filters( 'vikappointments_check_file_allowed', bool $allow, string $fileJModel $model )

Fires while checking whether the file to upload/delete is allowed.


Description

Trigger hook to allow the plugins to extend the validation of a specific file, in order to allow the upload of file types that are not supported by default or to allow the cancellation of specific files.

NOTE: the system checks whether the specified type of the file to upload is contained within the default list of allowed extensions, which are grouped in categories. Here's all the supported categories:

  • image - for images and pictures (such as .png);
  • video - for multimedia files (such as .mp4);
  • audio - for sounds and audio files (such as .mp3);
  • archive - for compressed archives (such as .zip);
  • document - for textual documents (such as .doc);
  • spreadsheet - for spreadsheet documents (such as .xls);
  • presentation - for presentation documents (such as .pps);
  • text - for plain text documents (such as .txt).

It is possible to support new categories by using the code below:

if (!array_key_exists('coding', $model->allowedFiles))
{
    // create new category
    $model->allowedFiles['coding'] = [];
}

// append new supported types
$model->allowedFiles['coding'][] = 'php';
$model->allowedFiles['coding'][] = 'xml';
$model->allowedFiles['coding'][] = 'html';
$model->allowedFiles['coding'][] = 'js';
$model->allowedFiles['coding'][] = 'css';

The supported extensions are REGEX compliant. The following example adds support for both CSS and SCSS at once. 

$model->allowedFiles['coding'][] = 's?css';

It is also possible to allow the upload with a simple IF statement.

if (preg_match("/\.webp/i", $file))
{
    return true;
}

NOTE: when it is needed to support new file types, it is appropriate to register them also within the vikappointments_before_save_media hook.


Parameters

$allow

(bool)  True to always allow the file upload, false to rely on the default algorithm.

$file

(string)  The file path/name to check.

$model

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


Example

The following example explains how to support new file extensions and how to drop support for a specific category.

/**
 * Trigger hook to allow the plugins to extend the validation of
 * a specific file, in order to support the upload of file types
 * that are not supported by default.
 *
 * @param  boolean  $allow  True to allow the file upload.
 * @param  string   $file   The file name/path to check.
 * @param  JModel   $model  The model instance.
 */
add_filter('vikappointments_check_file_allowed', function($allow, $file, $model)
{
    // add support for WebP extension
    $model->allowedFiles['image'] = 'webp';

    // drop support for audio files
    unset($model->allowedFiles['audio']);

    return $allow;
}, 10, 3);

Changelog

Version Description
1.2 Introduced.
Last Update: 2021-08-09
Helpful?
This site uses cookies. By continuing to browse you accept their use. Further information