Advertisement
Juc1

/sites/all/themes/omega/omega/panels/layouts/omega/omega.inc

Jan 25th, 2014
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.05 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * @file
  5.  * Registers Omega layouts as Panels layouts.
  6.  */
  7.  
  8. /**
  9.  * Implementation of hook_LAYOUT_panels_layouts().
  10.  */
  11. function omega_omega_panels_layouts() {
  12.   // We cannot use the 'path' property here and anywhere in our layouts declared
  13.   // in omega_panels_get_sublayouts() because panels uses it for both, the icon
  14.   // and the include file in panels_theme() which we don't want. It's something
  15.   // that works for things like 'flexible' layouts but not when the child plugin
  16.   // mechanism is only used for an alternative discovery logic as in this
  17.   // case.
  18.   return array(
  19.     'title' => t('Omega'),
  20.     'category' => t('Omega page layouts'),
  21.     'description' => t('Omega page layouts to be used as Panels Everywhere site templates.'),
  22.     'get child' => 'omega_panels_get_sublayout',
  23.     'get children' => 'omega_panels_get_sublayouts',
  24.     'theme' => 'omega_panels',
  25.     'admin theme' => 'omega_panels_admin',
  26.     'regions' => array(),
  27.   );
  28. }
  29.  
  30. /**
  31.  * Callback to retrieve a single Omega layout.
  32.  */
  33. function omega_panels_get_sublayout($plugin, $layout, $sublayout) {
  34.   $layouts = omega_panels_get_sublayouts($plugin, $layout);
  35.   if (isset($layouts["$layout:$sublayout"])) {
  36.     return $layouts["$layout:$sublayout"];
  37.   }
  38. }
  39.  
  40. /**
  41.  * Callback to retrieve all Omega layouts.
  42.  */
  43. function omega_panels_get_sublayouts($plugin, $layout) {
  44.   $path = drupal_get_path('theme', 'omega');
  45.   require_once "$path/includes/omega.inc";
  46.  
  47.   $layouts = array();
  48.   if ($items = omega_layouts_info('omega')) {
  49.     foreach ($items as $name => $info) {
  50.       // Compute the relative path to the actual layout.
  51.       $relative = _omega_relative_path("$path/panels/layouts/omega", $info['path']);
  52.  
  53.       $layouts["$layout:$name"] = array(
  54.         'name' => "$layout:$name",
  55.         'title' => $info['info']['name'],
  56.         'omega_layout' => $name,
  57.       ) + $plugin;
  58.  
  59.       // Panels calls the preview image 'icon'.
  60.       if (isset($info['info']['preview'])) {
  61.         $layouts["$layout:$name"]['icon'] = "$relative{$info['info']['preview']}";
  62.       }
  63.  
  64.       // Layouts may provide 'admin css' files.
  65.       if (isset($info['info']['admin css'])) {
  66.         $layouts["$layout:$name"]['admin css'] = "$relative{$info['info']['admin css']}";
  67.       }
  68.  
  69.       // Various optional elements from the .info file of the layout.
  70.       foreach (array('regions', 'category', 'description') as $key) {
  71.         if (isset($info['info'][$key])) {
  72.           $layouts["$layout:$name"][$key] = $info['info'][$key];
  73.         }
  74.       }
  75.     }
  76.   }
  77.   return $layouts;
  78. }
  79.  
  80. /**
  81.  * Implements hook_preprocess_omega_panels_admin().
  82.  */
  83. function template_preprocess_omega_panels_admin(&$variables) {
  84.   _omega_panels_layout_preprocess($variables);
  85. }
  86.  
  87. /**
  88.  * Theme function for rendering an Omega layout on the Panels admin UI.
  89.  */
  90. function theme_omega_panels_admin(&$variables) {
  91.   // Clean up the theme hook suggestion so we don't end up in an infinite loop.
  92.   unset($variables['theme_hook_suggestion'], $variables['theme_hook_suggestions']);
  93.  
  94.   $hook = str_replace('-', '_', $variables['omega_layout']['template']);
  95.   return theme($hook, $variables);
  96. }
  97.  
  98. /**
  99.  * Theme function for rendering an Omega layout.
  100.  */
  101. function theme_omega_panels(&$variables) {
  102.   $path = drupal_get_path('theme', 'omega');
  103.   require_once "$path/template.php";
  104.  
  105.   return theme_omega_page_layout($variables);
  106. }
  107.  
  108. /**
  109.  * Implements hook_preprocess_omega_panels().
  110.  */
  111. function template_preprocess_omega_panels(&$variables) {
  112.   $path = drupal_get_path('theme', 'omega');
  113.   require_once "$path/includes/omega.inc";
  114.  
  115.   _omega_panels_layout_preprocess($variables);
  116.  
  117.   $layout = $variables['omega_layout'];
  118.   $page = &$variables['page'];
  119.  
  120.   // Wrap every page item in a region template.
  121.   foreach ($layout['info']['regions'] as $region => $name) {
  122.     if (!empty($variables['page'][$region])) {
  123.       $page[$region] = array(
  124.         '#region' => $region,
  125.         '#theme_wrappers' => array('region'),
  126.         '#children' => $page[$region],
  127.       );
  128.     }
  129.   }
  130.  
  131.   // Place dummy blocks in each region if the 'demo regions' setting is active
  132.   // to force regions to be rendered.
  133.   if (omega_extension_enabled('development') && omega_theme_get_setting('omega_demo_regions', TRUE) && user_access('administer site configuration')) {
  134.     foreach ($layout['info']['regions'] as $region => $name) {
  135.       if (empty($page[$region])) {
  136.         $page[$region]['#theme_wrappers'] = array('region');
  137.         $page[$region]['#region'] = $region;
  138.       }
  139.  
  140.       $page[$region]['#name'] = $name;
  141.       $page[$region]['#debug'] = TRUE;
  142.     }
  143.   }
  144. }
  145.  
  146. /**
  147.  * Helper function for preprocessing an Omega layout in Panels.
  148.  */
  149. function _omega_panels_layout_preprocess(&$variables) {
  150.   $layouts = omega_layouts_info();
  151.   $variables['omega_layout'] = $layouts[$variables['layout']['omega_layout']];
  152.  
  153.   // Place a reference to the 'content' array so that the variables are
  154.   // accessible through both, the default page.tpl.php accessor and the panels
  155.   // layout accessor.
  156.   $variables['page'] = &$variables['content'];
  157.  
  158.   // Provide some harmless default variables that would otherwise be added by
  159.   // template_preprocess_page().
  160.   $variables['base_path'] = base_path();
  161.   $variables['front_page'] = url();
  162.   $variables['language'] = $GLOBALS['language'];
  163.   $variables['language']->dir = $GLOBALS['language']->direction ? 'rtl' : 'ltr';
  164.  
  165.   // In order to not render any of these items we simply set them as an empty
  166.   // string.
  167.   $variables['feed_icons'] = '';
  168.   $variables['logo'] = '';
  169.   $variables['main_menu'] = '';
  170.   $variables['secondary_menu'] = '';
  171.   $variables['action_links'] = '';
  172.   $variables['site_name'] = '';
  173.   $variables['site_slogan'] = '';
  174.   $variables['tabs'] = '';
  175.   $variables['breadcrumb'] = '';
  176.   $variables['title'] = '';
  177.   $variables['messages'] = '';
  178. }
  179.  
  180. /**
  181.  * Computes the relative path between two absolute paths.
  182.  *
  183.  * @param string $from
  184.  *   An absolute path.
  185.  * @param string $to
  186.  *   An absolute path.
  187.  *
  188.  * @return string
  189.  *   The relative path between $from and $to.
  190.  */
  191. function _omega_relative_path($from, $to) {
  192.   // Some compatibility fixes for Windows paths.
  193.   $from = is_dir($from) ? rtrim($from, '\/') . '/' : $from;
  194.   $to = is_dir($to) ? rtrim($to, '\/') . '/' : $to;
  195.   $from = str_replace('\\', '/', $from);
  196.   $to = str_replace('\\', '/', $to);
  197.   $from = explode('/', $from);
  198.   $to = explode('/', $to);
  199.   $relative = $to;
  200.  
  201.   foreach ($from as $depth => $dir) {
  202.     // Find first non-matching directory.
  203.     if ($dir === $to[$depth]) {
  204.       // Ignore this directory.
  205.       array_shift($relative);
  206.     }
  207.     else {
  208.       // Get number of remaining dirs to $from.
  209.       $remaining = count($from) - $depth;
  210.       if ($remaining > 1) {
  211.         // Add traversals up to first matching directory.
  212.         $padding = (count($relative) + $remaining - 1) * -1;
  213.         $relative = array_pad($relative, $padding, '..');
  214.         break;
  215.       }
  216.       else {
  217.         $padding[0] = './' . $relative[0];
  218.       }
  219.     }
  220.   }
  221.  
  222.   return implode('/', $relative);
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement