You are here

public function ContentImport::contentImportcallback in Content Import 8.9

Content Import Sample CSV Creation.

File

src/Form/ContentImport.php, line 118

Class

ContentImport
Configure Content Import settings for this site.

Namespace

Drupal\contentimport\Form

Code

public function contentImportcallback(array &$form, FormStateInterface $form_state) {
  global $base_url;
  $ajax_response = new AjaxResponse();
  $content_type = $form_state
    ->getValue('contentimport_contenttype');
  $import_type = $form_state
    ->getValue('contentimport_importtype');
  $fields = get_fields($content_type);
  $fieldArray = $fields['name'];
  $content_type_fields = $import_type == 2 ? 'nodeid,title,langcode' : 'title,langcode';
  foreach ($fieldArray as $val) {
    $content_type_fields .= $val . ',';
  }
  $content_type_fields = substr($content_type_fields, 0, -1);
  $sampleFile = $content_type . '.csv';
  $handle = fopen("sites/default/files/" . $sampleFile, "w+") or die("There is no permission to create log file. Please give permission for sites/default/file!");
  fwrite($handle, $content_type_fields);
  $result = '<a class="button button--primary" href="' . $base_url . '/sites/default/files/' . $sampleFile . '">Click here to download Sample CSV</a>';
  $ajax_response
    ->addCommand(new HtmlCommand('#content_import_fields_change_wrapper', $result));
  return $ajax_response;
}