4

I'm trying to simplify the admin UX on a site with a few different custom style plugins. I have plugins A, B, C, and D.

I want plugins A and B only to be able to be applied to menu block panes.

I want plugin C only to be able to be applied to a minipanel when the minipanel is placed via something like Panelizer.

I see that I can easily hook_form_alter() the #options in the form but I don't seem to be able to find the kind of context information that I need to know which ones to hide.

How can I selectively hide style plugin options for irrelevant elements?

3

I figured out two ways to do this.

First way

It requires that any style plugins you want to filter be

  1. provided by the module in which you're doing the filtering (unless you want to hand-code in a list of modules or something), and
  2. named such that the machine name includes the name(s) of the content type(s) of the panes on which it should be able to be selected.

Inside a hook_form_FORM_ID_alter():

// Only show style plugins relevant to the type of pane we have.
$type = $form_state['pane']->type;
$this_module = basename(__FILE__, '.module');

foreach ($form['style']['#options'] as $key => $style) {
  $style_info = panels_get_style($key);
  $module = $style_info['module'];
  // If the style plugin was defined by our module and it does not have the
  // current pane's type in the name, don't make it an option.
  if (($module == $this_module) && (strpos($key, $type) === FALSE)) {
    unset($form['style']['#options'][$key]);
  }
}

Second Way

After doing it the first way, I wanted to make it more robust (not rely on naming conventions) so I submitted a feature request to Panels and attached some patches. Issue here: https://www.drupal.org/node/2320045

This way allows you to just add something like 'content type' => array('foo', 'bar', 'baz'), (Where foo, bar, and baz are the machine names of Panels content types) to the $plugin declaration, along with the title and description and all that. If you have plugins that don't have that, they won't break, they'll just continue to appear as options for every content type. If you do specify some types, it will only appear as an option on panels of those types.

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.