Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
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 (Drupal & Laravel)

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

Web Posts

Search

Page Facebook