Hi,

i was trying to replace every comment with a minipanel.

Therefore i created a module, which should integrate a comment as context, and the cid as argument.
But according to my little experience with drupal, it doesn't seem to be such an easy task.

I managed to get the "comment" context shown in the list for context. But that's all.

The plan was, to create a context and a content type for panels, create a mini panel, and overwrite phptemplate_comment() with the output of a mini panel.

But i've got the following issues:
- I can't get the comment_content content-type working
- Therefore i can't test, if the rest is working as it should ;)
- Output the mini panel from template.php

here is the code for the module so far:

<?php
function panels_comments_comment($a1, $op){
	switch($op) {
		case "view":

	}
}

function panels_comments_panels_contexts() {
  $args['comment'] = array(
    'title' => t("Comment"),
    'description' => t('A comment object.'),
    'context' => 'panels_comments_context_create_comment',
    'settings form' => 'panels_comments_context_settings_form',
    'settings form validate' => 'panels_comments_context_settings_form_validate',
    'keyword' => 'comment',
    'context name' => 'comment',
  );
  return $args;
}

function panels_comments_context_create_comment($empty, $data = NULL, $conf = FALSE) {
  $context = new panels_context('comment');
	$context->plugin = 'comment';
	if($empty) {
		return $context;
	}

  if($conf) {
    $data = comment_load($data['cid']);
  }
  if (!empty($data)) {
    $context->data = $data;
    $context->title = $data->subject;
    $context->argument = $data->cid;
    return $context;
  }
}

function panels_comments_comment_panels_arguments() {
  $args['comment'] = array(
    'title' => t('Comment ID'),
    'keyword' => 'comment',
    'description' => t('Restrict the argument to a comment id'),
    'context' => 'panels_comments_comment_contenxt',
    'settings form' => 'panels_comments_comment_settings_form',
    'settings form submit' => 'panels_comments_comment_settings_form_submit',
    'displays' => 'panels_comments_comment_displays',
    'choose display' => 'panels_comments_choose_display',
  );
  return $args;
}

function panels_comments_comment_context($arg = NULL, $conf = NULL, $empty = FALSE) {
  if ($empty) {
  	return panels_context_create_empty('comment');
  }

  if (!is_numeric($arg)) {
    return FALSE;
  }

  $comment = comment_load($arg);
  if (!$comment) {
  	return FALSE;
  }

  return panels_context_create('comment', $comment);
}

/**
 * Settings form for the argument
 */
function panels_comments_comment_settings_form($conf) {
  $options = array();
  foreach (node_get_types() as $type => $info) {
    $options[$type] = $info->name;
  }

  $form['types'] = array(
    '#title' => t('Node types'),
    '#type' => 'checkboxes',
    '#options' => $options,
    '#default_value' => $conf['types'],
    '#prefix' => '<div class="clear-block">',
    '#suffix' => '</div>',
  );

  $form['own_default'] = array(
    '#title' => t('Use different default display'),
    '#type' => 'checkbox',
    '#description' => t('If checked, when this argument is present it will use its own display rather than the default. Displays not selected in the "Own display" field will use this one.'),
    '#default_value' => $conf['own_default'],
  );

  $form['displays'] = array(
    '#title' => t('Own display'),
    '#type' => 'checkboxes',
    '#options' => $options,
    '#default_value' => $conf['displays'],
    '#description' => t('Each checked type will get its own special display to layout its content. Only types set in Node types, above, should be set here. Types not set here will use the default display.'),
    '#prefix' => '<div class="clear-block">',
    '#suffix' => '</div>',
  );

  return $form;
}

/**
 * There appears to be a bit of a bug with the way we're handling forms; it causes
 * 'checkboxes' to get invalid values added to them when empty. This takes care
 * of that.
 */
function panels_comments_comment_settings_form_submit(&$values) {
  $types = node_get_types();
  if (!empty($values['types'])) {
    foreach ($values['types'] as $type => $value) {
      if (empty($types[$type])) {
        unset($values['types'][$type]);
      }
    }
  }
  if (!empty($values['displays'])) {
    foreach ($values['displays'] as $type => $value) {
      if (empty($types[$type])) {
        unset($values['displays'][$type]);
      }
    }
  }
}

/**
 * What additional displays does this argument provide?
 */
function panels_comments_comment_displays($conf, $id) {
  $displays = array();
  if (!empty($conf['own_default'])) {
    $displays['default'] = array(
      'title' => t('comment ID @id Default', array('@id' => $id)),
      'context' => 'comment',
    );
  }

  if (is_array($conf['displays'])) {
    $options = array();
    foreach (node_get_types() as $type => $info) {
      $options[$type] = $info->name;
    }
    foreach (array_keys(array_filter($conf['displays'])) as $type) {
      $displays[$type] = array(
        'title' => t('Comment ID @id @type', array('@id' => $id, '@type' => $options[$type])),
        // Tell it to base the template for this display off of the default.
        'default' => 'default',
        'context' => 'comment',
      );
    }
  }
  return $displays;
}

/**
 * Based upon the settings and the context, choose which display to use.
 */
function panels_comments_comment_choose_display($conf, $context) {
  if (empty($context->data)) {
    return;
  }

  if (!empty($conf['displays'][$context->data->type])) {
    return $context->data->type;
  }

  // Please note that 'default' is a special display.
  if (!empty($conf['own_default'])) {
    return 'default';
  }

  // Empty return says to use the default display.
  return;
}

function panels_comments_comment_content_panels_content_types() {
  $items['comment_content'] = array(
    'title' => t('Comment content'),
    'weight' => -10,
    'single' => TRUE, // only provides a single content type
    'content_types' => 'panels_comments_content_types_comment_content',
    'render callback' => 'panels_comments_content_comment_content',
    'add callback' => 'panels_comments_admin_edit_comment_content',
    'edit callback' => 'panels_comments_admin_edit_comment_content',
    'title callback' => 'panels_comments_admin_title_comment_content',
  );
  return $items;
}

function panels_comments_admin_title_comment_comments($conf, $context) {
  return t('"@s" comment', array('@s' => $context->identifier));
}

function panels_comments_content_types_comment_content() {
  return array(
    'comment_content' => array(
      'title' => t('Comment Content'),
      'icon' => 'icon_node.png',
      'path' => panels_get_path('content_types/node'),
      'description' => t('A comment.'),
      'required context' => new panels_required_context(t('Comment'), 'comment'),
      'category' => array(t('Comment context'), -9),
    ),
  );
}

function panels_comments_content_comment_content($conf, $panel_args, $context) {
  $comment = isset($context->data) ? drupal_clone($context->data) : NULL;
  $block->module = 'comments';
  $block->delta = $comment->nid;

  $block->subject = t('Comments');
  if (empty($comment)) {
    $block->content = t('Comment comments go here.');
  }
  else {
    $block->content = panels_comments_comment_render($comment, $conf);
    // Update the history table, stating that this user viewed this node.
    comment_tag_new($comment->cid);
  }

  return $block;
}

function panels_comments_admin_edit_comment_content($id, $parents, $conf = array()) {
  $form[] = array();
  return $form;
}

function panels_comments_comment_render($comment, $conf) {
  return "COMMENT CONTENT";
}

/* HELPER FUNCTIONS */

function comment_load($cid) {
  return db_fetch_object(db_query('SELECT * FROM {comments} WHERE cid = %d', $cid));
}
?>

I don't know what's wrong and where to go on. But maybe anybody has an idea or could use my code as a basic (in fact, it is just copied, pasted and edited a little bit from other content types and contexts)

Comments

1kenthomas’s picture

Subscribe.

sun’s picture

Status: Active » Closed (won't fix)

I'm pretty sure that no one will ever be able to provide support on this issue.