diff --git a/blog/2016/09/11/drupal-8-custom-csv-import/example/custom_csv_import/src/Form/ImportForm.php b/blog/2016/09/11/drupal-8-custom-csv-import/example/custom_csv_import/src/Form/ImportForm.php index c9c3195..b36f0fb 100644 --- a/blog/2016/09/11/drupal-8-custom-csv-import/example/custom_csv_import/src/Form/ImportForm.php +++ b/blog/2016/09/11/drupal-8-custom-csv-import/example/custom_csv_import/src/Form/ImportForm.php @@ -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'] = [ @@ -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()); + } } # Теперь, не важно, был ли старый файл или нет, нам нужно сохранить # новый файл. diff --git a/blog/2016/09/11/drupal-8-custom-csv-import/index.ru.md b/blog/2016/09/11/drupal-8-custom-csv-import/index.ru.md index 68d69a9..66c8399 100644 --- a/blog/2016/09/11/drupal-8-custom-csv-import/index.ru.md +++ b/blog/2016/09/11/drupal-8-custom-csv-import/index.ru.md @@ -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'] = [ @@ -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()); + } } # Теперь, не важно, был ли старый файл или нет, нам нужно сохранить # новый файл. diff --git a/blog/2017/03/16/drupal-8-custom-csv-import-optimization/example/custom_csv_import/src/Form/ImportForm.php b/blog/2017/03/16/drupal-8-custom-csv-import-optimization/example/custom_csv_import/src/Form/ImportForm.php index 6d887b8..c199284 100644 --- a/blog/2017/03/16/drupal-8-custom-csv-import-optimization/example/custom_csv_import/src/Form/ImportForm.php +++ b/blog/2017/03/16/drupal-8-custom-csv-import-optimization/example/custom_csv_import/src/Form/ImportForm.php @@ -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'] = [ @@ -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()); + } } # Теперь, не важно, был ли старый файл или нет, нам нужно сохранить # новый файл.