_validator = $validator; $this->gridPool = $gridPool; parent::__construct( $context, $entitySnapshot, $entityRelationComposite, $attribute, $sequenceManager, $connectionName ); } /** * Resource initialization * * @return void */ protected function _construct() { $this->_init('sales_order_address', 'entity_id'); } /** * Return configuration for all attributes * * @return array */ public function getAllAttributes() { $attributes = [ 'city' => __('City'), 'company' => __('Company'), 'country_id' => __('Country'), 'email' => __('Email'), 'firstname' => __('First Name'), 'lastname' => __('Last Name'), 'region_id' => __('State/Province'), 'street' => __('Street Address'), 'telephone' => __('Phone Number'), 'postcode' => __('Zip/Postal Code'), ]; asort($attributes); return $attributes; } /** * Performs validation before save * * @param \Magento\Framework\Model\AbstractModel $object * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object) { parent::_beforeSave($object); if (!$object->getParentId() && $object->getOrder()) { $object->setParentId($object->getOrder()->getId()); } // Init customer address id if customer address is assigned $customerData = $object->getCustomerAddressData(); if ($customerData) { $object->setCustomerAddressId($customerData->getId()); } $warnings = $this->_validator->validate($object); if (!empty($warnings)) { throw new \Magento\Framework\Exception\LocalizedException( __("We can't save the address:\n%1", implode("\n", $warnings)) ); } return $this; } }