1 file.install file_update_1006()

Add the default file display settings and convert saved file display settings from Drupal 7 file_entity.

File

core/modules/file/file.install, line 995
Install, update and uninstall functions for File module.

Code

function file_update_1006() {
  $default_file_displays = array(
    'audio' => array(
      'type' => 'audio',
      'default' => array(
        'formatter' => 'file_field_file_audio',
        'settings' => array(
          'file_field_file_audio' => array(
            'controls' => 1,
            'autoplay' => 0,
            'loop' => 0,
            'preload' => '',
            'multiple_file_behavior' => 'tags',
          ),
        ),
      ),
      'preview' => array(
        'formatter' => 'file_field_file_audio',
        'settings' => array(
          'file_field_file_audio' => array(
            'controls' => 1,
            'autoplay' => 0,
            'loop' => 0,
            'preload' => '',
            'multiple_file_behavior' => 'tags',
          ),
        ),
      ),
    ),
    'document' => array(
      'type' => 'document',
      'default' => array(
        'formatter' => 'file_field_file_download_link',
        'settings' => array(
          'file_field_file_download_link' => array(
            'text' => 'Download [file:name]',
          ),
        ),
      ),
      'preview' => array(
        'formatter' => 'file_field_file_download_link',
        'settings' => array(
          'file_field_file_download_link' => array(
            'text' => 'Download [file:name]',
          ),
        ),
      ),
    ),
    'image' => array(
      'type' => 'image',
      'default' => array(
        'formatter' => 'file_field_image',
        'settings' => array(
          'file_field_image' => array(
            'image_style' => '',
            'image_link' => '',
            'image_float' => '',
          ),
        ),
      ),
      'preview' => array(
        'formatter' => 'file_field_image',
        'settings' => array(
          'file_field_image' => array(
            'image_style' => 'thumbnail',
            'image_link' => '',
            'image_float' => '',
          ),
        ),
      ),
    ),
    'video' => array(
      'type' => 'video',
      'default' => array(
        'formatter' => 'file_field_file_video',
        'settings' => array(
          'file_field_file_video' => array(
            'controls' => 1,
            'autoplay' => 0,
            'loop' => 0,
            'muted' => 0,
            'width' => '',
            'height' => '',
            'preload' => '',
            'multiple_file_behavior' => 'tags',
          ),
        ),
      ),
      'preview' => array(
        'formatter' => 'file_field_file_video',
        'settings' => array(
          'file_field_file_video' => array(
            'controls' => 1,
            'autoplay' => 0,
            'loop' => 0,
            'muted' => 0,
            'width' => '',
            'height' => '',
            'preload' => '',
            'multiple_file_behavior' => 'tags',
          ),
        ),
      ),
    ),
  );
  foreach ($default_file_displays as $machine => $display) {
    $config = config('file_display.' . $machine);
    if ($config->isNew()) {
      $config->setData($display);
      $config->save();
    }
  }

  if (db_table_exists('file_display')) {
    $types = array();
    $formatter_weights = array();
    $result = db_query("SELECT name, weight, settings FROM {file_display} WHERE status = 1")->fetchAll();
    foreach ($result as $record) {
      $name_parts = explode('__', $record->name);
      $filetype = $name_parts[0];
      $view_mode = $name_parts[1];
      $formatter = $name_parts[2];

      $settings = unserialize($record->settings);
      $types[$filetype][$view_mode]['settings'][$formatter] = $settings;

      $formatter_weights[$filetype][$view_mode][$formatter] = $record->weight;
    }

    foreach ($types as $filetype => $view_modes) {
      $config = config('file_display.' . $filetype);
      $config->set('type', $filetype);

      foreach ($view_modes as $name => $settings) {
        // Pick one formatter for each view mode. Select by lowest weight.
        $weight_formatters = array_flip($formatter_weights[$filetype][$name]);
        ksort($weight_formatters);
        $formatter = reset($weight_formatters);

        $view_mode_settings = array(
          'formatter' => $formatter,
          'settings' => $settings['settings'],
        );
        $config->set($name, $view_mode_settings);
      }

      $config->save();
    }

    // Delete the file type table.
    db_drop_table('file_display');
  }
}