3

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)

8

To render the existing image, please use the below code

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

You need to give the fid in array format. Please try it and let me know whether you got the result

0

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;
  }
?>
  • How we can set multiple default value with remove button in custom form in Drupal 7 – steniya May 8 at 6:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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