GenericButton.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cms\Block\Adminhtml\Page\Edit;
  7. use Magento\Backend\Block\Widget\Context;
  8. use Magento\Cms\Api\PageRepositoryInterface;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. /**
  11. * Class GenericButton
  12. */
  13. class GenericButton
  14. {
  15. /**
  16. * @var Context
  17. */
  18. protected $context;
  19. /**
  20. * @var PageRepositoryInterface
  21. */
  22. protected $pageRepository;
  23. /**
  24. * @param Context $context
  25. * @param PageRepositoryInterface $pageRepository
  26. */
  27. public function __construct(
  28. Context $context,
  29. PageRepositoryInterface $pageRepository
  30. ) {
  31. $this->context = $context;
  32. $this->pageRepository = $pageRepository;
  33. }
  34. /**
  35. * Return CMS page ID
  36. *
  37. * @return int|null
  38. */
  39. public function getPageId()
  40. {
  41. try {
  42. return $this->pageRepository->getById(
  43. $this->context->getRequest()->getParam('page_id')
  44. )->getId();
  45. } catch (NoSuchEntityException $e) {
  46. }
  47. return null;
  48. }
  49. /**
  50. * Generate url by route and parameters
  51. *
  52. * @param string $route
  53. * @param array $params
  54. * @return string
  55. */
  56. public function getUrl($route = '', $params = [])
  57. {
  58. return $this->context->getUrlBuilder()->getUrl($route, $params);
  59. }
  60. }