Hiding Form Fields in Drupal 8

14 Aug 2023

Imagine that you have created a content type with several fields. Depending on your needs, you may not require all of these fields to be displayed when viewing the entire node. Luckily, there is a simple way on Drupal how to hide a field on a form.
Code snippets may be applied to conditionally hide or enable a form field programmatically When you need to hide or show a form field. based on the specific value from another field, the Drupal form API hide field allows Drupal 8 hide form fields relating to values chosen in other fields. 

Drupal 8 Hide Field Programmatically

Drupal hide form fields module is a storage field as a field set in Drupal 8. This module stores metadata on each group in the array known as #fieldgroups used in a re-render option. 
Below is an instruction for hiding and showing webform components applying the hook_form_alter with the #access property. Typically, you may apply the webform module’s built-in component access controls via the UI and business logic to hide elements related to various user properties or fields.
When the user is prohibited from seeing the page means that menu links leading are also hidden from menus. Nevertheless, there is a time when the prohibition is insufficient, and you must hide menu elements programmatically. A specific page is the registration page that actually redirects to the hide-fied edit form.
For each view mode, Drupal allows you to hide or enable fields, change the position that fields are rendered in, hide or show the field’s label, and change the display of certain types of pages. For instance, when the field’s content is trimmed.

How to Hide a Field on a Form?

If you operated with the Field UI in Drupa 7, you should know that you may protect fields from being displayed while viewing entities, for instance, content, users, etc. It was pretty simple; you went to the Manage Display tab of an entity and moved the field to the Hidden space, as shown in the pic below.
 

So you could hide field output from being shown when viewing the entity. However, what according to when editing that entity? There was no idea in the Drupal 7 field UI how to hide a field on a form. You had to write some form of hook_form_alter in a custom module and manually make the field hidden. The excellent news, Drupal 8 doesn’t require writing any code to hide fields on a form. Fields may be set to hide via the form display configuration.

So, we will now add a new field to our user accounts and hide it on the user form. Let’s create a new field as in Drupal 7 by following to Configuration-People-Account setting. Create a boolean labeled Pants.

Set the On and Off values appropriately and save.

Set the default value to “Yes” and hit save.

The next step is crucial. Go back to the account settings configuration page and find the Manage Form Display tab. A hidden field widget is added to Drupal 8, and you may drag this widget down to the Disabled region. They both do the same things. Don’t forget to save!

The next step is when users go to create a new account or edit the existing one.

Nevertheless, you may notice that the default values are displayed on our profile page after creating the new account, as we set a default value for the field.

You may use this step for the further form view models. Then go back to the Manage Form Display tab on the account settings configuration page, and at the bottom, enable custom display settings to register form view mode.

Now you may see two various modes for our form. The default mode is applied while editing the user and the new register mode is when creating a new user. So, let's display "Pants" in the default mode.

While editing the user account, you can see the form field for pants, which is still hidden on the registration form.

The opportunities are endless by combining improved field UI and view modes for entities in Drupal 8. Being able to hide fields on forms is just another possibility to make Drupal development convenient to build websites with.

Drupal 8 Hide Form Elements

You may safely apply three major methods to hide a form element. Use '#type' => 'value' on the component that will be affected by the value for the lament still being passed through the submit/validate functions with its # value as usual. This is the most preferred method.
There is a list of available elements or all form elements: you will see \Drupal\Core\Render\Element\RenderElement 
for additional information, and a list of properties typical to all render elements, including form elements as well. Properties are specific to particular elements and documented in the element's class.

Drupal 8 Hide Form Field

If you apply "'#access' => 'FALSE,'" you hide the field in the form but miss its value of it. To support the default_value and hide the field, you have to store the default value, disable the field item, and create it again with the value before you disable it. Here's an example.
\Drupal\Core\Entity\Display\EntityFormDisplayInterface The entity form display is associated with the given form mode. Outmoded as of Drupal 8.0.x and will be removed before Drupal 9.0.0.

Drupal Hide Field on Edit Form

The code snippet may be applied to hide a field in the edit form in Drupal 8.
Apply the "hook_form_alter" to hide the particular form field:

<!--?php
/**
 * @param $form
 * @param $form_state
 * @param $form_id
 */
function mymodule_form_alter(&$form, &$form_state, $form_id) {
 if ($form_id == 'contenttypename_node_form') {
 $form['your_field_name']['#access'] = 0;
 }
}
or use this:

$form['your_field_name']['#access'] = FALSE;
PHPCopy

$current_user = \Drupal::currentUser();
$roles = $current_user->getRoles();
$form['your_field_name']['#access'] = in_array('editor', $roles);

Drupal 8 Field Permissions

The Field Permissions module provides website administrations to set field-level permissions to edit, display and create fields on any entity.

Top features

  • Entitled Field Permissions on any entities are not just nodes;
  • Role-based field permissions allow different displaying patterns according to what access the user is provided with;
  • Authorplevel permissions allow the displaying and editing fields according to who is the entity owner.
  • Permissions for each field are not entitled by default. Instead, administrators can allow these permissions explicitly for the fields where the feature is required.

Instructions

When the Field OPermissions module is installed. You will need to edit the field settings form to entitle permissions for each field where this feature is required. You can allow each of the following permissions types:

  • Create your own value for the field
  • Edit your own value for the field
  • Edit anyone's value for the field
  • View your own value for the field
  • View anyone's value for the field

Some Use Cases for Field Permissions

  • The requirement to hide a field for anonymous users on the front end.
  • Adding new fields to content for testing purposes before being released to the public.
  • Display extra info to registered users.
  • Restrict edit access to individual user roles for fields.
  • Provide anonymous users to create values for fields.

The good thing about the module is that when you don’t provide specific permissions for a field, the default settings are applied at the content type level.

To Sum Up

Use these alternatives to entitle role-based permissions for each field. When permissions are entitled to a certain field, access to this field will be denied by default, and explicit permissions should be granted to certain user roles from the permissions administration page. On the other hand, when these alternatives are disabled, field permissions are inherited from node display and edit permissions. For instance, users who can view a particular node will also be able to view this field, and so on.