View source
<?php
function _farm_ui_menu_local_tasks_alter(&$data, $router_item, $root_path) {
$actions = module_invoke_all('farm_ui_actions');
uasort($actions, 'drupal_sort_weight');
if ($router_item['page_callback'] == 'farm_asset_view') {
$asset_id = check_plain(arg(2));
foreach ($actions as $name => $action) {
if (empty($actions[$name]['assets'])) {
continue;
}
if (!in_array('all', $actions[$name]['assets'])) {
$asset = farm_asset_load($asset_id);
if (!in_array($asset->type, $actions[$name]['assets'])) {
continue;
}
}
$link = farm_ui_action_link($actions[$name]['title'], $actions[$name]['href'], array(
'destination' => 'farm/asset/' . $asset_id,
'farm_asset' => $asset_id,
));
if (!empty($link)) {
$data['actions']['output'][] = $link;
}
}
}
elseif ($router_item['page_callback'] == 'views_page') {
foreach ($actions as $name => $action) {
if (empty($actions[$name]['views'])) {
continue;
}
$view_name = reset($router_item['page_arguments']);
if (!in_array($view_name, $actions[$name]['views'])) {
continue;
}
$link = farm_ui_action_link($actions[$name]['title'], $actions[$name]['href'], array(
'destination' => $root_path,
));
if (!empty($link)) {
$data['actions']['output'][] = $link;
}
}
}
$path_actions = array();
foreach ($actions as $name => $action) {
if (!empty($action['paths'])) {
foreach ($action['paths'] as $path) {
$path_actions[$path][] = $name;
}
}
}
if (array_key_exists($root_path, $path_actions)) {
foreach ($path_actions[$root_path] as $name) {
$link = farm_ui_action_link($actions[$name]['title'], $actions[$name]['href'], array(
'destination' => $root_path,
));
if ($root_path == 'taxonomy/term/%' || substr($root_path, 0, 11) == 'farm/area/%') {
$area_id = check_plain(arg(2));
$term = taxonomy_term_load($area_id);
if (empty($term->vocabulary_machine_name) || $term->vocabulary_machine_name != 'farm_areas') {
continue;
}
$link = farm_ui_action_link($actions[$name]['title'], $actions[$name]['href'], array(
'destination' => 'taxonomy/term/' . $area_id,
'farm_area' => $area_id,
));
}
if (!empty($link)) {
$data['actions']['output'][] = $link;
}
}
}
}
function farm_ui_action_link($title, $path, $query = array()) {
if (!drupal_valid_path($path)) {
return array();
}
$action_link = array(
'#theme' => 'menu_local_action',
'#link' => array(
'title' => $title,
'href' => $path,
'localized_options' => array(
'query' => $query,
),
),
);
return $action_link;
}