public function handleAction(GridField $gridField, $actionName, $arguments, $data)
    {
        if ($actionName == 'add') {
            $dbField = $this->getDataObjectField();

            $objClass = $gridField->getModelClass();

            /**
             * @var DataObject $obj
             */
            $obj = $objClass::create();

            if ($obj->hasField($dbField)) {
                $obj->setCastedField($dbField, $data['gridfieldaddbydbfield'][$obj->ClassName][$dbField]);

                if ($obj->canCreate()) {
                    $id = $gridField->getList()->add($obj);
                    if (!$id) {
                        $gridField->setCustomValidationMessage(
                            _t(
                                __CLASS__ . '.AddFail',
                                'Unable to save {class} to the database.',
                                'Unable to add the DataObject.',
                                [
                                    'class' => get_class($obj),
                                ]
                            )
                        );
                    }
                } else {
                    return Security::permissionFailure(
                        Controller::curr(),
                        _t(
                            __CLASS__ . '.PermissionFail',
                            'You don\'t have permission to create a {class}.',
                            'Unable to add the DataObject.',
                            [
                                'class' => get_class($obj)
                            ]
                        )
                    );
                }
            } else {
                throw new UnexpectedValueException(
                    sprintf(
                        'Invalid field (%s) on %s.',
                        $dbField,
                        $obj->ClassName
                    )
                );
            }
        }

        return null;
    }
public function handleAction(GridField $gridField, $actionName, $arguments, $data)
    {
        if ($actionName == 'add') {
            $dbField = $this->getDataObjectField();

            $objClass = $gridField->getModelClass();

            /**
             * @var DataObject $obj
             */
            $obj = $objClass::create();

            if ($obj->hasField($dbField)) {
                $obj->setCastedField($dbField, $data['gridfieldaddbydbfield'][$obj->ClassName][$dbField]);

                if ($obj->canCreate()) {
                    $id = $gridField->getList()->add($obj);
                    if (!$id) {
                        $gridField->setCustomValidationMessage(
                            _t(
                                __CLASS__ . '.AddFail',
                                'Unable to save {class} to the database.',
                                'Unable to add the DataObject.',
                                [
                                    'class' => get_class($obj),
                                ]
                            )
                        );
                    }
                } else {
                    return Security::permissionFailure(
                        Controller::curr(),
                        _t(
                            __CLASS__ . '.PermissionFail',
                            'You don\'t have permission to create a {class}.',
                            'Unable to add the DataObject.',
                            [
                                'class' => get_class($obj)
                            ]
                        )
                    );
                }
            } else {
                throw new UnexpectedValueException(
                    sprintf(
                        'Invalid field (%s) on %s.',
                        $dbField,
                        $obj->ClassName
                    )
                );
            }
        }

        return null;
    }
public function handleAction ( GridField $ gridField , $ actionName , $ arguments , $ data ) { if ( $ actionName == 'add' ) { $ dbField = $ this -> getDataObjectField ( ) ; $ objClass = $ gridField -> getModelClass ( ) ; /**
             * @var DataObject $obj
             */ $ obj = $ objClass :: create ( ) ; if ( $ obj -> hasField ( $ dbField ) ) { $ obj -> setCastedField ( $ dbField , $ data [ 'gridfieldaddbydbfield' ] [ $ obj -> ClassName ] [ $ dbField ] ) ; if ( $ obj -> canCreate ( ) ) { $ id = $ gridField -> getList ( ) -> add ( $ obj ) ; if ( ! $ id ) { $ gridField -> setCustomValidationMessage ( _t ( __CLASS__ . '.AddFail' , 'Unable to save {class} to the database.' , 'Unable to add the DataObject.' , [ 'class' => get_class ( $ obj ) , ] ) ) ; } } else { return Security :: permissionFailure ( Controller :: curr ( ) , _t ( __CLASS__ . '.PermissionFail' , 'You don\'t have permission to create a {class}.' , 'Unable to add the DataObject.' , [ 'class' => get_class ( $ obj ) ] ) ) ; } } else { throw new UnexpectedValueException ( sprintf ( 'Invalid field (%s) on %s.' , $ dbField , $ obj -> ClassName ) ) ; } } return null ; }
Handles the add action for the given DataObject.

@param $gridField GridField
@param $actionName string
@param $arguments mixed
@param $data array

@return null|HTTPResponse

@throws UnexpectedValueException
Handles the add action for the given DataObject .