Attributes.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace Cpcommerce\ShippingNotifications\Model\Config\Source;
  3. use Magento\Framework\Data\OptionSourceInterface;
  4. use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory;
  5. class Attributes implements OptionSourceInterface{
  6. public function __construct(
  7. CollectionFactory $collectionFactory
  8. ){
  9. $this->collectionFactory = $collectionFactory;
  10. }
  11. public function toOptionArray()
  12. {
  13. $options = [];
  14. foreach($this->getOptions() as $optionValue => $optionLabel){
  15. $options[] = ['value' => $optionValue, 'label' => $optionLabel];
  16. }
  17. return $options;
  18. }
  19. public function getOptions()
  20. {
  21. $collection = $this->collectionFactory->create();
  22. $collection->addFieldToFilter('main_table.frontend_input', 'select');
  23. $collection->addOrder('attribute_code', 'asc');
  24. $options = ['' => _('-- Empty --')];
  25. foreach($collection->getItems() as $attribute){
  26. $options[$attribute->getAttributeCode()] = $attribute->getAttributeCode();
  27. }
  28. return $options;
  29. }
  30. }