3
0
Fork 0
mirror of https://github.com/farmOS/farmOS.git synced 2024-02-23 11:37:38 +01:00
farmOS/modules/farm/farm_taxonomy/farm_taxonomy.module
2017-05-30 16:01:00 -04:00

191 lines
5.1 KiB
Plaintext

<?php
/**
* @file
* Code for the Farm Taxonomy feature.
*/
include_once 'farm_taxonomy.features.inc';
/**
* Implements hook_farm_access_perms().
*/
function farm_taxonomy_farm_access_perms($role) {
// Assemble a list of entity types provided by this module.
$types = array(
'taxonomy' => array(
'farm_log_categories',
'farm_materials',
'farm_season',
),
);
// Grant different CRUD permissions based on the role.
$perms = array();
switch ($role) {
// Farm Manager and Worker
case 'Farm Manager':
case 'Farm Worker':
$perms = farm_access_entity_perms($types);
break;
// Farm Viewer
case 'Farm Viewer':
$perms = farm_access_entity_perms($types, array('view'));
break;
}
return $perms;
}
/**
* Implements hook_entity_view_alter().
*/
function farm_taxonomy_entity_view_alter(&$build, $type) {
// If the entity is not a taxonomy term, bail.
if ($type != 'taxonomy_term') {
return;
}
// If the term entity is not there, bail.
if (empty($build['#term'])) {
return;
}
// Pull the term entity out of the build array.
$term = $build['#term'];
// Set the term page breadcrumb.
_farm_taxonomy_term_set_breadcrumb($term);
// Invoke hook_farm_taxonomy_term_view_views() to get a list of Views.
$views = module_invoke_all('farm_taxonomy_term_view_views', $term);
// Process the list of Views into a standardized list,
// and prepare to order by weight and name.
$weight_index = array();
$name_index = array();
foreach ($views as $key => $data) {
// If the data is just a name, wrap it in an array.
if (!is_array($data)) {
$data = array(
'name' => $data,
);
}
// Merge with defaults.
$defaults = array(
'arg' => 1,
'weight' => 0,
);
$views[$key] = array_merge($defaults, $data);
// Add to the weight and name indexes for sorting.
$weight_index[$key] = $views[$key]['weight'];
$name_index[$key] = $views[$key]['name'];
}
// Sort the Views by weight ascending, name ascending.
array_multisort($weight_index, SORT_ASC, $name_index, SORT_ASC, $views);
// Add the Views to the entity's render array.
foreach ($views as $key => $data) {
// Load the View, and bail if it isn't found.
$view = views_get_view($data['name']);
if (empty($view)) {
continue;
}
// Determine the argument position (default to 1).
// This looks for the presence of $data['arg'] to learn which argument
// in the View we should send $term->tid into. This is useful if the View
// has multiple contextual filters, and the term filter is not first.
// Any arguments that come before the term argument will receive 'all'
// as their input.
$args = array();
$arg_pos = isset($data['arg']) ? $data['arg'] : 1;
for ($i = 1; $i <= $arg_pos; $i++) {
if ($i == $arg_pos) {
$args[] = $term->tid;
}
else {
$args[] = 'all';
}
}
// If a specific display was specified, use it. Otherwise use 'default'.
if (!empty($data['display'])) {
$display = $data['display'];
}
else {
$display = 'default';
}
$view->set_display($display);
// Get the View's default title.
// We intentionally do this before we build the preview so that the title
// is not overridden by the arguments. This keeps the title simple on the
// actual term page, but more descriptive in other contexts.
$title = $view->get_title();
// Build the View preview.
$preview = $view->preview($display, $args);
// Only display if the View has results (and 'always' is not TRUE).
if (empty($data['always']) && $view->total_rows == 0) {
continue;
}
// Build the output.
$output = '<h3 id="' . $title . '">' . $title . '</h3>' . $preview;
// Add the output to the entity build array.
$build[$data['name']] = array(
'#markup' => $output,
'#weight' => 100 + $key,
);
}
}
/**
* Helper function for setting farm taxonomy term page breadcrumbs.
*
* @param object $term
* The taxonomy term.
*/
function _farm_taxonomy_term_set_breadcrumb($term) {
// Ask other modules for breadcrumbs for this term.
// We don't know if this term is part of a farm vocabulary yet, so we will
// only set the breadcrumb if this returns results.
$module_breadcrumbs = module_invoke_all('farm_taxonomy_breadcrumb', $term);
// If other modules provided breadcrumbs...
if (!empty($module_breadcrumbs)) {
// Get the default breadcrumb.
$default_breadcrumb = drupal_get_breadcrumb();
// Shift the first item off (home) and start a new breadcrumb array with it.
$home = array_shift($default_breadcrumb);
$breadcrumb = array($home);
// Add "Farm".
$farm = l(t('Farm'), 'farm');
$breadcrumb[] = $farm;
// Add the breadcrumbs provided by other modules.
$breadcrumb = array_merge($breadcrumb, $module_breadcrumbs);
// Finally, put the original breadcrumbs back onto the end.
$breadcrumb = array_merge($breadcrumb, $default_breadcrumb);
// Set the breadcrumb.
drupal_set_breadcrumb($breadcrumb);
}
}