From 6566d88e1f9127484bbdc020cc092110839b7e77 Mon Sep 17 00:00:00 2001 From: Michael Stenta Date: Wed, 1 Aug 2018 13:37:32 -0400 Subject: [PATCH] Add the ability to pass notes and log categories into farm_log_create() and farm_quantity_log_create(). --- modules/farm/farm_log/farm_log.module | 40 ++++++++++++++++++- .../farm_quantity_log.module | 8 +++- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/modules/farm/farm_log/farm_log.module b/modules/farm/farm_log/farm_log.module index de3c6117..dca59de8 100644 --- a/modules/farm/farm_log/farm_log.module +++ b/modules/farm/farm_log/farm_log.module @@ -699,11 +699,15 @@ function farm_log_query($time = REQUEST_TIME, $done = TRUE, $single = TRUE) { * Boolean indicating whether or not the log is done. * @param array $assets * An array of assets to reference in the log. + * @param string $notes + * Notes to add to the log. + * @param array $categories + * An array of categories to add to the log. * * @return \Log * Returns a saved log entity. */ -function farm_log_create($type, $name = '', $timestamp = REQUEST_TIME, $done = TRUE, $assets = array()) { +function farm_log_create($type, $name = '', $timestamp = REQUEST_TIME, $done = TRUE, $assets = array(), $notes = '', $categories = array()) { // Create a new log entity. $log = entity_create('log', array('type' => $type)); @@ -735,6 +739,40 @@ function farm_log_create($type, $name = '', $timestamp = REQUEST_TIME, $done = T } } + // Add the notes. + if (!empty($notes)) { + $log->field_farm_notes[LANGUAGE_NONE][0]['value'] = $notes; + $log->field_farm_notes[LANGUAGE_NONE][0]['format'] = 'farm_format'; + } + + // Add the categories. + if (!empty($categories)) { + foreach ($categories as $category) { + + // Look up the category taxonomy term. + $terms = taxonomy_get_term_by_name($category, 'farm_log_categories'); + + // If terms were found, use the first one. + if (!empty($terms)) { + $term = reset($terms); + } + + // If a term wasn't found, create it. + else { + $vocab = taxonomy_vocabulary_machine_name_load('farm_log_categories'); + $term = new stdClass(); + $term->name = check_plain($category); + $term->vid = $vocab->vid; + taxonomy_term_save($term); + } + + // Add the category to the log. + $log->field_farm_log_category[LANGUAGE_NONE][] = array( + 'tid' => $term->tid, + ); + } + } + // Set the log owner to the currently authenticated user. global $user; $log->field_farm_log_owner[LANGUAGE_NONE][] = array( diff --git a/modules/farm/farm_quantity/farm_quantity_log/farm_quantity_log.module b/modules/farm/farm_quantity/farm_quantity_log/farm_quantity_log.module index e65672cd..7f1d1433 100644 --- a/modules/farm/farm_quantity/farm_quantity_log/farm_quantity_log.module +++ b/modules/farm/farm_quantity/farm_quantity_log/farm_quantity_log.module @@ -205,14 +205,18 @@ function farm_quantity_log_asset_query($asset_id, $measure = NULL, $label = NULL * 'label' => 'Foo', * ), * ); + * @param string $notes + * Notes to add to the log. + * @param array $categories + * An array of categories to add to the log. * * @return \Log * Returns a saved log entity. */ -function farm_quantity_log_create($type, $name = '', $timestamp = REQUEST_TIME, $done = TRUE, $assets = array(), $measurements = array()) { +function farm_quantity_log_create($type, $name = '', $timestamp = REQUEST_TIME, $done = TRUE, $assets = array(), $measurements = array(), $notes = '', $categories = array()) { // Create a new log entity. - $log = farm_log_create($type, $name, $timestamp, $done, $assets); + $log = farm_log_create($type, $name, $timestamp, $done, $assets, $notes, $categories); // Iterate through the measurements. foreach ($measurements as $measurement) {