1

I am trying to limit access to each item in a paragraph field. My goal is to only let the creator of each item change the specific item. I installed and enabled the Field Permission module and this is almost exactly what I am looking for. I have configure custom permissions for each field in the paragraph. The only problem right now is that each paragraph item is still visible even though none of the fields are showned:

enter image description here

Is there a way to hide or disable a paragraph item for all users except the owner?

  • Do paragraphs even have an authoring workflow (assigned to a uid and such)? – Kevin May 14 '17 at 14:42
  • No, it doesn't, but you could store that in an additional field with some custom code and then do entity edit access based on that. Field permissions will not help here. – Berdir May 14 '17 at 15:10
2

Each paragraph entity has a user reference base field which if not set to something else defaults to the current user logged in.

So the only thing you have to do is implement a hook to control the entity access; something like

/**
 * Implements hook_ENTITY_TYPE_access().
 */
function custom_mod_paragraph_access(EntityInterface $entity, $operation, AccountInterface $account) {

  if ($operation !== 'update') {
    return AccessResult::neutral();
  }


  if ($entity->getOwnerId() !== $account->id()) {
    return AccessResult::forbidden();
  }

  return AccessResult::neutral();
}

And the result will like this:

enter image description here

As you can see, the User would still be able to delete the paragraph but not edit it. So you'd probably want to update the code above to forbid the delete operation as well.

This does also only "disable" the paragraph and not hide it.

To actually hide them, I suppose you would need to make a form alter and then set them to have #access = false.

  • This seem to be what I'm searching for. Using this approach will it be possible to hide only one paragraph item? – Cyclonecode May 15 '17 at 11:53
  • Do you mean a specific paragraph type? – johndevman May 15 '17 at 14:08
  • As in my above example this paragraph kan have multiple rows. – Cyclonecode May 15 '17 at 19:20
  • Yes, in this case I need to control access to different paragraph fields. – Cyclonecode May 17 '17 at 13:15
2

Field permissions is not what you want, that only does permissions for fields, not for the entire entity like you want.

There's a hook called hook_ENTITY_TYPE_access, you can use it as hook_paragraphs_item_access(), that would let you alter permissions (Look at the paragraph_bundle_permissions submodule for an example).

As others have said paragraph items don't actually have the owner on them - neither does field collection. They aren't really mean't to be separated like this. You can add the property yourself if you want to.

I recommend changing the Paragraphs to not be paragraphs and be something like Nodes instead. If they have permissions and are owned by someone, paragraphs aren't really the right thing. You can use Inline Entity Form if you want to embed a form for it. Since they are nodes you should be able to have more control on permissions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.