Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Startdate.php
Go to the documentation of this file.
1 <?php
7 
17 class Startdate extends \Magento\Eav\Model\Entity\Attribute\Backend\Datetime
18 {
24  protected $_date;
25 
32  public function __construct(
33  \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
34  \Magento\Framework\Stdlib\DateTime\DateTime $date
35  ) {
36  $this->_date = $date;
37  parent::__construct($localeDate);
38  }
39 
46  protected function _getValueForSave($object)
47  {
48  $attributeName = $this->getAttribute()->getName();
49  $startDate = $object->getData($attributeName);
50 
51  return $startDate;
52  }
53 
61  public function beforeSave($object)
62  {
63  $startDate = $this->_getValueForSave($object);
64  if ($startDate === false) {
65  return $this;
66  }
67 
68  $object->setData($this->getAttribute()->getName(), $startDate);
69  parent::beforeSave($object);
70  return $this;
71  }
72 
81  public function validate($object)
82  {
83  $attr = $this->getAttribute();
84  $maxDate = $attr->getMaxValue();
85  $startDate = $this->_getValueForSave($object);
86  if ($startDate === false) {
87  return true;
88  }
89 
90  if ($maxDate) {
91  $date = $this->_date;
92  $value = $date->timestamp($startDate);
93  $maxValue = $date->timestamp($maxDate);
94 
95  if ($value > $maxValue) {
96  $message = __('Make sure the To Date is later than or the same as the From Date.');
97  $eavExc = new \Magento\Eav\Model\Entity\Attribute\Exception($message);
98  $eavExc->setAttributeCode($attr->getName());
99  throw $eavExc;
100  }
101  }
102  return true;
103  }
104 }
__construct(\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Magento\Framework\Stdlib\DateTime\DateTime $date)
Definition: Startdate.php:32
$attr
Definition: text.phtml:8
__()
Definition: __.php:13
$message
$value
Definition: gender.phtml:16