You are here

protected function FarmFieldFactory::modifyFileField in farmOS 2.x

File and image field modifier.

Parameters

\Drupal\Core\Field\BaseFieldDefinition &$field: A base field definition object.

array $options: An array of options.

1 call to FarmFieldFactory::modifyFileField()
FarmFieldFactory::buildFieldDefinition in modules/core/field/src/FarmFieldFactory.php
Builds a field definition with farmOS opinions.

File

modules/core/field/src/FarmFieldFactory.php, line 529

Class

FarmFieldFactory
Factory for generating farmOS field definitions.

Namespace

Drupal\farm_field

Code

protected function modifyFileField(BaseFieldDefinition &$field, array $options = []) {

  // Determine the upload directory.
  $file_directory = 'farm/[date:custom:Y]-[date:custom:m]';
  if (!empty($options['file_directory'])) {
    $file_directory = $options['file_directory'];
  }

  // Set field settings.
  $settings = [
    'file_directory' => $file_directory,
    'max_filesize' => '',
    'handler' => 'default:file',
    'handler_settings' => [],
  ];
  switch ($options['type']) {
    case 'file':
      $settings['file_extensions'] = 'csv doc docx gz geojson gpx kml kmz logz mp3 odp ods odt ogg pdf ppt pptx tar tif tiff txt wav xls xlsx zip';
      $settings['description_field'] = TRUE;
      break;
    case 'image':
      $settings['file_extensions'] = 'png gif jpg jpeg';
      $settings['max_resolution'] = '';
      $settings['min_resolution'] = '';
      $settings['alt_field'] = FALSE;
      $settings['alt_field_required'] = FALSE;
      $settings['title_field'] = FALSE;
      $settings['title_field_required'] = FALSE;
      $settings['default_image'] = [
        'uuid' => '',
        'alt' => '',
        'title' => '',
        'width' => NULL,
        'height' => NULL,
      ];
      break;
  }
  $field
    ->setSettings($settings);

  // Build form and view display settings.
  switch ($options['type']) {
    case 'file':
      $form_display_options = [
        'type' => 'file_generic',
        'settings' => [
          'progress_indicator' => 'throbber',
        ],
        'weight' => $options['weight']['form'] ?? 0,
      ];
      $view_display_options = [
        'type' => 'file_table',
        'label' => 'visually_hidden',
        'settings' => [
          'use_description_as_link_text' => TRUE,
        ],
        'weight' => $options['weight']['view'] ?? 0,
      ];
      break;
    case 'image':
      $form_display_options = [
        'type' => 'image_image',
        'settings' => [
          'preview_image_style' => 'medium',
          'progress_indicator' => 'throbber',
        ],
        'weight' => $options['weight']['form'] ?? 0,
      ];
      $view_display_options = [
        'type' => 'image',
        'label' => 'visually_hidden',
        'settings' => [
          'image_style' => 'large',
          'image_link' => 'file',
        ],
        'weight' => $options['weight']['view'] ?? 0,
      ];
      break;
  }
  $field
    ->setDisplayOptions('form', $form_display_options);
  $field
    ->setDisplayOptions('view', $view_display_options);
}