2

I have added input box in admin's system.xml along with other fields as below:

<field id="mapping" translate="label comment tooltip" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="0">
 <label>Exclude Days</label>
 <frontend_model>VENDOR\DeliveryCountdown\Block\Adminhtml\System\Config\Form\Field\excludeDays</frontend_model>
 <backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
 <comment>
 <![CDATA[Add bank holidays that are not included in delivery / cut off times.]]>
 </comment>
 <tooltip>Delivery and cuttoff not include these days.</tooltip>
</field>

This display in admin as:

enter image description here

I can add many input boxes by clicking ADD button. Below is code to receive input dates of above form:

$offDays = $this->scopeConfig->getValue('deliverycountdown/general/mapping', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

But output is as below:

a:3:{s:17:"_1509434435073_73";

a:2:{s:6:"field1";s:4:"AAAA";s:6:"field2";s:10:"2017-11-01";}s:18:"_1509434856288_288";

a:2:{s:6:"field1";s:4:"BBBB";s:6:"field2";s:10:"2017-11-03";}s:18:"_1509434861359_359"; a:2:{s:6:"field1";s:4:"CCCC";s:6:"field2";s:10:"2017-11-06";} }

It would great help to get dates from this file (i think JSON).

Or guide me another way if thats possible to add fields in admin and get at front-end block file.

asked Oct 31, 2017 at 9:28

2 Answers 2

2

For grids controls in system configuration using backend model

<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>

which serialize array data on save and unserialize before passing to view model.

You need to use \Magento\Framework\Serialize\SerializerInterface::unserialize (for test you can use unserialize) to get original field value with serialized backend model.

Note, that in Magento 2.2 json serializer using as default serializer, so if you will work with low level unserialize then you module will throw exception in 2.2.


$offDays = $this->scopeConfig->getValue('deliverycountdown/general/mapping', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
print_r(unserialize($offDays));
answered Nov 3, 2017 at 8:05
6
  • Where to add this : \Magento\Framework\Serialize\SerializerInterface::unserialize Commented Nov 3, 2017 at 9:13
  • add Magento\Framework\Serialize\SerializerInterface as argument in constructor and use $this->serializer->unserialize($offDayas); Commented Nov 3, 2017 at 9:42
  • My construct in magento 2.1.9 as below==== public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\Stdlib\DateTime\DateTime $date // For date and time functions ) { $this->date = $date; $this->scopeConfig = $scopeConfig; parent::__construct( $context ); } =========== Please guide Commented Nov 3, 2017 at 12:33
  • add ` \Magento\Framework\Serialize\SerializerInterface $serializer` after $date argument and $this->serializer = $serializer; in constructor body Commented Nov 3, 2017 at 12:54
  • I had tried the same in my module but PHPSTORM highlight error as : Serialize namespace not defined. Site dont load in browser Commented Nov 3, 2017 at 13:00
0

I done it with following string functions and its working fine.

 $offDaysString='a:9:{s:17:"_1509434435073_73";
a:2:{s:6:"field1";s:4:"AAAA";s:6:"field2";s:10:"2017-11-01";}s:18:"_1509434856288_288";
a:2:{s:6:"field1";s:4:"BBBB";s:6:"field2";s:10:"2017-11-03";}s:18:"_1509434861359_359";
a:2:{s:6:"field1";s:4:"CCCC";s:6:"field2";s:10:"2017-11-06";}s:18:"_1509435862561_561";
a:2:{s:6:"field1";s:5:"DDDDD";s:6:"field2";s:10:"2017-11-09";}s:18:"_1509435867703_703";
a:2:{s:6:"field1";s:12:"EEEEEEEEEEEE";s:6:"field2";s:10:"2017-11-12";}s:17:"_1509435870047_47";
a:2:{s:6:"field1";s:12:"FFFFFFFFFFFF";s:6:"field2";s:10:"2017-11-14";}s:18:"_1509435872376_376";
a:2:{s:6:"field1";s:14:"GGGGGGGGGGGGGG";s:6:"field2";s:10:"2017-11-16";}s:18:"_1509435874879_879";
a:2:{s:6:"field1";s:12:"HHHHHHHHHHHH";s:6:"field2";s:10:"2017-11-17";}s:18:"_1509435877463_463";
a:2:{s:6:"field1";s:13:"IIIIIIIIIIIII";s:6:"field2";s:10:"2017-11-20";}}' ; 
 $needle = 's:10:"';
 $count = substr_count($offDaysString, $needle);
 $position = 0;
 for ($i = 0; $i < $count; $i++) {
 $pos = strpos($offDaysString, $needle, $position);
 $position = $pos + 6;// length of this - s:10:
 $startPoint[] = $position;
 $Offdates[] = substr($offDaysString, $startPoint[$i], 10); // 10 = 2017年11月20日
 }
 print_r($Offdates); 

For me, above code working fine and i put it in function in block file and called in template file displayed dates.

If anyone get a better to receive values on front-end block then post answer. Thanks

answered Nov 3, 2017 at 7:54
1
  • you must not use parsing of serialized values, see my answer Commented Nov 3, 2017 at 8:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.