Add pane to panel display database
2014 11 Oct

Programmatically add pane to panel display in database

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);
}

Following 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 further help - https://www.drupal.org/project/panels.

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!

Latest Blogs

Website Web Design tips

10 Effective Website Web Design Tips: How to Design a Website That Works

A website is an important identity of any brand in today’s digital-first era. Users will first visit the website to gather information about any brand or company.

Read More

Website Revamp Process

When to plan for your website revamp and how?

The only constant in the world is "CHANGE". Though this line has become a cliche, it is very important to implement the same in our businesses.

Read More

Wireframing Tools

7 Awesome Wireframing Tools You Should Be Using

In the last decade, the importance of UI/ UX designing has increased significantly in the segment of websites, apps and mobile games mockup design and development.

Read More

CMS to use for website

Are you getting confused about which CMS to choose for your new website?

A very happy new year 2020! I am sure the celebration was fun. And I am also sure that the targets, the meetings, the daily chores are already haunting you.

Read More