- Issue created by @gilbertdelyon
- π«π·France gilbertdelyon
That's me again!
Seems to work as follows:
- Go to admin / content / filter published content / select all
- Go to bottom of the page and save content/all selected elements
A warning should be added to documentation because when a rule is not working as expected newbies like me can susspect a wrong setting while re-saving content will solve the issue. - πΊπΈUnited States partdigital
Hi gilbertdelyon, thanks for reaching out!
That is a clever workaround! Only risk with that approach is that it will assign the access policy based on the currently logged-in user. If you performed that action with a role that wasn't allowed to assign the "department" policy for example, it wouldn't change the policy, even if the department field had a value.
This has been on my radar for a while, perhaps I can address this with a custom "Save default access policy" action? This would account for the original author.
You can also achieve the same result with this code added to an update hook. Note that if you have a lot of nodes with missing policies you probably want to put this into a batch.
$entityTypeManager = \Drupal::entityTypeManager(); $selectionManager = \Drupal::service('access_policy.selection'); $nids = $entityTypeManager->getStorage('node')->getQuery() ->accessCheck(FALSE) ->condition('access_policy', NULL, 'IS NULL') ->execute(); $nodes = $entityTypeManager->getStorage('node')->loadMultiple($nids); foreach ($nodes as $node) { // Get the original author. This is necessary because different authors may // have access to different access policies. $owner = $node->getOwner(); $policies = $selectionManager->getDefaultPolicy($node, $owner); if (!empty($policies)) { $node->set('access_policy', $policies); $node->save(); } }
- π«π·France gilbertdelyon
Thank you for this prompt answer and for the piece of code.
If you performed that action with a role that wasn't allowed to assign the "department" policy for example, it wouldn't change the policy, even if the department field had a value.
As installing Acces Policy needs admin permissions I also resaved content with admin permissions, so I think this is not a big issue.
BTW. It seems your code is dedicated to nodes. Would also work for existing medias? (only for my personal knowledge - I had no access control over medias before using Access Policy module)
- πΊπΈUnited States partdigital
Yes that code will work with media as well. If you want to do this dynamically you can use this method to fetch all the entity types that have access policies.
\Drupal::service('access_policy.information')->getAllEnabledEntityTypes();
- π«π·France gilbertdelyon
Thank you, I appreciate
So, I will close this issue. - Status changed to Closed: works as designed
about 1 year ago 2:26pm 18 December 2023