Commit d55b420a authored by alexpott's avatar alexpott

Issue #2812811 by Sam152, timmillwood, amateescu, alexpott, catch: Use...

Issue #2812811 by Sam152, timmillwood, amateescu, alexpott, catch: Use EntityPublishedInterface during moderation of entities to add support beyond nodes
parent b1500e07
......@@ -4,6 +4,7 @@
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityHandlerInterface;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
......@@ -32,6 +33,11 @@ public function onPresave(ContentEntityInterface $entity, $default_revision, $pu
// This is probably not necessary if configuration is setup correctly.
$entity->setNewRevision(TRUE);
$entity->isDefaultRevision($default_revision);
// Update publishing status if it can be updated and if it needs updating.
if (($entity instanceof EntityPublishedInterface) && $entity->isPublished() !== $published_state) {
$published_state ? $entity->setPublished() : $entity->setUnpublished();
}
}
/**
......
......@@ -3,7 +3,6 @@
namespace Drupal\content_moderation\Entity\Handler;
use Drupal\content_moderation\ModerationInformationInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -39,18 +38,6 @@ public static function createInstance(ContainerInterface $container, EntityTypeI
);
}
/**
* {@inheritdoc}
*/
public function onPresave(ContentEntityInterface $entity, $default_revision, $published_state) {
if ($this->shouldModerate($entity, $published_state)) {
parent::onPresave($entity, $default_revision, $published_state);
// Only nodes have a concept of published.
/** @var \Drupal\node\NodeInterface $entity */
$entity->setPublished($published_state);
}
}
/**
* {@inheritdoc}
*/
......@@ -74,22 +61,4 @@ public function enforceRevisionsBundleFormAlter(array &$form, FormStateInterface
}
}
/**
* Check if an entity's default revision and/or state needs adjusting.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity to check.
* @param bool $published_state
* Whether the state being transitioned to is a published state or not.
*
* @return bool
* TRUE when either the default revision or the state needs to be updated.
*/
protected function shouldModerate(ContentEntityInterface $entity, $published_state) {
// @todo clarify the first condition.
// First condition is needed so you can add a translation.
// Second condition checks to see if the published status has changed.
return $entity->isDefaultTranslation() || $entity->isPublished() !== $published_state;
}
}
<?php
namespace Drupal\entity_test\Entity;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\Core\Entity\EntityPublishedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
/**
* Defines the test entity class.
*
* @ContentEntityType(
* id = "entity_test_mulrevpub",
* label = @Translation("Test entity - revisions, data table, and published interface"),
* handlers = {
* "view_builder" = "Drupal\entity_test\EntityTestViewBuilder",
* "access" = "Drupal\entity_test\EntityTestAccessControlHandler",
* "form" = {
* "default" = "Drupal\entity_test\EntityTestForm",
* "delete" = "Drupal\entity_test\EntityTestDeleteForm"
* },
* "translation" = "Drupal\content_translation\ContentTranslationHandler",
* "views_data" = "Drupal\views\EntityViewsData",
* "route_provider" = {
* "html" = "Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider",
* },
* },
* base_table = "entity_test_mulrevpub",
* data_table = "entity_test_mulrevpub_property_data",
* revision_table = "entity_test_mulrevpub_revision",
* revision_data_table = "entity_test_mulrevpub_property_revision",
* admin_permission = "administer entity_test content",
* translatable = TRUE,
* show_revision_ui = TRUE,
* entity_keys = {
* "id" = "id",
* "uuid" = "uuid",
* "bundle" = "type",
* "revision" = "revision_id",
* "label" = "name",
* "langcode" = "langcode",
* "published" = "status",
* },
* links = {
* "add-form" = "/entity_test_mulrevpub/add",
* "canonical" = "/entity_test_mulrevpub/manage/{entity_test_mulrevpub}",
* "delete-form" = "/entity_test/delete/entity_test_mulrevpub/{entity_test_mulrevpub}",
* "edit-form" = "/entity_test_mulrevpub/manage/{entity_test_mulrevpub}/edit",
* "revision" = "/entity_test_mulrevpub/{entity_test_mulrevpub}/revision/{entity_test_mulrevpub_revision}/view",
* }
* )
*/
class EntityTestMulRevPub extends EntityTestMulRev implements EntityPublishedInterface {
use EntityPublishedTrait;
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
return parent::baseFieldDefinitions($entity_type) + EntityPublishedTrait::publishedBaseFieldDefinitions($entity_type);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment