mirror of
https://github.com/farmOS/farmOS.git
synced 2024-02-23 11:37:38 +01:00
Merge branch '2.x-asset-group-views-field' into 2.x
This commit is contained in:
commit
9092ee65a6
|
@ -29,54 +29,90 @@ function farm_group_entity_base_field_info(EntityTypeInterface $entity_type) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_views_data_alter().
|
||||
*/
|
||||
function farm_group_views_data_alter(array &$data) {
|
||||
|
||||
// Add the computed group membership field to assets.
|
||||
$data['asset']['group'] = [
|
||||
'title' => t('Current group'),
|
||||
'field' => [
|
||||
'id' => 'asset_group',
|
||||
'field_name' => 'group',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_views_pre_view().
|
||||
*/
|
||||
function farm_group_views_pre_view(ViewExecutable $view, $display_id, array &$args) {
|
||||
|
||||
// Only alter the farm_log page and CSV export views.
|
||||
if ($view->id() != 'farm_log' || in_array($display_id, ['block_upcoming', 'block_late'])) {
|
||||
return;
|
||||
}
|
||||
// Alter the farm_asset View.
|
||||
if ($view->id() == 'farm_asset') {
|
||||
|
||||
// Make sure the is_group_assignment field exists.
|
||||
/** @var \Drupal\Core\Field\BaseFieldDefinition[] $definitions */
|
||||
$definitions = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions('log');
|
||||
if (isset($definitions['is_group_assignment'])) {
|
||||
|
||||
// Get field name and table name.
|
||||
$definition = $definitions['is_group_assignment'];
|
||||
$field_name = $definition->getName();
|
||||
/** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
|
||||
$table_mapping = \Drupal::entityTypeManager()->getStorage('log')->getTableMapping();
|
||||
$table = $table_mapping->getFieldTableName($definition->getName());
|
||||
|
||||
// Define filter options.
|
||||
$opts = [
|
||||
'operator' => '=',
|
||||
'value' => 'All',
|
||||
'exposed' => TRUE,
|
||||
'expose' => [
|
||||
'label' => t('Is group assignment'),
|
||||
'identifier' => 'is_group_assignment',
|
||||
// Add the computed "Group" field before the "Location" field.
|
||||
$field_options = [
|
||||
'type' => 'entity_reference_label',
|
||||
'label' => t('Group'),
|
||||
'settings' => [
|
||||
'link' => TRUE,
|
||||
],
|
||||
];
|
||||
// Add the filter.
|
||||
$filter_id = $view->addHandler($view->current_display, 'filter', $table, $field_name, $opts);
|
||||
|
||||
// Move the is_group_assignment filter to render before the status filter.
|
||||
$filters = $view->getDisplay()->getOption('filters');
|
||||
if ($index = array_search('status', array_keys($filters))) {
|
||||
$filter = $filters[$filter_id];
|
||||
unset($filters[$filter_id]);
|
||||
// Use array_merge + array_splice instead of array_splice so we can
|
||||
// preserve the array keys.
|
||||
$filters = array_merge(array_slice($filters, 0, $index), [$filter_id => $filter], array_slice($filters, $index));
|
||||
}
|
||||
// Set the new sorted filters array.
|
||||
$view->getDisplay()->setOption('filters', $filters);
|
||||
$field_id = $view->addHandler($display_id, 'field', 'asset', 'group', $field_options);
|
||||
farm_ui_views_sort_field($view, $display_id, $field_id, 'location', TRUE);
|
||||
}
|
||||
|
||||
// Alter the farm_log View.
|
||||
if ($view->id() == 'farm_log') {
|
||||
|
||||
// Do not alter the upcoming/late block displays.
|
||||
if (in_array($display_id, ['block_upcoming', 'block_late'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Make sure the is_group_assignment field exists.
|
||||
/** @var \Drupal\Core\Field\BaseFieldDefinition[] $definitions */
|
||||
$definitions = \Drupal::service('entity_field.manager')
|
||||
->getBaseFieldDefinitions('log');
|
||||
if (isset($definitions['is_group_assignment'])) {
|
||||
|
||||
// Get field name and table name.
|
||||
$definition = $definitions['is_group_assignment'];
|
||||
$field_name = $definition->getName();
|
||||
/** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
|
||||
$table_mapping = \Drupal::entityTypeManager()
|
||||
->getStorage('log')
|
||||
->getTableMapping();
|
||||
$table = $table_mapping->getFieldTableName($definition->getName());
|
||||
|
||||
// Define filter options.
|
||||
$opts = [
|
||||
'operator' => '=',
|
||||
'value' => 'All',
|
||||
'exposed' => TRUE,
|
||||
'expose' => [
|
||||
'label' => t('Is group assignment'),
|
||||
'identifier' => 'is_group_assignment',
|
||||
],
|
||||
];
|
||||
// Add the filter.
|
||||
$filter_id = $view->addHandler($view->current_display, 'filter', $table, $field_name, $opts);
|
||||
|
||||
// Move the is_group_assignment filter to render before the status filter.
|
||||
$filters = $view->getDisplay()->getOption('filters');
|
||||
if ($index = array_search('status', array_keys($filters))) {
|
||||
$filter = $filters[$filter_id];
|
||||
unset($filters[$filter_id]);
|
||||
// Use array_merge + array_splice instead of array_splice so we can
|
||||
// preserve the array keys.
|
||||
$filters = array_merge(array_slice($filters, 0, $index), [$filter_id => $filter], array_slice($filters, $index));
|
||||
}
|
||||
// Set the new sorted filters array.
|
||||
$view->getDisplay()->setOption('filters', $filters);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
16
modules/asset/group/src/Plugin/views/field/AssetGroup.php
Normal file
16
modules/asset/group/src/Plugin/views/field/AssetGroup.php
Normal file
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\farm_group\Plugin\views\field;
|
||||
|
||||
use Drupal\views\Plugin\views\field\EntityField;
|
||||
|
||||
/**
|
||||
* A field that displays asset group.
|
||||
*
|
||||
* @ingroup views_field_handlers
|
||||
*
|
||||
* @ViewsField("asset_group")
|
||||
*/
|
||||
class AssetGroup extends EntityField {
|
||||
|
||||
}
|
|
@ -240,25 +240,8 @@ function farm_ui_views_add_bundle_handlers(ViewExecutable $view, string $display
|
|||
}
|
||||
|
||||
// Sort the field handlers if necessary.
|
||||
// Based off the \Drupal\views_ui\Form\Ajax\Rearrange.php method of
|
||||
// ordering handlers in views.
|
||||
if (!empty($sort_field)) {
|
||||
|
||||
// Get the existing field handlers.
|
||||
$type = 'field';
|
||||
$types = ViewExecutable::getHandlerTypes();
|
||||
$display = $view->displayHandlers->get($display_id);
|
||||
$field_handlers = $display->getOption($types[$type]['plural']);
|
||||
|
||||
// Define the new field handler and insert at desired position.
|
||||
$new_field_handler = [$new_field_id => $field_handlers[$new_field_id]];
|
||||
$keys = array_keys($field_handlers);
|
||||
$index = array_search($sort_field, $keys, TRUE);
|
||||
$pos = empty($index) ? count($field_handlers) : $index + 1;
|
||||
$new_field_handlers = array_merge(array_slice($field_handlers, 0, $pos, TRUE), $new_field_handler, array_slice($field_handlers, $pos));
|
||||
|
||||
// Set the display to use the sorted field handlers.
|
||||
$display->setOption($types[$type]['plural'], $new_field_handlers);
|
||||
farm_ui_views_sort_field($view, $display_id, $new_field_id, $sort_field);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -315,6 +298,47 @@ function farm_ui_views_add_bundle_handlers(ViewExecutable $view, string $display
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function for sorting a field handler.
|
||||
*
|
||||
* Based off the \Drupal\views_ui\Form\Ajax\Rearrange.php method of ordering
|
||||
* handlers in views.
|
||||
*
|
||||
* @param \Drupal\views\ViewExecutable $view
|
||||
* The View to add handlers to.
|
||||
* @param string $display_id
|
||||
* The ID of the View display to add handlers to.
|
||||
* @param string $field_id
|
||||
* The ID of the field to sort.
|
||||
* @param string $base_field_id
|
||||
* The ID of an existing field in the View. The field defined by $field_id
|
||||
* will be added before/after this field in the View.
|
||||
* @param bool $before
|
||||
* If TRUE, the field will be added before the field defined by $base_field_id
|
||||
* instead of after.
|
||||
*/
|
||||
function farm_ui_views_sort_field(ViewExecutable $view, string $display_id, string $field_id, string $base_field_id, bool $before = FALSE) {
|
||||
|
||||
// Get the existing field handlers.
|
||||
$type = 'field';
|
||||
$types = ViewExecutable::getHandlerTypes();
|
||||
$display = $view->displayHandlers->get($display_id);
|
||||
$field_handlers = $display->getOption($types[$type]['plural']);
|
||||
|
||||
// Define the new field handler and insert at desired position.
|
||||
$new_field_handler = [$field_id => $field_handlers[$field_id]];
|
||||
$keys = array_keys($field_handlers);
|
||||
$index = array_search($base_field_id, $keys, TRUE);
|
||||
$pos = empty($index) ? count($field_handlers) : $index;
|
||||
if (!$before) {
|
||||
$pos++;
|
||||
}
|
||||
$new_field_handlers = array_merge(array_slice($field_handlers, 0, $pos, TRUE), $new_field_handler, array_slice($field_handlers, $pos, NULL, TRUE));
|
||||
|
||||
// Set the display to use the sorted field handlers.
|
||||
$display->setOption($types[$type]['plural'], $new_field_handlers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_views_pre_render().
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue