2020-11-17 12:21:53 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
2020-12-12 18:20:20 +01:00
|
|
|
* Contains farm_entity.module.
|
2020-11-17 12:21:53 +01:00
|
|
|
*/
|
|
|
|
|
2021-06-02 13:45:34 +02:00
|
|
|
use Drupal\Core\Entity\ContentEntityFormInterface;
|
|
|
|
use Drupal\Core\Entity\EntityInterface;
|
2020-11-17 12:21:53 +01:00
|
|
|
use Drupal\Core\Entity\EntityTypeInterface;
|
2021-03-13 02:37:23 +01:00
|
|
|
use Drupal\Core\Form\FormStateInterface;
|
2021-06-05 01:51:31 +02:00
|
|
|
use Drupal\entity\EntityAccessControlHandler;
|
|
|
|
use Drupal\entity\EntityPermissionProvider;
|
2020-12-12 18:20:20 +01:00
|
|
|
use Drupal\farm_entity\BundlePlugin\FarmEntityBundlePluginHandler;
|
2021-02-17 02:12:38 +01:00
|
|
|
use Drupal\farm_entity\Routing\DefaultHtmlRouteProvider;
|
2020-12-12 12:43:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements hook_module_implements_alter().
|
|
|
|
*/
|
2020-12-12 18:20:20 +01:00
|
|
|
function farm_entity_module_implements_alter(&$implementations, $hook) {
|
2020-12-12 12:43:57 +01:00
|
|
|
|
|
|
|
// Make sure this module's hook_entity_type_build() runs before the
|
|
|
|
// entity module's implementation, so that we can override the bundle plugin
|
2020-12-12 20:32:49 +01:00
|
|
|
// handler, and so that we can set the Log entity type's bundle_plugin_type.
|
2020-12-12 18:20:20 +01:00
|
|
|
$module = 'farm_entity';
|
2020-12-12 12:43:57 +01:00
|
|
|
if ($hook == 'entity_type_build') {
|
|
|
|
$implementation = [$module => $implementations[$module]];
|
|
|
|
unset($implementations[$module]);
|
|
|
|
$implementations = array_merge($implementation, $implementations);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements hook_entity_type_build().
|
|
|
|
*/
|
2020-12-12 18:20:20 +01:00
|
|
|
function farm_entity_entity_type_build(array &$entity_types) {
|
2020-12-30 15:14:59 +01:00
|
|
|
/** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
|
2020-12-12 12:43:57 +01:00
|
|
|
|
2021-04-20 18:14:19 +02:00
|
|
|
// 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();
|
2021-06-05 01:51:31 +02:00
|
|
|
$entity_types[$bundle_entity_type]->setHandlerClass('access', EntityAccessControlHandler::class);
|
|
|
|
$entity_types[$bundle_entity_type]->setHandlerClass('permission_provider', EntityPermissionProvider::class);
|
2021-04-20 18:14:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 01:00:09 +01:00
|
|
|
// Enable the use of bundle plugins on specific entity types.
|
|
|
|
foreach (['asset', 'log', 'plan', 'quantity'] as $entity_type) {
|
2020-12-12 12:43:57 +01:00
|
|
|
if (!empty($entity_types[$entity_type])) {
|
2020-12-13 14:50:59 +01:00
|
|
|
$entity_types[$entity_type]->set('bundle_plugin_type', $entity_type . '_type');
|
2020-12-12 18:20:20 +01:00
|
|
|
$entity_types[$entity_type]->setHandlerClass('bundle_plugin', FarmEntityBundlePluginHandler::class);
|
2021-02-17 02:12:38 +01:00
|
|
|
|
|
|
|
// 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);
|
2020-12-30 15:14:59 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-12 12:43:57 +01:00
|
|
|
}
|
2020-11-17 12:21:53 +01:00
|
|
|
|
2021-01-15 00:05:53 +01:00
|
|
|
/**
|
|
|
|
* Implements hook_entity_field_storage_info_alter().
|
2021-01-22 20:20:19 +01:00
|
|
|
*
|
|
|
|
* @todo https://www.drupal.org/project/farm/issues/3194206
|
2021-01-15 00:05:53 +01:00
|
|
|
*/
|
|
|
|
function farm_entity_entity_field_storage_info_alter(&$fields, EntityTypeInterface $entity_type) {
|
|
|
|
|
|
|
|
// Bail if not a farm entity type that allows bundle plugins.
|
2021-02-18 01:00:09 +01:00
|
|
|
if (!in_array($entity_type->id(), ['log', 'asset', 'plan', 'quantity'])) {
|
2021-01-15 00:05:53 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 13:45:34 +02:00
|
|
|
/**
|
|
|
|
* Implements hook_entity_presave().
|
|
|
|
*
|
|
|
|
* Forces revisions on all farm entities if the entity type supports them and
|
|
|
|
* the bundle has them enabled. This removes the option for users to disable a
|
|
|
|
* revision per-entity but as JSON:API doesn't support revisions yet, this is a
|
|
|
|
* trade-off that allows us to create revisions consistently on both the UI and
|
|
|
|
* the API.
|
|
|
|
*/
|
|
|
|
function farm_entity_entity_presave(EntityInterface $entity) {
|
|
|
|
|
|
|
|
// Only apply to farm controlled entities.
|
|
|
|
$entity_types = [
|
|
|
|
'asset',
|
|
|
|
'log',
|
|
|
|
'plan',
|
|
|
|
'quantity',
|
|
|
|
];
|
|
|
|
if (!in_array($entity->getEntityTypeId(), $entity_types)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force create new revision as json api doesn't do that by default.
|
|
|
|
// @see https://www.drupal.org/project/drupal/issues/2993557
|
|
|
|
// @see https://www.drupal.org/project/drupal/issues/2795279
|
|
|
|
// @see https://github.com/json-api/json-api/pull/824
|
|
|
|
if ($entity->type->entity->shouldCreateNewRevision() && $entity->getEntityType()->isRevisionable()) {
|
2021-06-09 17:01:04 +02:00
|
|
|
/** @var \Drupal\Core\Entity\RevisionLogInterface $entity */
|
|
|
|
|
|
|
|
// Always create a new revision.
|
2021-06-02 13:45:34 +02:00
|
|
|
$entity->setNewRevision(TRUE);
|
2021-06-09 17:01:04 +02:00
|
|
|
|
|
|
|
// If the new revision log message matches the original, then set a blank
|
|
|
|
// revision log message. We don't want the same message repeated across
|
|
|
|
// every revision created by the API.
|
|
|
|
if (!empty($entity->original)) {
|
|
|
|
if ($entity->original->get('revision_log_message')->value == $entity->get('revision_log_message')->value) {
|
|
|
|
$entity->setRevisionLogMessage('');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the user ID and creation time.
|
2021-06-09 16:09:47 +02:00
|
|
|
$entity->setRevisionUserId(\Drupal::currentUser()->getAccount()->id());
|
2021-06-02 13:45:34 +02:00
|
|
|
$entity->setRevisionCreationTime(\Drupal::time()->getRequestTime());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements hook_form_alter().
|
|
|
|
*
|
|
|
|
* Hides the revision control from the user, @see farm_entity_entity_presave()
|
|
|
|
*/
|
|
|
|
function farm_entity_form_alter(&$form, FormStateInterface $form_state, $form_id) {
|
|
|
|
|
|
|
|
// Only alter content entity forms.
|
|
|
|
$form_object = $form_state->getFormObject();
|
|
|
|
if (!($form_object instanceof ContentEntityFormInterface)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only apply to farm controlled entities.
|
|
|
|
$entity = $form_object->getEntity();
|
|
|
|
$entity_types = [
|
|
|
|
'asset',
|
|
|
|
'log',
|
|
|
|
'plan',
|
|
|
|
'quantity',
|
|
|
|
];
|
|
|
|
if (!in_array($entity->getEntityTypeId(), $entity_types)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable access to the revision checkbox.
|
|
|
|
$form['revision']['#access'] = FALSE;
|
|
|
|
}
|