drupal_find_theme_templates($cache, $extension, $path)
Allows themes and/or theme engines to easily discover overridden templates.
Parameters
$cache: The existing cache of theme hooks to test against.
$extension: The extension that these templates will have.
$path: The path to search.
File
- core/includes/theme.inc, line 193
- The theme system, which controls the output of Drupal.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | function drupal_find_theme_templates( $cache , $extension , $path ) { $implementations = array (); // Collect paths to all sub-themes grouped by base themes. These will be // used for filtering. This allows base themes to have sub-themes in its // folder hierarchy without affecting the base themes template discovery. $theme_paths = array (); foreach (\Drupal::service( 'theme_handler' )->listInfo() as $theme_info ) { if (! empty ( $theme_info ->base_theme)) { $theme_paths [ $theme_info ->base_theme][ $theme_info ->getName()] = $theme_info ->getPath(); } } foreach ( $theme_paths as $basetheme => $subthemes ) { foreach ( $subthemes as $subtheme => $subtheme_path ) { if (isset( $theme_paths [ $subtheme ])) { $theme_paths [ $basetheme ] = array_merge ( $theme_paths [ $basetheme ], $theme_paths [ $subtheme ]); } } } $theme = \Drupal::theme()->getActiveTheme()->getName(); $subtheme_paths = isset( $theme_paths [ $theme ]) ? $theme_paths [ $theme ] : array (); // Escape the periods in the extension. $regex = '/' . str_replace ( '.' , '\.' , $extension ) . '$/' ; // Get a listing of all template files in the path to search. $files = file_scan_directory( $path , $regex , array ( 'key' => 'filename' )); // Find templates that implement registered theme hooks and include that in // what is returned so that the registry knows that the theme has this // implementation. foreach ( $files as $template => $file ) { // Ignore sub-theme templates for the current theme. if ( strpos ( $file ->uri, str_replace ( $subtheme_paths , '' , $file ->uri)) !== 0) { continue ; } // Remove the extension from the filename. $template = str_replace ( $extension , '' , $template ); // Transform - in filenames to _ to match function naming scheme // for the purposes of searching. $hook = strtr ( $template , '-' , '_' ); if (isset( $cache [ $hook ])) { $implementations [ $hook ] = array ( 'template' => $template , 'path' => dirname( $file ->uri), ); } // Match templates based on the 'template' filename. foreach ( $cache as $hook => $info ) { if (isset( $info [ 'template' ])) { $template_candidates = array ( $info [ 'template' ], str_replace ( $info [ 'theme path' ] . '/templates/' , '' , $info [ 'template' ])); if (in_array( $template , $template_candidates )) { $implementations [ $hook ] = array ( 'template' => $template , 'path' => dirname( $file ->uri), ); } } } } // Find templates that implement possible "suggestion" variants of registered // theme hooks and add those as new registered theme hooks. See // drupal_find_theme_functions() for more information about suggestions and // the use of 'pattern' and 'base hook'. $patterns = array_keys ( $files ); foreach ( $cache as $hook => $info ) { $pattern = isset( $info [ 'pattern' ]) ? $info [ 'pattern' ] : ( $hook . '__' ); if (!isset( $info [ 'base hook' ]) && ! empty ( $pattern )) { // Transform _ in pattern to - to match file naming scheme // for the purposes of searching. $pattern = strtr ( $pattern , '_' , '-' ); $matches = preg_grep( '/^' . $pattern . '/' , $patterns ); if ( $matches ) { foreach ( $matches as $match ) { $file = $match ; // Remove the extension from the filename. $file = str_replace ( $extension , '' , $file ); // Put the underscores back in for the hook name and register this // pattern. $arg_name = isset( $info [ 'variables' ]) ? 'variables' : 'render element' ; $implementations [ strtr ( $file , '-' , '_' )] = array ( 'template' => $file , 'path' => dirname( $files [ $match ]->uri), $arg_name => $info [ $arg_name ], 'base hook' => $hook , ); } } } } return $implementations ; } |
Please login to continue.