Commit bf31e8cb authored by catch's avatar catch

Issue #2903524 by timmillwood, Sam152, amateescu, yoroy: Don't add quickedit...

Issue #2903524 by timmillwood, Sam152, amateescu, yoroy: Don't add quickedit to displays where entities have a forward revision
parent 30366eba
......@@ -11,6 +11,7 @@
* entities, enabling them for in-place editing.
*/
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Routing\RouteMatchInterface;
......@@ -157,9 +158,40 @@ function quickedit_preprocess_field(&$variables) {
*/
function quickedit_entity_view_alter(&$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
$build['#cache']['contexts'][] = 'user.permissions';
if (!\Drupal::currentUser()->hasPermission('access in-place editing')) {
if (!\Drupal::currentUser()->hasPermission('access in-place editing') || !_quickedit_entity_is_latest_revision($entity)) {
return;
}
$build['#attributes']['data-quickedit-entity-id'] = $entity->getEntityTypeId() . '/' . $entity->id();
}
/**
* Check if a loaded entity is the latest revision.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity to check.
*
* @return bool
* TRUE if the loaded entity is the latest revision, FALSE otherwise.
*
* @todo Remove this method once better support for pending revisions is added
* to core https://www.drupal.org/node/2784201.
*
* @internal
*/
function _quickedit_entity_is_latest_revision(ContentEntityInterface $entity) {
$entity_type_manager = \Drupal::entityTypeManager();
$entity_definition = $entity_type_manager->getDefinition($entity->getEntityTypeId());
if (!$entity_definition->isRevisionable()) {
return TRUE;
}
$revision_ids = $entity_type_manager
->getStorage($entity->getEntityTypeId())
->getQuery()
->allRevisions()
->condition($entity_definition->getKey('id'), $entity->id())
->sort($entity_definition->getKey('revision'), 'DESC')
->range(0, 1)
->execute();
return $entity->getLoadedRevisionId() == array_keys($revision_ids)[0];
}
......@@ -42,6 +42,13 @@ class QuickEditLoadingTest extends WebTestBase {
*/
protected $authorUser;
/**
* A test node.
*
* @var \Drupal\node\NodeInterface
*/
protected $testNode;
/**
* A author user with permissions to access in-place editor.
*
......@@ -74,7 +81,7 @@ protected function setUp() {
$node_type->save();
// Create one node of the above node type using the above text format.
$this->drupalCreateNode([
$this->testNode = $this->drupalCreateNode([
'type' => 'article',
'body' => [
0 => [
......@@ -317,6 +324,24 @@ public function testUserWithPermission() {
}
}
/**
* Test quickedit does not appear for entities with pending revisions.
*/
public function testWithPendingRevision() {
$this->drupalLogin($this->editorUser);
$this->drupalGet('node/' . $this->testNode->id());
$this->assertRaw('data-quickedit-entity-id="node/' . $this->testNode->id() . '"');
$this->testNode->title = 'Updated node';
$this->testNode->setNewRevision(TRUE);
$this->testNode->isDefaultRevision(FALSE);
$this->testNode->save();
$this->drupalGet('node/' . $this->testNode->id());
$this->assertNoRaw('data-quickedit-entity-id="node/' . $this->testNode->id() . '"');
}
/**
* Tests the loading of Quick Edit for the title base field.
*/
......
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