1.20.x file.module _file_types_build()

Builds and returns the list of available file types.

This function reads from disk individual config files that define the available content types in the system. Modules may bundle a file type with their module by including a "file.type.[type_name].json" config file in their module's config directory. Each file should contain:

  • type: (required)
  • name: (required) The human-readable name of the file type.
  • description: (required) A brief description of the file type.
  • mimetypes: (required)

Return value

An object with two properties::

  • names: Associative array of the names of file types, keyed by the type.
  • types: Associative array of file type objects, keyed by the type.

These arrays will also include obsolete types: types that were previously defined by modules that have been disabled, likely by a module that was provided a file type and then was disabled. These are indicated in the type object by $type->disabled being set to TRUE.

File

modules/file/file.module, line 2375
Defines a "managed_file" Form API field and a "file" field for Field module.

Code

function _file_types_build() {
  $cid = 'file_types:' . $GLOBALS['language']->langcode;

  $_file_types = &backdrop_static(__FUNCTION__);
  if (isset($_file_types)) {
    return $_file_types;
  }
  if ($cache = cache()->get($cid)) {
    $_file_types = $cache->data;
    return $_file_types;
  }

  $_file_types = (object) array('types' => array(), 'names' => array());
  module_load_include('inc', 'file', 'file.admin');

  $config_names = config_get_names_with_prefix('file.type.');
  foreach ($config_names as $config_name) {
    $file_type_data = config($config_name)->get();

    // Check if the file type is disabled or the module is enabled.
    $module = $file_type_data['module'];
    $module_disabled = TRUE;
    if ($module === 'file' || module_exists($module)) {
      $module_disabled = FALSE;
    }

    // If the module is enabled, $disabled is forced to TRUE.
    $file_type_data['disabled'] = !empty($file_type_data['disabled']) || $module_disabled;

    $type = str_replace('file.type.', '', $config_name);
    $_file_types->types[$type] = (object) $file_type_data;
    $_file_types->names[$type] = $file_type_data['name'];
  }

  // Allow modules to load their defaults into the file type info.
  foreach (module_implements('file_type_load') as $module) {
    $function = $module . '_file_type_load';
    $function($_file_types->types);
  }

  cache()->set($cid, $_file_types);

  return $_file_types;
}