Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ReviewRenderer.php
Go to the documentation of this file.
1 <?php
9 
12 
14 {
20  protected $_availableTemplates = [
21  self::FULL_VIEW => 'Magento_Review::helper/summary.phtml',
22  self::SHORT_VIEW => 'Magento_Review::helper/summary_short.phtml',
23  ];
24 
30  protected $_reviewFactory;
31 
37  public function __construct(
38  \Magento\Framework\View\Element\Template\Context $context,
39  \Magento\Review\Model\ReviewFactory $reviewFactory,
40  array $data = []
41  ) {
42  $this->_reviewFactory = $reviewFactory;
43  parent::__construct($context, $data);
44  }
45 
55  public function getReviewsSummaryHtml(
56  \Magento\Catalog\Model\Product $product,
57  $templateType = self::DEFAULT_VIEW,
58  $displayIfNoReviews = false
59  ) {
60  if (!$product->getRatingSummary()) {
61  $this->_reviewFactory->create()->getEntitySummary($product, $this->_storeManager->getStore()->getId());
62  }
63 
64  if (!$product->getRatingSummary() && !$displayIfNoReviews) {
65  return '';
66  }
67  // pick template among available
68  if (empty($this->_availableTemplates[$templateType])) {
70  }
71  $this->setTemplate($this->_availableTemplates[$templateType]);
72 
73  $this->setDisplayIfEmpty($displayIfNoReviews);
74 
75  $this->setProduct($product);
76 
77  return $this->toHtml();
78  }
79 
85  public function getRatingSummary()
86  {
87  return $this->getProduct()->getRatingSummary()->getRatingSummary();
88  }
89 
95  public function getReviewsCount()
96  {
97  return $this->getProduct()->getRatingSummary()->getReviewsCount();
98  }
99 
106  public function getReviewsUrl($useDirectLink = false)
107  {
108  $product = $this->getProduct();
109  if ($useDirectLink) {
110  return $this->getUrl(
111  'review/product/list',
112  ['id' => $product->getId(), 'category' => $product->getCategoryId()]
113  );
114  }
115  return $product->getUrlModel()->getUrl($product, ['_ignore_category' => true]);
116  }
117 }
getReviewsSummaryHtml(\Magento\Catalog\Model\Product $product, $templateType=self::DEFAULT_VIEW, $displayIfNoReviews=false)
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Review\Model\ReviewFactory $reviewFactory, array $data=[])
$templateType
Definition: list.phtml:37