diff --git a/modules/farm/farm_log/farm_log_activity/farm_log_activity.module b/modules/farm/farm_log/farm_log_activity/farm_log_activity.module index 817231706..e6f32bd40 100644 --- a/modules/farm/farm_log/farm_log_activity/farm_log_activity.module +++ b/modules/farm/farm_log/farm_log_activity/farm_log_activity.module @@ -41,23 +41,3 @@ function farm_log_activity_farm_taxonomy_term_view_views($term) { ), ); } - -/** - * Implements hook_farm_area_links(). - */ -function farm_log_activity_farm_area_links($id) { - $links = array(); - - // Add link to activities. - $view = views_get_view('farm_log_activity'); - $view->preview('default', array('all', $id)); - if ($view->total_rows > 0) { - $links[] = array( - 'title' => t('Activities') . ': ' . $view->total_rows, - 'href' => 'farm/logs/activities/all/' . $id, - 'weight' => -100, - ); - } - - return $links; -} diff --git a/modules/farm/farm_log/farm_log_input/farm_log_input.module b/modules/farm/farm_log/farm_log_input/farm_log_input.module index 085d9ddee..5855474d5 100644 --- a/modules/farm/farm_log/farm_log_input/farm_log_input.module +++ b/modules/farm/farm_log/farm_log_input/farm_log_input.module @@ -41,23 +41,3 @@ function farm_log_input_farm_taxonomy_term_view_views($term) { ), ); } - -/** - * Implements hook_farm_area_links(). - */ -function farm_log_input_farm_area_links($id) { - $links = array(); - - // Add link to inputs. - $view = views_get_view('farm_log_input'); - $view->preview('default', array('all', $id)); - if ($view->total_rows > 0) { - $links[] = array( - 'title' => t('Inputs') . ': ' . $view->total_rows, - 'href' => 'farm/logs/inputs/all/' . $id, - 'weight' => 10, - ); - } - - return $links; -} diff --git a/modules/farm/farm_log/farm_log_observation/farm_log_observation.module b/modules/farm/farm_log/farm_log_observation/farm_log_observation.module index 8b9bf7c70..9c64b74d4 100644 --- a/modules/farm/farm_log/farm_log_observation/farm_log_observation.module +++ b/modules/farm/farm_log/farm_log_observation/farm_log_observation.module @@ -43,23 +43,3 @@ function farm_log_observation_farm_taxonomy_term_view_views($term) { ), ); } - -/** - * Implements hook_farm_area_links(). - */ -function farm_log_observation_farm_area_links($id) { - $links = array(); - - // Add link to observations. - $view = views_get_view('farm_log_observation'); - $view->preview('default', array('all', $id)); - if ($view->total_rows > 0) { - $links[] = array( - 'title' => t('Observations') . ': ' . $view->total_rows, - 'href' => 'farm/logs/observations/all/' . $id, - 'weight' => -100, - ); - } - - return $links; -} diff --git a/modules/farm/farm_soil/farm_soil_test/farm_soil_test.module b/modules/farm/farm_soil/farm_soil_test/farm_soil_test.module index cce85a992..d732a455b 100644 --- a/modules/farm/farm_soil/farm_soil_test/farm_soil_test.module +++ b/modules/farm/farm_soil/farm_soil_test/farm_soil_test.module @@ -38,22 +38,3 @@ function farm_soil_test_farm_taxonomy_term_view_views($term) { 'farm_log_soil_tests', ); } - -/** - * Implements hook_farm_area_links(). - */ -function farm_soil_test_farm_area_links($id) { - $links = array(); - - // Add link to soil tests. - $view = views_get_view('farm_log_soil_tests'); - $view->preview('default', array($id)); - if ($view->total_rows > 0) { - $links[] = array( - 'title' => t('Soil tests') . ': ' . $view->total_rows, - 'href' => 'farm/logs/soil-tests/' . $id, - ); - } - - return $links; -} diff --git a/modules/farm/farm_ui/farm_ui.api.php b/modules/farm/farm_ui/farm_ui.api.php index a74b73075..bdd02ecd6 100644 --- a/modules/farm/farm_ui/farm_ui.api.php +++ b/modules/farm/farm_ui/farm_ui.api.php @@ -73,7 +73,8 @@ function hook_farm_ui_entities() { 'farm_asset' => 'planting', // Set 'areas' to TRUE if the log type can be used on areas. - // This will add an action link on area pages. + // This will add an action link on area pages, and will show a link in + // the area details popup. 'areas' => TRUE, ), ), diff --git a/modules/farm/farm_ui/farm_ui.module b/modules/farm/farm_ui/farm_ui.module index 95ecb1422..e3101f596 100644 --- a/modules/farm/farm_ui/farm_ui.module +++ b/modules/farm/farm_ui/farm_ui.module @@ -526,5 +526,59 @@ function farm_ui_farm_area_links($id) { } } + // Add area links for some log types. + if (!empty($ui_info['log'])) { + foreach ($ui_info['log'] as $log_type => $info) { + + // Only proceed if the log type has 'areas' set to TRUE. + if (empty($info['areas']) || $info['areas'] !== TRUE) { + continue; + } + + // If a View is not available, skip it. + if (empty($info['view'])) { + continue; + } + + // Log View arguments are a bit more complicated than Asset Views. Most + // logs apply to assets, so have an asset contextual filter first, and an + // area contextual filter second. Some logs do not apply to assets, so + // the first contextual filter is the area ID. The pattern we follow here + // is: if the entity UI info has 'farm_asset' set to 'none', then we + // assume that 'area ID' is the first argument. Otherwise, we assume that + // 'asset ID' is the first, and 'area ID' is the second. In the case of + // area links, we only care about the 'area ID', so we set the 'asset ID' + // argument to 'all'. + if ($info['farm_asset'] == 'none') { + $args = array($id); + } + else { + $args = array('all', $id); + } + + // Load the View and generate a preview to count rows. + $view = views_get_view($info['view']); + $view->preview('default', $args); + + // If there are no results, skip it. + if (empty($view->total_rows)) { + continue; + } + + // Build the View page path with arguments. + $path = farm_ui_view_page_path($info['view']); + foreach ($args as $arg) { + $path .= '/' . $arg; + } + + // Build a link. + $links[] = array( + 'title' => $info['label_plural'] . ': ' . $view->total_rows, + 'href' => $path, + 'weight' => 10, + ); + } + } + return $links; } diff --git a/modules/farm/farm_water/farm_water_test/farm_water_test.module b/modules/farm/farm_water/farm_water_test/farm_water_test.module index 2daf09f89..143c86dc9 100644 --- a/modules/farm/farm_water/farm_water_test/farm_water_test.module +++ b/modules/farm/farm_water/farm_water_test/farm_water_test.module @@ -38,22 +38,3 @@ function farm_water_test_farm_taxonomy_term_view_views($term) { 'farm_water_test', ); } - -/** - * Implements hook_farm_area_links(). - */ -function farm_water_test_farm_area_links($id) { - $links = array(); - - // Add link to water tests. - $view = views_get_view('farm_water_test'); - $view->preview('default', array($id)); - if ($view->total_rows > 0) { - $links[] = array( - 'title' => t('Water tests') . ': ' . $view->total_rows, - 'href' => 'farm/logs/water-tests/' . $id, - ); - } - - return $links; -}