You are here

public static function MetatagImport::validateCsvHeader in Metatag Import Export CSV 8

Validate the CSV header row.

Helper for validate_csv_header().

Parameters

\Drupal\file\FileInterface $file: The uploaded file.

Return value

array An array of errors.

1 call to MetatagImport::validateCsvHeader()
validate_csv_header in ./metatag_import_export_csv.module
File upload validator.

File

src/MetatagImport.php, line 24

Class

MetatagImport
Class for batch process for metatag import.

Namespace

Drupal\metatag_import_export_csv

Code

public static function validateCsvHeader(FileInterface $file) {
  $url = $file
    ->getFileUri();
  $filepath = file_create_url($url);
  $handle = fopen($filepath, 'r');
  $headers = fgetcsv($handle);

  // Close the CSV file, as the batch setup needs to open it from the top.
  fclose($handle);
  $errors = [];
  if (!in_array('path_alias', $headers)) {
    if (!in_array('entity_type', $headers)) {
      $errors[] = t("The CSV must have an 'entity_type' column if it does not have a 'path_alias' column.");
    }
    if (!in_array('entity_id', $headers)) {
      $errors[] = t("The CSV must have an 'entity_id' column if it does not have a 'path_alias' column.");
    }
  }
  return $errors;
}