Panels 2: Creating contexts and relationships that are node type aware

Last updated on
30 April 2025

You are browsing documentation for an older version of Drupal, which is not supported any longer, and the information may not be correct. See current documentation for Contributed modules

Contexts

When creating a new pane for panels 2, you might add a content type like:

  $types['foo'] = array(
    'title' => t('Foo'),
    'icon' => 'icon_node.png',
    'path' => panels_get_path('content_types/node'),
    'category' => array(t('FooP'), -9),
    'required context' => new panels_required_context(t('Node'), 'node'),
  );

This new pane will only be available if a node context exists. What if you only want it to be available to specify node types, because you require specific CCK fields? You can actually require a context of a specific node type.

  $types['foo'] = array(
    'title' => t('Foo'),
    'icon' => 'icon_node.png',
    'path' => panels_get_path('content_types/node'),
    'category' => array(t('FooP'), -9),
    'required context' => new panels_required_context(t('Node'), 'node-page'),
  );

Now the pane will only be available when a context to a 'page' node exists.

Relationships

When creating a relationship between argument contexts and other contexts, it's also possible to specify node types. You may normally write a relationship like:

function my_module_panels_relationships() {
  $args['foo_from_node'] = array(
    'title' => t("Foo from Node"),
    'keyword' => 'foo',
    'description' => t('Adds a Foo from a Node context.'),
    'required context' => new panels_required_context(t('Node'), 'node'),
    'context' => 'my_module_foo_from_node_context',
  );
  return $args;
}

function my_module_foo_from_node_context($context = NULL, $conf) {
  // If unset it wants a generic, unfilled context, which is just NULL
  if (empty($context->data)) {
    return panels_context_create_empty('node', NULL);
  }

  if ($node = get_some_other_node($context->data)) {
    return panels_context_create('node', $node);
  }
}

This relationship will require a node context before adding a 'foo' context. However, it's simple to add a node type specific requirement:

function my_module_panels_relationships() {
  $args['foo_from_page'] = array(
    'title' => t("Foo from Page"),
    'keyword' => 'foo',
    'description' => t('Adds a Foo from a Page context.'),
    'required context' => new panels_required_context(t('Page'), 'node-page'),
    'context' => 'my_module_foo_from_page_context',
  );
  return $args;
}

function my_module_foo_from_page_context($context = NULL, $conf) {
  // If unset it wants a generic, unfilled context, which is just NULL
  if (empty($context->data)) {
    return panels_context_create_empty('node', array('types' => array('page')));
  }

  if ($node = get_some_other_node($context->data)) {
    return panels_context_create('node', $node);
  }
}

Now the relationship will required a 'page' node context before allowing the 'foo' context to be added.

Help improve this page

Page status: Not set

You can: