mirror of
https://github.com/farmOS/farmOS.git
synced 2024-02-23 11:37:38 +01:00
163 lines
4.5 KiB
PHP
163 lines
4.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @file
|
|
* Contains farm_entity.module.
|
|
*/
|
|
|
|
use Drupal\Core\Entity\EntityTypeInterface;
|
|
use Drupal\farm_entity\BundlePlugin\FarmEntityBundlePluginHandler;
|
|
|
|
/**
|
|
* Implements hook_module_implements_alter().
|
|
*/
|
|
function farm_entity_module_implements_alter(&$implementations, $hook) {
|
|
|
|
// Make sure this module's hook_entity_type_build() runs before the
|
|
// entity module's implementation, so that we can override the bundle plugin
|
|
// handler, and so that we can set the Log entity type's bundle_plugin_type.
|
|
$module = 'farm_entity';
|
|
if ($hook == 'entity_type_build') {
|
|
$implementation = [$module => $implementations[$module]];
|
|
unset($implementations[$module]);
|
|
$implementations = array_merge($implementation, $implementations);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_entity_type_build().
|
|
*/
|
|
function farm_entity_entity_type_build(array &$entity_types) {
|
|
|
|
// Enable the use of bundle plugins for asset, log, and plan entity types.
|
|
/** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
|
|
foreach (['asset', 'log', 'plan'] as $entity_type) {
|
|
if (!empty($entity_types[$entity_type])) {
|
|
$entity_types[$entity_type]->set('bundle_plugin_type', $entity_type . '_type');
|
|
$entity_types[$entity_type]->setHandlerClass('bundle_plugin', FarmEntityBundlePluginHandler::class);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_entity_base_field_info().
|
|
*/
|
|
function farm_entity_entity_base_field_info(EntityTypeInterface $entity_type) {
|
|
|
|
// Include helper functions.
|
|
module_load_include('inc', 'farm_entity', 'farm_entity.base_fields');
|
|
|
|
// Add common base fields to all asset types.
|
|
if ($entity_type->id() == 'asset') {
|
|
return farm_entity_asset_base_fields();
|
|
}
|
|
|
|
// Add common base fields to all log types.
|
|
elseif ($entity_type->id() == 'log') {
|
|
return farm_entity_log_base_fields();
|
|
}
|
|
|
|
// Add common base fields to all plan types.
|
|
elseif ($entity_type->id() == 'plan') {
|
|
return farm_entity_plan_base_fields();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_entity_base_field_info_alter().
|
|
*/
|
|
function farm_entity_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
|
|
|
|
// Only alter asset, log, and plan fields.
|
|
if (!in_array($entity_type->id(), ['asset', 'log', 'plan'])) {
|
|
return;
|
|
}
|
|
|
|
$alter_fields = [
|
|
'name' => [
|
|
'label' => 'hidden',
|
|
'weight' => -100,
|
|
],
|
|
'timestamp' => [
|
|
'weight' => -90,
|
|
],
|
|
'status' => [
|
|
'weight' => -80,
|
|
],
|
|
'created' => [
|
|
'hidden' => TRUE,
|
|
],
|
|
'uid' => [
|
|
'hidden' => TRUE,
|
|
],
|
|
];
|
|
foreach ($alter_fields as $name => $options) {
|
|
|
|
// If the field does not exist on this entity type, skip it.
|
|
if (empty($fields[$name])) {
|
|
continue;
|
|
}
|
|
|
|
// Load the form and view display options.
|
|
$form_display_options = $fields[$name]->getDisplayOptions('form');
|
|
$view_display_options = $fields[$name]->getDisplayOptions('view');
|
|
|
|
// Set the field weight.
|
|
if (!empty($options['weight'])) {
|
|
$form_display_options['weight'] = $view_display_options['weight'] = $options['weight'];
|
|
}
|
|
|
|
// Hide the field, if desired.
|
|
if (!empty($options['hidden'])) {
|
|
$form_display_options = ['region' => 'hidden'];
|
|
$view_display_options = ['region' => 'hidden'];
|
|
}
|
|
|
|
// Or, hide the label, if desired.
|
|
elseif (!empty($options['label']) && $options['label'] == 'hidden') {
|
|
$view_display_options['label'] = 'hidden';
|
|
}
|
|
|
|
// Otherwise, set the label to inline.
|
|
else {
|
|
$view_display_options['label'] = 'inline';
|
|
}
|
|
|
|
// Change state field from transition form to default.
|
|
if ($name == 'status') {
|
|
$view_display_options['type'] = 'list_default';
|
|
}
|
|
|
|
// Save the options.
|
|
$fields[$name]->setDisplayOptions('form', $form_display_options);
|
|
$fields[$name]->setDisplayOptions('view', $view_display_options);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_views_data_alter().
|
|
*/
|
|
function farm_entity_views_data_alter(array &$data) {
|
|
|
|
// Because Drupal core does not provide full Views integration for base fields
|
|
// we must manually specify the state_machine_state views filter for the
|
|
// status field.
|
|
// Define the views filter settings.
|
|
$status_filter = [
|
|
'id' => 'state_machine_state',
|
|
'field_name' => 'status',
|
|
];
|
|
$tables = [
|
|
'asset_field_data',
|
|
'asset_field_revision',
|
|
'log_field_data',
|
|
'log_field_revision',
|
|
'plan_field_data',
|
|
'plan_field_revision',
|
|
];
|
|
foreach ($tables as $table) {
|
|
if (!empty($data[$table]['status'])) {
|
|
$data[$table]['status']['filter'] = $status_filter;
|
|
}
|
|
}
|
|
}
|