How to create Hidden type Field in Drupal 8 Entity

drupaldrupal-8

I am using drupal 8, I have an Entity, I want to add a hidden type field in Entity Form. How I can add hidden field type? like below

<form>
    <input type='hidden' name='my_hidden' />
</form>

Code Generating Form is as below:

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) 
{
   $fields = parent::baseFieldDefinitions($entity_type);
    $fields['id'] = BaseFieldDefinition::create('integer')
      ->setLabel(t('ID'))
      ->setDescription(t('The ID of the Timeslot entity.'))
      ->setReadOnly(TRUE);
    return $fields;
}

Best Answer

There are two steps to make a field hidden in drupal 8 entity forms.

  1. If you want to make an existing field hidden you may alter the form and update that field as

    $form['your_field_name']['widget'][0]['value']['#type'] = 'hidden';

  2. You can use https://www.drupal.org/project/field_hidden and by enabling this module, Select the 'Hidden field' widget for a field in the entity type's 'Manage form display' dialogue.

Related Topic