Pimcore\Model\Object\ClassDefinition\Service::importObjectBrickFromJson PHP Method

importObjectBrickFromJson() public static method

public static importObjectBrickFromJson ( $objectBrick, $json, $throwException = false ) : boolean
$objectBrick
$json
return boolean
    public static function importObjectBrickFromJson($objectBrick, $json, $throwException = false)
    {
        $importData = \Zend_Json::decode($json);
        // reverse map the class name to the class ID, see: self::generateObjectBrickJson()
        $toAssignClassDefinitions = [];
        if (is_array($importData["classDefinitions"])) {
            foreach ($importData["classDefinitions"] as &$cd) {
                if (is_numeric($cd["classname"])) {
                    $class = Object\ClassDefinition::getById($cd["classname"]);
                    if ($class) {
                        $toAssignClassDefinitions[] = $cd;
                    }
                } else {
                    $class = Object\ClassDefinition::getByName($cd["classname"]);
                    if ($class) {
                        $cd["classname"] = $class->getId();
                        $toAssignClassDefinitions[] = $cd;
                    }
                }
            }
        }
        if (!is_null($importData["layoutDefinitions"])) {
            $layout = self::generateLayoutTreeFromArray($importData["layoutDefinitions"], $throwException);
            $objectBrick->setLayoutDefinitions($layout);
        }
        $objectBrick->setClassDefinitions($toAssignClassDefinitions);
        $objectBrick->setParentClass($importData["parentClass"]);
        $objectBrick->save();
        return true;
    }

Usage Example

 public function createObjectBrick($name, $jsonPath = null)
 {
     try {
         $objectBrick = Object\Objectbrick\Definition::getByKey($name);
     } catch (\Exception $e) {
         if ($jsonPath == null) {
             $jsonPath = PIMCORE_PLUGINS_PATH . "/CoreShop/install/fieldcollection-{$name}.json";
         }
         $objectBrick = new Object\Objectbrick\Definition();
         $objectBrick->setKey($name);
         $json = file_get_contents($jsonPath);
         $result = Plugin::getEventManager()->trigger('install.objectbrick.preCreate', $this, array("objectbrickName" => $name, "json" => $json), function ($v) {
             return !preg_match('/[^,:{}\\[\\]0-9.\\-+Eaeflnr-u \\n\\r\\t]/', preg_replace('/"(\\.|[^"\\\\])*"/', '', $v));
         });
         if ($result->stopped()) {
             $resultJson = $result->last();
             if ($resultJson) {
                 $json = $resultJson;
             }
         }
         Object\ClassDefinition\Service::importObjectBrickFromJson($objectBrick, $json, true);
     }
     return $objectBrick;
 }
All Usage Examples Of Pimcore\Model\Object\ClassDefinition\Service::importObjectBrickFromJson