public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options)
    {
        if (!$this->config('status')) {
            return true;
        }

        if (!$options['atomic']) {
            throw new FatalErrorException(__d('field', 'Entities in fieldable tables can only be saved using transaction. Set [atomic = true]'));
        }

        if (!$this->_validation($entity)) {
            return false;
        }

        $this->_cache['createValues'] = [];
        foreach ($this->_attributesForEntity($entity) as $attr) {
            if (!$this->_toolbox->propertyExists($entity, $attr->get('name'))) {
                continue;
            }

            $field = $this->_prepareMockField($entity, $attr);
            $result = $field->beforeSave($this->_fetchPost($field));

            if ($result === false) {
                $this->attachEntityFields($entity);

                return false;
            }

            $data = [
                'eav_attribute_id' => $field->get('metadata')->get('attribute_id'),
                'entity_id' => $this->_toolbox->getEntityId($entity),
                "value_{$field->metadata['type']}" => $field->get('value'),
                'extra' => $field->get('extra'),
            ];

            if ($field->get('metadata')->get('value_id')) {
                $valueEntity = TableRegistry::get('Eav.EavValues')->get($field->get('metadata')->get('value_id'));
                $valueEntity = TableRegistry::get('Eav.EavValues')->patchEntity($valueEntity, $data, ['validate' => false]);
            } else {
                $valueEntity = TableRegistry::get('Eav.EavValues')->newEntity($data, ['validate' => false]);
            }

            if ($entity->isNew() || $valueEntity->isNew()) {
                $this->_cache['createValues'][] = $valueEntity;
            } elseif (!TableRegistry::get('Eav.EavValues')->save($valueEntity)) {
                $this->attachEntityFields($entity);
                $event->stopPropagation();

                return false;
            }
        }

        $this->attachEntityFields($entity);

        return true;
    }
public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options)
    {
        if (!$this->config('status')) {
            return true;
        }

        if (!$options['atomic']) {
            throw new FatalErrorException(__d('field', 'Entities in fieldable tables can only be saved using transaction. Set [atomic = true]'));
        }

        if (!$this->_validation($entity)) {
            return false;
        }

        $this->_cache['createValues'] = [];
        foreach ($this->_attributesForEntity($entity) as $attr) {
            if (!$this->_toolbox->propertyExists($entity, $attr->get('name'))) {
                continue;
            }

            $field = $this->_prepareMockField($entity, $attr);
            $result = $field->beforeSave($this->_fetchPost($field));

            if ($result === false) {
                $this->attachEntityFields($entity);

                return false;
            }

            $data = [
                'eav_attribute_id' => $field->get('metadata')->get('attribute_id'),
                'entity_id' => $this->_toolbox->getEntityId($entity),
                "value_{$field->metadata['type']}" => $field->get('value'),
                'extra' => $field->get('extra'),
            ];

            if ($field->get('metadata')->get('value_id')) {
                $valueEntity = TableRegistry::get('Eav.EavValues')->get($field->get('metadata')->get('value_id'));
                $valueEntity = TableRegistry::get('Eav.EavValues')->patchEntity($valueEntity, $data, ['validate' => false]);
            } else {
                $valueEntity = TableRegistry::get('Eav.EavValues')->newEntity($data, ['validate' => false]);
            }

            if ($entity->isNew() || $valueEntity->isNew()) {
                $this->_cache['createValues'][] = $valueEntity;
            } elseif (!TableRegistry::get('Eav.EavValues')->save($valueEntity)) {
                $this->attachEntityFields($entity);
                $event->stopPropagation();

                return false;
            }
        }

        $this->attachEntityFields($entity);

        return true;
    }
public function beforeSave ( Event $ event , EntityInterface $ entity , ArrayObject $ options ) { if ( ! $ this -> config ( 'status' ) ) { return true ; } if ( ! $ options [ 'atomic' ] ) { throw new FatalErrorException ( __d ( 'field' , 'Entities in fieldable tables can only be saved using transaction. Set [atomic = true]' ) ) ; } if ( ! $ this -> _validation ( $ entity ) ) { return false ; } $ this -> _cache [ 'createValues' ] = [ ] ; foreach ( $ this -> _attributesForEntity ( $ entity ) as $ attr ) { if ( ! $ this -> _toolbox -> propertyExists ( $ entity , $ attr -> get ( 'name' ) ) ) { continue ; } $ field = $ this -> _prepareMockField ( $ entity , $ attr ) ; $ result = $ field -> beforeSave ( $ this -> _fetchPost ( $ field ) ) ; if ( $ result === false ) { $ this -> attachEntityFields ( $ entity ) ; return false ; } $ data = [ 'eav_attribute_id' => $ field -> get ( 'metadata' ) -> get ( 'attribute_id' ) , 'entity_id' => $ this -> _toolbox -> getEntityId ( $ entity ) , "value_{$field->metadata['type']}" => $ field -> get ( 'value' ) , 'extra' => $ field -> get ( 'extra' ) , ] ; if ( $ field -> get ( 'metadata' ) -> get ( 'value_id' ) ) { $ valueEntity = TableRegistry :: get ( 'Eav.EavValues' ) -> get ( $ field -> get ( 'metadata' ) -> get ( 'value_id' ) ) ; $ valueEntity = TableRegistry :: get ( 'Eav.EavValues' ) -> patchEntity ( $ valueEntity , $ data , [ 'validate' => false ] ) ; } else { $ valueEntity = TableRegistry :: get ( 'Eav.EavValues' ) -> newEntity ( $ data , [ 'validate' => false ] ) ; } if ( $ entity -> isNew ( ) || $ valueEntity -> isNew ( ) ) { $ this -> _cache [ 'createValues' ] [ ] = $ valueEntity ; } elseif ( ! TableRegistry :: get ( 'Eav.EavValues' ) -> save ( $ valueEntity ) ) { $ this -> attachEntityFields ( $ entity ) ; $ event -> stopPropagation ( ) ; return false ; } } $ this -> attachEntityFields ( $ entity ) ; return true ; }
Before an entity is saved.

Here is where we dispatch each custom field's `$_POST` information to its
corresponding Field Handler, so they can operate over their values.

Fields Handler's `beforeSave()` method is automatically invoked for each
attached field for the entity being processed, your field handler should look
as follow:

```php
use Field\Handler;

class TextField extends Handler
{
public function beforeSave(Field $field, $post)
{
// alter $field, and do nifty things with $post
// return FALSE; will halt the operation
}
}
```

Field Handlers should **alter** `$field->value` and `$field->extra` according
to its needs using the provided **$post** argument.

**NOTE:** Returning boolean FALSE will halt the whole Entity's save operation.

@param \Cake\Event\Event $event The event that was triggered
@param \Cake\Datasource\EntityInterface $entity The entity being saved
@param \ArrayObject $options Additional options given as an array
@throws \Cake\Error\FatalErrorException When using this behavior in non-atomic mode
@return bool True if save operation should continue
Before an entity is saved .