Link.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sitemap\Block\Adminhtml\Grid\Renderer;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot;
  9. use Magento\Framework\App\ObjectManager;
  10. /**
  11. * Sitemap grid link column renderer
  12. */
  13. class Link extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
  14. {
  15. /**
  16. * @var \Magento\Framework\Filesystem $filesystem
  17. */
  18. protected $_filesystem;
  19. /**
  20. * @var \Magento\Sitemap\Model\SitemapFactory
  21. */
  22. protected $_sitemapFactory;
  23. /**
  24. * @var DocumentRoot
  25. */
  26. protected $documentRoot;
  27. /**
  28. * @param \Magento\Backend\Block\Context $context
  29. * @param \Magento\Sitemap\Model\SitemapFactory $sitemapFactory
  30. * @param \Magento\Framework\Filesystem $filesystem
  31. * @param array $data
  32. * @param DocumentRoot $documentRoot
  33. */
  34. public function __construct(
  35. \Magento\Backend\Block\Context $context,
  36. \Magento\Sitemap\Model\SitemapFactory $sitemapFactory,
  37. \Magento\Framework\Filesystem $filesystem,
  38. array $data = [],
  39. DocumentRoot $documentRoot = null
  40. ) {
  41. $this->_sitemapFactory = $sitemapFactory;
  42. $this->_filesystem = $filesystem;
  43. $this->documentRoot = $documentRoot ?: ObjectManager::getInstance()->get(DocumentRoot::class);
  44. parent::__construct($context, $data);
  45. }
  46. /**
  47. * Prepare link to display in grid
  48. *
  49. * @param \Magento\Framework\DataObject $row
  50. * @return string
  51. */
  52. public function render(\Magento\Framework\DataObject $row)
  53. {
  54. /** @var $sitemap \Magento\Sitemap\Model\Sitemap */
  55. $sitemap = $this->_sitemapFactory->create();
  56. $sitemap->setStoreId($row->getStoreId());
  57. $url = $this->escapeHtml($sitemap->getSitemapUrl($row->getSitemapPath(), $row->getSitemapFilename()));
  58. $fileName = preg_replace('/^\//', '', $row->getSitemapPath() . $row->getSitemapFilename());
  59. $documentRootPath = $this->documentRoot->getPath();
  60. $directory = $this->_filesystem->getDirectoryRead($documentRootPath);
  61. if ($directory->isFile($fileName)) {
  62. return sprintf('<a href="%1$s">%1$s</a>', $url);
  63. }
  64. return $url;
  65. }
  66. }