Merge hook_farm_ui_asset_views() and hook_farm_ui_taxonomy_views().

This commit is contained in:
Michael Stenta 2017-06-27 10:16:58 -04:00
parent d644e54b6d
commit 3f16718b4a
14 changed files with 202 additions and 313 deletions

View File

@ -194,63 +194,51 @@ function farm_crop_farm_ui_entities() {
}
/**
* Implements hook_farm_ui_asset_views().
* Implements hook_farm_ui_entity_views().
*/
function farm_crop_farm_ui_asset_views($farm_asset) {
// If the entity is not a planting, bail.
if ($farm_asset->type != 'planting') {
return array();
}
function farm_crop_farm_ui_entity_views($entity_type, $bundle, $entity) {
$views = array();
// Return a list of Views to include on Plantings.
return array(
array(
if ($entity_type == 'farm_asset' && $bundle == 'planting') {
$views[] = array(
'name' => 'farm_log_movement',
'weight' => 100,
),
array(
);
$views[] = array(
'name' => 'farm_asset_children',
'display' => 'page',
'title' => t('Children'),
'weight' => 110,
),
);
}
);
}
/**
* Implements hook_farm_ui_taxonomy_views().
*/
function farm_crop_farm_ui_taxonomy_views($term) {
// If the entity is a taxonomy_term...
elseif ($entity_type == 'taxonomy_term') {
switch ($entity->vocabulary_machine_name) {
// Start a list of View names.
$views = array();
// Farm areas:
case 'farm_areas':
$views[] = array(
'name' => 'farm_plantings',
'weight' => -20,
);
break;
// Switch logic depending on the vocabulary.
switch ($term->vocabulary_machine_name) {
// Farm crops.
case 'farm_crops':
$views[] = array(
'name' => 'farm_plantings',
'arg' => 2,
'always' => TRUE,
);
break;
// Farm areas:
case 'farm_areas':
$views[] = array(
'name' => 'farm_plantings',
'arg' => 1,
'weight' => -20,
);
break;
// Farm crops.
case 'farm_crops':
$views[] = array(
'name' => 'farm_plantings',
'arg' => 2,
'always' => TRUE,
);
break;
// Farm crop family.
case 'farm_crop_families':
$views[] = 'farm_crops';
break;
// Farm crop family.
case 'farm_crop_families':
$views[] = 'farm_crops';
break;
}
}
return $views;

View File

@ -31,41 +31,28 @@ function farm_equipment_farm_ui_entities() {
}
/**
* Implements hook_farm_ui_asset_views().
* Implements hook_farm_ui_entity_views().
*/
function farm_equipment_farm_ui_asset_views($farm_asset) {
// If the entity is not equipment, bail.
if ($farm_asset->type != 'equipment') {
return array();
}
function farm_equipment_farm_ui_entity_views($entity_type, $bundle, $entity) {
$views = array();
// Return a list of Views to include on Equipment.
return array(
array(
if ($entity_type == 'farm_asset' && $bundle == 'equipment') {
$views[] = array(
'name' => 'farm_log_movement',
'weight' => 100,
),
);
}
/**
* Implements hook_farm_ui_taxonomy_views().
*/
function farm_equipment_farm_ui_taxonomy_views($term) {
// If the term is not an area, bail.
if ($term->vocabulary_machine_name != 'farm_areas') {
return array();
);
}
// Return a list of Views to include on Areas.
return array(
array(
// Show equipment in area pages.
elseif ($entity_type == 'taxonomy_term' && $entity->vocabulary_machine_name == 'farm_areas') {
$views[] = array(
'name' => 'farm_equipment',
'weight' => -10,
),
);
);
}
return $views;
}
/**

View File

@ -58,66 +58,55 @@ function farm_livestock_farm_area_type_info() {
}
/**
* Implements hook_farm_ui_asset_views().
* Implements hook_farm_ui_entity_views().
*/
function farm_livestock_farm_ui_asset_views($farm_asset) {
// If the entity is not an animal, bail.
if ($farm_asset->type != 'animal') {
return array();
}
function farm_livestock_farm_ui_entity_views($entity_type, $bundle, $entity) {
$views = array();
// Return a list of Views to include on Animals.
return array(
array(
if ($entity_type == 'farm_asset' && $bundle == 'animal') {
$views[] = array(
'name' => 'farm_log_movement',
'weight' => 100,
),
array(
);
$views[] = array(
'name' => 'farm_asset_children',
'display' => 'page',
'title' => t('Children'),
'weight' => 110,
),
);
}
);
}
/**
* Implements hook_farm_ui_taxonomy_views().
*/
function farm_livestock_farm_ui_taxonomy_views($term) {
// If the entity is a taxonomy_term...
elseif ($entity_type == 'taxonomy_term') {
switch ($entity->vocabulary_machine_name) {
// Start a list of View names.
$views = array();
// Farm areas:
case 'farm_areas':
$views[] = array(
'name' => 'farm_animals',
'weight' => -10,
);
break;
// Switch logic depending on the vocabulary.
switch ($term->vocabulary_machine_name) {
// Farm animal groups:
case 'farm_animal_groups':
$views[] = array(
'name' => 'farm_animals',
'arg' => 2,
'always' => TRUE,
);
break;
// Farm areas:
case 'farm_areas':
$views[] = array(
'name' => 'farm_animals',
'weight' => -10,
);
break;
// Farm animal groups:
case 'farm_animal_groups':
$views[] = array(
'name' => 'farm_animals',
'arg' => 2,
'always' => TRUE,
);
break;
// Farm animal types:
case 'farm_animal_types':
$views[] = array(
'name' => 'farm_animals',
'arg' => 3,
'always' => TRUE,
);
break;
// Farm animal types:
case 'farm_animal_types':
$views[] = array(
'name' => 'farm_animals',
'arg' => 3,
'always' => TRUE,
);
break;
}
}
return $views;

View File

@ -25,20 +25,18 @@ function farm_log_activity_farm_ui_entities() {
}
/**
* Implements hook_farm_ui_taxonomy_views().
* Implements hook_farm_ui_entity_views().
*/
function farm_log_activity_farm_ui_taxonomy_views($term) {
// If the term is not an area, bail.
if ($term->vocabulary_machine_name != 'farm_areas') {
return array();
}
function farm_log_activity_farm_ui_entity_views($entity_type, $bundle, $entity) {
$views = array();
// Return a list of Views to include on Areas.
return array(
array(
if ($entity_type == 'taxonomy_term' && $entity->vocabulary_machine_name == 'farm_areas') {
$views[] = array(
'name' => 'farm_log_activity',
'arg' => 2,
),
);
);
}
return $views;
}

View File

@ -25,20 +25,18 @@ function farm_log_input_farm_ui_entities() {
}
/**
* Implements hook_farm_ui_taxonomy_views().
* Implements hook_farm_ui_entity_views().
*/
function farm_log_input_farm_ui_taxonomy_views($term) {
// If the term is not an area, bail.
if ($term->vocabulary_machine_name != 'farm_areas') {
return array();
}
function farm_log_input_farm_ui_entity_views($entity_type, $bundle, $entity) {
$views = array();
// Return a list of Views to include on Areas.
return array(
array(
if ($entity_type == 'taxonomy_term' && $entity->vocabulary_machine_name == 'farm_areas') {
$views[] = array(
'name' => 'farm_log_input',
'arg' => 2,
),
);
);
}
return $views;
}

View File

@ -11,25 +11,23 @@ include_once 'farm_log_movement.features.inc';
include_once 'farm_log_movement.location.inc';
/**
* Implements hook_farm_ui_taxonomy_views().
* Implements hook_farm_ui_entity_views().
*/
function farm_log_movement_farm_ui_taxonomy_views($term) {
// If the term is not an area, bail.
if ($term->vocabulary_machine_name != 'farm_areas') {
return array();
}
function farm_log_movement_farm_ui_entity_views($entity_type, $bundle, $entity) {
$views = array();
// Return a list of Views to include on Areas.
return array(
if ($entity_type == 'taxonomy_term' && $entity->vocabulary_machine_name == 'farm_areas') {
// Area asset history (at the bottom).
array(
$views[] = array(
'name' => 'farm_area_assets',
'weight' => 100,
'always' => TRUE,
),
);
);
}
return $views;
}
/**

View File

@ -25,22 +25,18 @@ function farm_log_observation_farm_ui_entities() {
}
/**
* Implements hook_farm_ui_taxonomy_views().
* Implements hook_farm_ui_entity_views().
*/
function farm_log_observation_farm_ui_taxonomy_views($term) {
// If the term is not an area, bail.
if ($term->vocabulary_machine_name != 'farm_areas') {
return array();
}
function farm_log_observation_farm_ui_entity_views($entity_type, $bundle, $entity) {
$views = array();
// Return a list of Views to include on Areas.
return array(
// Observations in this area.
array(
if ($entity_type == 'taxonomy_term' && $entity->vocabulary_machine_name == 'farm_areas') {
$views[] = array(
'name' => 'farm_log_observation',
'arg' => 2,
),
);
);
}
return $views;
}

View File

@ -22,40 +22,27 @@ function farm_sensor_farm_ui_entities() {
}
/**
* Implements hook_farm_ui_asset_views().
* Implements hook_farm_ui_entity_views().
*/
function farm_sensor_farm_ui_asset_views($farm_asset) {
// If the entity is not sensor, bail.
if ($farm_asset->type != 'sensor') {
return array();
}
function farm_sensor_farm_ui_entity_views($entity_type, $bundle, $entity) {
$views = array();
// Return a list of Views to include on Sensors.
return array(
array(
if ($entity_type == 'farm_asset' && $bundle == 'sensor') {
$views[] = array(
'name' => 'farm_log_movement',
'weight' => 100,
),
);
}
/**
* Implements hook_farm_ui_taxonomy_views().
*/
function farm_sensor_farm_ui_taxonomy_views($term) {
// If the term is not an area, bail.
if ($term->vocabulary_machine_name != 'farm_areas') {
return array();
);
}
// Return a list of Views to include on Areas.
return array(
array(
// Show sensors in area pages.
elseif ($entity_type == 'taxonomy_term' && $entity->vocabulary_machine_name == 'farm_areas') {
$views[] = array(
'name' => 'farm_sensors',
),
);
);
}
return $views;
}
/**

View File

@ -524,22 +524,16 @@ function farm_sensor_listener_load($key) {
}
/**
* Implements hook_farm_ui_asset_views().
* Implements hook_farm_ui_entity_views().
*/
function farm_sensor_listener_farm_ui_asset_views($farm_asset) {
function farm_sensor_listener_farm_ui_entity_views($entity_type, $bundle, $entity) {
// If the entity is not a sensor, bail.
if ($farm_asset->type != 'sensor') {
return array();
// Show sensor data View on listener sensors.
$views = array();
if ($entity_type == 'farm_asset' && $bundle == 'sensor' && $entity->sensor_type == 'listener') {
$views[] = array(
'farm_sensor_data',
);
}
// If the sensor is not a listener, bail.
if ($farm_asset->sensor_type != 'listener') {
return array();
}
// Return a list of Views to include on listener sensors.
return array(
'farm_sensor_data',
);
return $views;
}

View File

@ -22,46 +22,33 @@ function farm_soil_compost_farm_ui_entities() {
}
/**
* Implements hook_farm_ui_asset_views().
* Implements hook_farm_ui_entity_views().
*/
function farm_soil_compost_farm_ui_asset_views($farm_asset) {
// If the entity is not a compost bail.
if ($farm_asset->type != 'compost') {
return array();
}
function farm_soil_compost_farm_ui_entity_views($entity_type, $bundle, $entity) {
$views = array();
// Return a list of Views to include on Compost assets.
return array(
array(
if ($entity_type == 'farm_asset' && $bundle == 'compost') {
$views[] = array(
'name' => 'farm_log_movement',
'weight' => 100,
),
array(
);
$views[] = array(
'name' => 'farm_asset_children',
'display' => 'page',
'title' => t('Children'),
'weight' => 110,
),
);
}
/**
* Implements hook_farm_ui_taxonomy_views().
*/
function farm_soil_compost_farm_ui_taxonomy_views($term) {
// If the term is not an area, bail.
if ($term->vocabulary_machine_name != 'farm_areas') {
return array();
);
}
// Return a list of Views to include on Areas.
return array(
array(
// Show compost in area pages.
elseif ($entity_type == 'taxonomy_term' && $entity->vocabulary_machine_name == 'farm_areas') {
$views[] = array(
'name' => 'farm_compost',
),
);
);
}
return $views;
}
/**

View File

@ -24,17 +24,17 @@ function farm_soil_test_farm_ui_entities() {
}
/**
* Implements hook_farm_ui_taxonomy_views().
* Implements hook_farm_ui_entity_views().
*/
function farm_soil_test_farm_ui_taxonomy_views($term) {
// If the term is not an area, bail.
if ($term->vocabulary_machine_name != 'farm_areas') {
return array();
}
function farm_soil_test_farm_ui_entity_views($entity_type, $bundle, $entity) {
$views = array();
// Return a list of Views to include on Areas.
return array(
'farm_log_soil_tests',
);
if ($entity_type == 'taxonomy_term' && $entity->vocabulary_machine_name == 'farm_areas') {
$views[] = array(
'name' => 'farm_log_soil_tests',
);
}
return $views;
}

View File

@ -85,7 +85,7 @@ function hook_farm_ui_entities() {
// View absolutely needs to be at the very top or the very bottom of
// the list.
/**
* @see hook_farm_ui_asset_views() and hook_farm_ui_taxonomy_views()
* @see hook_farm_ui_entity_views()
*/
'weight' => 10,
),
@ -115,18 +115,32 @@ function hook_farm_ui_entities() {
}
/**
* Attach Views to asset view pages.
* Attach Views to an entity page.
*
* @param FarmAsset $farm_asset
* The farm asset entity.
* @param $entity_type
* The entity type. Currently supports: 'farm_asset' or 'taxonomy_term'.
* @param $bundle
* The entity bundle.
* @param $entity
* The loaded entity object.
*
* @return array
* Returns an array of View names to attach to farm asset pages.
* Returns an array of View to attach to taxonomy term pages.
* Each element in the array can either be the name of a View,
* or an array of options, including:
* 'name' - the machine name of the View
* 'display' - which display of the View should be used
* 'arg' - which argument the term id should be passed to in the View
* (this is useful if the View has more than one contextual filter)
* 'weight' - the weight of the View in the taxonomy page
* (this is useful for changing the order of Views)
* 'always' - always display, even if there are no View results
* (default is FALSE)
*/
function hook_farm_ui_asset_views(FarmAsset $farm_asset) {
function hook_farm_ui_entity_views($entity_type, $bundle, $entity) {
// If the entity is not a planting, bail.
if ($farm_asset->type != 'planting') {
// If the entity is not a planting asset, bail.
if (!($entity_type == 'farm_asset' && $bundle == 'planting')) {
return array();
}
@ -138,48 +152,6 @@ function hook_farm_ui_asset_views(FarmAsset $farm_asset) {
// Example 2: explicitly set details like display, argument position,
// and weight.
array(
'name' => 'farm_log_input',
'display' => 'block',
'arg' => 2,
'weight' => 10,
),
);
}
/**
* Attach Views to taxonomy term pages.
*
* @param object $term
* The taxonomy term entity.
*
* @return array
* Returns an array of View to attach to taxonomy term pages.
* Each element in the array can either be the name of a View,
* or an array of options, including:
* 'name' - the machine name of the View
* 'display' - which display of the View should be used
* 'arg' - which argument the term id should be passed to in the View
* (this is useful if the View has more than one contextual filter)
* 'weight' - the weight of the View in the taxonomy page
* (this is useful for changing the order of Views)
* 'always' - always display, even if there are no View results
* (default is FALSE)
*/
function hook_farm_ui_taxonomy_views($term) {
// If the term is not a crop, bail.
if ($term->vocabulary_machine_name != 'farm_crops') {
return array();
}
// Return a list of Views to include on Crops.
return array(
// Example 1: simple View machine name.
'farm_planting',
// Example 2: explicitly set details like display, argument position, weight.
array(
'name' => 'farm_log_input',
'display' => 'block',

View File

@ -50,7 +50,7 @@ function farm_ui_entity_view($entity, $type, $view_mode, $langcode) {
drupal_set_breadcrumb($breadcrumb);
// Add Views to the entity.
$views = farm_ui_entity_views($type, $entity);
$views = farm_ui_entity_views($type, $bundle, $entity);
if (!empty($views)) {
$entity->content['views'] = $views;
$entity->content['views']['#weight'] = 100;
@ -62,13 +62,15 @@ function farm_ui_entity_view($entity, $type, $view_mode, $langcode) {
*
* @param $entity_type
* The entity type. Currently supports: 'farm_asset' or 'taxonomy_term'.
* @param $entity_bundle
* The entity bundle.
* @param $entity
* The loaded entity object.
*
* @return array
* Returns a render array of Views to add to the entity page.
*/
function farm_ui_entity_views($entity_type, $entity) {
function farm_ui_entity_views($entity_type, $entity_bundle, $entity) {
// Start an empty build array.
$build = array();
@ -101,15 +103,8 @@ function farm_ui_entity_views($entity_type, $entity) {
}
}
// Ask modules for Views, based on the type.
switch ($entity_type) {
case 'farm_asset':
$module_views = module_invoke_all('farm_ui_asset_views', $entity);
break;
case 'taxonomy_term':
$module_views = module_invoke_all('farm_ui_taxonomy_views', $entity);
break;
}
// Ask modules for Views.
$module_views = module_invoke_all('farm_ui_entity_views', $entity_type, $entity_bundle, $entity);
if (!empty($module_views)) {
$views = array_merge($views, $module_views);
}

View File

@ -24,17 +24,17 @@ function farm_water_test_farm_ui_entities() {
}
/**
* Implements hook_farm_ui_taxonomy_views().
* Implements hook_farm_ui_entity_views().
*/
function farm_water_test_farm_ui_taxonomy_views($term) {
// If the term is not an area, bail.
if ($term->vocabulary_machine_name != 'farm_areas') {
return array();
}
function farm_water_test_farm_ui_entity_views($entity_type, $bundle, $entity) {
$views = array();
// Return a list of Views to include on Areas.
return array(
'farm_water_test',
);
if ($entity_type == 'taxonomy_term' && $entity->vocabulary_machine_name == 'farm_areas') {
$views[] = array(
'name' => 'farm_water_test',
);
}
return $views;
}