class FeedsCSVParser in Feeds 8.2
Defines a CSV feed parser.
Plugin annotation
@Plugin(
id = "csv",
title = @Translation("CSV parser"),
description = @Translation("Parse CSV files.")
)
Hierarchy
- class \Drupal\feeds\Plugin\FeedsConfigurable
- class \Drupal\feeds\Plugin\FeedsPlugin implements FeedsSourceInterface
- class \Drupal\feeds\Plugin\FeedsParser
- class \Drupal\feeds\Plugin\feeds\parser\FeedsCSVParser
- class \Drupal\feeds\Plugin\FeedsParser
- class \Drupal\feeds\Plugin\FeedsPlugin implements FeedsSourceInterface
Expanded class hierarchy of FeedsCSVParser
3 string references to 'FeedsCSVParser'
- FeedsUIUserInterfaceTestCase::testEditFeedConfiguration in feeds_ui/
feeds_ui.test - UI functionality tests on feeds_ui_overview(), feeds_ui_create_form(), Change plugins on feeds_ui_edit_page().
- feeds_import_feeds_importer_default in feeds_import/
feeds_import.feeds_importer_default.inc - Implementation of hook_feeds_importer_default().
- _feeds_feeds_plugins in ./
feeds.plugins.inc - Break out for feeds_feed_plugins().
File
- lib/
Drupal/ feeds/ Plugin/ feeds/ parser/ FeedsCSVParser.php, line 28 - Contains the FeedsCSVParser class.
Namespace
Drupal\feeds\Plugin\feeds\parserView source
class FeedsCSVParser extends FeedsParser {
/**
* Implements FeedsParser::parse().
*/
public function parse(FeedsSource $source, FeedsFetcherResult $fetcher_result) {
$source_config = $source
->getConfigFor($this);
$state = $source
->state(FEEDS_PARSE);
// Load and configure parser.
feeds_include_library('ParserCSV.inc', 'ParserCSV');
$parser = new ParserCSV();
$delimiter = $source_config['delimiter'] == 'TAB' ? "\t" : $source_config['delimiter'];
$parser
->setDelimiter($delimiter);
$iterator = new ParserCSVIterator($fetcher_result
->getFilePath());
if (empty($source_config['no_headers'])) {
// Get first line and use it for column names, convert them to lower case.
$header = $this
->parseHeader($parser, $iterator);
if (!$header) {
return;
}
$parser
->setColumnNames($header);
}
// Determine section to parse, parse.
$start = $state->pointer ? $state->pointer : $parser
->lastLinePos();
$limit = $source->importer
->getLimit();
$rows = $this
->parseItems($parser, $iterator, $start, $limit);
// Report progress.
$state->total = filesize($fetcher_result
->getFilePath());
$state->pointer = $parser
->lastLinePos();
$progress = $parser
->lastLinePos() ? $parser
->lastLinePos() : $state->total;
$state
->progress($state->total, $progress);
// Create a result object and return it.
return new FeedsParserResult($rows, $source->feed_nid);
}
/**
* Get first line and use it for column names, convert them to lower case.
* Be aware that the $parser and iterator objects can be modified in this
* function since they are passed in by reference
*
* @param ParserCSV $parser
* @param ParserCSVIterator $iterator
* @return
* An array of lower-cased column names to use as keys for the parsed items.
*/
protected function parseHeader(ParserCSV $parser, ParserCSVIterator $iterator) {
$parser
->setLineLimit(1);
$rows = $parser
->parse($iterator);
if (!count($rows)) {
return FALSE;
}
$header = array_shift($rows);
foreach ($header as $i => $title) {
$header[$i] = trim(drupal_strtolower($title));
}
return $header;
}
/**
* Parse all of the items from the CSV.
*
* @param ParserCSV $parser
* @param ParserCSVIterator $iterator
* @return
* An array of rows of the CSV keyed by the column names previously set
*/
protected function parseItems(ParserCSV $parser, ParserCSVIterator $iterator, $start = 0, $limit = 0) {
$parser
->setLineLimit($limit);
$parser
->setStartByte($start);
$rows = $parser
->parse($iterator);
return $rows;
}
/**
* Override parent::getMappingSources().
*/
public function getMappingSources() {
return FALSE;
}
/**
* Override parent::getSourceElement() to use only lower keys.
*/
public function getSourceElement(FeedsSource $source, FeedsParserResult $result, $element_key) {
return parent::getSourceElement($source, $result, drupal_strtolower($element_key));
}
/**
* Define defaults.
*/
public function sourceDefaults() {
return array(
'delimiter' => $this->config['delimiter'],
'no_headers' => $this->config['no_headers'],
);
}
/**
* Source form.
*
* Show mapping configuration as a guidance for import form users.
*/
public function sourceForm($source_config) {
$form = array();
$form['#weight'] = -10;
$mappings = feeds_importer($this->id)->processor->config['mappings'];
$sources = $uniques = array();
foreach ($mappings as $mapping) {
$sources[] = check_plain($mapping['source']);
if (!empty($mapping['unique'])) {
$uniques[] = check_plain($mapping['source']);
}
}
$output = t('Import !csv_files with one or more of these columns: !columns.', array(
'!csv_files' => l(t('CSV files'), 'http://en.wikipedia.org/wiki/Comma-separated_values'),
'!columns' => implode(', ', $sources),
));
$items = array();
$items[] = format_plural(count($uniques), t('Column <strong>!column</strong> is mandatory and considered unique: only one item per !column value will be created.', array(
'!column' => implode(', ', $uniques),
)), t('Columns <strong>!columns</strong> are mandatory and values in these columns are considered unique: only one entry per value in one of these column will be created.', array(
'!columns' => implode(', ', $uniques),
)));
$items[] = l(t('Download a template'), 'import/' . $this->id . '/template');
$form['help'] = array(
'#prefix' => '<div class="help">',
'#suffix' => '</div>',
'description' => array(
'#prefix' => '<p>',
'#markup' => $output,
'#suffix' => '</p>',
),
'list' => array(
'#theme' => 'item_list',
'#items' => $items,
),
);
$form['delimiter'] = array(
'#type' => 'select',
'#title' => t('Delimiter'),
'#description' => t('The character that delimits fields in the CSV file.'),
'#options' => array(
',' => ',',
';' => ';',
'TAB' => 'TAB',
'|' => '|',
'+' => '+',
),
'#default_value' => isset($source_config['delimiter']) ? $source_config['delimiter'] : ',',
);
$form['no_headers'] = array(
'#type' => 'checkbox',
'#title' => t('No Headers'),
'#description' => t('Check if the imported CSV file does not start with a header row. If checked, mapping sources must be named \'0\', \'1\', \'2\' etc.'),
'#default_value' => isset($source_config['no_headers']) ? $source_config['no_headers'] : 0,
);
return $form;
}
/**
* Define default configuration.
*/
public function configDefaults() {
return array(
'delimiter' => ',',
'no_headers' => 0,
);
}
/**
* Build configuration form.
*/
public function configForm(&$form_state) {
$form = array();
$form['delimiter'] = array(
'#type' => 'select',
'#title' => t('Default delimiter'),
'#description' => t('Default field delimiter.'),
'#options' => array(
',' => ',',
';' => ';',
'TAB' => 'TAB',
'|' => '|',
'+' => '+',
),
'#default_value' => $this->config['delimiter'],
);
$form['no_headers'] = array(
'#type' => 'checkbox',
'#title' => t('No headers'),
'#description' => t('Check if the imported CSV file does not start with a header row. If checked, mapping sources must be named \'0\', \'1\', \'2\' etc.'),
'#default_value' => $this->config['no_headers'],
);
return $form;
}
public function getTemplate() {
$mappings = feeds_importer($this->id)->processor->config['mappings'];
$sources = $uniques = array();
foreach ($mappings as $mapping) {
if (!empty($mapping['unique'])) {
$uniques[] = check_plain($mapping['source']);
}
else {
$sources[] = check_plain($mapping['source']);
}
}
$sep = ',';
$columns = array();
foreach (array_merge($uniques, $sources) as $col) {
if (strpos($col, $sep) !== FALSE) {
$col = '"' . str_replace('"', '""', $col) . '"';
}
$columns[] = $col;
}
drupal_add_http_header('Cache-Control', 'max-age=60, must-revalidate');
drupal_add_http_header('Content-Disposition', 'attachment; filename="' . $this->id . '_template.csv"');
drupal_add_http_header('Content-type', 'text/csv; charset=utf-8');
print implode($sep, $columns);
return;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FeedsConfigurable:: |
protected | property | ||
FeedsConfigurable:: |
protected | property | CTools export enabled status of this object. | |
FeedsConfigurable:: |
protected | property | ||
FeedsConfigurable:: |
protected | property | ||
FeedsConfigurable:: |
public static | property | ||
FeedsConfigurable:: |
public | function | Similar to setConfig but adds to existing configuration. | |
FeedsConfigurable:: |
public | function | Submission handler for configForm(). | 2 |
FeedsConfigurable:: |
public | function | Validation handler for configForm(). | 3 |
FeedsConfigurable:: |
public | function | Copy a configuration. | 1 |
FeedsConfigurable:: |
public | function | Determine whether this object is persistent and enabled. I. e. it is defined either in code or in the database and it is enabled. | 1 |
FeedsConfigurable:: |
public | function | Implements getConfig(). | 1 |
FeedsConfigurable:: |
public static | function | Instantiate a FeedsConfigurable object. | 1 |
FeedsConfigurable:: |
public | function | Set configuration. | |
FeedsConfigurable:: |
public | function | Override magic method __get(). Make sure that $this->config goes through getConfig(). | |
FeedsConfigurable:: |
public | function | Override magic method __isset(). This is needed due to overriding __get(). | |
FeedsCSVParser:: |
public | function |
Define default configuration. Overrides FeedsConfigurable:: |
|
FeedsCSVParser:: |
public | function |
Build configuration form. Overrides FeedsConfigurable:: |
|
FeedsCSVParser:: |
public | function |
Override parent::getMappingSources(). Overrides FeedsParser:: |
|
FeedsCSVParser:: |
public | function |
Override parent::getSourceElement() to use only lower keys. Overrides FeedsParser:: |
|
FeedsCSVParser:: |
public | function | ||
FeedsCSVParser:: |
public | function |
Implements FeedsParser::parse(). Overrides FeedsParser:: |
|
FeedsCSVParser:: |
protected | function | Get first line and use it for column names, convert them to lower case. Be aware that the $parser and iterator objects can be modified in this function since they are passed in by reference | |
FeedsCSVParser:: |
protected | function | Parse all of the items from the CSV. | |
FeedsCSVParser:: |
public | function |
Define defaults. Overrides FeedsPlugin:: |
|
FeedsCSVParser:: |
public | function |
Source form. Overrides FeedsPlugin:: |
|
FeedsParser:: |
public | function | Clear all caches for results for given source. | |
FeedsParser:: |
public | function |
Implements FeedsPlugin::pluginType(). Overrides FeedsPlugin:: |
|
FeedsPlugin:: |
public static | function | Get all available plugins. | |
FeedsPlugin:: |
public static | function | Gets all available plugins of a particular type. | |
FeedsPlugin:: |
public static | function | Determines whether given plugin is derived from given base plugin. | |
FeedsPlugin:: |
public | function |
Returns TRUE if $this->sourceForm() returns a form. Overrides FeedsSourceInterface:: |
|
FeedsPlugin:: |
public static | function | Loads on-behalf implementations from mappers/ directory. | |
FeedsPlugin:: |
public | function |
Save changes to the configuration of this object.
Delegate saving to parent (= Feed) which will collect
information from this object by way of getConfig() and store it. Overrides FeedsConfigurable:: |
|
FeedsPlugin:: |
public | function |
A source is being deleted. Overrides FeedsSourceInterface:: |
2 |
FeedsPlugin:: |
public | function |
Validation handler for sourceForm. Overrides FeedsSourceInterface:: |
2 |
FeedsPlugin:: |
public | function |
A source is being saved. Overrides FeedsSourceInterface:: |
2 |
FeedsPlugin:: |
public static | function | Determines the type of a plugin. | |
FeedsPlugin:: |
protected | function |
Constructor. Overrides FeedsConfigurable:: |