- First commit to issue fork.
- last updatealmost 2 years ago PHPLint Failed
- @secretsayan opened merge request.
- last updatealmost 2 years ago 40 pass
- last updatealmost 2 years ago 40 pass
EntityReferenceRevisionsItem::preSave() currently hard-codes the following:
      $is_affected = !$this->getFieldDefinition()->isTranslatable() || ($host instanceof TranslatableRevisionableInterface && $host->hasTranslationChanges());
      if ($is_affected && !$host->isNew() && $this->entity && $this->entity->getEntityType()->get('entity_revision_parent_id_field')) {
        if ($host->isNewRevision()) {
          $this->entity->setNewRevision();
          $needs_save = TRUE;
        }
My proposal is to move this to a policy pattern, similar to how Drupal core's page_cache_response_policy service works. It's a chained policy service that invokes each registered policy, and if any policy says to deny, then the response is not cached in the page cache.
Similarly, ERR could introduce a entity_reference_revisions.revision_creation_policy service that chains to registered policies, and if any policy says to create a new revision, then the chained policy says to create a new revision. The above code could then be changed to:
if (\Drupal::service('entity_reference_revisions.revision_creation_policy')->shouldCreateNewRevision($this)) {
  $this->entity->setNewRevision();
  $needs_save = TRUE;
}
By default, ERR could register a CompositeEntityReferencedByNewHostRevisionViaUntranslatableReference policy that does:
class CompositeEntityReferencedByNewHostRevisionViaUntranslatableReference implements RevisionCreationPolicyInterface {
  function shouldCreateNewRevision(EntityReferenceRevisionsItem $item) {
    $host = $this->getEntity();
    if (
      // A composite entity
      $this->entity && $this->entity->getEntityType()->get('entity_revision_parent_id_field') &&
      // Referenced by a new host revision
      !$host->isNew() && $host->isNewRevision() && 
      // Via an untranslatable reference
      !$this->getFieldDefinition()->isTranslatable()
    ) {
      return TRUE;
    }
  }
}
and a CompositeEntityReferencedByNewHostRevisionWithTranslationChanges policy that does:
class CompositeEntityReferencedByNewHostRevisionWithTranslationChanges implements RevisionCreationPolicyInterface {
  function shouldCreateNewRevision(EntityReferenceRevisionsItem $item) {
    $host = $this->getEntity();
    if (
      // A composite entity
      $this->entity && $this->entity->getEntityType()->get('entity_revision_parent_id_field') &&
      // Referenced by a new host revision
      !$host->isNew() && $host->isNewRevision() && 
      // With translation changes
      $host instanceof TranslatableRevisionableInterface && $host->hasTranslationChanges()
    ) {
      return TRUE;
    }
  }
}
The combination of these 2 policies would implement that same behavior as is currently hard-coded within preSave(), so would be backwards compatible. But a new module could remove one or both of these policies and instead add a different policy for handling the last item within the Problem/Motivation section.
Active
1.0
Code
Not all content is available!
It's likely this issue predates Contrib.social: some issue and comment data are missing.