units_ui.module in Units of Measurement 7
Same filename and directory in other branches
Provide UI for managing available units and conversions between them.
File
units_ui.moduleView source
<?php
/**
* @file
* Provide UI for managing available units and conversions between them.
*/
/**
* Implements hook_permission().
*/
function units_ui_permission() {
return array(
'administer measures' => array(
'title' => t('Administer measurements'),
'description' => t('Add/edit/delete available physical measures.'),
),
'administer units' => array(
'title' => t('Administer measurement units'),
'description' => t('Add/edit/delete available units of measurement.'),
),
);
}
/**
* Implements hook_admin_paths().
*/
function units_ui_admin_paths() {
return array(
'units-ui/*' => TRUE,
);
}
/**
* Implements hook_entity_info_alter().
*
* Add UI controller info to entity types 'units_measure' and 'units_unit' and
* alter access callback to one provided by units_ui module, which respects
* permissions defined in units_ui module.
*/
function units_ui_entity_info_alter(&$entity_info) {
$entity_info['units_unit']['admin ui'] = array(
'path' => 'units-ui/%units_measure_machine_name',
// This number denotes what position in path the bundle argument is
// located at.
'path bundle argument position' => 1,
'file' => 'units_ui.pages.inc',
'controller class' => 'UnitsUnitUIController',
);
$entity_info['units_unit']['access callback'] = 'units_ui_entity_access';
// For each bundle we add ability to manage fields via Field API.
foreach ($entity_info['units_unit']['bundles'] as $bundle => $bundle_info) {
$entity_info['units_unit']['bundles'][$bundle]['admin'] = array(
'path' => 'admin/structure/units-measure/manage/%units_measure_machine_name',
'real path' => 'admin/structure/units-measure/manage/' . $bundle,
'bundle argument' => 4,
);
}
$entity_info['units_unit']['translation'] = array(
'entity_translation' => array(
'class' => 'UnitsEntityTranslationHandler',
'base path' => $entity_info['units_unit']['admin ui']['path'] . '/manage/%units_unit_machine_name',
'path wildcard' => '%units_unit_machine_name',
// This is a custom property from Units UI module that will be used in
// its UnitsEntityTranslationHandler class.
'bundle argument' => 1,
),
);
$entity_info['units_measure']['admin ui'] = array(
'path' => 'admin/structure/units-measure',
'file' => 'units_ui.pages.inc',
'controller class' => 'UnitsMeasureUIController',
);
$entity_info['units_measure']['access callback'] = 'units_ui_entity_access';
}
/**
* Implements hook_menu_breadcrumb_alter().
*/
function units_ui_menu_breadcrumb_alter(&$active_trail, $item) {
// We want to embed the link to unit measures overview page on all pages that
// are related to managements of units.
$prefix = 'units-ui/';
$tail = array();
$active_trail = array_reverse($active_trail);
foreach ($active_trail as $k => $trail) {
if (drupal_substr($trail['href'], 0, drupal_strlen($prefix)) == $prefix) {
$tail[] = $trail;
unset($active_trail[$k]);
}
else {
break;
}
}
$active_trail = array_reverse($active_trail);
if (drupal_substr($item['path'], 0, drupal_strlen($prefix)) == $prefix) {
$measure_entity_info = entity_get_info('units_measure');
$active_trail[] = menu_get_item($measure_entity_info['admin ui']['path']);
}
$active_trail = array_merge($active_trail, array_reverse($tail));
}
/**
* Access callback for entity types 'units_measure' and 'units_unit'.
*
* This access callback overrides one defined in units.module once units_ui
* module becomes enabled. This access callback respects permissions defined
* in units_ui module.
*
* @param string $op
* The operation being performed. One of 'view', 'update', 'create' or
* 'delete'
* @param object $entity
* Entity object on which the operation is requested to be performed
* @param object $account
* Fully loaded user object of the account who requests to perform the
* operation
* @param string $entity_type
* Entity type on which the operation is requested to be performed
*
* @return bool
* Whether access has been granted
*/
function units_ui_entity_access($op, $entity, $account, $entity_type) {
switch ($entity_type) {
case 'units_measure':
$perm = 'administer measures';
break;
case 'units_unit':
$perm = 'administer units';
break;
}
if (!isset($perm)) {
// Seems like this access callback is triggered on an unexpected entity
// type.
return FALSE;
}
switch ($op) {
case 'view':
return TRUE;
break;
case 'update':
case 'create':
case 'delete':
return user_access($perm, $account);
break;
}
// Just in case, fallback on negative response. Although normally this
// statement should never be reached.
return FALSE;
}
/**
* Menu title callback for UI of Units Unit entity.
*
* @param string $measure
* Bundle string of units unit entity type
*
* @return string
* Title of menu path
*/
function units_ui_unit_title($measure) {
$unit_info = entity_get_info('units_unit');
$entity_label = isset($unit_info['plural label']) ? $unit_info['plural label'] : $unit_info['label'] . 's';
return t('@entity_label of @entity_bundle_label', array(
'@entity_label' => $entity_label,
'@entity_bundle_label' => entity_label('units_measure', $measure),
));
}
Functions
Name | Description |
---|---|
units_ui_admin_paths | Implements hook_admin_paths(). |
units_ui_entity_access | Access callback for entity types 'units_measure' and 'units_unit'. |
units_ui_entity_info_alter | Implements hook_entity_info_alter(). |
units_ui_menu_breadcrumb_alter | Implements hook_menu_breadcrumb_alter(). |
units_ui_permission | Implements hook_permission(). |
units_ui_unit_title | Menu title callback for UI of Units Unit entity. |