src/ElementsExtension.php
public static function create_default_elements(SiteTree $record): int
    {
        $count = 0;
        if (!$record || !$record->ID)
        {
            throw new SS_HTTPResponse_Exception("Bad record ID #" . (int)$data['ID'], 404);
        }

        if ($relationNames = ElementsExtension::page_element_relation_names($record))
        {
            $defaultElements = $record->getDefaultElements();
            if (count($relationNames) > 0)
            {
                foreach ($relationNames as $relationName => $elementsClasses)
                {
                    if (isset($defaultElements[$relationName]))
                    {
                        $elementClasses = $defaultElements[$relationName];
                        foreach ($elementClasses as $className)
                        {
                            $definedElements = $record->ElementsByRelation($relationName)->map('ClassName', 'ClassName');
                            if (!isset($definedElements[$className]))
                            {
                                $element = new $className;
                                $element->populate('PageID', $record->ID, $relationName);
                                $element->write();
                                $count++;
                            }
                        }
                    }
                }
            }
        }
        return $count;
    }
public static function create_default_elements(SiteTree $record): int
    {
        $count = 0;
        if (!$record || !$record->ID)
        {
            throw new SS_HTTPResponse_Exception("Bad record ID #" . (int)$data['ID'], 404);
        }

        if ($relationNames = ElementsExtension::page_element_relation_names($record))
        {
            $defaultElements = $record->getDefaultElements();
            if (count($relationNames) > 0)
            {
                foreach ($relationNames as $relationName => $elementsClasses)
                {
                    if (isset($defaultElements[$relationName]))
                    {
                        $elementClasses = $defaultElements[$relationName];
                        foreach ($elementClasses as $className)
                        {
                            $definedElements = $record->ElementsByRelation($relationName)->map('ClassName', 'ClassName');
                            if (!isset($definedElements[$className]))
                            {
                                $element = new $className;
                                $element->populate('PageID', $record->ID, $relationName);
                                $element->write();
                                $count++;
                            }
                        }
                    }
                }
            }
        }
        return $count;
    }
public static function create_default_elements ( SiteTree $ record ) : int { $ count = 0 ; if ( ! $ record || ! $ record -> ID ) { throw new SS_HTTPResponse_Exception ( "Bad record ID #" . ( int ) $ data [ 'ID' ] , 404 ) ; } if ( $ relationNames = ElementsExtension :: page_element_relation_names ( $ record ) ) { $ defaultElements = $ record -> getDefaultElements ( ) ; if ( count ( $ relationNames ) > 0 ) { foreach ( $ relationNames as $ relationName => $ elementsClasses ) { if ( isset ( $ defaultElements [ $ relationName ] ) ) { $ elementClasses = $ defaultElements [ $ relationName ] ; foreach ( $ elementClasses as $ className ) { $ definedElements = $ record -> ElementsByRelation ( $ relationName ) -> map ( 'ClassName' , 'ClassName' ) ; if ( ! isset ( $ definedElements [ $ className ] ) ) { $ element = new $ className ; $ element -> populate ( 'PageID' , $ record -> ID , $ relationName ) ; $ element -> write ( ) ; $ count ++ ; } } } } } } return $ count ; }
Create_default elements , if setup via config @see self::getDefaultElements().
@param  SiteTree $record
@return int
Create_default elements if setup via config