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);

 

 

Comments

Add new comment

Restricted HTML

Search

Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.

 

Contact Us

All the content is FREE but I still need your help

 

Become a patreon