mirror of
https://github.com/farmOS/farmOS.git
synced 2024-02-23 11:37:38 +01:00
180 lines
5.2 KiB
PHP
180 lines
5.2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @file
|
|
* The farmOS UI Views module.
|
|
*/
|
|
|
|
use Drupal\Core\Entity\EntityTypeInterface;
|
|
use Drupal\views\ViewExecutable;
|
|
|
|
/**
|
|
* Implements hook_entity_type_build().
|
|
*/
|
|
function farm_ui_views_entity_type_build(array &$entity_types) {
|
|
/** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
|
|
|
|
// Override the "collection" link path for assets, logs, and plans to use
|
|
// the Views provided by this module.
|
|
$collection_paths = [
|
|
'asset' => '/assets',
|
|
'log' => '/logs',
|
|
'plan' => '/plans',
|
|
];
|
|
foreach ($collection_paths as $entity_type => $path) {
|
|
if (!empty($entity_types[$entity_type])) {
|
|
$entity_types[$entity_type]->setLinkTemplate('collection', $path);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_local_tasks_alter().
|
|
*/
|
|
function farm_ui_views_local_tasks_alter(&$local_tasks) {
|
|
|
|
// Remove the local task plugin definition for farm entity collection links.
|
|
$entity_types = ['asset', 'log', 'plan'];
|
|
foreach ($entity_types as $type) {
|
|
if (!empty($local_tasks["entity.$type.collection"])) {
|
|
unset($local_tasks["entity.$type.collection"]);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_farm_dashboard_groups().
|
|
*/
|
|
function farm_ui_views_farm_dashboard_groups() {
|
|
$groups = [];
|
|
|
|
// If the plan module is enabled, add a plans group.
|
|
if (\Drupal::service('module_handler')->moduleExists('plan')) {
|
|
$groups['second']['plans'] = [
|
|
'#weight' => 10,
|
|
];
|
|
}
|
|
|
|
// Add a logs group.
|
|
$groups['first']['logs'] = [
|
|
'#weight' => 10,
|
|
];
|
|
|
|
return $groups;
|
|
}
|
|
|
|
/**
|
|
* Implements hook_farm_dashboard_panes().
|
|
*/
|
|
function farm_ui_views_farm_dashboard_panes() {
|
|
$panes = [];
|
|
|
|
// If the plan module is enabled, add active plans pane.
|
|
if (\Drupal::service('module_handler')->moduleExists('plan')) {
|
|
$panes['active_plans'] = [
|
|
'view' => 'farm_plan',
|
|
'view_display_id' => 'block_active',
|
|
'group' => 'plans',
|
|
'region' => 'second',
|
|
'weight' => 0,
|
|
];
|
|
}
|
|
|
|
// Add upcoming and late logs panes.
|
|
$panes['upcoming_tasks'] = [
|
|
'view' => 'farm_log',
|
|
'view_display_id' => 'block_upcoming',
|
|
'group' => 'logs',
|
|
'region' => 'first',
|
|
'weight' => 10,
|
|
];
|
|
$panes['late_tasks'] = [
|
|
'view' => 'farm_log',
|
|
'view_display_id' => 'block_late',
|
|
'group' => 'logs',
|
|
'region' => 'first',
|
|
'weight' => 11,
|
|
];
|
|
|
|
return $panes;
|
|
}
|
|
|
|
/**
|
|
* Implements hook_entity_base_field_info_alter().
|
|
*/
|
|
function farm_ui_views_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
|
|
|
|
// Use Entity Browser widget for certain asset reference fields.
|
|
$alter_fields = [
|
|
'log' => [
|
|
'asset',
|
|
],
|
|
'quantity' => [
|
|
'inventory_asset',
|
|
],
|
|
];
|
|
foreach ($alter_fields as $entity_type_id => $field_names) {
|
|
if ($entity_type->id() != $entity_type_id) {
|
|
continue;
|
|
}
|
|
foreach ($field_names as $field_name) {
|
|
if (!empty($fields[$field_name])) {
|
|
/** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
|
|
$form_display_options = $fields[$field_name]->getDisplayOptions('form');
|
|
$form_display_options['type'] = 'entity_browser_entity_reference';
|
|
$form_display_options['settings'] = [
|
|
'entity_browser' => 'farm_asset',
|
|
'field_widget_display' => 'label',
|
|
'field_widget_remove' => TRUE,
|
|
'open' => TRUE,
|
|
'selection_mode' => 'selection_append',
|
|
'field_widget_edit' => FALSE,
|
|
'field_widget_replace' => FALSE,
|
|
'field_widget_display_settings' => [],
|
|
];
|
|
$fields[$field_name]->setDisplayOptions('form', $form_display_options);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|