Problem/Motivation

The permissions callback provider class, being handled by the controller resolver, is capable of dependency injection by implementing ContainerInjectionInterface. The following patch adds that for the node module permission provider. The same should be done for other core modules that provide permission callbacks.

Proposed resolution

Make NodePermissions implement ContainerInjectionInterface
Add autowiring of entity type manager

Remaining tasks

Review

API changes

NodePermissions now requires an $entityTypeManager argument.

Issue fork drupal-3092001

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

krystalcode created an issue. See original summary.

krystalcode’s picture

kim.pepper’s picture

Status: Active » Needs review

Let's trigger the testbot by marking needs review.

joachim’s picture

Status: Needs review » Needs work

Looks good overall, just a nitpick:

+++ b/core/modules/node/src/NodePermissions.php
@@ -32,13 +71,13 @@ public function nodeTypePermissions() {
    * Returns a list of node permissions for a given node type.
    *
-   * @param \Drupal\node\Entity\NodeType $type
+   * @param \Drupal\node\NodeTypeInterface $type
    *   The node type.
    *
    * @return array
    *   An associative array of permission names and descriptions.
    */
-  protected function buildPermissions(NodeType $type) {
+  protected function buildPermissions(NodeTypeInterface $type) {

Those changes seem unrelated to me.

felribeiro’s picture

Status: Needs work » Needs review
StatusFileSize
new2.2 KB
new718 bytes

Status: Needs review » Needs work

The last submitted patch, 5: node-dependency-injection-permission-callbacks-3092001-5.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

felribeiro’s picture

Status: Needs work » Needs review
StatusFileSize
new2.17 KB
new1.02 KB
hardik_patel_12’s picture

StatusFileSize
new5.04 KB
new6.69 KB

Removing unused Drupal\Core\StringTranslation\TranslationInterface and unused $this->stringTranslation service injection it is not used in file.

Using \Drupal\Core\Entity\EntityStorageInterface instead of Drupal\Core\Config\Entity\ConfigEntityStorageInterface for injecting $this->nodeTypeStorage services.

Fixing some drupal standards error also, Kindly review a new patch.

Status: Needs review » Needs work

The last submitted patch, 8: node-dependency-injection-permission-callbacks-3092001-8.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

hardik_patel_12’s picture

StatusFileSize
new5.01 KB
new6.45 KB

Removing unused Drupal\Core\StringTranslation\TranslationInterface and unused $this->stringTranslation service injection it is not used in file.

Fixing some drupal standards error also for patch at #7 , Kindly review a new patch.

hardik_patel_12’s picture

Status: Needs work » Needs review
vivek panicker’s picture

Assigned: Unassigned » vivek panicker
Issue tags: +GCWIndia2020

Will review the patch and provide feedbacks/updates if necessary.

vivek panicker’s picture

Made compatible to coding standards issue.

vivek panicker’s picture

Assigned: vivek panicker » Unassigned
hardik_patel_12’s picture

@vivek Panicker , kindly upload interdiff also along with the patch whenever you do some changes in patch. It will be helpful for other to track what's exactly changes done.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

ranjith_kumar_k_u’s picture

StatusFileSize
new2 KB

Rerolled #13 for 9.5

elber’s picture

Assigned: Unassigned » elber

I will revise it.

elber’s picture

I'm keep revising it

elber’s picture

Version: 9.4.x-dev » 9.5.x-dev
Assigned: elber » Unassigned
Status: Needs review » Reviewed & tested by the community
StatusFileSize
new5.4 KB

I revised the reroll #25 I applied in drupal9-9.5 version

Core keeps working as expected.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 20: 3092001-20.patch, failed testing. View results

nitin shrivastava’s picture

StatusFileSize
new2 KB

#13 patch is not applied successfully on 9.5.x , reroll for 9.5.x.

akram khan’s picture

StatusFileSize
new2.03 KB
new1.5 KB

Fixed CCF #25

akram khan’s picture

StatusFileSize
new1.99 KB

try to fix

sahil.goyal’s picture

StatusFileSize
new2 KB
new379 bytes

trying Fix the PHPCS error

nivethasubramaniyan’s picture

StatusFileSize
new2 KB

Fixing CCF errors

elber’s picture

StatusFileSize
new32.08 KB

Hi I applied the patch #29 after that I ran these phpcs commands :

phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml .

phpcs --standard=DrupalPractice --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml .

I found that errors in screenshot

elber’s picture

Assigned: Unassigned » elber
elber’s picture

Assigned: elber » Unassigned
Status: Needs work » Reviewed & tested by the community

Hi everyone the lasts rerrols erased the changes previous did before. I revised the patch #20 and I was able to apply in drupal 9.5 version, and the changes were kept. You can discard the others patches that was made after patch #20, because it erased the changes of the issue.

The last submitted patch, 20: 3092001-20.patch, failed testing. View results

The last submitted patch, 20: 3092001-20.patch, failed testing. View results

xjm’s picture

Version: 9.5.x-dev » 10.1.x-dev
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs followup

As an API addition/internal BC break, this should be targeted for the development branch (currently 10.1.x).

  1. +++ b/core/modules/node/src/NodePermissions.php
    @@ -2,25 +2,56 @@
    -use Drupal\Core\StringTranslation\StringTranslationTrait;
     use Drupal\node\Entity\NodeType;
    +use Drupal\Core\StringTranslation\StringTranslationTrait;
    

    This reordering of use statements is out of scope and also against our default pattern of having them alphabetized (so Drupal\Core should come before Drupal\node).

  2. +++ b/core/modules/node/src/NodePermissions.php
    @@ -2,25 +2,56 @@
    +  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    +    $this->nodeTypeStorage = $entity_type_manager->getStorage('node_type');
    

    For BC purposes, since these objects previously would have been constructed directly instead of by the factory method, this should probably allow the entity type manager to default to NULL. The parameter documentation should then be updated accordingly.

    And, if null is passed, it should load the storage conditionally using \Drupal::entityTypeManager(), with a deprecation warning.

  3. +++ b/core/modules/node/src/NodePermissions.php
    @@ -2,25 +2,56 @@
    +    return new static(
    +      $container->get('entity_type.manager')
    +    );
    

    This should be on a single line.

  4. +++ b/core/modules/node/src/NodePermissions.php
    @@ -2,25 +2,56 @@
       public function nodeTypePermissions() {
    +    // Generate node permissions for all node types.
         return $this->generatePermissions(NodeType::loadMultiple(), [$this, 'buildPermissions']);
    

    This comment addition is out of scope, and also seems unnecessary?

  5. +++ b/core/modules/node/src/NodePermissions.php
    @@ -2,25 +2,56 @@
         return $this->generatePermissions(NodeType::loadMultiple(), [$this, 'buildPermissions']);
    

    I think this needs to be changed to use the entity type manager as well? Otherwise, this will load from the default storage even if someone injects a different one, which could lead to data integrity errors.

The issue summary also says this should be done for other modules that provide permissions callbacks. Can we get a followup meta for that?

Thanks everyone!

xjm’s picture

Issue tags: +Needs change record

This will also need a change record. Thanks!

ameymudras’s picture

Status: Needs work » Needs review
StatusFileSize
new2.13 KB
new1.88 KB

Fixed the code review issues specified in #35

ameymudras’s picture

StatusFileSize
new2.13 KB

There was an issue with the above patch please ignore it

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

This issue is being reviewed by the kind folks in Slack, #need-reveiw-queue. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge require as a guide.

Moving back to NW for the change record and follow up mentioned in #35

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

hardik_patel_12’s picture

Updated the DI in permission callbacks for TaxonomyPermissions.php, a change record is still needed.

joachim’s picture

Issue tags: -Needs change record

> This will also need a change record. Thanks!

I don't think this needs a CR - the class is internal. It's not even a service.

krystalcode’s picture

Removing unused Drupal\Core\StringTranslation\TranslationInterface and unused $this->stringTranslation service injection it is not used in file.

The string translation service is used many times in this file, every time $this->t() is called. The StringTranslationTrait takes care of it if it is missing, but it's hacky and renders the class more difficult to test. It is there for cases where dependency injection is not available or for developer that do not know how to do it, but it should be injected when possible: https://git.drupalcode.org/project/drupal/-/blob/11.x/core/lib/Drupal/Co...

acbramley made their first commit to this issue’s fork.

acbramley’s picture

Status: Needs work » Needs review

Rebased, updated to use autowiring and constructor property promotion, changed to not inject entity storage, added CR and deprecation message although I somewhat agree this is overkill.

acbramley’s picture

Issue summary: View changes
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs followup

Opened #3516433: META: Use dependency injections in permission callbacks to track others

Change here though seems pretty straightforward though, good deprecation.

LGTM

quietone’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs title update

Setting to needs work for an item in the MR. Also, this needs a title update.

I see that xjm asked for a Meta to make these changes in other permission callback but I wonder if these can all be done at once. Not sure.

nicxvan made their first commit to this issue’s fork.

nicxvan’s picture

Title: Dependency injection in permission callbacks » Dependency injection in NodePermissions
Status: Needs work » Needs review
Issue tags: -Needs title update

I updated the title and took care of the cr link.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Title seems good now, change still looks good.

  • catch committed fc7274d2 on 11.2.x
    Issue #3092001 by hardik_patel_12, acbramley, felribeiro, ameymudras,...

  • catch committed a3e346ee on 11.x
    Issue #3092001 by hardik_patel_12, acbramley, felribeiro, ameymudras,...

catch’s picture

Version: 11.x-dev » 11.2.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 11.x, thanks!

I jumped the gun and deleted the CR because we don't need change records for constructor bc, then realised that we do need them because phpcs demands one for all depecation messages. I really think we need to change that requirement, but for now I made a new CR and a redirect from the old one to the new one.

Status: Fixed » Closed (fixed)

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

xjm’s picture

Amending attribution.