Hi,

I tried the following to permanently store a file upload setting in themes' advanced theme settings form using managed_upload but the file is stored temporarily nevertheless. What am I doing wrong here ?

function d8panvyom_form_system_theme_settings_alter(&$form, \Drupal\Core\Form\FormStateInterface &$form_state, $form_id = NULL) {
        // Quick info block
	$form ['d8panvyom_quick_info_block'] = array (
			'#type' => 'fieldset',
			'#title' => t ( 'Quick info' )
	);

       // Shri Ganesh naman image
      $form ['d8panvyom_quick_info_block'] ['d8panvyom_shri_ganesh_naman_image'] = array (
        '#title' => t('Shri Ganesh naman image'),
        '#type' => 'managed_file',
        '#upload_location' => file_default_scheme () . '://',
        '#description' => t ( 'Shri Ganesh naman image' ),
        '#upload_validators' => array (
					'file_validate_extensions' => array (
							'gif png jpg jpeg'
					)
			),
        '#default_value' => theme_get_setting ( 'd8panvyom_shri_ganesh_naman_image' ),
    );

    // Form submission handler
    $form['#submit'][] = 'd8panvyom_form_system_theme_settings_submit';

}

function d8panvyom_form_system_theme_settings_submit(&$form, \Drupal\Core\Form\FormStateInterface &$form_state, $form_id = NULL) {

    $d8panvyom_shri_ganesh_naman_image_id = $form_state['values']['d8panvyom_quick_info_block'] ['d8panvyom_shri_ganesh_naman_image'];

     $d8panvyom_shri_ganesh_naman_image_file = \Drupal\file\Entity\File::load ( $d8panvyom_shri_ganesh_naman_image_id );

    // set status to permanent so that its not deleted
    $d8panvyom_shri_ganesh_naman_image_file->status = FILE_STATUS_PERMANENT;
    \Drupal\file\Entity\File::save($d8panvyom_shri_ganesh_naman_image_file);

}

Comments

oriol_e9g’s picture

There is a core bug that needs to be fixed: https://www.drupal.org/node/1862892