Generalize "Archive assets" importer target and add to all log type importers.

This commit is contained in:
Michael Stenta 2020-04-23 08:26:49 -04:00
parent 247bc024ad
commit 7069c8eff9
4 changed files with 61 additions and 69 deletions

View File

@ -212,12 +212,12 @@ function farm_import_add_importer_fields($type, $bundle, $importer) {
$mappings = array();
}
// Add fields, if they exist on the bundle.
// Add fields, if they exist on the bundle, or if they are "custom".
foreach ($mappings as $field => $mapping) {
if (!empty($mapping['real_field'])) {
$field = $mapping['real_field'];
}
if (!empty(field_info_instance($type, $field, $bundle))) {
if (!empty($mapping['custom']) || !empty(field_info_instance($type, $field, $bundle))) {
$importer->config['processor']['config']['mappings'][] = $mapping;
}
}
@ -306,6 +306,13 @@ function farm_import_log_field_mappings() {
'language' => 'und',
'real_field' => 'field_farm_asset',
),
'farm_import_log_archive_assets' => array(
'source' => 'Archive assets',
'target' => 'farm_import_log_archive_assets',
'unique' => FALSE,
'language' => 'und',
'custom' => TRUE,
),
'field_farm_area' => array(
'source' => 'Areas',
'target' => 'field_farm_area',

View File

@ -68,6 +68,10 @@ function farm_import_feeds_tamper_default() {
$export[$feeds_tamper->id] = $feeds_tamper;
}
// Convert "Archive assets" to boolean (defaults to False).
$feeds_tamper = farm_import_feeds_tamper_plugin('log', $bundle, 'Archive assets', 'boolean_default_false');
$export[$feeds_tamper->id] = $feeds_tamper;
// If an "Areas" field exists, explode it into an array of area names and
// trim whitespace.
if (!empty(field_info_instance('log', 'field_farm_area', $bundle))) {

View File

@ -89,6 +89,54 @@ function farm_import_farm_ui_actions() {
return $actions;
}
/**
* Implements hook_feeds_processor_targets_alter().
*/
function farm_import_feeds_processor_targets_alter(&$targets, $type, $bundle) {
// Add a target for the "Archive assets" column on logs.
if ($type == 'log') {
$targets['farm_import_log_archive_assets'] = array(
'name' => t('Archive assets'),
'description' => t('Archives the assets referenced on the log.'),
'callback' => 'farm_import_log_archive_assets',
);
}
}
/**
* Callback for 'farm_import_log_archive_assets' target.
*/
function farm_import_log_archive_assets($source, $entity, $target, $value, $mapping) {
// Don't do anything if we weren't given any data.
if (empty($value)) {
return;
}
// In case the value arrives as an array.
if (is_array($value)) {
$value = reset($value);
}
// Do nothing if the archiving is set to any of the FALSE values.
if (empty($value)) {
return;
}
// Load log entity metadata wrapper.
$log_wrapper = entity_metadata_wrapper('log', $entity);
// Iterate through the assets and archive them (if they aren't already).
foreach ($log_wrapper->field_farm_asset as $asset_wrapper) {
$asset = $asset_wrapper->value();
if (empty($asset->archived)) {
$asset->archived = REQUEST_TIME;
farm_asset_save($asset);
}
}
}
/**
* Helper function for generating standardized tamper plugin definitions.
*

View File

@ -39,75 +39,8 @@ function farm_log_harvest_feeds_importer_default_alter(&$importers) {
'unique' => FALSE,
'language' => 'und',
),
array(
'source' => 'Archive assets',
'target' => 'log_farm_harvest_archive_assets',
'unique' => FALSE,
'language' => 'und',
),
);
$importer_mappings =& $importers[$name]->config['processor']['config']['mappings'];
$importer_mappings = array_merge($importer_mappings, $mappings);
}
}
/**
* Implements hook_feeds_tamper_default_alter().
*/
function farm_log_harvest_feeds_tamper_default_alter(&$feeds_tampers) {
// If farm_import is not installed, bail.
if (!module_exists('farm_import')) {
return;
}
// Convert "Archive assets" to boolean.
$feeds_tamper = farm_import_feeds_tamper_plugin('log', 'farm_harvest', 'Archive assets', 'boolean_default_false');
$feeds_tampers[$feeds_tamper->id] = $feeds_tamper;
}
/**
* Implements hook_feeds_processor_targets_alter().
*/
function farm_log_harvest_feeds_processor_targets_alter(&$targets, $type, $bundle) {
if ($type == 'log' && $bundle == 'farm_harvest') {
$targets['log_farm_harvest_archive_assets'] = array(
'name' => t('Archive assets'),
'description' => t('Archives the assets referenced on the log.'),
'callback' => 'farm_log_harvest_archive_assets',
);
}
}
/**
* Callback for 'log_farm_harvest_archive_assets' target.
*/
function farm_log_harvest_archive_assets($source, $entity, $target, $value, $mapping) {
// Don't do anything if we weren't given any data.
if (empty($value)) {
return;
}
// In case the value arrives as an array.
if (is_array($value)) {
$value = reset($value);
}
// Do nothing if the archiving is set to any of the FALSE values.
if (empty($value)) {
return;
}
// Load log entity metadata wrapper.
$log_wrapper = entity_metadata_wrapper('log', $entity);
// Iterate through the assets and archive them (if they aren't already).
foreach ($log_wrapper->field_farm_asset as $asset_wrapper) {
$asset = $asset_wrapper->value();
if (empty($asset->archived)) {
$asset->archived = REQUEST_TIME;
farm_asset_save($asset);
}
}
}