You are here

protected function FeedsDataProcessor::processMappings in Feeds 6

Create a new field for each new: mapping.

2 calls to FeedsDataProcessor::processMappings()
FeedsDataProcessor::addConfig in plugins/FeedsDataProcessor.inc
Override parent::addConfig().
FeedsDataProcessor::setConfig in plugins/FeedsDataProcessor.inc
Override parent::setConfig().

File

plugins/FeedsDataProcessor.inc, line 374
Definition of FeedsDataProcessor.

Class

FeedsDataProcessor
Creates simple table records from feed items. Uses Data module.

Code

protected function processMappings(&$config) {
  if (!empty($config['mappings'])) {
    foreach ($config['mappings'] as &$mapping) {
      @(list($new, $type) = explode(':', $mapping['target']));

      // Create a new field with targets that start with "new:"
      if ($new == 'new') {

        // Build a field name from the source key.
        $field_name = data_safe_name($mapping['source']);

        // Get the full schema spec from data.
        $type = data_get_field_definition($type);

        // Add the field to the table.
        $schema = $this
          ->table()
          ->get('table_schema');
        if (!isset($schema['fields'][$field_name])) {
          $mapping['target'] = $this
            ->table()
            ->addField($field_name, $type);

          // Let the user know.
          drupal_set_message(t('Created new field "@name".', array(
            '@name' => $field_name,
          )));
        }
        else {
          throw new Exception(t('Field @field_name already exists as a mapping target. Remove it from mapping if you would like to map another source to it. Remove it from !data_table table if you would like to change its definition.', array(
            '@field_name' => $field_name,
            '!data_table' => l($this
              ->table()
              ->get('name'), 'admin/content/data'),
          )));
        }
      }
    }
  }
}