Problem/Motivation

@dawehner noted several points of improvement to the code committed in #1901100: Make Edit module work with TempStore, so revisions are not saved on all atomic field edits.

Just a couple of points I realized when reviewing the patch.

+++ b/core/modules/edit/lib/Drupal/edit/Access/EditEntityAccessCheck.phpundefined
@@ -0,0 +1,67 @@
+    return $entity->access('update');
Access checkers should return static::ALLOW and static::DENY.
+++ b/core/modules/edit/lib/Drupal/edit/Access/EditEntityAccessCheck.phpundefined
@@ -0,0 +1,67 @@
+      if (!$entity_type || !entity_get_info($entity_type)) {
...
+      $entity = entity_load($entity_type, $entity_id);

This information should be better injected.

+++ b/core/modules/edit/lib/Drupal/edit/Access/EditEntityAccessCheckInterface.phpundefined
@@ -0,0 +1,22 @@
+interface EditEntityAccessCheckInterface {

It is hard to see why this need to be a new interface? There is no single place in core where this is called, beside of the only class which implements it.

+++ b/core/modules/edit/lib/Drupal/edit/EditController.phpundefined
@@ -24,6 +27,23 @@
+  public function __construct(TempStoreFactory $temp_store_factory = NULL) {
+    $this->tempStoreFactory = $temp_store_factory ?: Drupal::service('user.tempstore');
+  }

I don't get why we even need lazy-injection, can't we just implement ControllerInterface and inject the dependency?

+++ b/core/modules/edit/lib/Drupal/edit/EditController.phpundefined
@@ -110,17 +130,27 @@ public function attachments(Request $request) {
+    if ($tempstore_entity && !(isset($_POST['reset']) && $_POST['reset'] === 'true')) {

$_POST could be easily replaced by using the information on the $request object.

Proposed resolution

Make the code better.

Remaining tasks

Address @dawehner's concerns.

User interface changes

None.

API changes

None.

#1901100: Make Edit module work with TempStore, so revisions are not saved on all atomic field edits

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Wim Leers’s picture

Access checkers should return static::ALLOW and static::DENY.

That is new then, we were following the older "latest best practices".

This information should be better injected.

Similarly.

It is hard to see why this need to be a new interface? There is no single place in core where this is called, beside of the only class which implements it.

True, I think Gábor did this mostly because that's what everything else is doing. Note that if parameter upcasting wasn't so terribly broken, we wouldn't need any of this at all! :(

I don't get why we even need lazy-injection, can't we just implement ControllerInterface and inject the dependency?

Great point — this slipped through the cracks!

$_POST could be easily replaced by using the information on the $request object.

Indeed! But at some point something was broken in $request, which caused me to just use $_POST instead until that got fixed.


We'll get it fixed :)

Wim Leers’s picture

Assigned: Unassigned » Wim Leers
Status: Active » Needs review
FileSize
7.28 KB

This information should be better injected.

I'm actually not sure what this means. Can you point to a code example that's doing it "right"?

Attached patch fixes everything except the above.

Gábor Hojtsy’s picture

I think this looks like a good update! I am also puzzled as to what should we do for entity injection(?).

tstoeckler’s picture

I think what is meant that instead of entity_get_info($entity_type) you should be calling Drupal::entityManager()->getDefinition($entity_type) and instead of entity_load($entity_type, $entity_id) you should be doing Drupal::entityManager()->getStorageController($entity_type)->load($entity_id) . Probably save Drupal::entityManager() in a local variable to save some keystrokes as well...

Gábor Hojtsy’s picture

Ok, injecting the entity manager sounds sensible, yup :)

Wim Leers’s picture

Status: Needs review » Needs work

#4: lovely, thanks :)

Wim Leers’s picture

Status: Needs work » Needs review
FileSize
3.58 KB
8.82 KB

Finished the last bit that I didn't address yet in #2, thanks to @tstoeckler's guidance in #4. I went a bit further, and am now also injecting the field.info service, so the call to field_info_instance() (which is deprecated in D8) is now also gone!

tstoeckler’s picture

+++ b/core/modules/edit/lib/Drupal/edit/Access/EditEntityAccessCheck.php
@@ -42,7 +42,7 @@ public function access(Route $route, Request $request) {
   public function accessEditEntity(EntityInterface $entity) {

I think this function should be protected.

+++ /dev/null
index 9ec569a..90313f9 100644
--- a/core/modules/edit/lib/Drupal/edit/Access/EditEntityFieldAccessCheck.php

--- a/core/modules/edit/lib/Drupal/edit/Access/EditEntityFieldAccessCheck.php
+++ b/core/modules/edit/lib/Drupal/edit/Access/EditEntityFieldAccessCheck.php

I think EditEntityFieldAccessCheck should use the same approach that EditEntityAccessCheck uses, i.e. no separate interface, protected method, static::ALLOW / static::DENY.

+++ b/core/modules/edit/lib/Drupal/edit/Access/EditEntityAccessCheck.php
@@ -42,7 +42,7 @@ public function access(Route $route, Request $request) {
   public function accessEditEntity(EntityInterface $entity) {

I think this function should be protected.

Wim Leers’s picture

Status: Needs review » Needs work
Wim Leers’s picture

Status: Needs work » Needs review
FileSize
2.54 KB
10.14 KB

I think this might be the RTBC one.

Gábor Hojtsy’s picture

Status: Needs review » Reviewed & tested by the community

This looks good to me!

catch’s picture

Title: Issue #1901100 does not follow best practices in several places in the committed patch » Clean up edit controller
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.x, thanks!

Wim Leers’s picture

Issue tags: -sprint

Automatically closed -- issue fixed for 2 weeks with no activity.