3
0
Fork 0
mirror of https://github.com/farmOS/farmOS.git synced 2024-02-23 11:37:38 +01:00

Abstract the logic for getting the bundle from a "By Type" display's arguments.

This commit is contained in:
Michael Stenta 2021-10-18 14:39:24 -04:00
parent 97309754c1
commit a3c03fdcdf
2 changed files with 26 additions and 14 deletions

View file

@ -226,3 +226,27 @@ function farm_ui_views_sort_field(ViewExecutable $view, string $display_id, stri
// Set the display to use the sorted field handlers.
$display->setOption($types[$type]['plural'], $new_field_handlers);
}
/**
* Helper function for getting the bundle from a "By Type" display's arguments.
*
* @param \Drupal\views\ViewExecutable $view
* The View object.
* @param string $display_id
* The display ID.
* @param array $args
* Arguments for the View.
*
* @return string
* Returns the bundle, or empty string if no bundle argument is found.
*/
function farm_ui_views_get_bundle_argument(ViewExecutable $view, string $display_id, array $args) {
$bundle = '';
if ($display_id == 'page_type' && !empty($args[0])) {
$bundle = $args[0];
}
elseif ($view->id() == 'farm_log' && $display_id == 'page_asset' && !empty($args[1]) && $args[1] != 'all') {
$bundle = $args[1];
}
return $bundle;
}

View file

@ -20,13 +20,7 @@ function farm_ui_views_views_pre_view(ViewExecutable $view, $display_id, array &
}
// If this is a "By type" display, alter the fields and filters.
$bundle = FALSE;
if ($display_id == 'page_type' && !empty($args[0])) {
$bundle = $args[0];
}
elseif ($view->id() == 'farm_log' && $display_id == 'page_asset' && !empty($args[1]) && $args[1] != 'all') {
$bundle = $args[1];
}
$bundle = farm_ui_views_get_bundle_argument($view, $display_id, $args);
if (!empty($bundle)) {
// Remove the type field and filter handlers.
@ -105,13 +99,7 @@ function farm_ui_views_views_pre_render(ViewExecutable $view) {
// If this is a "By type" display and a bundle argument is specified, load
// the bundle label and set the title.
$bundle = FALSE;
if ($view->current_display == 'page_type' && !empty($view->args[0])) {
$bundle = $view->args[0];
}
elseif ($view->id() == 'farm_log' && $view->current_display == 'page_asset' && !empty($view->args[1]) && $view->args[1] != 'all') {
$bundle = $view->args[1];
}
$bundle = farm_ui_views_get_bundle_argument($view, $view->current_display, $view->args);
if (!empty($bundle)) {
$bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($view->getBaseEntityType()->id());
if (!empty($bundles[$bundle])) {