“ How you look at it is pretty much how you'll see it ! "
Recently, in a project, I came across a problem statement where I was supposed to display a Node Form to an authenticated user with access to edit only limited set of fields, in a way similar to maintaining a Linkedin profile.
The ideal solution is to create a separate form mode with the ability to access only a limited number of fields and keeping all other fields disabled from displaying in the form itself .
In my mind, this looked pretty easy to implement; but I was stuck at a point where I had to use this “Custom Form Mode”. This is when I realised, Drupal 8 core does not provide any interface or help to make use of the “Custom Form Mode”. We have to programmatically call the custom form mode and display it to the type of user intended.
Also I came across this module Form Mode Control which does provide help in terms of managing permissions and form modes through an admin interface, but if you want to avoid installing another contributed module for a small implementation(similar to our case) which does not involve managing multiple roles / users and also does not require providing a manageable admin interface then this simple solution is a way to go !
Through this post, we’ll look into the following :
Before we move ahead with actual implementation, first let’s understand the basics
According to the Drupal documentation here
Whereas,
/**
* Implements hook_entity_type_build().
*/
function display_custom_form_mode_entity_type_build(array &$entity_types) {
$entity_types['node']->setFormClass('company_form_mode', 'Drupal\node\NodeForm');
}
/**
* Implements \Drupal\block\BlockBase::build().
*/
public function build() {
$build = array();
$nids = $this->getNodeFromCurrentUserProfile();
//Display Node Form.
if (!empty($nids)) {
$node_form = Node::load($nids);
$build['form'] = $this->entityFormBuilder->getForm($node_form, 'company_form_mode');
}
return $build;
}
public static function getNodeFromCurrentUserProfile() {
$nids = '';
// Fetch Email ID
$current = \Drupal::currentUser();
if ($current->id()) {
$account = \Drupal\user\Entity\User::load($current->id());
if (!empty($account)) {
$email = $account->getEmail();
}
}
//Fetch Company Id.
if (!empty($email)) {
$query = \Drupal::database()->select('node__field_mail', 'nce');
$query = $query->fields('nce', array('entity_id'));
$query = $query->condition('nce.bundle
', 'agency');
$query = $query->condition('nce.field_mail_value
', $email);
$nids = $query->execute()->fetchField();
}
return $nids;
}
With this, we have now successfully created the Custom Form Mode and used it to display it as a Block to an authenticated user.We have come a long way in Drupal 8 and now it’s high time to get some implementations up and running as a part of Drupal Core as things like these are an essential component of site Building. If it was difficult to understand in terms of steps, please refer the Youtube video Link below to get some more clarity on the steps :
Also you can follow this github link to get the code and get the Custom Form Mode implementation up and running : https://github.com/panshulK/CustomFormMode
Thank You.