Skip to main content

I am have been stugglingstruggling with this for the past day or so. I am simply trying to create a custom entity and populate/or update the image field with the fid. Whats weird is that every other form field iI am passing iI can get the value for but this managed_file field for uploading an image always returns NULL when I call dpm().

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;

Within my form_build function, I use the following code.

// Manage file.
$form['theimage'] = array(
  '#type' => 'managed_file',
  '#title' => 'Managed file',
  '#description' => $this->t('Manage file, #type = managed_file'),
  //'#default_value' => $form_state->getValue('theimage'),
  '#upload_location' => 'public://images/',
  '#required' => FALSE,
);

In the submission handler, I use the following code.

// Get the fid value, upload it and save it to the managed file table. 
$fid = $form_state->getValue(['theimage', 0]);
dpm($fid);
if (!empty($fid)) {
  $file = File::load($fid);
  $file->setPermanent();
  $file->save(); 
}

Any thoughts, solutions, or recommendations would be greatly appreciated.

I am have been stuggling with this for the past day or so. I am simply trying to create a custom entity and populate/or update the image field with the fid. Whats weird is that every other form field i am passing i can get the value for but this managed_file field for uploading an image always returns NULL when I call dpm().

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;

Within my form_build function I use the following code.

// Manage file.
$form['theimage'] = array(
  '#type' => 'managed_file',
  '#title' => 'Managed file',
  '#description' => $this->t('Manage file, #type = managed_file'),
  //'#default_value' => $form_state->getValue('theimage'),
  '#upload_location' => 'public://images/',
  '#required' => FALSE,
);

In the submission handler, I use the following code.

// Get the fid value, upload it and save it to the managed file table. 
$fid = $form_state->getValue(['theimage', 0]);
dpm($fid);
if (!empty($fid)) {
  $file = File::load($fid);
  $file->setPermanent();
  $file->save(); 
}

Any thoughts, solutions, or recommendations would be greatly appreciated.

I am have been struggling with this for the past day or so. I am simply trying to create a custom entity and populate/or update the image field with the fid. Whats weird is that every other form field I am passing I can get the value for but this managed_file field for uploading an image always returns NULL when I call dpm().

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;

Within my form_build function, I use the following code.

// Manage file.
$form['theimage'] = array(
  '#type' => 'managed_file',
  '#title' => 'Managed file',
  '#description' => $this->t('Manage file, #type = managed_file'),
  //'#default_value' => $form_state->getValue('theimage'),
  '#upload_location' => 'public://images/',
  '#required' => FALSE,
);

In the submission handler, I use the following code.

// Get the fid value, upload it and save it to the managed file table. 
$fid = $form_state->getValue(['theimage', 0]);
dpm($fid);
if (!empty($fid)) {
  $file = File::load($fid);
  $file->setPermanent();
  $file->save(); 
}

Any thoughts, solutions, or recommendations would be greatly appreciated.

edited title; added 6 characters in body; added 7 characters in body
Source Link
leymannx
  • 18.8k
  • 6
  • 72
  • 126

"manage file" "managed_file" form field type returns null value in my form submission

I am have been stuggling with this for the past day or so. I am simply trying to create a custom entity and populate/or update the image field with the fid. Whats weird is that every other form field i am passing i can get the value for but this managed_file field for uploading an image always returns NULL when I call dpm().

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;

Within my form_build function I use the following code.

// Manage file.
$form['theimage'] = array(
  '#type' => 'managed_file',
  '#title' => 'Managed file',
  '#description' => $this->t('Manage file, #type = managed_file'),
  //'#default_value' => $form_state->getValue('theimage'),
  '#upload_location' => 'public://images/',
  '#required' => FALSE,
);

In the submission handler, I use the following code.

// Get the fid value, upload it and save it to the managed file table. 
$fid = $form_state->getValue(['theimage', 0]);
dpm($fid);
if (!empty($fid)) {
  $file = File::load($fid);
  $file->setPermanent();
  $file->save(); 
}

Any thoughts, solutions, or recommendations would be greatly appreciated.

"manage file" form field type returns null value in my form submission

I am have been stuggling with this for the past day or so. I am simply trying to create a custom entity and populate/or update the image field with the fid. Whats weird is that every other form field i am passing i can get the value for but this managed_file field for uploading an image always returns NULL when I call dpm().

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;

Within my form_build function I use the following code.

// Manage file.
$form['theimage'] = array(
  '#type' => 'managed_file',
  '#title' => 'Managed file',
  '#description' => $this->t('Manage file, #type = managed_file'),
  //'#default_value' => $form_state->getValue('theimage'),
'#upload_location' => 'public://images/',
'#required' => FALSE,
);

In the submission handler, I use the following code.

// Get the fid value, upload it and save it to the managed file table. 
$fid = $form_state->getValue(['theimage', 0]);
dpm($fid);
if (!empty($fid)) {$file = File::load($fid);
 $file->setPermanent();
 $file->save(); 
}

Any thoughts, solutions, or recommendations would be greatly appreciated.

"managed_file" form field type returns null value in my form submission

I am have been stuggling with this for the past day or so. I am simply trying to create a custom entity and populate/or update the image field with the fid. Whats weird is that every other form field i am passing i can get the value for but this managed_file field for uploading an image always returns NULL when I call dpm().

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;

Within my form_build function I use the following code.

// Manage file.
$form['theimage'] = array(
  '#type' => 'managed_file',
  '#title' => 'Managed file',
  '#description' => $this->t('Manage file, #type = managed_file'),
  //'#default_value' => $form_state->getValue('theimage'),
  '#upload_location' => 'public://images/',
  '#required' => FALSE,
);

In the submission handler, I use the following code.

// Get the fid value, upload it and save it to the managed file table. 
$fid = $form_state->getValue(['theimage', 0]);
dpm($fid);
if (!empty($fid)) {
  $file = File::load($fid);
  $file->setPermanent();
  $file->save(); 
}

Any thoughts, solutions, or recommendations would be greatly appreciated.

improved grammar
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

Drupal 8 Manage_file "manage file" form field type returns null value in my form submission

I am have been stuggling with this for the past day or so. I am simply trying to create a custom entity and populate/or update the image field with the fid. Whats weird is that every other form field i am passing i can get the value for but this managed_file field for uploading an image always returns NULL when i do a dpm();I call dpm().

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;

Within my form_build function i have:I use the following code.

// Manage file.
$form['theimage'] = array(
  '#type' => 'managed_file',
  '#title' => 'Managed file',
  '#description' => $this->t('Manage file, #type = managed_file'),
  //'#default_value' => $form_state->getValue('theimage'),
'#upload_location' => 'public://images/',
'#required' => FALSE,
);

Within my submit function i have:In the submission handler, I use the following code.

// Get the fid value, upload it and save it to the managed file table. 
$fid = $form_state->getValue(['theimage', 0]);
dpm($fid);
if (!empty($fid)) {$file = File::load($fid);
 $file->setPermanent();
 $file->save(); 
}

Any thoughts, solutions, or recommendations would be greatly appreciated. Thanks.

Drupal 8 Manage_file form field type returns null value in my form submission

I am have been stuggling with this for the past day or so. I am simply trying to create a custom entity and populate/or update the image field with the fid. Whats weird is that every other form field i am passing i can get the value for but this managed_file field for uploading an image always returns NULL when i do a dpm();

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;

Within my form_build function i have:

// Manage file.
$form['theimage'] = array(
  '#type' => 'managed_file',
  '#title' => 'Managed file',
  '#description' => $this->t('Manage file, #type = managed_file'),
  //'#default_value' => $form_state->getValue('theimage'),
'#upload_location' => 'public://images/',
'#required' => FALSE,
);

Within my submit function i have:

// Get the fid value, upload it and save it to the managed file table. 
$fid = $form_state->getValue(['theimage', 0]);
dpm($fid);
if (!empty($fid)) {$file = File::load($fid);
 $file->setPermanent();
 $file->save(); 
}

Any thoughts, solutions, or recommendations would be greatly appreciated. Thanks.

"manage file" form field type returns null value in my form submission

I am have been stuggling with this for the past day or so. I am simply trying to create a custom entity and populate/or update the image field with the fid. Whats weird is that every other form field i am passing i can get the value for but this managed_file field for uploading an image always returns NULL when I call dpm().

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;

Within my form_build function I use the following code.

// Manage file.
$form['theimage'] = array(
  '#type' => 'managed_file',
  '#title' => 'Managed file',
  '#description' => $this->t('Manage file, #type = managed_file'),
  //'#default_value' => $form_state->getValue('theimage'),
'#upload_location' => 'public://images/',
'#required' => FALSE,
);

In the submission handler, I use the following code.

// Get the fid value, upload it and save it to the managed file table. 
$fid = $form_state->getValue(['theimage', 0]);
dpm($fid);
if (!empty($fid)) {$file = File::load($fid);
 $file->setPermanent();
 $file->save(); 
}

Any thoughts, solutions, or recommendations would be greatly appreciated.

Source Link
cthrucode
  • 155
  • 1
  • 1
  • 10
Loading