/**
  * @param string $type
  * @return $this
  */
 public function setType($type)
 {
     parent::setType($type);
     switch ($this->type) {
         case self::TYPE_BOOL:
             $this->setValueConverter(function ($value) {
                 return CmfConfig::transBase('.item_details.field.bool.' . ($value ? 'yes' : 'no'));
             });
             break;
         case self::TYPE_IMAGE:
             $this->setRenderer(function ($field, $actionConfig, array $dataForView) {
                 return DataRendererConfig::create('cmf::details/image')->setData($dataForView);
             });
             $this->setValueConverter(function ($value, DbColumnConfig $columnConfig, array $record) {
                 if (!empty($value) && is_array($value) && !empty($value['url']) && is_array($value['url'])) {
                     if (count($value['url']) > 0) {
                         unset($value['url']['source']);
                     }
                     $images = [];
                     $translationPath = CmfConfig::getInstance()->custom_dictionary_name() . '.' . $columnConfig->getDbTableConfig()->getName() . '.item_details.field.' . $columnConfig->getName() . '_version.';
                     foreach ($value['url'] as $key => $url) {
                         $images[] = ['label' => trans($translationPath . $key), 'url' => $url];
                     }
                     return $images;
                 } else {
                     return [];
                 }
             });
             break;
     }
     return $this;
 }
 public static function doDefaultValueConversionByType($value, $type)
 {
     switch ($type) {
         case ItemDetailsFieldConfig::TYPE_JSON_TREE:
             if (!is_array($value) && $value !== null) {
                 if (is_string($value) || is_numeric($value) || is_bool($value)) {
                     $value = json_decode($value, true);
                     if ($value === null) {
                         $value = 'Failed to decode JSON: ' . print_r($value, true);
                     }
                 } else {
                     $value = 'Invalid value for JSON: ' . print_r($value, true);
                 }
             }
             return json_encode($value, JSON_UNESCAPED_UNICODE);
         default:
             return parent::doDefaultValueConversionByType($value, $type);
     }
 }
Example #3
0
 /**
  * @param string $type
  * @return $this
  */
 public function setType($type)
 {
     parent::setType($type);
     return $this;
 }
Example #4
0
 /**
  * @param string $default
  * @return string
  * @throws \PeskyCMF\Scaffold\ScaffoldFieldException
  */
 public function getLabel($default = '')
 {
     $suffix = $this->isDbField() && $this->getTableColumnConfig()->isRequiredOnAnyAction() ? '*' : '';
     return parent::getLabel($default) . $suffix;
 }