public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary)
    {
        $viewModes = $this->viewModes();
        $query->formatResults(function ($results) use ($viewModes) {
            return $results->map(function ($instance) use ($viewModes) {
                if (!is_object($instance)) {
                    return $instance;
                }

                foreach ($viewModes as $viewMode) {
                    $instanceViewModes = $instance->view_modes;
                    $viewModeDefaults = array_merge([
                        'label_visibility' => 'above',
                        'shortcodes' => false,
                        'hidden' => false,
                        'ordering' => 0,
                    ], (array)$instance->defaultViewModeSettings($viewMode));

                    if (!isset($instanceViewModes[$viewMode])) {
                        $instanceViewModes[$viewMode] = [];
                    }

                    $instanceViewModes[$viewMode] = array_merge($viewModeDefaults, $instanceViewModes[$viewMode]);
                    $instance->set('view_modes', $instanceViewModes);
                }

                $settingsDefaults = (array)$instance->defaultSettings();
                if (!empty($settingsDefaults)) {
                    $instanceSettings = $instance->get('settings');
                    foreach ($settingsDefaults as $k => $v) {
                        if (!isset($instanceSettings[$k])) {
                            $instanceSettings[$k] = $v;
                        }
                    }
                    $instance->set('settings', $instanceSettings);
                }

                return $instance;
            });
        });
    }
public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary)
    {
        $viewModes = $this->viewModes();
        $query->formatResults(function ($results) use ($viewModes) {
            return $results->map(function ($instance) use ($viewModes) {
                if (!is_object($instance)) {
                    return $instance;
                }

                foreach ($viewModes as $viewMode) {
                    $instanceViewModes = $instance->view_modes;
                    $viewModeDefaults = array_merge([
                        'label_visibility' => 'above',
                        'shortcodes' => false,
                        'hidden' => false,
                        'ordering' => 0,
                    ], (array)$instance->defaultViewModeSettings($viewMode));

                    if (!isset($instanceViewModes[$viewMode])) {
                        $instanceViewModes[$viewMode] = [];
                    }

                    $instanceViewModes[$viewMode] = array_merge($viewModeDefaults, $instanceViewModes[$viewMode]);
                    $instance->set('view_modes', $instanceViewModes);
                }

                $settingsDefaults = (array)$instance->defaultSettings();
                if (!empty($settingsDefaults)) {
                    $instanceSettings = $instance->get('settings');
                    foreach ($settingsDefaults as $k => $v) {
                        if (!isset($instanceSettings[$k])) {
                            $instanceSettings[$k] = $v;
                        }
                    }
                    $instance->set('settings', $instanceSettings);
                }

                return $instance;
            });
        });
    }
public function beforeFind ( Event $ event , Query $ query , ArrayObject $ options , $ primary ) { $ viewModes = $ this -> viewModes ( ) ; $ query -> formatResults ( function ( $ results ) use ( $ viewModes ) { return $ results -> map ( function ( $ instance ) use ( $ viewModes ) { if ( ! is_object ( $ instance ) ) { return $ instance ; } foreach ( $ viewModes as $ viewMode ) { $ instanceViewModes = $ instance -> view_modes ; $ viewModeDefaults = array_merge ( [ 'label_visibility' => 'above' , 'shortcodes' => false , 'hidden' => false , 'ordering' => 0 , ] , ( array ) $ instance -> defaultViewModeSettings ( $ viewMode ) ) ; if ( ! isset ( $ instanceViewModes [ $ viewMode ] ) ) { $ instanceViewModes [ $ viewMode ] = [ ] ; } $ instanceViewModes [ $ viewMode ] = array_merge ( $ viewModeDefaults , $ instanceViewModes [ $ viewMode ] ) ; $ instance -> set ( 'view_modes' , $ instanceViewModes ) ; } $ settingsDefaults = ( array ) $ instance -> defaultSettings ( ) ; if ( ! empty ( $ settingsDefaults ) ) { $ instanceSettings = $ instance -> get ( 'settings' ) ; foreach ( $ settingsDefaults as $ k => $ v ) { if ( ! isset ( $ instanceSettings [ $ k ] ) ) { $ instanceSettings [ $ k ] = $ v ; } } $ instance -> set ( 'settings' , $ instanceSettings ) ; } return $ instance ; } ) ; } ) ; }
Here we set default values for each view mode if they were not defined before.

@param \Cake\Event\Event $event The event that was triggered
@param \Cake\ORM\Query $query The query object
@param \ArrayObject $options Additional options given as an array
@param bool $primary Whether this find is a primary query or not
@return void
Here we set default values for each view mode if they were not defined before .