I've just written a plugin to allow commerce_order entities to be panelized (the plugin file and the class are included below).

When I go to edit the contexts the page loads but I see these errors:

Notice: Undefined property: stdClass::$contexts in ctools_context_add_context_form() (line 145 of sites/all/modules/contrib/ctools/includes/context-admin.inc).
Notice: Undefined property: stdClass::$relationships in ctools_context_add_relationship_form() (line 205 of sites/all/modules/contrib/ctools/includes/context-admin.inc).

When I go to the edit layout and edit content pages I am presented with a MENU_NOT_FOUND:
The requested page "/admin/config/content/panelizer/commerce_order/commerce_booking.email_html/layout" could not be found.

Here are the plugin an class:
commerce_order.inc:

?php
/**
 * @file
 * Definition of the commerce_order plugin.
 */

$plugin = array(
  'handler' => 'PanelizerEntityCommerceOrder',
);

PanelizerEntityCommerceOrder.class.inc:

/**
 * @file
 * Class for the Panelizer commerce_order entity plugin.
 */
 
/**
 * Panelizer Entity commerce_order plugin class.
 *
 * Handles commerce_order specific functionality for Panelizer.
 */
class PanelizerEntityCommerceOrder extends PanelizerEntityDefault {
  /**
   * True if the entity supports revisions.
   */
  public $supports_revisions = TRUE; // Can be sniffed
  public $entity_admin_root = 'admin/commerce/config/order-types/manage/%commerce_order_type'; // Can be sniffed
  public $entity_admin_bundle = 5; // Can be sniffed.
  public $views_table = 'node';
  public $uses_page_manage = FALSE;
  
  /**
   * Determine if the entity allows revisions.
   */
  public function entity_allows_revisions($entity) {
    $retval[0] = $this->supports_revisions;
    $retval[1] = user_access('administer commerce_order entities');

    return $retval;
  }
  
  function get_default_display($bundle, $view_mode) {
    $display = parent::get_default_display($bundle, $view_mode);
    // Add the node title to the display since we can't get that automatically.
    $display->title = '%commerce_order:label';
    
    return $display;
  }
  
  /**
   * Implements a delegated hook_form_alter.
   *
   * We want to add Panelizer settings for the bundle to the commerce_order_type form.
   */
  public function hook_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == 'commerce_order_type_form') {
      if (isset($form['type'])) {
        $bundle = $form['type']['#default_value'];
        $this->add_bundle_setting_form($form, $form_state, $bundle, array('type'));
      }
    }
  }
  
  /**
   * Implements entity_access();
   */
  public function entity_access($op, $entity) {
    return entity_access($op, 'commerce_order', $entity);
  }
  
  /**
   * Implements entity_save();
   */
  public function entity_save($entity) {
    return entity_save('commerce_order', $entity);
  }
}
  

Comments

rlmumford’s picture

Category: support » bug

It looks like the problem is in panelizer_default_layout_page around line 375:

  $panelizer = $handler->get_default_panelizer_object($bundle, $name);
  if (empty($panelizer)) {
    return MENU_NOT_FOUND;
  }

If there are no settings for the $panelizer it return MENU_NOT_FOUND. I suppose this means that you have to implement a default_panelizer_defaults hook whenever you expose an entity to be panelized. Surely there must be a way to initialize an Panelizer object with the minimum settings possible rather than not letting the site-builder configure there panels at all.

I notice that for the context page, the $panelizer is got using a different method:

$panelizer = panelizer_context_cache_get('default', $cache_key);
if (empty($panelizer)) {
  return MENU_NOT_FOUND;
}

I'm going to keep looking and see if there is a way to return a sensible default that applies to all entities. I'm also going to change this to a bug.

rlmumford’s picture

The problem is fixed by including 'panelizer_defaults' => TRUE, as one of the hooks in the hooks array of the plugin. Thus the commerce_order plugin is:

$plugin = array(
  'handler' => 'PanelizerEntityCommerceOrder',
  'uses page manager' => FALSE,
  'hooks' => array(
    'menu' => TRUE,
    'admin_paths' => TRUE,
    'permission' => TRUE,
    'panelizer_defaults' => TRUE,
    'form_alter' => TRUE,
    'views_data_alter' => TRUE,
  ),
);

Can we have some better documentation about the importance of these hooks? Could we also add them to the default plugin (especially as the panelizer_defaults hook implementation is provided by the default class!)

merlinofchaos’s picture

Title: Cannot edit the Layout or Content of a Panelized entity. » Write better documentation about the hook settings in the entity .inc file
Category: bug » task

Ahh, my assumption was that most people would copy one of the existing entities rather than the default. Having that documented in the default plugin makes sense, though. Changing this to a task.

merlinofchaos’s picture

Issue summary: View changes

Added code example

damienmckenna’s picture

Component: Code » Documentation
Issue summary: View changes