function submitForm(array &$form, FormStateInterface $form_state) { $result = ManageStorage::fetchRowFields('id', $this->id, array('fid')); file_delete($result['fid']); ManageStorage::delete($this->id); drupal_set_message('CSV deleted successfully.'); $form_state->setRedirect('node_csv_uploader.admin'); }
public function contentOverview() { // Table Header $header = array('id' => array('data' => t('Id')), 'file_name' => array('data' => t('File Name')), 'content_type' => array('data' => t('Content Type')), 'operations' => array('data' => t('Delete'))); $rows = array(); $count = 1; foreach (ManageStorage::getAll() as $key => $content) { $file = file_load($content->fid); $file_link = ''; if ($file !== NULL) { $file_name_value = $file->filename->getvalue(); $file_name_part = explode('.', $file_name_value[0]['value']); $file_name = $file_name_part[0]; $file_uri = $file->uri->getvalue(); $file_link = \Drupal::l(t($file_name), Url::fromUri(file_create_url($file_uri[0]['value']))); } $rows[$key] = array('data' => array($count, $file_link, $content->content_type, \Drupal::l(t('Delete'), Url::fromRoute('node_csv_uploader.delete', ['id' => $content->id])))); $count++; } $table = array('#type' => 'table', '#header' => $header, '#rows' => $rows, '#attributes' => array('id' => 'upload-node-csv'), '#empty' => $this->t('No uploaded csv file available.')); return $table; }
function submitForm(array &$form, FormStateInterface $form_state) { $current_user = \Drupal::currentUser(); $values = $form_state->getValues(); $fid = $values['file_name'][0]; $file_uri = $this->getFileUri($fid); $file = new CSVFileReader($file_uri[0]['value']); $delimiter = ','; $enclosure = '"'; $escape = '\\'; $file->setCsvControl($delimiter, $enclosure, $escape); $file->execute(); $rows = $file->getRows(); $valid_csv = false; foreach ($rows as $key => $value) { $title = isset($value['title']) ? $value['title'] : ''; $address = isset($value['address']) ? $value['address'] : ''; if (!empty($title) and !empty($address)) { $valid_csv = true; $latitude = isset($value['latitude']) ? $value['latitude'] : 0; $longitude = isset($value['longitude']) ? $value['longitude'] : 0; if (empty($latitude) and empty($longitude)) { $google_map = new GoogleMaps(); $map_results = $google_map->setAddress($title . ', ' . $address)->get(); $latitude = $map_results->getLatitude() != NULL ? $map_results->getLatitude() : 0; $longitude = $map_results->getLongitude() != NULL ? $map_results->getLongitude() : 0; } $node = Node::create(array('type' => $this->content_type, 'title' => $title, 'langcode' => 'en', 'uid' => $current_user->id(), 'status' => 1, 'field_latitude' => array('value' => $latitude), 'field_longitude' => array('value' => $longitude), 'field_country' => array('value' => isset($value['country']) ? $value['country'] : ''), 'field_address' => array('value' => $address))); $node->save(); } else { $valid_csv = false; break; } } if ($valid_csv) { ManageStorage::add(array('fid' => $fid, 'content_type' => $this->content_type)); drupal_set_message(t('Your file content has been uploaded successfully.')); $form_state->setRedirect('node_csv_uploader.admin'); } else { drupal_set_message(t('Please upload valid csv file, file format is not matched.'), 'error'); $form_state->setRedirect('node_csv_uploader.add'); } return $form; }