3
0
Fork 0
mirror of https://github.com/farmOS/farmOS.git synced 2024-02-23 11:37:38 +01:00

Merge branch 'farm_asset_property_feeds' into 7.x-1.x

This commit is contained in:
Michael Stenta 2017-11-27 16:26:34 -05:00
commit 92546e4edb
2 changed files with 148 additions and 0 deletions

View file

@ -0,0 +1,40 @@
<?php
/**
* @file
* Hooks provided by farm_asset_property.
*
* This file contains no working PHP code; it exists to provide additional
* documentation for doxygen as well as to document hooks in the standard
* Drupal manner.
*/
/**
* @defgroup farm_asset_property Farm asset property module integrations.
*
* Module integrations with the farm_asset_property module.
*/
/**
* @defgroup farm_asset_property_hooks Farm asset property's hooks
* @{
* Hooks that can be implemented by other modules in order to extend farm_asset_property.
*/
/**
* Defines farm asset properties maintained by this module.
*
* @return array
* Returns an array of farm asset property names.
*/
function hook_farm_asset_property() {
return array(
'farm_grazing_animal_type',
'farm_grazing_planned_arrival',
'farm_grazing_planned_departure',
);
}
/**
* @}
*/

View file

@ -136,6 +136,93 @@ function farm_asset_property_is_set($asset_id, $name) {
return !empty($set);
}
/**
* Implements hook_feeds_processor_targets().
*/
function farm_asset_property_feeds_processor_targets($entity_type, $bundle) {
$targets = array();
// If this is not a farm_asset entity, bail.
if ($entity_type != 'farm_asset') {
return $targets;
}
// Ask modules for available asset properties.
$properties = module_invoke_all('farm_asset_property');
// If there are no properties, bail.
if (empty($properties) || !is_array($properties)) {
return $targets;
}
// Create a mapping target for each property.
foreach ($properties as $property) {
$targets['farm_asset_property:' . $property] = array(
'name' => t('Farm asset property: @property', array('@property' => $property)),
'description' => t('Sets the @property property on a farm asset entity.', array('@property' => $property)),
'callback' => 'farm_asset_property_feeds_set_target',
);
}
// Return the targets.
return $targets;
}
/**
* Callback for setting an asset property during a Feeds import.
*/
function farm_asset_property_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
// If values are empty, bail. Asset properties are always single values, so
// we are only going to look t the first array element.
if (empty($values[0])) {
return;
}
// Get the value.
$value = reset($values);
// Split the $target variable to get the property name.
$parts = explode(':', $target);
// If the first part isn't 'farm_asset_property', something's wrong. Bail.
if ($parts[0] != 'farm_asset_property') {
return;
}
// If the second part is empty, bail.
if (empty($parts[1])) {
return;
}
// Get the property name.
$property = $parts[1];
// Add the property to the $entity->farm_asset_property. We don't have an
// entity ID yet, so we can't save directly. This module implements
// hook_entity_insert() to set any asset properties stored in that array.
$entity->farm_asset_property[$property] = $value;
}
/**
* Implements hook_entity_insert().
*/
function farm_asset_property_entity_insert($entity, $type) {
// Only act on farm asset entities.
if ($type != 'farm_asset') {
return;
}
// If the entity has any properties defined in $entity->farm_asset_property,
// save them to the database.
if (!empty($entity->farm_asset_property) && is_array($entity->farm_asset_property)) {
foreach ($entity->farm_asset_property as $name => $value) {
farm_asset_property_set($entity->id, $name, $value);
}
}
}
/**
* Implements hook_entity_delete().
*/
@ -154,3 +241,24 @@ function farm_asset_property_entity_delete($entity, $type) {
// Delete all properties of an asset when the asset is deleted.
db_query('DELETE FROM {farm_asset_property} WHERE id=:id', array(':id' => $entity->id));
}
/**
* Implements hook_modules_uninstalled().
*/
function farm_asset_property_modules_uninstalled($modules) {
// When a module is uninstalled, delete any asset properties that were
// maintained by it. This assumes that the module declared its properties
// via hook_farm_asset_propert().
foreach ($modules as $module) {
$hook = 'farm_asset_property';
if (function_exists($module . '_' . $hook)) {
$properties = module_invoke($module, $hook);
if (!empty($properties) && is_array($properties)) {
foreach ($properties as $property) {
db_query('DELETE FROM {farm_asset_property} WHERE name = :name', array('name' => $property));
}
}
}
}
}