Skip to content

Instantly share code, notes, and snippets.

@banoodle
Created February 6, 2021 03:51
Show Gist options
  • Save banoodle/de07f7970d9ba18c925a85c3e2ff75e2 to your computer and use it in GitHub Desktop.
Save banoodle/de07f7970d9ba18c925a85c3e2ff75e2 to your computer and use it in GitHub Desktop.
Drupal 8 hook_form_media_form_alter hooks that add entity ids to entity edit forms
<?php
/**
* Implements hook_form_node_form_alter().
*/
function your_module_form_node_form_alter(&$form, &$form_state, $form_id) {
$node = \Drupal::request()->attributes->get('node');
// If it's the edit form as opposed to the add form.
if (!empty($node)) {
$orig_title = $node->getTitle();
$nid = $node->id();
$new_title = $orig_title . ' (node id: ' . $nid . ')';
$form['#title'] = $new_title;
}
}
/**
* Implements hook_form_media_form_alter().
*/
function your_module_form_media_form_alter(&$form, &$form_state, $form_id) {
$entity = \Drupal::request()->attributes->get('media');
// If it's the edit form as opposed to the add form.
if (!empty($entity)) {
$orig_title = $entity->getName();
$mid = $entity->id();
$new_title = $orig_title . ' (media id: ' . $mid . ')';
$form['#title'] = $new_title;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment