I've run into an instance where I need to add a field to a Paragraph bundle but only have it accessible when it's been added to certain content types.

I've tried hook_field_widget_form_alter but I'm apparently not using it correctly as I'm unable to see any tangible effect on the field.

How would I pre-process this field on the admin side so that I could conditionally disable it. The field has a unique machine name that is not shared used in any other content types or paragraphs.

Thank you!

  • 1
    Are we talking about a Paragraph type? I would just create a new field and select which paragraph types are permitted for that field for that specific content type. For just a vanilla field, did you try using '#access' => FALSE when altering it in the form alter? I don't know if that affects visibility for user 1 or not, so you would need to test that as a lower user. – Kevin Oct 25 '17 at 15:37
  • @Kevin I was hoping there was a way to control this like you describe but I couldn't find it. I have the field added to my Paragraph Type, but I can't figure out how to make that field only accessible on certain content types. – Vecta Oct 25 '17 at 15:40
  • 1
    By which ones are permitted I was talking about Paragraph types allowed, but you are just talking about a vanilla field. A form alter should be the way here, you just need to check the node edited (obtainable from the request object) for its type, then hide or show the field based on #access. – Kevin Oct 25 '17 at 15:43
  • @Kevin Ah, makes sense. Thank you! Am I on the right track using hook_field_widget_form_alter? Do you have any tips on targeting that specific field with my alter function? – Vecta Oct 25 '17 at 15:46

Your Answer

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

Browse other questions tagged or ask your own question.