πΈπ³Senegal tsene
Template suggestion "entity-moderation-form.html.twig" based on current state and content type where the form is displayed
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function mytheme_theme_suggestions_entity_moderation_form_alter(array &$suggestions, array $variables) {
$form = $variables['form'] ?? NULL;
if (!$form) {
return;
}
// Tentative d'obtenir l'Γ©tat actuel
$current_state = NULL;
$safe_current_state = NULL;
// Get the current state
if (isset($form['current'])) {
if (isset($form['current']['#markup'])) {
$current_state = $form['current']['#markup'];
$safe_current_state = strtolower(preg_replace('/[^a-zA-Z0-9]+/', '_', $current_state));
}
}
// Get the current route match service
$route_match = \Drupal::routeMatch();
// Try to get the node from the current route
$node_parameter = $route_match->getParameter('node');
// Initialize variables
$node_bundle = NULL;
// Check if we have a node parameter and extract information
if ($node_parameter instanceof \Drupal\Core\Entity\EntityInterface) {
$node_bundle = $node_parameter->bundle();
} elseif ($node_parameter instanceof \Drupal\Core\Field\FieldItemListInterface) {
$node_bundle = $node_parameter->getEntity()->bundle();
} elseif (is_numeric($route_match->getRawParameter('node'))) {
// Load the node to get the bundle
$nid = $route_match->getRawParameter('node');
$node = \Drupal\node\Entity\Node::load($nid);
if ($node) {
$node_bundle = $node->bundle();
}
}
// Add template suggestions
if ($safe_current_state) {
$suggestions[] = "entity_moderation_form__" . $safe_current_state;
}
if ($node_bundle) {
$suggestions[] = "entity_moderation_form__" . $node_bundle;
}
if ($safe_current_state && $node_bundle) {
$suggestions[] = "entity_moderation_form__" . $node_bundle . "__" . $safe_current_state;
}
// For debugging
// \Drupal::logger('mytheme')->notice('Template suggestions: @suggestions', ['@suggestions' => print_r($suggestions, TRUE)]);
}