Skip to content

Instantly share code, notes, and snippets.

@flocondetoile
Forked from opi/module.php
Created March 11, 2019 13:20
Show Gist options
  • Save flocondetoile/167ee660b85c89747fe21bbc2b928187 to your computer and use it in GitHub Desktop.
Save flocondetoile/167ee660b85c89747fe21bbc2b928187 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