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

Blockchain Integration Into Public Digital Good

The Role of Blockchain in Digital Public Goods: Use Cases and Innovations

Digital public goods, such as open-source software, IT models, and standards, are the backbone of our digital infrastructure.

Read More

Role of Open Source and Digital Public Goods

Boost DPG Development: How Open Source Maximizes Efficiency

The emergence of open-source workflow solutions has revolutionized workflow management.

Read More

Digital Public Goods Alliance Strategy 2021–2026

Boosting Digital Infrastructure with Digital Public Goods

The United Nations (UN) defines a roadmap for Digital Public Goods (DPGs) as open-source software, open data, open AI models, open standards, and open content.

Read More

Best Practices for Software Testing

Power of Software Testing: Why Software Teams Use It Effectively

In the modern digital era, where software is used in nearly every aspect of everyday life, the importance of software testing cannot be emphasized.

Read More