Skip to content

Commit

Permalink
Fixing errors when file getting by ID from configs - doesn't exists (#4)
Browse files Browse the repository at this point in the history
* Fixing errors when file getting by ID from configs - doesn't exists

* Update the other content with the same code

---------

Co-authored-by: Niklan <[email protected]>
  • Loading branch information
alekskn and Niklan authored Jul 9, 2024
1 parent 5ac65b1 commit 805a9ac
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,22 @@ public function buildForm(array $form, FormStateInterface $form_state) {
# Если загружен файл, отображаем дополнительные элементы формы.
if (!empty($config->get('fid'))) {
$file = File::load($config->get('fid'));
$created = \Drupal::service('date.formatter')
->format($file->created->value, 'medium');

$form['file_information'] = [
'#markup' => $this->t('This file was uploaded at @created.', ['@created' => $created]),
];


# Добавляем кнопку для начала импорта со своим собственным submit handler.
$form['actions']['start_import'] = [
'#type' => 'submit',
'#value' => $this->t('Start import'),
'#submit' => ['::startImport'],
'#weight' => 100,
];
if ($file) {
$created = \Drupal::service('date.formatter')
->format($file->created->value, 'medium');

$form['file_information'] = [
'#markup' => $this->t('This file was uploaded at @created.', ['@created' => $created]),
];

# Добавляем кнопку для начала импорта со своим собственным submit handler.
$form['actions']['start_import'] = [
'#type' => 'submit',
'#value' => $this->t('Start import'),
'#submit' => ['::startImport'],
'#weight' => 100,
];
}
}

$form['additional_settings'] = [
Expand Down Expand Up @@ -119,8 +120,10 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
# используем.
if (!empty($fid_old)) {
$previous_file = File::load($fid_old);
\Drupal::service('file.usage')
->delete($previous_file, 'custom_csv_import', 'config_form', $previous_file->id());
if ($previous_file) {
\Drupal::service('file.usage')
->delete($previous_file, 'custom_csv_import', 'config_form', $previous_file->id());
}
}
# Теперь, не важно, был ли старый файл или нет, нам нужно сохранить
# новый файл.
Expand Down
33 changes: 18 additions & 15 deletions blog/2016/09/11/drupal-8-custom-csv-import/index.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -590,21 +590,22 @@ public function buildForm(array $form, FormStateInterface $form_state) {
# Если загружен файл, отображаем дополнительные элементы формы.
if (!empty($config->get('fid'))) {
$file = File::load($config->get('fid'));
$created = \Drupal::service('date.formatter')
->format($file->created->value, 'medium');

$form['file_information'] = [
'#markup' => $this->t('This file was uploaded at @created.', ['@created' => $created]),
];
if ($file) {
$created = \Drupal::service('date.formatter')
->format($file->created->value, 'medium');

$form['file_information'] = [
'#markup' => $this->t('This file was uploaded at @created.', ['@created' => $created]),
];

# Добавляем кнопку для начала импорта со своим собственным submit handler.
$form['actions']['start_import'] = [
'#type' => 'submit',
'#value' => $this->t('Start import'),
'#submit' => ['::startImport'],
'#weight' => 100,
];
# Добавляем кнопку для начала импорта со своим собственным submit handler.
$form['actions']['start_import'] = [
'#type' => 'submit',
'#value' => $this->t('Start import'),
'#submit' => ['::startImport'],
'#weight' => 100,
];
}
}

$form['additional_settings'] = [
Expand Down Expand Up @@ -660,8 +661,10 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
# используем.
if (!empty($fid_old)) {
$previous_file = File::load($fid_old);
\Drupal::service('file.usage')
->delete($previous_file, 'custom_csv_import', 'config_form', $previous_file->id());
if ($previous_file) {
\Drupal::service('file.usage')
->delete($previous_file, 'custom_csv_import', 'config_form', $previous_file->id());
}
}
# Теперь, не важно, был ли старый файл или нет, нам нужно сохранить
# новый файл.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,30 @@ public function buildForm(array $form, FormStateInterface $form_state) {
# Если загружен файл, отображаем дополнительные элементы формы.
if (!empty($config->get('fid'))) {
$file = File::load($config->get('fid'));
$created = \Drupal::service('date.formatter')
->format($file->created->value, 'medium');

$form['file_information'] = [
'#markup' => $this->t('This file was uploaded at @created.', ['@created' => $created]),
];

$form['import_plugin'] = [
'#title' => $this->t('Select content type to import'),
'#type' => 'select',
'#options' => $this->getPluginList(),
'#empty_option' => '- Select -',
];

# Добавляем кнопку для начала импорта со своим собственным submit handler.
$form['actions']['start_import'] = [
'#type' => 'submit',
'#value' => $this->t('Start import'),
'#submit' => ['::startImport'],
'#weight' => 100,
'#name' => 'start_import',
];
if ($file) {
$created = \Drupal::service('date.formatter')
->format($file->created->value, 'medium');

$form['file_information'] = [
'#markup' => $this->t('This file was uploaded at @created.', ['@created' => $created]),
];

$form['import_plugin'] = [
'#title' => $this->t('Select content type to import'),
'#type' => 'select',
'#options' => $this->getPluginList(),
'#empty_option' => '- Select -',
];

# Добавляем кнопку для начала импорта со своим собственным submit handler.
$form['actions']['start_import'] = [
'#type' => 'submit',
'#value' => $this->t('Start import'),
'#submit' => ['::startImport'],
'#weight' => 100,
'#name' => 'start_import',
];
}
}

$form['additional_settings'] = [
Expand Down Expand Up @@ -162,8 +164,10 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
# используем.
if (!empty($fid_old)) {
$previous_file = File::load($fid_old);
\Drupal::service('file.usage')
->delete($previous_file, 'custom_csv_import', 'config_form', $previous_file->id());
if ($previous_file) {
\Drupal::service('file.usage')
->delete($previous_file, 'custom_csv_import', 'config_form', $previous_file->id());
}
}
# Теперь, не важно, был ли старый файл или нет, нам нужно сохранить
# новый файл.
Expand Down

0 comments on commit 805a9ac

Please sign in to comment.