Add pane to panel display database
2014 11 Oct

Programmatically add pane to panel display in database

By Somedutta Ghosh

Following is an example code that shows how to add a custom content pane to a panel display. NOTE: this code is for adding pane to a panel display which exists in the database and not in a feature file.

Create a  new custom content pane using the function panels_new_pane($type, $subtype, $set_defaults = FALSE).

$new_pane = panels_new_pane('custom','custom',TRUE);

Once the new pane is successfully created set its content and visibility rule.

if ($new_pane) {
  // Set up content for new custom content pane
  $new_pane->configuration['admin_title'] = t(‘This is an example pane’);
  $new_pane->configuration['body'] = ‘<p>’ . t(‘Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.’) . ‘</p>’;
}

 

As an example I am setting the ‘String URL’ visibility rule for this pane. So I'll be showing my new pane only on the path 'example-page'.

ctools_include('context');
$plugin = ctools_get_access_plugin('path_visibility');
$plugin = ctools_access_new_test($plugin);
$plugin['settings']['paths'] = ‘example-page’;
$new_pane->access['plugins'][] = $plugin;

 

Now we must load the panel display to add the new pane. As an example I am adding the new pane to a region ‘sidebar_block’.

For this we need the did value for the panel display. For finding out the did, you have to work a little harder :)

#1 First find out the machine name of your panel display. You can figure that out on the panel display edit page itself by following the url - admin/structure/pages/nojs/operation/site_template/handlers/site_template_panel_context_N/content. ‘site_template_panel_context_N’ is the machine name for your panel display where N will be an integer.

#2 Next look in the table ‘page_manager_handlers’, inside the column ‘conf’. Fetch that column from the database for the row corresponding to your panel display, ie. the name field should match your panel display machine name. Unserialize the conf data and the variable ‘did’ inside the array will be the did value you are looking for.

You could user the following code to figure out your did. 

$query = db_select('page_manager_handlers', 'pmh')
->fields('pmh', array('conf'))
->condition('pmh.name', 'site_template_panel_context_N');
$result = $query->execute()->fetchColumn();
$conf = unserialize($result);
print $conf['did'];

Once you have your did, you then have to load the panel display, add your new pane and save it. Thats it!

$did = 1; // considering did = 1 as an example here
$display = panels_load_display($did);
if($display) {
  $display->add_pane($new_pane, 'sidebar_blocks');
  panels_save_display($display);
}

So here is the entire code

// Create new pane and configure content and visibility settings
$new_pane = panels_new_pane('custom','custom',TRUE);
if ($new_pane) {
  // Set up content for new custom content pane
  $new_pane->configuration['admin_title'] = t(‘This is an example pane’);
  $new_pane->configuration['body'] = ‘<p>’ . t(‘Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.’) . ‘</p>’;

  // Set up path visibility rule for new custom content pane
  ctools_include('context');
  $plugin = ctools_get_access_plugin('path_visibility');
  $plugin = ctools_access_new_test($plugin);
  $plugin['settings']['paths'] = ‘example-page’;
  $new_pane->access['plugins'][] = $plugin;

  $query = db_select('page_manager_handlers', 'pmh')
           ->fields('pmh', array('conf'))
           ->condition('pmh.name', 'site_template_panel_context_N');
  $result = $query->execute()->fetchColumn();
  $conf = unserialize($result);

  // Load the panel display to add the new pane.
  $did = $conf['did'];
  if (is_numeric($did)) {
    $display = panels_load_display($did);
    if($display) {
      $display->add_pane($new_pane, 'sidebar_blocks');
      panels_save_display($display);
    }
  }
  else {
    drupal_set_message(t('Incorrect reference to panels display'), 'warning', FALSE);
    watchdog('hcl_campaign', 'did passed to panels_load_display is not int - @did.', array('@did' => $did));
  }
}
else {
  drupal_set_message(t('Failed to create new pane.'), 'warning', FALSE);
}

Follwing the above code you could add existing content line blocks, views, page elements, mini panels, etc. Then you could set different settings for the pane like visibility settings, style settings, css settings etc. You could manually add panes using the panel's UI and play around with their settings and then load the panel display object and look at the settings for those panes to see how to implement the same programmatically. You should surely use the Devel module - https://www.drupal.org/project/devel and look around in the panels API for furthur help - http://drupalcontrib.org/api/drupal/contributions!panels!panels.module/g....

When I had to implement this, I spent almost an entire day searching through google with as many keywords I could think of but not a single article that I found could help me with this task. Finally, I went through the panel’s API and within a few hours I had my job done! So here’s hoping this saves somebody at least sometime and gives them some direction :)

Please let me know in the comments if you have any queries :) And all the best with this!

Featured blog

web-personalization

Personalized Content is a Proven Entity !!

Irrespective of how big a business icon or brand you are, increasing the relevance of your website will always be critical to your success.

Read More

Git Hooks

Git hooks for better codes

We are programmers and we are always on the lookout for ways to improve our code. A good and structured way of coding defines the completeness of a programmer.

Read More

Drupal ,varnish cache

Hard time with Drupal, Varnish Cache and Cookies

Using a reverse proxy server in front of a web server is usually needed for every big site and it is a very good thing to do so as reverse proxy server will handle all the anonymous traff

Read More

Say no to captcha

Say no to captcha - Various Spam Protection Methods

Maintaining high traffic websites have their own merits and demerits, the most annoying thing about them is SPAM.

Read More