5

I am creating my own Panels Style Plugins.

The styles are applied to a Panel Pane which contains a field.

From the Style, I can add a wrapper DIV to modify the display of the field.

However, I would like to be able to modify the actual class on the field itself (this would negate the need for a wrapper DIV).

In my field.tpl.php file I have placed a PHP variable for the class, but I'm not sure how I can access and modify this variable from outside the field.tpl.php file. Could some one give me some pointers.

Thanks!

5
+50

Check out Semantic Panels. It lets you choose HTML element and classes for wrapper, the field and the label. The module provides the same options that are available to customize the field output in Views.

Here's a screenshot from the user interface when you customize the field.

Semantic Panels - settings

6
  • Thanks! Can I use that to manipulate the class from my Panel style (e.g. the class should only be added if the panel style is active). Thanks for any help you can offer! – big_smile Dec 17 '12 at 14:42
  • Semantic Panels is a style on it's own so yes, it will only add your settings when active. – Pontus Nilsson Dec 18 '12 at 7:24
  • it doesn't seem to let you modify the classes applied to fields contained within the Pane. Is that possible, or am I doing something wrong? Thanks. – big_smile Dec 18 '12 at 12:57
  • Did you choose Semantic panels as style? What settings did you change? Content, wrapper? – Pontus Nilsson Dec 19 '12 at 12:31
  • It seems that module only adds a wrapper with the class to the field. It doesn't add it to the actual field. e.g. <a class="field" href="#"></a> would become <div class="mynewclass"><a class="field" href="#"></a></div> instead of <a class="field mynewclass" href="#"></a> – big_smile Dec 20 '12 at 8:53
0

You should be able to it via hook_preprocess_HOOK . Try to dpm($vars), classes should be one of them, you will then be able to change it.

7
  • Thanks for your help, but I'm not quite sure how to use this. I tried panels_preprocess_HOOK & panels_preprocess_field in my Panel Style, but this didn't do anything. I then tried panels_preprocess, which displays an array, which has a theme_hook_suggestion field of panels_pane__entity_field and panels_pane__entity_field__node:field_call_to_action. However, neither of these work. Also, once I have made a class for my field, how do I get it to display in field.tpl.php (the class should only appear when the panels style is in use). – big_smile Dec 10 '12 at 18:49
  • are you doing the style in a module ? – mojzis Dec 10 '12 at 19:15
  • It's a panels plugin which I have put in my theme (as apparently panels plugins go in the theme folder). Thanks for any help you can offer! – big_smile Dec 10 '12 at 19:28
  • then the hook name should start with your theme name : YOUR_THEME_preprocess_field – mojzis Dec 10 '12 at 19:44
  • Thanks for your help! That works, but it effects all fields. I only want to target one field (called call to action). When I use YOUR_THEME_preprocess_field it gives the following theme_hook_suggestions: field__field_call_to_action , field__field_call_to_action__content. I have tried prefixing each of these with THEMENAME_preprocess_field , THEMENAME_preprocess_ and THEMENAME_field but it seems to have no effect. Thanks for any guidance you can offer, I really appreciate your help! – big_smile Dec 10 '12 at 20:41
0

You should be able to use preprocess_panels_pane(). I use it to remove the default classes from Panels.

function THEME_NAME_preprocess_panels_pane($vars) {
  $content = $vars['output'];
  // basic classes
  $vars['classes'] = '';
  $vars['id'] = '';

  ctools_include('cleanstring');

  // Add id and custom class if sent in.
  if (!empty($content->content)) {
    if (!empty($content->css_id)) {
      $vars['id'] = ' id="' . $content->css_id . '"';
    }
    if (!empty($content->css_class)) {
      $vars['classes'] .= $content->css_class;
    }
  }

  // administrative links, only if there is permission.
  $vars['admin_links'] = '';
  if (user_access('view pane admin links') && !empty($content->admin_links)) {
    $vars['admin_links'] = theme('links', $content->admin_links);
  }

  $vars['title'] = !empty($content->title) ? $content->title : '';

  $vars['feeds'] = !empty($content->feeds) ? implode(' ', $content->feeds) : '';
  $vars['content'] = !empty($content->content) ? $content->content : '';  

  $vars['links'] = !empty($content->links) ? theme('links', $content->links) : '';
  $vars['more'] = '';
  if (!empty($content->more)) {
    if (empty($content->more['title'])) {
      $content->more['title'] = t('more');
    }
    $vars['more'] = l($content->more['title'], $content->more['href'], $content->more);
  }
}
3
  • Thank you so much for your help! How do I make it work so only my Panel style creates the variable that can then be passed into my field.tpl.php file. If I use THEME_NAME_preprocess_panels_pane($vars), it will make all Panel styles effect the output and not just my current style. Thanks for any help you can offer. – big_smile Dec 14 '12 at 18:26
  • It seems I misunderstood exactly what you were trying to do... This might be an odd question but are you using theme_PLUGINNAME_STYLENAME_style_render_pane()? Can you not access it from there? – Karl Jóhann Dec 14 '12 at 21:24
  • No, the only one that seems to work is panels_preprocess(). – big_smile Dec 15 '12 at 15:38
0

Seeing as it's just a field that you're trying to alter, have you tried the Field Formatter Class module?

This module allows site administrators to add classes to the outer HTML wrapper for any field display.

Classes can be specified at the Manage display settings for content types, users and other entities.

On a side note, the Fences module doesn't add classes to your fields, but it is WELL worth knowing about if you want to reduce the markup/div soup that Drupal adds.

1
  • This module seems to change the class on the field, but it effects all fields and not just the one that has my panel style. – big_smile Dec 20 '12 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.