farmOS/farm_area.module

250 lines
5.6 KiB
Plaintext

<?php
/**
* @file
* Code for the Farm Area feature.
*/
include_once 'farm_area.features.inc';
/**
* Implements hook_farm_area_type_info().
*/
function farm_area_farm_area_type_info() {
return array(
'property' => array(
'label' => t('Property'),
'weight' => 0,
),
'building' => array(
'label' => t('Building'),
'weight' => 10,
),
'field' => array(
'label' => t('Field'),
'weight' => 20,
),
'water' => array(
'label' => t('Water feature'),
'weight' => 90,
),
'landmark' => array(
'label' => t('Landmark'),
'weight' => 100,
),
);
}
/**
* Get information about all available area types.
*/
function farm_area_types() {
// Get available types from modules.
$area_types = module_invoke_all('farm_area_type_info');
// Iterate through the types and create an index by weight.
$weight_index = array();
foreach ($area_types as $key => $type) {
// Default weight is zero.
if (empty($type['weight'])) {
$type['weight'] = 0;
}
// Add it to the weight index array.
$weight_index[$key] = $type['weight'];
}
// Sort the weight index array.
asort($weight_index);
// Iterate through the weight index to build the final sorted list.
$area_types_sorted = array();
foreach ($weight_index as $key => $weight) {
$area_types_sorted[$key] = $area_types[$key];
}
// Return the sorted list.
return $area_types_sorted;
}
/**
* Get an array of available farm area type options.
*
* @return array
* Returns an array of farm area type options provided by modules, for use
* in a form select element.
*/
function farm_area_type_options() {
// Start with an empty options array.
$options = array();
// Get available types from modules.
$area_types = module_invoke_all('farm_area_type_info');
// Iterate through the weight index to build the final options list.
foreach ($area_types as $key => $type) {
if (!empty($type['label'])) {
$options[$key] = $type['label'];
}
}
// Return the list of options.
return $options;
}
/**
* Implements hook_farm_admin_actions().
*/
function farm_area_farm_admin_actions() {
// Define farm admin actions.
$actions = array(
'area' => array(
'title' => t('Add an area'),
'href' => 'admin/structure/taxonomy/farm_areas/add',
'views' => array(
'farm_areas',
),
'paths' => array(
'farm',
),
),
);
return $actions;
}
/**
* Implements hook_farm_taxonomy_breadcrumb().
*/
function farm_area_farm_taxonomy_breadcrumb($term) {
$breadcrumb = array();
// If the term is in farm_areas, add a link to /farm/areas.
if ($term->vocabulary_machine_name == 'farm_areas') {
$breadcrumb[] = l(t('Areas'), 'farm/areas');
}
return $breadcrumb;
}
/**
* Implements hook_farm_area_details().
*/
function farm_area_farm_area_details($id) {
// Start a render array.
$output = array();
// Add area links to the details.
$output[] = array(
'#type' => 'markup',
'#markup' => '<p>' . farm_area_get_links($id) . '</p>',
'#weight' => 100,
);
// Return the render array.
return $output;
}
/**
* Generate area details.
*
* @param int $id
* The area id.
*
* @return string
* Returns a string of links.
*/
function farm_area_get_details($id) {
// Call out to modules that want to provide links.
$area_details = module_invoke_all('farm_area_details', check_plain($id));
// Render and return.
return drupal_render($area_details);
}
/**
* Generate area links, sorted by weight.
*
* @param int $id
* The area id.
* @param string $separator
* Characters to use as a separator between links in the returned string.
*
* @return string
* Returns a string of links.
*/
function farm_area_get_links($id, $separator = ' | ') {
// Call out to modules that want to provide links.
$area_links = module_invoke_all('farm_area_links', check_plain($id));
// Build an index of links and their weights.
$weight_index = array();
foreach ($area_links as $key => $link) {
// Default the weight to zero if it hasn't been set.
if (!isset($link['weight'])) {
$link['weight'] = 0;
}
// Assign the weight to the index, based on it's key in the original array.
$weight_index[$key] = $link['weight'];
}
// Sort the index by weight.
asort($weight_index);
// Rebuild the array of links based on their weights.
$sorted_area_links = array();
foreach ($weight_index as $key => $weight) {
$sorted_area_links[] = $area_links[$key];
}
// Generate an array of Drupal links.
$links = array();
foreach ($sorted_area_links as $link) {
// If a title and href are available...
if (!empty($link['title']) && !empty($link['href'])) {
// Build link options.
$options = array();
if (!empty($link['options'])) {
$options = $link['options'];
}
// Build the link and add it to the array.
$links[] = l($link['title'], $link['href'], $options);
}
}
// Convert to a string with a separator and return.
return implode($separator, $links);
}
/**
* Implements hook_page_build().
*/
function farm_area_page_build(&$page) {
// If this is the farm dashboard, display the areas map.
if (current_path() == 'farm') {
// Load the areas map.
$map = \Drupal\openlayers\Openlayers::load('Map', 'farm_areas');
// Build the map and add it to the page content.
$page['content']['farm_areas'] = $map->build();
// Set the weight to -100 so that it appears on top.
$page['content']['farm_areas']['#weight'] = -100;
// Set the content region #sorted flag to FALSE so that it resorts.
$page['content']['#sorted'] = FALSE;
}
}