src/ElementsExtension.php
public function gridFieldForElementRelation(
        FieldList $fields,
        $relationName,
        $relation
    ) {
        // sort relations
        asort($relation);

        $config = GridFieldConfig_RelationEditor::create()
            ->removeComponentsByType(GridFieldDeleteAction::class)
            ->removeComponentsByType(GridFieldAddExistingAutocompleter::class)
            ->addComponent(new GridFieldOrderableRows('Sort'))
            ->addComponent(new GridFieldDeleteAction())
        ;

        // attach default elements action
        if (
            $this->owner->canEdit()
            && !$this->defaultsCreated()
            && $this->owner->getDefaultElements()
            && isset($this->owner->getDefaultElements()[$relationName])
        ) {
            $config->addComponent(new GridFieldDefaultElementsButton());
        }

        if (count($relation) > 1)
        {
            $config
                ->removeComponentsByType(GridFieldAddNewButton::class)
                ->addComponent($multiClass = new GridFieldAddNewMultiClass())
            ;

            $multiClass->setClasses(ElementsExtension::map_classnames($relation));
        }

        $config
            ->getComponentByType(GridFieldPaginator::class)
            ->setItemsPerPage(150)
        ;

        $columns = [
            'CMSTypeInfo' => _t(__CLASS__ . '.CMSTypeInfo', 'Type'),
            'CMSSummary' => _t(__CLASS__ . '.CMSSummary', 'Summary'),
        ];

        if ($this->owner->hasExtension(ElementBase::FLUENT_CLASS))
        {
            $columns['Languages'] = _t(__CLASS__ . '.Languages', 'Lang');
        }

        if (count($relation) == 1
            && $summaryFields = singleton($relation[0])->summaryFields()
        ) {
            $columns = array_merge($columns, $summaryFields);
        }

        $config
            ->getComponentByType(GridFieldDataColumns::class)
            ->setDisplayFields($columns)
        ;

        $tabName = "Root.{$relationName}";

        // if only one relation is set, add gridfield to main tab
        if (count($this->elementRelations) == 1) $tabName = "Root.Main";

        $label = _t("Element_Relations.{$relationName}", $relationName);

        $config
            ->getComponentByType(GridFieldDetailForm::class)
            ->setItemRequestClass(VersionedElement_ItemRequest::class)
        ;

        $fields->addFieldToTab(
            $tabName,
            $gridField = GridField::create(
                $relationName,
                $label,
                $this->owner->ElementsByRelation($relationName),
                $config
            )
        );

        $gridField->addExtraClass('elements-gridfield');


        if (count($relation) == 1) $gridField->setModelClass($relation[0]);

        if (count($this->elementRelations) > 1)
        {
            $fields
                ->findOrMakeTab($tabName)
                ->setTitle($label)
            ;
        }
        return $this->owner;
    }
public function gridFieldForElementRelation(
        FieldList $fields,
        $relationName,
        $relation
    ) {
        // sort relations
        asort($relation);

        $config = GridFieldConfig_RelationEditor::create()
            ->removeComponentsByType(GridFieldDeleteAction::class)
            ->removeComponentsByType(GridFieldAddExistingAutocompleter::class)
            ->addComponent(new GridFieldOrderableRows('Sort'))
            ->addComponent(new GridFieldDeleteAction())
        ;

        // attach default elements action
        if (
            $this->owner->canEdit()
            && !$this->defaultsCreated()
            && $this->owner->getDefaultElements()
            && isset($this->owner->getDefaultElements()[$relationName])
        ) {
            $config->addComponent(new GridFieldDefaultElementsButton());
        }

        if (count($relation) > 1)
        {
            $config
                ->removeComponentsByType(GridFieldAddNewButton::class)
                ->addComponent($multiClass = new GridFieldAddNewMultiClass())
            ;

            $multiClass->setClasses(ElementsExtension::map_classnames($relation));
        }

        $config
            ->getComponentByType(GridFieldPaginator::class)
            ->setItemsPerPage(150)
        ;

        $columns = [
            'CMSTypeInfo' => _t(__CLASS__ . '.CMSTypeInfo', 'Type'),
            'CMSSummary' => _t(__CLASS__ . '.CMSSummary', 'Summary'),
        ];

        if ($this->owner->hasExtension(ElementBase::FLUENT_CLASS))
        {
            $columns['Languages'] = _t(__CLASS__ . '.Languages', 'Lang');
        }

        if (count($relation) == 1
            && $summaryFields = singleton($relation[0])->summaryFields()
        ) {
            $columns = array_merge($columns, $summaryFields);
        }

        $config
            ->getComponentByType(GridFieldDataColumns::class)
            ->setDisplayFields($columns)
        ;

        $tabName = "Root.{$relationName}";

        // if only one relation is set, add gridfield to main tab
        if (count($this->elementRelations) == 1) $tabName = "Root.Main";

        $label = _t("Element_Relations.{$relationName}", $relationName);

        $config
            ->getComponentByType(GridFieldDetailForm::class)
            ->setItemRequestClass(VersionedElement_ItemRequest::class)
        ;

        $fields->addFieldToTab(
            $tabName,
            $gridField = GridField::create(
                $relationName,
                $label,
                $this->owner->ElementsByRelation($relationName),
                $config
            )
        );

        $gridField->addExtraClass('elements-gridfield');


        if (count($relation) == 1) $gridField->setModelClass($relation[0]);

        if (count($this->elementRelations) > 1)
        {
            $fields
                ->findOrMakeTab($tabName)
                ->setTitle($label)
            ;
        }
        return $this->owner;
    }
public function gridFieldForElementRelation ( FieldList $ fields , $ relationName , $ relation ) { // sort relations asort ( $ relation ) ; $ config = GridFieldConfig_RelationEditor :: create ( ) -> removeComponentsByType ( GridFieldDeleteAction :: class ) -> removeComponentsByType ( GridFieldAddExistingAutocompleter :: class ) -> addComponent ( new GridFieldOrderableRows ( 'Sort' ) ) -> addComponent ( new GridFieldDeleteAction ( ) ) ; // attach default elements action if ( $ this -> owner -> canEdit ( ) && ! $ this -> defaultsCreated ( ) && $ this -> owner -> getDefaultElements ( ) && isset ( $ this -> owner -> getDefaultElements ( ) [ $ relationName ] ) ) { $ config -> addComponent ( new GridFieldDefaultElementsButton ( ) ) ; } if ( count ( $ relation ) > 1 ) { $ config -> removeComponentsByType ( GridFieldAddNewButton :: class ) -> addComponent ( $ multiClass = new GridFieldAddNewMultiClass ( ) ) ; $ multiClass -> setClasses ( ElementsExtension :: map_classnames ( $ relation ) ) ; } $ config -> getComponentByType ( GridFieldPaginator :: class ) -> setItemsPerPage ( 150 ) ; $ columns = [ 'CMSTypeInfo' => _t ( __CLASS__ . '.CMSTypeInfo' , 'Type' ) , 'CMSSummary' => _t ( __CLASS__ . '.CMSSummary' , 'Summary' ) , ] ; if ( $ this -> owner -> hasExtension ( ElementBase :: FLUENT_CLASS ) ) { $ columns [ 'Languages' ] = _t ( __CLASS__ . '.Languages' , 'Lang' ) ; } if ( count ( $ relation ) == 1 && $ summaryFields = singleton ( $ relation [ 0 ] ) -> summaryFields ( ) ) { $ columns = array_merge ( $ columns , $ summaryFields ) ; } $ config -> getComponentByType ( GridFieldDataColumns :: class ) -> setDisplayFields ( $ columns ) ; $ tabName = "Root.{$relationName}" ; // if only one relation is set, add gridfield to main tab if ( count ( $ this -> elementRelations ) == 1 ) $ tabName = "Root.Main" ; $ label = _t ( "Element_Relations.{$relationName}" , $ relationName ) ; $ config -> getComponentByType ( GridFieldDetailForm :: class ) -> setItemRequestClass ( VersionedElement_ItemRequest :: class ) ; $ fields -> addFieldToTab ( $ tabName , $ gridField = GridField :: create ( $ relationName , $ label , $ this -> owner -> ElementsByRelation ( $ relationName ) , $ config ) ) ; $ gridField -> addExtraClass ( 'elements-gridfield' ) ; if ( count ( $ relation ) == 1 ) $ gridField -> setModelClass ( $ relation [ 0 ] ) ; if ( count ( $ this -> elementRelations ) > 1 ) { $ fields -> findOrMakeTab ( $ tabName ) -> setTitle ( $ label ) ; } return $ this -> owner ; }
Adds a GridField for a elements relation

@param  FieldList $fields
@param  string    $relationName
@return DataObject
Adds a GridField for a elements relation