Skip to content

Instantly share code, notes, and snippets.

@opi
Created March 11, 2019 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save opi/cdc6735e7a3b9936a206a73596b2d4cd to your computer and use it in GitHub Desktop.
Save opi/cdc6735e7a3b9936a206a73596b2d4cd to your computer and use it in GitHub Desktop.
Drupal 8 Custom form mode
<?php
/**
* Implements hook_entity_form_display_alter().
*/
function MY_MODULE_entity_form_display_alter(&$form_display, array $context) {
if ($context['entity_type'] == 'node' && $context['bundle'] == 'MY_BUNDLE') {
$current_user = \Drupal::currentUser();
if ($current_user->hasPermission('CUSTOM PERMISSION')) {
$storage = \Drupal::entityManager()->getStorage('entity_form_display');
$new_form_display = $storage->load('node.MY_BUNDLE.FORM_MODE_ID');
if ($new_form_display) {
$form_display = $new_form_display;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment