I'm trying to add a body class on a panel page indicating the layout being used. However, looking at the html preprocess function, I see nothing in the entire array that indicates that the page is generated Panels (other than rendered HTML).

Am I missing something, or is there some function or code that I could use in the HTML preprocess to help me determine that?

Thanks,
Rene

Comments

merlinofchaos’s picture

Status: Active » Fixed

If it's a page manager page, page_manager_get_current_page() can help with that determination. (This'll work with Panelizer since it uses Page Manager for rendering).

Note that the latest Panels actually has a field that lets you directly add and remove body classes on the settings tab.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

lmeurs’s picture

Thanks for the tip, this seemed to help me:

/**
 * Implements hook_preprocess_html().
 */
function MYTHEME_preprocess_html(&$variables) {
  // Add class name to body tag about whether the page manager is used or not.
  $page_manager_current_page = page_manager_get_current_page();
  $variables['classes_array'][] = (empty($page_manager_current_page) ? 'no-' : '') . 'page-manager-page';
}
BillyTom’s picture

Category: Support request » Feature request
Issue summary: View changes
Status: Closed (fixed) » Needs review

@lmeurs This is a very nice tip.

Should something basic like adding a body class not be in the module by default? If you are doing your page layout with panels you might need this information. And if you don't then it is not obtrusive in any way.

damienmckenna’s picture

Version: 7.x-3.2 » 7.x-3.x-dev
Priority: Minor » Normal
Status: Needs review » Fixed

Panels already provides a way of doing this, on the main Settings page there's a field called "Add body CSS classes".

BillyTom’s picture

Status: Fixed » Needs review

This is not exactly what the OP suggested. What he suggested was that Panels automatically adds the machine name of the used layout to the body.

You can do this manually by adding your own class with "Add body CSS classes", but this can be tedious if you have to manage lots of panels.

Also, it is a potential source of errors if you change the layout of a panel but forget to update the class name in "Add body CSS classes".

So I would say it is a different feature.

Also, Drupal core already provides lots of body classes for easier styling. Maybe Panels should follow in the same footsteps.

damienmckenna’s picture

Title: Add body class on panel pages » Automatically add Panels layout / variant / display name as body class
Component: Documentation » User interface

Clarifying the title.

Not all sites will want this, so maybe it should be optional?

mglaman’s picture

Status: Needs review » Active

No patch - moving status from CNR. IMO this should be a "won't fix." There are plenty of ways to add this in a theme and not drop more classes into the markup than needed.

If you want to add classes to the body, then you could follow the example in #3.

<?php
/**
 * Implements hook_preprocess_html().
 */
function MYTHEME_preprocess_html(&$variables) {
  $page = page_manager_get_current_page();
  if($page) {
    $layout_name = $page['handler']->conf['display']->layout;
    $variables['classes_array'][] = 'panels-layout-' . $layout_name;
  }
}
?>

IF you want this to work with Panelizer, you need to do this (Panelizer doesn't properly register with page_manager_get_current_page()). I've been meaning to flush it out, but it fixed our problem.

<?php
function ARGG_panelizer_pre_render_alter(&$panelizer, &$display, $entity) {
  // Panelizer hijacks Page Manager and does not ping its context task. This is
  // how Kalatheme and other modules check for Panels (unless baked in, like
  // Panels Breadcrumbs.)
  //

  // Is it even needed to define all of this, or simply passing an empty array
  // good enough? Who knows. This is more fun, right?
  page_manager_get_current_page(array(
    'name' => 'node-view',
    'task' => array('node_view'),
    'subtask' => array(),
    'contexts' => $display->contexts,
    'arguments' => $display->args,
    'handler' => panelizer_get_entity_plugin($panelizer->panelizer_type),
  ));
}
?>

In ref to #2280191: Panelizer does not register with page_manager_get_current_page().

BillyTom’s picture

@mglaman: In my experience more body classes are always a good thing.

I can't see a downside to adding this function to the module.

If additional classes are properly prefixed (panels-layout-x) then there should not be a conflict with other modules either.

osopolar’s picture

Not actually what requested by this issue, but in some cases may helpful: #2502121: Allow panel panes to add body classes.

rajdcube’s picture

I am getting issue with the flow of product variation display as a single product in drupal 8.
Can anyone help me what will be the steps to follow to work on this?