Move hooks for adding Views to asset and term pages to farm_ui module.

This commit is contained in:
Michael Stenta 2017-06-26 12:40:52 -04:00
parent e3e8ddddbe
commit 0fb73ffcad
19 changed files with 315 additions and 364 deletions

View File

@ -1,59 +0,0 @@
<?php
/**
* @file
* Hooks provided by farm_asset.
*
* 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_asset Farm asset module integrations.
*
* Module integrations with the farm_asset module.
*/
/**
* @defgroup farm_asset_hooks Farm asset's hooks
* @{
* Hooks that can be implemented by other modules in order to extend farm_asset.
*/
/**
* Attach Views to asset view pages.
*
* @param FarmAsset $farm_asset
* The farm asset entity.
*
* @return array
* Returns an array of View names to attach to farm asset pages.
*/
function hook_farm_asset_view_views(FarmAsset $farm_asset) {
// If the entity is not a planting, bail.
if ($farm_asset->type != 'planting') {
return array();
}
// Return a list of Views to include on Plantings.
return array(
// Example 1: simple View machine name.
'farm_activity',
// Example 2: explicitly set details like display, argument position,
// and weight.
array(
'name' => 'farm_log_input',
'display' => 'block',
'arg' => 2,
'weight' => 10,
),
);
}
/**
* @}
*/

View File

@ -326,96 +326,6 @@ function farm_asset_entity_view($entity, $type, $view_mode, $langcode) {
'#markup' => '<div><strong>Active:</strong> ' . $status . '</div>',
'#weight' => -100,
);
// Invoke hook_farm_asset_view_views() to get a list of Views.
$views = module_invoke_all('farm_asset_view_views', $entity);
// 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 $entity->id into. This is useful if the View
// has multiple contextual filters, and the entity 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[] = $entity->id;
}
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 asset 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.
if ($view->total_rows == 0) {
continue;
}
// Build the output.
$output = '<h3 id="' . $title . '">' . $title . '</h3>' . $preview;
// Add the output to the entity build array.
$entity->content['views'][$data['name']] = array(
'#markup' => $output,
'#weight' => 100 + $key,
);
}
}
/****************************************************************

View File

@ -192,9 +192,9 @@ function farm_crop_farm_ui_entities() {
}
/**
* Implements hook_farm_asset_view_views().
* Implements hook_farm_ui_asset_views().
*/
function farm_crop_farm_asset_view_views($farm_asset) {
function farm_crop_farm_ui_asset_views($farm_asset) {
// If the entity is not a planting, bail.
if ($farm_asset->type != 'planting') {
@ -241,9 +241,9 @@ function farm_crop_farm_asset_view_views($farm_asset) {
}
/**
* Implements hook_farm_taxonomy_term_view_views().
* Implements hook_farm_ui_taxonomy_views().
*/
function farm_crop_farm_taxonomy_term_view_views($term) {
function farm_crop_farm_ui_taxonomy_views($term) {
// Start a list of View names.
$views = array();

View File

@ -30,9 +30,9 @@ function farm_equipment_farm_ui_entities() {
}
/**
* Implements hook_farm_asset_view_views().
* Implements hook_farm_ui_asset_views().
*/
function farm_equipment_farm_asset_view_views($farm_asset) {
function farm_equipment_farm_ui_asset_views($farm_asset) {
// If the entity is not equipment, bail.
if ($farm_asset->type != 'equipment') {
@ -65,9 +65,9 @@ function farm_equipment_farm_asset_view_views($farm_asset) {
}
/**
* Implements hook_farm_taxonomy_term_view_views().
* Implements hook_farm_ui_taxonomy_views().
*/
function farm_equipment_farm_taxonomy_term_view_views($term) {
function farm_equipment_farm_ui_taxonomy_views($term) {
// If the term is not an area, bail.
if ($term->vocabulary_machine_name != 'farm_areas') {

View File

@ -57,9 +57,9 @@ function farm_livestock_farm_area_type_info() {
}
/**
* Implements hook_farm_asset_view_views().
* Implements hook_farm_ui_asset_views().
*/
function farm_livestock_farm_asset_view_views($farm_asset) {
function farm_livestock_farm_ui_asset_views($farm_asset) {
// If the entity is not an animal, bail.
if ($farm_asset->type != 'animal') {
@ -102,9 +102,9 @@ function farm_livestock_farm_asset_view_views($farm_asset) {
}
/**
* Implements hook_farm_taxonomy_term_view_views().
* Implements hook_farm_ui_taxonomy_views().
*/
function farm_livestock_farm_taxonomy_term_view_views($term) {
function farm_livestock_farm_ui_taxonomy_views($term) {
// Start a list of View names.
$views = array();

View File

@ -24,9 +24,9 @@ function farm_log_activity_farm_ui_entities() {
}
/**
* Implements hook_farm_taxonomy_term_view_views().
* Implements hook_farm_ui_taxonomy_views().
*/
function farm_log_activity_farm_taxonomy_term_view_views($term) {
function farm_log_activity_farm_ui_taxonomy_views($term) {
// If the term is not an area, bail.
if ($term->vocabulary_machine_name != 'farm_areas') {

View File

@ -24,9 +24,9 @@ function farm_log_input_farm_ui_entities() {
}
/**
* Implements hook_farm_taxonomy_term_view_views().
* Implements hook_farm_ui_taxonomy_views().
*/
function farm_log_input_farm_taxonomy_term_view_views($term) {
function farm_log_input_farm_ui_taxonomy_views($term) {
// If the term is not an area, bail.
if ($term->vocabulary_machine_name != 'farm_areas') {

View File

@ -11,9 +11,9 @@ include_once 'farm_log_movement.features.inc';
include_once 'farm_log_movement.location.inc';
/**
* Implements hook_farm_taxonomy_term_view_views().
* Implements hook_farm_ui_taxonomy_views().
*/
function farm_log_movement_farm_taxonomy_term_view_views($term) {
function farm_log_movement_farm_ui_taxonomy_views($term) {
// If the term is not an area, bail.
if ($term->vocabulary_machine_name != 'farm_areas') {

View File

@ -24,9 +24,9 @@ function farm_log_observation_farm_ui_entities() {
}
/**
* Implements hook_farm_taxonomy_term_view_views().
* Implements hook_farm_ui_taxonomy_views().
*/
function farm_log_observation_farm_taxonomy_term_view_views($term) {
function farm_log_observation_farm_ui_taxonomy_views($term) {
// If the term is not an area, bail.
if ($term->vocabulary_machine_name != 'farm_areas') {

View File

@ -22,9 +22,9 @@ function farm_log_sale_farm_ui_entities() {
}
/**
* Implements hook_farm_asset_view_views().
* Implements hook_farm_ui_asset_views().
*/
function farm_log_sale_farm_asset_view_views($farm_asset) {
function farm_log_sale_farm_ui_asset_views($farm_asset) {
// Show a list of sales at the top of all asset pages.
return array(

View File

@ -22,9 +22,9 @@ function farm_sensor_farm_ui_entities() {
}
/**
* Implements hook_farm_asset_view_views().
* Implements hook_farm_ui_asset_views().
*/
function farm_sensor_farm_asset_view_views($farm_asset) {
function farm_sensor_farm_ui_asset_views($farm_asset) {
// If the entity is not sensor, bail.
if ($farm_asset->type != 'sensor') {
@ -49,9 +49,9 @@ function farm_sensor_farm_asset_view_views($farm_asset) {
}
/**
* Implements hook_farm_taxonomy_term_view_views().
* Implements hook_farm_ui_taxonomy_views().
*/
function farm_sensor_farm_taxonomy_term_view_views($term) {
function farm_sensor_farm_ui_taxonomy_views($term) {
// If the term is not an area, bail.
if ($term->vocabulary_machine_name != 'farm_areas') {

View File

@ -524,9 +524,9 @@ function farm_sensor_listener_load($key) {
}
/**
* Implements hook_farm_asset_view_views().
* Implements hook_farm_ui_asset_views().
*/
function farm_sensor_listener_farm_asset_view_views($farm_asset) {
function farm_sensor_listener_farm_ui_asset_views($farm_asset) {
// If the entity is not a sensor, bail.
if ($farm_asset->type != 'sensor') {

View File

@ -22,9 +22,9 @@ function farm_soil_compost_farm_ui_entities() {
}
/**
* Implements hook_farm_asset_view_views().
* Implements hook_farm_ui_asset_views().
*/
function farm_soil_compost_farm_asset_view_views($farm_asset) {
function farm_soil_compost_farm_ui_asset_views($farm_asset) {
// If the entity is not a compost bail.
if ($farm_asset->type != 'compost') {
@ -63,9 +63,9 @@ function farm_soil_compost_farm_asset_view_views($farm_asset) {
}
/**
* Implements hook_farm_taxonomy_term_view_views().
* Implements hook_farm_ui_taxonomy_views().
*/
function farm_soil_compost_farm_taxonomy_term_view_views($term) {
function farm_soil_compost_farm_ui_taxonomy_views($term) {
// If the term is not an area, bail.
if ($term->vocabulary_machine_name != 'farm_areas') {

View File

@ -24,9 +24,9 @@ function farm_soil_test_farm_ui_entities() {
}
/**
* Implements hook_farm_taxonomy_term_view_views().
* Implements hook_farm_ui_taxonomy_views().
*/
function farm_soil_test_farm_taxonomy_term_view_views($term) {
function farm_soil_test_farm_ui_taxonomy_views($term) {
// If the term is not an area, bail.
if ($term->vocabulary_machine_name != 'farm_areas') {

View File

@ -1,70 +0,0 @@
<?php
/**
* @file
* Hooks provided by farm_taxonomy.
*
* 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_taxonomy Farm taxonomy module integrations.
*
* Module integrations with the farm_taxonomy module.
*/
/**
* @defgroup farm_taxonomy_hooks Farm taxonomy's hooks
* @{
* Hooks that can be implemented by other modules in order to extend
* farm_taxonomy.
*/
/**
* 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_taxonomy_term_view_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',
'arg' => 2,
'weight' => 10,
'always' => TRUE,
),
);
}
/**
* @}
*/

View File

@ -5,112 +5,3 @@
*/
include_once 'farm_taxonomy.features.inc';
/**
* 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'];
// 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,
);
}
}

View File

@ -102,6 +102,82 @@ function hook_farm_ui_entities() {
return $entities;
}
/**
* Attach Views to asset view pages.
*
* @param FarmAsset $farm_asset
* The farm asset entity.
*
* @return array
* Returns an array of View names to attach to farm asset pages.
*/
function hook_farm_ui_asset_views(FarmAsset $farm_asset) {
// If the entity is not a planting, bail.
if ($farm_asset->type != 'planting') {
return array();
}
// Return a list of Views to include on Plantings.
return array(
// Example 1: simple View machine name.
'farm_activity',
// 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',
'arg' => 2,
'weight' => 10,
'always' => TRUE,
),
);
}
/**
* @}
*/

View File

@ -48,6 +48,209 @@ function farm_ui_entity_view($entity, $type, $view_mode, $langcode) {
// Set the breadcrumb.
drupal_set_breadcrumb($breadcrumb);
// If this is a farm_asset entity, add Views to it.
if ($type == 'farm_asset') {
// Invoke hook_farm_ui_asset_views() to get a list of Views.
$views = module_invoke_all('farm_ui_asset_views', $entity);
// 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 $entity->id into. This is useful if the View
// has multiple contextual filters, and the entity 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[] = $entity->id;
}
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 asset 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.
if ($view->total_rows == 0) {
continue;
}
// Build the output.
$output = '<h3 id="' . $title . '">' . $title . '</h3>' . $preview;
// Add the output to the entity build array.
$entity->content['views'][$data['name']] = array(
'#markup' => $output,
'#weight' => 100 + $key,
);
}
}
}
/**
* Implements hook_entity_view_alter().
*/
function farm_ui_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'];
// Invoke hook_farm_ui_taxonomy_views() to get a list of Views.
$views = module_invoke_all('farm_ui_taxonomy_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,
);
}
}
/**

View File

@ -24,9 +24,9 @@ function farm_water_test_farm_ui_entities() {
}
/**
* Implements hook_farm_taxonomy_term_view_views().
* Implements hook_farm_ui_taxonomy_views().
*/
function farm_water_test_farm_taxonomy_term_view_views($term) {
function farm_water_test_farm_ui_taxonomy_views($term) {
// If the term is not an area, bail.
if ($term->vocabulary_machine_name != 'farm_areas') {