Move hook_farm_admin_actions() to new farm_ui module as hook_farm_ui_action().

This commit is contained in:
Michael Stenta 2017-06-21 14:46:31 -04:00
parent 6f34c8f98f
commit 449a465c98
22 changed files with 247 additions and 207 deletions

View File

@ -48,3 +48,4 @@ dependencies[] = farm_log
dependencies[] = farm_map
dependencies[] = farm_quantity
dependencies[] = farm_taxonomy
dependencies[] = farm_ui

View File

@ -21,33 +21,6 @@
* Hooks that can be implemented by other modules in order to extend farm_admin.
*/
/**
* Provide links for farm admins.
*
* @return array
* Returns an array of actions and their meta information (see example below).
*/
function hook_farm_admin_actions() {
// Define farm area actions.
$actions = array(
'foo' => array(
'title' => t('Add a foo log'),
'href' => 'log/add/farm_foo',
'paths' => array(
'farm/asset/%/foo',
),
'assets' => array(
'bar',
),
'views' => array(
'foo_view',
),
),
);
return $actions;
}
/**
* Add output to the /farm/help page.
*

View File

@ -69,9 +69,9 @@ function farm_admin_farm_access_perms($role) {
}
/**
* Implements hook_farm_admin_actions().
* Implements hook_farm_ui_actions().
*/
function farm_admin_farm_admin_actions() {
function farm_admin_farm_ui_actions() {
// Add a link to /farm/people for adding new users.
$actions = array(
@ -245,152 +245,6 @@ function farm_admin_navbar() {
return $items;
}
/**
* Implements hook_menu_local_tasks_alter().
*/
function farm_admin_menu_local_tasks_alter(&$data, $router_item, $root_path) {
// Load path actions from modules.
$actions = module_invoke_all('farm_admin_actions');
// If no path actions were provided, bail.
if (empty($actions)) {
return;
}
// Sort by weight.
uasort($actions, 'drupal_sort_weight');
// Add actions to farm/asset/* paths.
if ($router_item['page_callback'] == 'farm_asset_view') {
// Get the asset id from the path.
$asset_id = check_plain(arg(2));
// Iterate through the actions.
foreach ($actions as $name => $action) {
// If the list of assets types is empty, skip it.
if (empty($actions[$name]['assets'])) {
continue;
}
// If 'all' assets are not allowed...
if (!in_array('all', $actions[$name]['assets'])) {
// Load the asset.
$asset = farm_asset_load($asset_id);
// If the asset's type is not in the list of acceptable types, skip it.
if (!in_array($asset->type, $actions[$name]['assets'])) {
continue;
}
}
// Build the action link.
$link = _farm_admin_action_link($actions[$name]['title'], $actions[$name]['href'], array('destination' => 'farm/asset/' . $asset_id, 'farm_asset' => $asset_id));
// Add the action link to the output.
$data['actions']['output'][] = $link;
}
}
// Add actions to Views pages.
elseif ($router_item['page_callback'] == 'views_page') {
// Iterate through the actions.
foreach ($actions as $name => $action) {
// If the list of Views is empty, skip it.
if (empty($actions[$name]['views'])) {
continue;
}
// Get the View name from the first page argument.
$view_name = reset($router_item['page_arguments']);
// If the View is not in the list of acceptable Views, skip it.
if (!in_array($view_name, $actions[$name]['views'])) {
continue;
}
// Build the action link.
$link = _farm_admin_action_link($actions[$name]['title'], $actions[$name]['href'], array('destination' => $root_path));
// Add the action link to the output.
$data['actions']['output'][] = $link;
}
}
// Create a new array of actions keyed by path.
$path_actions = array();
foreach ($actions as $name => $action) {
if (!empty($action['paths'])) {
foreach ($action['paths'] as $path) {
$path_actions[$path][] = $name;
}
}
}
// Add actions depending on the root path.
if (array_key_exists($root_path, $path_actions)) {
foreach ($path_actions[$root_path] as $name) {
// Generate the action link.
$link = _farm_admin_action_link($actions[$name]['title'], $actions[$name]['href'], array('destination' => $root_path));
if ($root_path == 'taxonomy/term/%' || substr($root_path, 0, 11) == 'farm/area/%') {
// Get the area id from the path.
$area_id = check_plain(arg(2));
// Load the area taxonomy term.
$term = taxonomy_term_load($area_id);
// If the taxonomy term is not a farm_area, bail.
if (empty($term->vocabulary_machine_name) || $term->vocabulary_machine_name != 'farm_areas') {
continue;
}
// Build the action link.
$link = _farm_admin_action_link($actions[$name]['title'], $actions[$name]['href'], array('destination' => 'taxonomy/term/' . $area_id, 'farm_area' => $area_id));
}
// Add the action link to the output.
$data['actions']['output'][] = $link;
}
}
}
/**
* Helper function for generating an action link.
*
* @param string $title
* The title of the action link.
* @param string $href
* The href path of the action link.
* @param array $query
* An array of additional query parameters to add.
*
* @return string
* Returns an action link.
*
* @see farm_admin_menu_local_tasks_alter()
*/
function _farm_admin_action_link($title, $href, $query = array()) {
$output = array(
'#theme' => 'menu_local_action',
'#link' => array(
'title' => $title,
'href' => $href,
'localized_options' => array(
'query' => $query,
),
),
);
return $output;
}
/**
* Generate the farm menu.
*

View File

@ -159,9 +159,9 @@ function farm_area_type_options() {
}
/**
* Implements hook_farm_admin_actions().
* Implements hook_farm_ui_actions().
*/
function farm_area_farm_admin_actions() {
function farm_area_farm_ui_actions() {
// Define farm admin actions.
$actions = array(

View File

@ -434,9 +434,9 @@ function farm_asset_entity_view($entity, $type, $view_mode, $langcode) {
}
/**
* Implements hook_farm_admin_actions().
* Implements hook_farm_ui_actions().
*/
function farm_asset_farm_admin_actions() {
function farm_asset_farm_ui_actions() {
// Define farm area actions.
$actions = array(

View File

@ -188,9 +188,9 @@ function farm_crop_farm_area_type_info() {
}
/**
* Implements hook_farm_admin_actions().
* Implements hook_farm_ui_actions().
*/
function farm_crop_farm_admin_actions() {
function farm_crop_farm_ui_actions() {
// Define actions.
$actions = array(

View File

@ -41,9 +41,9 @@ function farm_equipment_farm_access_perms($role) {
}
/**
* Implements hook_farm_admin_actions().
* Implements hook_farm_ui_actions().
*/
function farm_equipment_farm_admin_actions() {
function farm_equipment_farm_ui_actions() {
// Define farm equipment actions.
$actions = array(

View File

@ -58,9 +58,9 @@ function farm_livestock_farm_area_type_info() {
}
/**
* Implements hook_farm_admin_actions().
* Implements hook_farm_ui_actions().
*/
function farm_livestock_farm_admin_actions() {
function farm_livestock_farm_ui_actions() {
// Define farm area actions.
$actions = array(

View File

@ -38,9 +38,9 @@ function farm_livestock_eggs_farm_access_perms($role) {
}
/**
* Implements hook_farm_admin_actions().
* Implements hook_farm_ui_actions().
*/
function farm_livestock_eggs_farm_admin_actions() {
function farm_livestock_eggs_farm_ui_actions() {
// Define farm area actions.
$actions = array(

View File

@ -32,9 +32,9 @@ function farm_log_farm_access_perms($role) {
}
/**
* Implements hook_farm_admin_actions().
* Implements hook_farm_ui_actions().
*/
function farm_log_farm_admin_actions() {
function farm_log_farm_ui_actions() {
// Define farm area actions.
$actions = array(

View File

@ -39,9 +39,9 @@ function farm_log_activity_farm_access_perms($role) {
}
/**
* Implements hook_farm_admin_actions().
* Implements hook_farm_ui_actions().
*/
function farm_log_activity_farm_admin_actions() {
function farm_log_activity_farm_ui_actions() {
// Define farm area actions.
$actions = array(

View File

@ -39,9 +39,9 @@ function farm_log_harvest_farm_access_perms($role) {
}
/**
* Implements hook_farm_admin_actions().
* Implements hook_farm_ui_actions().
*/
function farm_log_harvest_farm_admin_actions() {
function farm_log_harvest_farm_ui_actions() {
// Define farm area actions.
$actions = array(

View File

@ -39,9 +39,9 @@ function farm_log_input_farm_access_perms($role) {
}
/**
* Implements hook_farm_admin_actions().
* Implements hook_farm_ui_actions().
*/
function farm_log_input_farm_admin_actions() {
function farm_log_input_farm_ui_actions() {
// Define farm area actions.
$actions = array(

View File

@ -39,9 +39,9 @@ function farm_log_observation_farm_access_perms($role) {
}
/**
* Implements hook_farm_admin_actions().
* Implements hook_farm_ui_actions().
*/
function farm_log_observation_farm_admin_actions() {
function farm_log_observation_farm_ui_actions() {
// Define farm area actions.
$actions = array(

View File

@ -38,9 +38,9 @@ function farm_log_sale_farm_access_perms($role) {
}
/**
* Implements hook_farm_admin_actions().
* Implements hook_farm_ui_actions().
*/
function farm_log_sale_farm_admin_actions() {
function farm_log_sale_farm_ui_actions() {
// Define farm area actions.
$actions = array(

View File

@ -7,9 +7,9 @@
include_once 'farm_sensor.features.inc';
/**
* Implements hook_farm_admin_actions().
* Implements hook_farm_ui_actions().
*/
function farm_sensor_farm_admin_actions() {
function farm_sensor_farm_ui_actions() {
// Define farm sensor actions.
$actions = array(

View File

@ -39,9 +39,9 @@ function farm_soil_compost_farm_access_perms($role) {
/**
* Implements hook_farm_admin_actions().
* Implements hook_farm_ui_actions().
*/
function farm_soil_compost_farm_admin_actions() {
function farm_soil_compost_farm_ui_actions() {
// Define farm compost actions.
$actions = array(

View File

@ -41,9 +41,9 @@ function farm_soil_test_farm_access_perms($role) {
}
/**
* Implements hook_farm_admin_actions().
* Implements hook_farm_ui_actions().
*/
function farm_soil_test_farm_admin_actions() {
function farm_soil_test_farm_ui_actions() {
// Define farm area actions.
$actions = array(

View File

@ -0,0 +1,54 @@
<?php
/**
* @file
* Hooks provided by farm_ui.
*
* This file contains no working PHP code; it exists to provide additional
* documentation for doxygen as well as to document hooks in the standard
* Drupal manner.
*/
/**
* @defgroup farm_ui Farm UI module integrations.
*
* Module integrations with the farm_ui module.
*/
/**
* @defgroup farm_ui_hooks Farm UI's hooks
* @{
* Hooks that can be implemented by other modules in order to extend
* farm_ui.
*/
/**
* Provide action links on specific paths, asset types, and views.
*
* @return array
* Returns an array of actions and their meta information (see example below).
*/
function hook_farm_ui_actions() {
// Define farm area actions.
$actions = array(
'foo' => array(
'title' => t('Add a foo log'),
'href' => 'log/add/farm_foo',
'paths' => array(
'farm/asset/%/foo',
),
'assets' => array(
'bar',
),
'views' => array(
'foo_view',
),
),
);
return $actions;
}
/**
* @}
*/

View File

@ -0,0 +1,6 @@
name = Farm UI
description = Provides standardized user interface elements to farmOS modules.
core = 7.x
package = farmOS
dependencies[] = farm_asset
dependencies[] = taxonomy

View File

@ -0,0 +1,152 @@
<?php
/**
* @file
* Farm UI module code.
*/
/**
* Implements hook_menu_local_tasks_alter().
*/
function farm_ui_menu_local_tasks_alter(&$data, $router_item, $root_path) {
// Load path actions from modules.
$actions = module_invoke_all('farm_ui_actions');
// If no path actions were provided, bail.
if (empty($actions)) {
return;
}
// Sort by weight.
uasort($actions, 'drupal_sort_weight');
// Add actions to farm/asset/* paths.
if ($router_item['page_callback'] == 'farm_asset_view') {
// Get the asset id from the path.
$asset_id = check_plain(arg(2));
// Iterate through the actions.
foreach ($actions as $name => $action) {
// If the list of assets types is empty, skip it.
if (empty($actions[$name]['assets'])) {
continue;
}
// If 'all' assets are not allowed...
if (!in_array('all', $actions[$name]['assets'])) {
// Load the asset.
$asset = farm_asset_load($asset_id);
// If the asset's type is not in the list of acceptable types, skip it.
if (!in_array($asset->type, $actions[$name]['assets'])) {
continue;
}
}
// Build the action link.
$link = _farm_ui_action_link($actions[$name]['title'], $actions[$name]['href'], array('destination' => 'farm/asset/' . $asset_id, 'farm_asset' => $asset_id));
// Add the action link to the output.
$data['actions']['output'][] = $link;
}
}
// Add actions to Views pages.
elseif ($router_item['page_callback'] == 'views_page') {
// Iterate through the actions.
foreach ($actions as $name => $action) {
// If the list of Views is empty, skip it.
if (empty($actions[$name]['views'])) {
continue;
}
// Get the View name from the first page argument.
$view_name = reset($router_item['page_arguments']);
// If the View is not in the list of acceptable Views, skip it.
if (!in_array($view_name, $actions[$name]['views'])) {
continue;
}
// Build the action link.
$link = _farm_ui_action_link($actions[$name]['title'], $actions[$name]['href'], array('destination' => $root_path));
// Add the action link to the output.
$data['actions']['output'][] = $link;
}
}
// Create a new array of actions keyed by path.
$path_actions = array();
foreach ($actions as $name => $action) {
if (!empty($action['paths'])) {
foreach ($action['paths'] as $path) {
$path_actions[$path][] = $name;
}
}
}
// Add actions depending on the root path.
if (array_key_exists($root_path, $path_actions)) {
foreach ($path_actions[$root_path] as $name) {
// Generate the action link.
$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/%') {
// Get the area id from the path.
$area_id = check_plain(arg(2));
// Load the area taxonomy term.
$term = taxonomy_term_load($area_id);
// If the taxonomy term is not a farm_area, bail.
if (empty($term->vocabulary_machine_name) || $term->vocabulary_machine_name != 'farm_areas') {
continue;
}
// Build the action link.
$link = _farm_ui_action_link($actions[$name]['title'], $actions[$name]['href'], array('destination' => 'taxonomy/term/' . $area_id, 'farm_area' => $area_id));
}
// Add the action link to the output.
$data['actions']['output'][] = $link;
}
}
}
/**
* Helper function for generating an action link.
*
* @param string $title
* The title of the action link.
* @param string $href
* The href path of the action link.
* @param array $query
* An array of additional query parameters to add.
*
* @return array
* Returns an action link.
*
* @see farm_ui_menu_local_tasks_alter()
*/
function _farm_ui_action_link($title, $href, $query = array()) {
$action_link = array(
'#theme' => 'menu_local_action',
'#link' => array(
'title' => $title,
'href' => $href,
'localized_options' => array(
'query' => $query,
),
),
);
return $action_link;
}

View File

@ -38,9 +38,9 @@ function farm_water_test_farm_access_perms($role) {
}
/**
* Implements hook_farm_admin_actions().
* Implements hook_farm_ui_actions().
*/
function farm_water_test_farm_admin_actions() {
function farm_water_test_farm_ui_actions() {
// Define farm area actions.
$actions = array(