Skip to main content
code indentation
Source Link
berliner
  • 2.9k
  • 1
  • 22
  • 29
public function buildForm(array $form, FormStateInterface $form_state) {   $config = $this->config('foundation.settings');   $form['icons'] = [   '#type'  => 'details',   '#title'  => t('Front Page Icons'),   '#open'  => FALSE,   'icon1'  => [   '#type'  => 'fieldset',   '#title'  => t('First Icon'),   'icon1_file'  => [   '#type'  => 'managed_file',   '#name'  => 'icon1',   '#title'  => t('First Icon Image'),   '#default_value'  => $config->get('icon1_file'),   '#upload_location' => 'public://',   ],   ],   ]; } 
public function submitForm(array&$formarray &$form, FormStateInterface $form_state) {   $imageid = $form_state->getValue('icon1_file');   $file = file_load($imageid[0]);   if (gettype($file) == 'object') {   $file->status = FILE_STATUS_PERMANENT;   }   $this->config('foundation.settings')   ->set('icon1_file', $form_state->getValue('icon1_file'))   ->save();   $file->save(); } 
public function buildForm(array $form, FormStateInterface $form_state) {   $config = $this->config('foundation.settings');   $form['icons'] = [   '#type'  => 'details',   '#title'  => t('Front Page Icons'),   '#open'  => FALSE,   'icon1'  => [   '#type'  => 'fieldset',   '#title'  => t('First Icon'),   'icon1_file'  => [   '#type'  => 'managed_file',   '#name'  => 'icon1',   '#title'  => t('First Icon Image'),   '#default_value'  => $config->get('icon1_file'),   '#upload_location' => 'public://',   ],   ],   ]; } 
public function submitForm(array&$form, FormStateInterface $form_state) {   $imageid = $form_state->getValue('icon1_file');   $file = file_load($imageid[0]);   if (gettype($file) == 'object') {   $file->status = FILE_STATUS_PERMANENT;   }   $this->config('foundation.settings')   ->set('icon1_file', $form_state->getValue('icon1_file'))   ->save();   $file->save(); } 
public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config('foundation.settings'); $form['icons'] = [ '#type' => 'details', '#title' => t('Front Page Icons'), '#open' => FALSE, 'icon1' => [ '#type' => 'fieldset', '#title' => t('First Icon'), 'icon1_file' => [ '#type' => 'managed_file', '#name' => 'icon1', '#title' => t('First Icon Image'), '#default_value' => $config->get('icon1_file'), '#upload_location' => 'public://', ], ], ]; } 
public function submitForm(array &$form, FormStateInterface $form_state) { $imageid = $form_state->getValue('icon1_file'); $file = file_load($imageid[0]); if (gettype($file) == 'object') { $file->status = FILE_STATUS_PERMANENT; } $this->config('foundation.settings') ->set('icon1_file', $form_state->getValue('icon1_file')) ->save(); $file->save(); } 
Tweeted twitter.com/StackDrupal/status/1261401029855281154
added 18 characters in body
Source Link
Amy
  • 5.9k
  • 14
  • 69
  • 133

The upload and saving works okay but I keep receiving thethis error after submitting

The upload and saving works okay but I keep receiving the error

The upload and saving works okay but I keep receiving this error after submitting

Source Link
Amy
  • 5.9k
  • 14
  • 69
  • 133

How to use the managed_file field in a custom form plugin

I'm attempting to create a module with a custom form that allows users to upload a file using a managed_file field. The name of my module is foundation_settings and the file structure is:

foundation_settings -foundation_settings.info.yml -foundation_settings.module -src -Form -FoundationForm.php 

Inside FoundationForm.php I'm building the form with the following code:

public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config('foundation.settings'); $form['icons'] = [ '#type' => 'details', '#title' => t('Front Page Icons'), '#open' => FALSE, 'icon1' => [ '#type' => 'fieldset', '#title' => t('First Icon'), 'icon1_file' => [ '#type' => 'managed_file', '#name' => 'icon1', '#title' => t('First Icon Image'), '#default_value' => $config->get('icon1_file'), '#upload_location' => 'public://', ], ], ]; } 

However I'm not sure how to handle the managed_file when the form is being submitted. In my submitForm function I have

public function submitForm(array&$form, FormStateInterface $form_state) { $imageid = $form_state->getValue('icon1_file'); $file = file_load($imageid[0]); if (gettype($file) == 'object') { $file->status = FILE_STATUS_PERMANENT; } $this->config('foundation.settings') ->set('icon1_file', $form_state->getValue('icon1_file')) ->save(); $file->save(); } 

The upload and saving works okay but I keep receiving the error

The file used in the First Icon Image field may not be referenced.

How are you supposed to use managed_file field in custom form plugins?