Skip to main content
Category:

Code snippet that can be used to add theme suggestions programmatically For tables in drupal 8.

Example 1:

/**
 * @param array $suggestions
 * @param array $variables
 * @param $hook
 */
function mymodule_theme_suggestions_table_alter(array &$suggestions, array &$variables, $hook)
{
  if (isset($variables['attributes']['id'])) {
    $table_id = $variables['attributes']['id'];
    $id = str_replace("-", "_", $table_id);
    $suggestions[] = $hook . '__' . $id;
  }
}

 

Example 2:

/**
 * @param array $suggestions
 * @param array $variables
 * @param $hook
 */
function THEMENAME_theme_suggestions_table_alter(array &$suggestions, array $variables, $hook) {
    $node = \Drupal::routeMatch()->getParameter('node');
    if ($node) {
        $nid = $node->id();
        $type = $node->getType();
        $suggestions[] = $hook . '__' .$type.'_'. $nid;
    }
}

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook