Làm cách nào để lưu tệp được tải lên vĩnh viễn trong bảng file_manged?


11

Làm cách nào để lưu tệp được tải lên với trạng thái bằng 1 trong bảng file_managed, trong Drupal 8?

Bất cứ khi nào tôi tải lên một tệp, nó được lưu trữ trong bảng file_managed với giá trị trạng thái 0.
Tôi đã sử dụng File::load( $form_state->getValue('image'))để tải tệp. Tôi cần làm gì tiếp theo?

Trong Drupal 7, tôi sẽ sử dụng $file->status = FILE_STATUS_PERMANENT. Mã tương đương cho Drupal 8 là gì?

class AddBannerForm extends FormBase {


public function getFormId()
{
  return 'add_banner_form';
}

public function buildForm(array $form, FormStateInterface $form_state)
{


  $form['image'] = array(
    '#type'          => 'managed_file',
    '#title'         => t('Choose Image File'),
    '#upload_location' => 'public://images/',
    '#default_value' => '',
    '#description'   => t('Specify an image(s) to display.'),
    '#states'        => array(
      'visible'      => array(
        ':input[name="image_type"]' => array('value' => t('Upload New Image(s)')),
      ),
    ),
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save image'),
  );

  return $form;
}


public function validateForm(array &$form, FormStateInterface $form_state)
{
    File::load( $form_state->getValue('image') );
}


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

}
}

Câu trả lời:


17

Cảm ơn bạn @Clive & @kiamlaluno

/* Fetch the array of the file stored temporarily in database */ 
   $image = $form_state->getValue('image');

/* Load the object of the file by it's fid */ 
   $file = File::load( $image[0] );

/* Set the status flag permanent of the file object */
   $file->setPermanent();

/* Save the file in database */
   $file->save();

1
Bây giờ bạn có thể làm điều này trong một tệp giản đồ không?
— Guillaume Bois

7
Đẹp @Jasodeep !! Tuy nhiên điều này không đủ để tôi làm việc. Sau khi thiết lập setPermanent()& save(). Tôi đã phải làm thêm một bước $file_usage = \Drupal::service('file.usage'); $file_usage->add($file, 'mymodule', 'mymodule', \Drupal::currentUser()->id()); :) hy vọng điều này sẽ giúp !!
— JayKandari


4

Sử dụng mã này để lưu hình ảnh vĩnh viễn ở dạng cấu hình, nếu bạn đang sử dụng Drupal 8.

public function submitForm(array &$form, FormStateInterface $form_state) {
  parent::submitForm($form, $form_state);
  $image = $form_state->getValue('welcome_image');
  // Load the object of the file by its fid. 
  $file = File::load($image[0]);
  // Set the status flag permanent of the file object.
  if (!empty($file)) {
    $file->setPermanent();
    // Save the file in the database.
    $file->save();
    $file_usage = \Drupal::service('file.usage'); 
    $file_usage->add($file, 'welcome', 'welcome', \Drupal::currentUser()->id());
  }
  $config = $this->config('welcome.settings');
  $config->set('welcome_text', $form_state->getValue('welcome_text'))
    ->set('welcome_image', $form_state->getValue('welcome_image'))
    ->save();
}
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookie và Chính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.