Skip to main content
Category:

Code snippet that can be used to hide a field in node edit form in Drupal 8.

use "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;

or if you want to show field based on role use:

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

 

 

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook