CERN Accelerating science

Is there a way to display a field when a checkbox is ticked in a content type form?

Hi there,

Here i come with some more question :slight_smile:

I wondered if there is a way to display a field in a content type form when a checkbox is ticked.

My goal is the following :


I would like to display a field to know the start/end date next to the position that the user will check.

Do you guys know if there is a way to do so ?

Many thanks,


Hey @mhutinet,

Yes it is possible, however it will require some coding on your side to manipulate the form. I will describe the steps in a discrete way below:


  1. Create a Drupal module. I will not get into lots of details for this, since the article I linked is a good resource. Lets name this module form_field_display
  2. Inside the module create a file called form_field_display.module. In this file, you should write the “form manipulation” code.
  3. Inside the newly created file, implement either hook_form_alter or hook_form_FORM_ID_alter functions. You can read more about hooks and how they work in this article. The difference between the two hooks is that in the case of hook_form_alter, the function will be called every time a form is loaded and in the case of hook_form_FORM_ID_alter the function will be called only when the form with ID= FORM_ID will be loaded. This stackoverflow article can guide you on how to find the FORM_ID.
  4. After implementing one of the two hooks and you are sure that they are called only when they should be called, the next step is to actually modify the form based on your needs. A basic tool that should be your friend during this process is the Devel module which includes the ksm function. The Devel module is installed in all CERN websites so all you have to do is to enable it. After enabling it you can use in your code the ksm function to display Drupal arrays. That way you can see and modify their values based on your needs.
  5. In your case you should find the field that implements the checkboxes and based on that display (or not) another field. Your code should be smth like the following snippet:
$checked_value =  $form['POSITION_FIELD']['#value']        
if ($checked_value == 'THE_VALUE_THAT_I_WANT'){

Important Notes

  • This is the generic logic behind it. Still you have to use ksm() as described in step 4 to find the exact machine name of the field that you want to manipulate. The latter is very important since Drupal is case-sensitive with machine names, so your machine names should be used in the exact same way as returned from the API.
  • As you will read in the hooks_article, the word hook in hook_form_alter needs to be replaced by the machine name of your module. In my example it should be form_field_display_form_alter.

Hope that helps. Let me know if you get stuck at any point.


Hello again,

Small follow-up about a couple of things:

  • There is also a module that might do something similar to what you want to achieve called Conditional Fields. However it is still on alpha version, so I highly recommend you not to have it on your site since it might break at any moment. So its better to do it yourself, since a bit of coding never hurt nobody :slight_smile:
  • Another resource regarding hook_form_alter can be found in paragraph types


Hi Kostas,

Thank you so much for your detailed answer :slight_smile:

I’ll try this out and let you know if don’t manage.