7

I created an edit custom form (of a node) via a custom module. From this form, I have a "managed file" field (for one picture) and I set his default value to a fid; However, when my form is rendered, the "managed file" field doesn't show the image ratached. I don't understand why

My code :

$form['project']['image'] = array(
  '#type' => 'managed_file',
  '#title' => t('Image'),
  '#default_value' => 10, //Set 10 for the test, the fid "10" exist, I checked
);

My form rendered:

enter image description here

As you see, no image is loaded whereas the default_value is set to an existing fid (10)

3 Answers 3

17

For a managed file form element, #default_value is an array containing the file ID, as in the following code.

$form['project']['image'] = array(
  '#type' => 'managed_file',
  '#title' => t('Image'),
  '#default_value' => array(10),
);
1

In My case i needed to add default files to a multiple managed file field element in drupal 8. my filed name was "field_dpe_submission_file"

  $fids = [
      10,
      11,
      12,
    ];

function setDefaultsForFileField(array &$form, $fids) {
    $widget_clone = $form['field_dpe_submission_file']['widget'][0];
    $form['field_dpe_submission_file']['widget']['#file_upload_delta'] = count($fids);
    $form['field_dpe_submission_file']['widget']['#value'] = null;
    $form['field_dpe_submission_file']['widget']['#defaults_loaded'] = TRUE;
    foreach ($fids as $key => $value) {
      $value = (int) $value;
      $form['field_dpe_submission_file']['widget'][$key] = $widget_clone;
      $file_array = [
        'fids' => [
          $value,
        ],
        'display' => '1',
        'description' => '',
        'upload' => ''
      ];
      $form['field_dpe_submission_file']['widget'][$key]['#value'] = $file_array;
      $form['field_dpe_submission_file']['widget'][$key]['#default_value'] = $file_array;
      $form['field_dpe_submission_file']['widget'][$key]['#input'] = TRUE;
      $form['field_dpe_submission_file']['widget'][$key]['#delta'] = $key;
      $name = 'field_dpe_submission_file[' . $key . ']';
      $form['field_dpe_submission_file']['widget'][$key]['#name'] = $name;
      $form['field_dpe_submission_file']['widget'][$key]['#theme'] = 'file_managed_file';
      $form['field_dpe_submission_file']['widget'][$key]['#multiple'] = FALSE;
      $form['field_dpe_submission_file']['widget'][$key]['fids'] = [
        '#type' => 'hidden',
        '#value' => [
          $value,
        ],
        '#theme' => 'input__hidden'
      ];
    }
    // Add the default widget back in.
    $count = count($fids);
    $form['field_dpe_submission_file']['widget'][$count] = $widget_clone;
  }
?>
0
0

Simple settings form with Managed file field with default value:

Config file form:

public function buildForm(array $form, FormStateInterface $form_state) {
 $config = $this->config('bk.settings');

$form['static'] = [
  '#type' => 'details',
  '#title' => t('Static Files'),
  '#open' => TRUE,
];

$form['static']['upload'] = [
  '#type' => 'managed_file',
  '#title' => t('File Upload.'),
  '#progress_indicator' => 'bar',
  '#progress_message'   => t('Please wait...'),
  '#upload_location' => 'public://static',
  '#description' => 'Allowed formats: jpg img png svg woff woff2, File location: public://static',
  '#upload_validators'  => [
    'file_validate_extensions' => ['jpg img png svg woff woff2'],
  ],
  '#default_value' => $config->get('upload'),
  '#multiple' => TRUE,
]; 
}

Form Submit:

public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);

if ($file_id = $form_state->getValue(['upload', '0'])) {
  $file = File::load($file_id);
  $file->setPermanent();
  $file->save();
}

  $config = $this->config('bk.settings');
  $config->set('upload', $form_state->getValue('upload'));
  $config->save();  
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.