verbb / comments

A Craft CMS plugin for managing comments directly within the CMS.
Other
137 stars 35 forks source link

CP Error for Shopify Product Elements #296

Closed caseyalee closed 7 months ago

caseyalee commented 7 months ago

Describe the bug

When comments are enabled for a Shopify product entry via {{ craft.comments.render(product.id) }}, the (CP) error return is:

[yii\base\UnknownPropertyException] yii\base\UnknownPropertyException: Getting unknown property: verbb\comments\elements\Comment::productView in /var/www/html/vendor/yiisoft/yii2/base/Component.php:154

Adding the switch/case line below to /src/elements/Comments.php appears to resolve the issue but I'm not sure if productView is specific to my case or not.

case 'productView':
  {
  return Html::encode($this->getComment());
  }

Steps to reproduce

  1. Enable comments for Shopify Products
  2. Post a comment
  3. Attempt to view comment in CP.

Craft CMS version

4.7.2

Plugin version

2.0.10

Multi-site?

No

Additional context

No response

engram-design commented 7 months ago

I'm not sure where productView as an attribute is coming from. Is that something custom on your end, like a custom field, or even something in a module? I can't seem to find a reference to productView in the Shopify plugin.

We probably shouldn't add a new attribute case to the tableAttributeHtml(), but I'm also not sure how the productView attribute has been attached to a comment. Any ideas?

caseyalee commented 7 months ago

@engram-design Thanks for taking a look. I was able to narrow this down to the "View" column in the Comments CP table. As long as this column is unchecked the error does not occur.

Could it be a future possibility to allow developers the chance to hook into the default switch case for non-standard element types?

engram-design commented 7 months ago

So that "View" column doesn't come from the Comments plugin, or the Shopify plugin. Any other plugins that might be adding this? Maybe you can search for "productView" in your "vendor" folder (it might take a bit).

But, yes! You can totally hook into the table index to add your own handling for things.

use yii\base\Event;
use verbb\comments\elements\Comment;
use craft\events\SetElementTableAttributeHtmlEvent;

Event::on(Comment::class, Comment::EVENT_SET_TABLE_ATTRIBUTE_HTML, function(SetElementTableAttributeHtmlEvent $event) {
    if ($event->attribute === 'productView') {
        $event->html = 'some text';
    }
});
caseyalee commented 7 months ago

@engram-design Thanks so much!