Skip to content

Instantly share code, notes, and snippets.

@sumanthkumarc
Created September 20, 2017 06:13
Show Gist options
  • Save sumanthkumarc/3400d2a8d23b16e5f64eab276330c5ed to your computer and use it in GitHub Desktop.
Save sumanthkumarc/3400d2a8d23b16e5f64eab276330c5ed to your computer and use it in GitHub Desktop.
Drupal 8: Altering the actions available for a node form, either add or edit.
<?php
function hook_MY_MODULE_form_alter(){
// Altering the actions available for a node form, either add or edit.
$build_info = $form_state->getBuildInfo();
if (isset($build_info['base_form_id']) && $build_info['base_form_id'] == 'node_form') {
$form_object = $form_state->getFormObject();
$entity = $form_object->getEntity();
$bundle = $entity->bundle();
// Any bundle key added here, will have unpublish option, else action is removed.
$allowed_bundles = [
'article',
];
if (!in_array($bundle, $allowed_bundles)) {
unset($form['actions']['unpublish']);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment