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

generateLayoutTreeFromArray() public static method

public static generateLayoutTreeFromArray ( $array, boolean $throwException = false ) : boolean
$array
$throwException boolean
return boolean
    public static function generateLayoutTreeFromArray($array, $throwException = false)
    {
        if (is_array($array) && count($array) > 0) {
            $class = "\\Pimcore\\Model\\Object\\ClassDefinition\\" . ucfirst($array["datatype"]) . "\\" . ucfirst($array["fieldtype"]);
            if (!\Pimcore\Tool::classExists($class)) {
                $class = "\\Object_Class_" . ucfirst($array["datatype"]) . "_" . ucfirst($array["fieldtype"]);
                if (!\Pimcore\Tool::classExists($class)) {
                    $class = null;
                }
            }
            if ($class) {
                $item = new $class();
                if (method_exists($item, "addChild")) {
                    // allows childs
                    $item->setValues($array, ["childs"]);
                    if (is_array($array) && is_array($array["childs"]) && $array["childs"]["datatype"]) {
                        $childO = self::generateLayoutTreeFromArray($array["childs"], $throwException);
                        $item->addChild($childO);
                    } elseif (is_array($array["childs"]) && count($array["childs"]) > 0) {
                        foreach ($array["childs"] as $child) {
                            $childO = self::generateLayoutTreeFromArray($child, $throwException);
                            if ($childO !== false) {
                                $item->addChild($childO);
                            } else {
                                if ($throwException) {
                                    throw new \Exception("Could not add child " . var_export($child, true));
                                }
                                return false;
                            }
                        }
                    }
                } else {
                    $item->setValues($array);
                }
                return $item;
            }
        }
        if ($throwException) {
            throw new \Exception("Could not add child " . var_export($array, true));
        }
        return false;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * @param string $name
  * @return ClassDefinition
  * @throws \Exception
  */
 public function createClass($name)
 {
     $def = file_get_contents(sprintf('%s/class_%s.json', $this->baseDir, $name));
     $conf = json_decode($def, true);
     $layoutDefinitions = ClassDefinition\Service::generateLayoutTreeFromArray($conf['layoutDefinitions']);
     if (!$layoutDefinitions) {
         throw new \Exception('Unable to generate class layout for ' . $name);
     }
     $class = ClassDefinition::create();
     $class->setName($name);
     $class->setUserOwner($this->getUser()->getId());
     $class->setLayoutDefinitions($layoutDefinitions);
     $class->setIcon($conf['icon']);
     $class->setAllowInherit($conf['allowInherit']);
     $class->setAllowVariants($conf['allowVariants']);
     $class->setParentClass($conf['parentClass']);
     $class->setPreviewUrl($conf['previewUrl']);
     $class->setPropertyVisibility($conf['propertyVisibility']);
     $class->save();
     return $class;
 }
All Usage Examples Of Pimcore\Model\Object\ClassDefinition\Service::generateLayoutTreeFromArray