mirror of
https://github.com/farmOS/farmOS.git
synced 2024-02-23 11:37:38 +01:00
228 lines
7 KiB
PHP
228 lines
7 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @file
|
|
* Contains farm_entity.module.
|
|
*/
|
|
|
|
use Drupal\Core\Entity\EntityTypeInterface;
|
|
use Drupal\entity\EntityViewsData;
|
|
use Drupal\farm_entity\BundlePlugin\FarmEntityBundlePluginHandler;
|
|
use Drupal\farm_entity\FarmLogViewsData;
|
|
use Drupal\farm_entity\Routing\DefaultHtmlRouteProvider;
|
|
|
|
/**
|
|
* 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) {
|
|
/** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
|
|
|
|
// Enable the use of bundle plugins on specific entity types.
|
|
foreach (['asset', 'log', 'plan', 'quantity'] 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);
|
|
|
|
// Deny access to the entity type add form. New entity types of entities
|
|
// with bundle plugins cannot be created in the UI.
|
|
// See https://www.drupal.org/project/farm/issues/3196423
|
|
$bundle_entity_type = $entity_types[$entity_type]->getBundleEntityType();
|
|
$route_providers = $entity_types[$bundle_entity_type]->getRouteProviderClasses();
|
|
$route_providers['default'] = DefaultHtmlRouteProvider::class;
|
|
$entity_types[$bundle_entity_type]->setHandlerClass('route_provider', $route_providers);
|
|
}
|
|
}
|
|
|
|
// Set the views data handler class to EntityViewsData for assets and plans.
|
|
// We don't need to do this for logs or quantities because their modules
|
|
// already do this.
|
|
foreach (['asset', 'plan'] as $entity_type) {
|
|
if (!empty($entity_types[$entity_type])) {
|
|
$entity_types[$entity_type]->setHandlerClass('views_data', EntityViewsData::class);
|
|
}
|
|
}
|
|
|
|
// With logs specifically, set the views data handler class to
|
|
// FarmLogViewsData (provided by us).
|
|
if (!empty($entity_types['log'])) {
|
|
$entity_types['log']->setHandlerClass('views_data', FarmLogViewsData::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_entity_field_storage_info_alter().
|
|
*
|
|
* @todo https://www.drupal.org/project/farm/issues/3194206
|
|
*/
|
|
function farm_entity_entity_field_storage_info_alter(&$fields, EntityTypeInterface $entity_type) {
|
|
|
|
// Bail if not a farm entity type that allows bundle plugins.
|
|
if (!in_array($entity_type->id(), ['log', 'asset', 'plan', 'quantity'])) {
|
|
return;
|
|
}
|
|
|
|
// Get all bundles of the entity type.
|
|
$bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($entity_type->id());
|
|
|
|
// Get all modules that provide bundle fields.
|
|
$modules = \Drupal::moduleHandler()->getImplementations('farm_entity_bundle_field_info');
|
|
|
|
// Invoke the hook for each module with each bundle.
|
|
foreach ($modules as $module) {
|
|
foreach (array_keys($bundles) as $bundle) {
|
|
$definitions = \Drupal::moduleHandler()
|
|
->invoke($module, 'farm_entity_bundle_field_info', [
|
|
$entity_type,
|
|
$bundle,
|
|
]);
|
|
|
|
// Set the provider for each field the module provided.
|
|
// This is required so that field storage definitions are created in the
|
|
// database when the module is installed.
|
|
foreach (array_keys($definitions) as $field) {
|
|
if (isset($fields[$field])) {
|
|
$fields[$field]->setProvider($module);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
}
|
|
}
|