Autopopulate categories on certain log types.

This commit is contained in:
Michael Stenta 2017-07-18 09:08:39 -04:00
parent 7bfe1cca5a
commit 8d121ad75e
8 changed files with 200 additions and 0 deletions

View File

@ -222,6 +222,17 @@ function farm_crop_farm_log_categories() {
return array('Plantings');
}
/**
* Implements hook_farm_log_categories_populate().
*/
function farm_crop_farm_log_categories_populate($log) {
$categories = array();
if (in_array($log->type, array('farm_seeding', 'farm_transplanting'))) {
$categories[] = 'Plantings';
}
return $categories;
}
/**
* Implements hook_feeds_importer_default_alter().
*/

View File

@ -39,6 +39,17 @@ function farm_equipment_farm_log_categories() {
return array('Equipment');
}
/**
* Implements hook_farm_log_categories_populate().
*/
function farm_equipment_farm_log_categories_populate($log) {
$categories = array();
if ($log->type == 'farm_maintenance') {
$categories[] = 'Equipment';
}
return $categories;
}
/**
* Implements hook_feeds_importer_default_alter().
*/

View File

@ -68,6 +68,17 @@ function farm_livestock_farm_log_categories() {
return array('Animals');
}
/**
* Implements hook_farm_log_categories_populate().
*/
function farm_livestock_farm_log_categories_populate($log) {
$categories = array();
if ($log->type == 'farm_medical') {
$categories[] = 'Animals';
}
return $categories;
}
/**
* Implements hook_feeds_importer_default_alter().
*/

View File

@ -37,6 +37,29 @@ function hook_farm_log_categories() {
);
}
/**
* Allow modules to automatically populate log categories in log forms. The
* category must exist already. Note that these will be passed through the t()
* function when they are added so that they can be translated. This does mean
* that they will only be translated once, to whatever the site's default
* language is.
*
* @param object $log
* A log entity.
*
* @return array
* Returns an array of log categories (as simple strings).
*/
function hook_farm_log_categories_populate($log) {
$categories = array();
if ($log->type == 'farm_water_test') {
$categories[] = 'Water';
}
return $categories;
}
/**
* @}
*/

View File

@ -399,3 +399,113 @@ function farm_log_update_7007(&$sandbox) {
// Create log categories on behalf of all enabled modules.
farm_log_categories_create_all();
}
/**
* Autopopulate categories on certain log types.
*/
function farm_log_update_7008(&$sandbox) {
// Process this in passes of 50 logs at a time.
$sandbox['#finished'] = 0;
$limit = 50;
// Keep track of progress.
if (!isset($sandbox['progress'])) {
// Start out at zero.
$sandbox['progress'] = 0;
// Count how many movement logs there are.
$sandbox['max'] = db_select('log')
->fields(NULL, array('id'))
->countQuery()
->execute()
->fetchField();
}
// Fetch the next set of logs.
$query = db_select('log')
->fields(NULL, array('id', 'type'))
->orderBy('id', 'ASC')
->range($sandbox['progress'], $limit);
$results = $query->execute();
// Iterate over the results...
foreach ($results as $row) {
// Increment progress.
$sandbox['progress']++;
// Load categories provided by modules.
$categories = module_invoke_all('farm_log_categories_populate', $row);
// If there are no categories, skip this log.
if (empty($categories)) {
continue;
}
// Check to see if the log has existing categories so we can increment
// the field's delta value accordingly.
$query = db_select('field_data_field_farm_log_category', 'cat');
$query->fields('cat', array('delta', 'field_farm_log_category_tid'));
$query->condition('cat.entity_type', 'log');
$query->condition('cat.deleted', 0);
$query->condition('cat.entity_id', $row->id);
$existing = $query->execute();
// Iterate over the existing records to build a list of term IDs and get
// the highest delta.
$delta = NULL;
$tids = array();
foreach ($existing as $field) {
if ($field->delta > $delta) {
$delta = $field->delta;
}
$tids[] = $field->field_farm_log_category_tid;
}
if (is_null($delta)) {
$delta = 0;
}
else {
$delta++;
}
// Iterate over the categories.
foreach ($categories as $category) {
// Load the term.
$terms = taxonomy_get_term_by_name($category, 'farm_log_categories');
$term = reset($terms);
// If the term already exists on the log, skip it.
if (in_array($term->tid, $tids)) {
continue;
}
// Write it to the database.
$record = array(
'entity_type' => 'log',
'bundle' => $row->type,
'deleted' => 0,
'entity_id' => $row->id,
'revision_id' => $row->id,
'language' => 'und',
'delta' => $delta,
'field_farm_log_category_tid' => $term->tid,
);
drupal_write_record('field_data_field_farm_log_category', $record);
drupal_write_record('field_revision_field_farm_log_category', $record);
// Increment delta.
$delta++;
}
}
// Tell Drupal whether or not we're finished.
if ($sandbox['max'] > 0) {
$sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
}
else {
$sandbox['#finished'] = 1;
}
}

View File

@ -223,6 +223,18 @@ function farm_log_prepopulate_log_form_references(&$form) {
$ids = array($ids);
}
// Allow modules to add log categories.
if ($field == 'field_farm_log_category' && !empty($form['#entity'])) {
$categories = module_invoke_all('farm_log_categories_populate', $form['#entity']);
if (!empty($categories)) {
foreach ($categories as $category) {
if (is_string($category)) {
$ids[] = t($category);
}
}
}
}
// If there are no IDs, skip.
if (empty($ids)) {
continue;

View File

@ -23,6 +23,17 @@ function farm_soil_test_farm_ui_entities() {
);
}
/**
* Implements hook_farm_log_categories_populate().
*/
function farm_soil_test_farm_log_categories_populate($log) {
$categories = array();
if ($log->type == 'farm_soil_test') {
$categories[] = 'Soil';
}
return $categories;
}
/**
* Implements hook_feeds_importer_default_alter().
*/

View File

@ -23,6 +23,17 @@ function farm_water_test_farm_ui_entities() {
);
}
/**
* Implements hook_farm_log_categories_populate().
*/
function farm_water_test_farm_log_categories_populate($log) {
$categories = array();
if ($log->type == 'farm_water_test') {
$categories[] = 'Water';
}
return $categories;
}
/**
* Implements hook_feeds_importer_default_alter().
*/