Skip to content
Snippets Groups Projects
Commit c46f7b92 authored by megachriz's avatar megachriz Committed by MegaChriz
Browse files

Issue #3046592 by MegaChriz: Added a hook to invoke before validation: hook_feeds_prevalidate().

parent 2dc64777
No related branches found
No related tags found
No related merge requests found
......@@ -144,6 +144,31 @@ function hook_feeds_before_update(FeedsSource $source, $item, $entity_id) {
}
}
/**
* Invoked before a feed item is validated.
*
* @param FeedsSource $source
* FeedsSource object that describes the source that is being imported.
* @param object $entity
* The entity object.
* @param array $item
* The parser result for this entity.
* @param int|null $entity_id
* The id of the current item which is going to be updated. If this is a new
* item, then NULL is passed.
*/
function hook_feeds_prevalidate(FeedsSource $source, $entity, $item, $entity_id) {
// Correct a field value to make it pass validation.
if (isset($entity->myfield)) {
foreach ($entity->myfield as $language => &$values) {
// There are only three values allowed. Throw away the rest.
if (count($values) > 3) {
$values = array_slice($values, 0, 3);
}
}
}
}
/**
* Invoked before a feed item is saved.
*
......
......@@ -31,6 +31,7 @@ function feeds_hook_info() {
'feeds_after_parse',
'feeds_before_import',
'feeds_before_update',
'feeds_prevalidate',
'feeds_presave',
'feeds_after_save',
'feeds_after_import',
......
......@@ -160,6 +160,9 @@ abstract class FeedsProcessor extends FeedsPlugin {
/**
* Validates an entity.
*
* @param object $entity
* The entity to validate.
*
* @throws FeedsValidationException $e
* Thrown if validation fails.
*/
......@@ -432,6 +435,9 @@ abstract class FeedsProcessor extends FeedsPlugin {
// Set property and field values.
$this->map($source, $parser_result, $entity);
// Allow modules to alter the entity before validating.
module_invoke_all('feeds_prevalidate', $source, $entity, $item, $entity_id);
$this->entityValidate($entity);
// Allow modules to alter the entity before saving.
......
<?php
/**
* @file
* Contains FeedsHooksTestCase.
*/
/**
* Tests for hooks invoked by Feeds not related to mapping.
*/
......@@ -16,6 +21,66 @@ class FeedsHooksTestCase extends FeedsWebTestCase {
);
}
/**
* Ensure that the prevalidate hook is invoked.
*
* @see feeds_tests_feeds_prevalidate()
*/
public function testPrevalidateHook() {
// Include FeedsProcessor.inc to make its constants available.
module_load_include('inc', 'feeds', 'plugins/FeedsProcessor');
// Set flag to test prevalidate hook.
variable_set('feeds_tests_hook_feeds_prevalidate', TRUE);
// Create CSV importer.
$this->createImporterConfiguration('Content CSV', 'csv');
$this->setSettings('csv', NULL, array('content_type' => '', 'import_period' => FEEDS_SCHEDULE_NEVER));
$this->setPlugin('csv', 'FeedsFileFetcher');
$this->setPlugin('csv', 'FeedsCSVParser');
$this->setSettings('csv', 'FeedsNodeProcessor', array(
'bundle' => 'article',
'update_existing' => FEEDS_UPDATE_EXISTING,
));
$this->addMappings('csv', array(
0 => array(
'source' => 'title',
'target' => 'title',
'unique' => TRUE,
),
));
// Create an article node that gets updated.
$node = $this->drupalCreateNode(array(
'type' => 'article',
'title' => 'Lorem ipsum',
));
// Import CSV file.
$this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv');
$this->assertText('Created 1 node');
$this->assertText('Updated 1 node');
$expected_result = array(
array(
'importer_id' => 'csv',
'title' => 'Lorem ipsum',
'item_guid' => 1,
'entity_id' => $node->nid,
),
array(
'importer_id' => 'csv',
'title' => 'Ut wisi enim ad minim veniam',
'item_guid' => 2,
'entity_id' => NULL,
),
);
// Assert that the hook was invoked. The variable should have been set in
// feeds_tests_feeds_prevalidate().
$this->assertEqual($expected_result, variable_get('feeds_tests_hook_feeds_prevalidate_results'));
}
/**
* Tests the hook hook_config_defaults().
*/
......
......@@ -5,6 +5,27 @@
* Feeds hooks implementations.
*/
/**
* Implements hook_feeds_prevalidate().
*
* @see FeedsHooksTestCase::testPrevalidateHook()
*/
function feeds_tests_feeds_prevalidate(FeedsSource $source, $entity, $item, $entity_id) {
if (!variable_get('feeds_tests_hook_feeds_prevalidate', FALSE)) {
return;
}
// Keep track of results so far.
$results = variable_get('feeds_tests_hook_feeds_prevalidate_results', array());
$results[] = array(
'importer_id' => $source->importer->id,
'title' => $entity->title,
'item_guid' => $item['guid'],
'entity_id' => $entity_id,
);
variable_set('feeds_tests_hook_feeds_prevalidate_results', $results);
}
/**
* Implements hook_feeds_config_defaults().
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment