Collection.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cms\Model\ResourceModel\Block;
  7. use Magento\Cms\Api\Data\BlockInterface;
  8. use \Magento\Cms\Model\ResourceModel\AbstractCollection;
  9. /**
  10. * CMS Block Collection
  11. */
  12. class Collection extends AbstractCollection
  13. {
  14. /**
  15. * @var string
  16. */
  17. protected $_idFieldName = 'block_id';
  18. /**
  19. * Event prefix
  20. *
  21. * @var string
  22. */
  23. protected $_eventPrefix = 'cms_block_collection';
  24. /**
  25. * Event object
  26. *
  27. * @var string
  28. */
  29. protected $_eventObject = 'block_collection';
  30. /**
  31. * Perform operations after collection load
  32. *
  33. * @return $this
  34. */
  35. protected function _afterLoad()
  36. {
  37. $entityMetadata = $this->metadataPool->getMetadata(BlockInterface::class);
  38. $this->performAfterLoad('cms_block_store', $entityMetadata->getLinkField());
  39. return parent::_afterLoad();
  40. }
  41. /**
  42. * Define resource model
  43. *
  44. * @return void
  45. */
  46. protected function _construct()
  47. {
  48. $this->_init(\Magento\Cms\Model\Block::class, \Magento\Cms\Model\ResourceModel\Block::class);
  49. $this->_map['fields']['store'] = 'store_table.store_id';
  50. $this->_map['fields']['block_id'] = 'main_table.block_id';
  51. }
  52. /**
  53. * Returns pairs block_id - title
  54. *
  55. * @return array
  56. */
  57. public function toOptionArray()
  58. {
  59. return $this->_toOptionArray('block_id', 'title');
  60. }
  61. /**
  62. * Add filter by store
  63. *
  64. * @param int|array|\Magento\Store\Model\Store $store
  65. * @param bool $withAdmin
  66. * @return $this
  67. */
  68. public function addStoreFilter($store, $withAdmin = true)
  69. {
  70. $this->performAddStoreFilter($store, $withAdmin);
  71. return $this;
  72. }
  73. /**
  74. * Join store relation table if there is store filter
  75. *
  76. * @return void
  77. */
  78. protected function _renderFiltersBefore()
  79. {
  80. $entityMetadata = $this->metadataPool->getMetadata(BlockInterface::class);
  81. $this->joinStoreRelationTable('cms_block_store', $entityMetadata->getLinkField());
  82. }
  83. }