Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
View.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Review\Block;
7 
16 {
22  protected $_template = 'Magento_Review::view.phtml';
23 
29  protected $_voteFactory;
30 
36  protected $_ratingFactory;
37 
43  protected $_reviewFactory;
44 
52  public function __construct(
53  \Magento\Catalog\Block\Product\Context $context,
54  \Magento\Review\Model\Rating\Option\VoteFactory $voteFactory,
55  \Magento\Review\Model\RatingFactory $ratingFactory,
56  \Magento\Review\Model\ReviewFactory $reviewFactory,
57  array $data = []
58  ) {
59  $this->_voteFactory = $voteFactory;
60  $this->_reviewFactory = $reviewFactory;
61  $this->_ratingFactory = $ratingFactory;
62 
63  parent::__construct(
64  $context,
65  $data
66  );
67  }
68 
74  public function getProductData()
75  {
76  return $this->_coreRegistry->registry('current_product');
77  }
78 
84  public function getReviewData()
85  {
86  return $this->_coreRegistry->registry('current_review');
87  }
88 
94  public function getBackUrl()
95  {
96  return $this->getUrl('*/*/list', ['id' => $this->getProductData()->getId()]);
97  }
98 
104  public function getRating()
105  {
106  if (!$this->getRatingCollection()) {
107  $ratingCollection = $this->_voteFactory->create()->getResourceCollection()->setReviewFilter(
108  $this->getReviewId()
109  )->setStoreFilter(
110  $this->_storeManager->getStore()->getId()
111  )->addRatingInfo(
112  $this->_storeManager->getStore()->getId()
113  )->load();
114  $this->setRatingCollection($ratingCollection->getSize() ? $ratingCollection : false);
115  }
116  return $this->getRatingCollection();
117  }
118 
124  public function getRatingSummary()
125  {
126  if (!$this->getRatingSummaryCache()) {
127  $this->setRatingSummaryCache(
128  $this->_ratingFactory->create()->getEntitySummary($this->getProductData()->getId())
129  );
130  }
131  return $this->getRatingSummaryCache();
132  }
133 
139  public function getTotalReviews()
140  {
141  if (!$this->getTotalReviewsCache()) {
142  $this->setTotalReviewsCache(
143  $this->_reviewFactory->create()->getTotalReviews(
144  $this->getProductData()->getId(),
145  false,
146  $this->_storeManager->getStore()->getId()
147  )
148  );
149  }
150  return $this->getTotalReviewsCache();
151  }
152 
159  public function dateFormat($date)
160  {
161  return $this->formatDate($date, \IntlDateFormatter::LONG);
162  }
163 
172  public function getReviewsSummaryHtml(
173  \Magento\Catalog\Model\Product $product,
174  $templateType = false,
175  $displayIfNoReviews = false
176  ) {
177  if (!$product->getRatingSummary()) {
178  $this->_reviewFactory->create()->getEntitySummary($product, $this->_storeManager->getStore()->getId());
179  }
180  return parent::getReviewsSummaryHtml($product, $templateType, $displayIfNoReviews);
181  }
182 }
formatDate( $date=null, $format=\IntlDateFormatter::SHORT, $showTime=false, $timezone=null)
$templateType
Definition: list.phtml:37
__construct(\Magento\Catalog\Block\Product\Context $context, \Magento\Review\Model\Rating\Option\VoteFactory $voteFactory, \Magento\Review\Model\RatingFactory $ratingFactory, \Magento\Review\Model\ReviewFactory $reviewFactory, array $data=[])
Definition: View.php:52
getReviewsSummaryHtml(\Magento\Catalog\Model\Product $product, $templateType=false, $displayIfNoReviews=false)
Definition: View.php:172