farmOS/modules/core/entity/farm_entity.module

284 lines
9.4 KiB
PHP

<?php
/**
* @file
* Contains farm_entity.module.
*/
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\farm_entity\BundlePlugin\FarmEntityBundlePluginHandler;
use Drupal\farm_entity\EntityTypeAccessControlHandler;
use Drupal\farm_entity\FarmEntityViewsData;
use Drupal\farm_entity\FarmLogViewsData;
use Drupal\farm_entity\FarmQuantityViewsData;
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);
}
// Make sure this module's hook_modules_installed runs after the entity
// module's implementation, so that we rebuild views data after bundle fields
// are installed.
if ($hook == 'modules_installed') {
$implementation = [$module => $implementations[$module]];
unset($implementations[$module]);
$implementations = array_merge($implementations, $implementation);
}
}
/**
* Implements hook_entity_type_build().
*/
function farm_entity_entity_type_build(array &$entity_types) {
/** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
// Allow the "view label" operation on the bundle entity type.
foreach (['asset', 'log', 'plan', 'quantity', 'data_stream'] as $entity_type) {
if (!empty($entity_types[$entity_type])) {
$bundle_entity_type = $entity_types[$entity_type]->getBundleEntityType();
$entity_types[$bundle_entity_type]->setHandlerClass('access', EntityTypeAccessControlHandler::class);
}
}
// 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 FarmEntityViewsData.
foreach (['asset', 'log', 'plan', 'quantity'] as $entity_type) {
if (!empty($entity_types[$entity_type])) {
// Use the correct class for each entity type.
// Logs and quantities provide their own that we must extend from.
$views_data_class = FarmEntityViewsData::class;
switch ($entity_type) {
case 'log':
$views_data_class = FarmLogViewsData::class;
break;
case 'quantity':
$views_data_class = FarmQuantityViewsData::class;
break;
}
$entity_types[$entity_type]->setHandlerClass('views_data', $views_data_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,
],
'type' => [
'weight' => -85,
'hidden' => 'form',
],
'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'])) {
if ($options['hidden'] === TRUE || $options['hidden'] === 'form') {
$form_display_options['region'] = 'hidden';
}
if ($options['hidden'] === TRUE || $options['hidden'] === 'view') {
$view_display_options['region'] = 'hidden';
}
}
// Hide the label, if desired.
if (!empty($options['label']) && $options['label'] == 'hidden') {
$view_display_options['label'] = 'hidden';
}
// Otherwise, set the label to inline.
else {
$view_display_options['label'] = 'inline';
}
switch ($name) {
// Change state field from transition form to default.
case 'status':
$view_display_options['type'] = 'list_default';
break;
// Don't display a link to the entity type reference.
case 'type':
$view_display_options['settings']['link'] = FALSE;
break;
}
// 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_form_BASE_FORM_ID_alter().
*/
function farm_entity_form_log_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Alter the Quantity inline entity form to set the default quantity type.
if (!empty($form['quantity']['widget']['actions']['bundle']['#options'])) {
$bundle_select = &$form['quantity']['widget']['actions']['bundle'];
// Load the log type storage.
/** @var \Drupal\log\Entity\Log $entity */
$entity = $form_state->getFormObject()->getEntity();
/** @var \Drupal\log\Entity\LogType $log_type_storage */
$log_type_storage = \Drupal::service('entity_type.manager')->getStorage('log_type')->load($entity->bundle());
// Load default quantity type from the bundle's third party settings.
// Default to standard, if it exists. Note that we do NOT add a dependency
// on farm_quantity_standard, because that would create a circular
// dependency. This only changes the default type to 'standard' if that
// option is available.
$default_type = $log_type_storage->getThirdPartySetting('farm_entity', 'default_quantity_type');
if (empty($default_type)) {
$default_type = 'standard';
}
// Set the default value.
if (array_key_exists($default_type, $bundle_select['#options'])) {
$bundle_select['#default_value'] = $default_type;
}
}
}
/**
* Implements hook_modules_installed().
*/
function farm_entity_modules_installed($modules, $is_syncing) {
// Reset the views data after installing modules.
// See https://www.drupal.org/project/entity/issues/3206703#comment-14073184
if (\Drupal::hasService('views.views_data')) {
\Drupal::service('views.views_data')->clear();
}
}