Select.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace Dotdigitalgroup\Email\Model\Adminhtml\Source\Customer\Attributes;
  3. class Select
  4. {
  5. /**
  6. * @var \Magento\Customer\Model\CustomerFactory
  7. */
  8. private $customerFactory;
  9. /**
  10. * Escaper
  11. *
  12. * @var \Magento\Framework\Escaper
  13. */
  14. private $escaper;
  15. /**
  16. * Select constructor.
  17. *
  18. * @param \Magento\Customer\Model\CustomerFactory $customerFactory
  19. * @param \Magento\Framework\Escaper $escaper
  20. */
  21. public function __construct(
  22. \Magento\Customer\Model\CustomerFactory $customerFactory,
  23. \Magento\Framework\Escaper $escaper
  24. ) {
  25. $this->customerFactory = $customerFactory;
  26. $this->escaper = $escaper;
  27. }
  28. /**
  29. * Customer custom attributes.
  30. *
  31. * @return array
  32. */
  33. public function toOptionArray()
  34. {
  35. $options = [];
  36. //exclude attributes from mapping
  37. $excluded = [
  38. 'created_at',
  39. 'created_in',
  40. 'dob',
  41. 'dotmailer_contact_id',
  42. 'email',
  43. 'firstname',
  44. 'lastname',
  45. 'gender',
  46. 'group_id',
  47. 'password_hash',
  48. 'prefix',
  49. 'rp_token',
  50. 'rp_token_create_at',
  51. 'website_id',
  52. ];
  53. $attributes = $this->customerFactory->create()
  54. ->getAttributes();
  55. foreach ($attributes as $attribute) {
  56. if ($attribute->getFrontendLabel()) {
  57. $code = $attribute->getAttributeCode();
  58. //escape the label in case of quotes
  59. $label = $this->escaper->escapeQuote($attribute->getFrontendLabel());
  60. if (!in_array($code, $excluded)) {
  61. $options[] = [
  62. 'value' => $attribute->getAttributeCode(),
  63. 'label' => $label,
  64. ];
  65. }
  66. }
  67. }
  68. return $options;
  69. }
  70. }