public function viewModeMove($viewMode, $id, $direction)
    {
        $this->_validateViewMode($viewMode);
        $instance = $this->_getOrThrow($id);
        $unordered = [];
        $position = false;
        $k = 0;
        $list = $this->_getInstances()
            ->sortBy(function ($fieldInstance) use ($viewMode) {
                if (isset($fieldInstance->view_modes[$viewMode]['ordering'])) {
                    return $fieldInstance->view_modes[$viewMode]['ordering'];
                }

                return 0;
            }, SORT_ASC);

        foreach ($list as $field) {
            if ($field->id === $instance->id) {
                $position = $k;
            }

            $unordered[] = $field;
            $k++;
        }

        if ($position !== false) {
            $ordered = array_move($unordered, $position, $direction);
            $before = md5(serialize($unordered));
            $after = md5(serialize($ordered));

            if ($before != $after) {
                foreach ($ordered as $k => $field) {
                    $viewModes = $field->view_modes;
                    $viewModes[$viewMode]['ordering'] = $k;
                    $field->set('view_modes', $viewModes);
                    $this->FieldInstances->save($field);
                }
            }
        }

        $this->title(__d('field', 'Change Field Order'));
        $this->redirect($this->referer());
    }
public function viewModeMove($viewMode, $id, $direction)
    {
        $this->_validateViewMode($viewMode);
        $instance = $this->_getOrThrow($id);
        $unordered = [];
        $position = false;
        $k = 0;
        $list = $this->_getInstances()
            ->sortBy(function ($fieldInstance) use ($viewMode) {
                if (isset($fieldInstance->view_modes[$viewMode]['ordering'])) {
                    return $fieldInstance->view_modes[$viewMode]['ordering'];
                }

                return 0;
            }, SORT_ASC);

        foreach ($list as $field) {
            if ($field->id === $instance->id) {
                $position = $k;
            }

            $unordered[] = $field;
            $k++;
        }

        if ($position !== false) {
            $ordered = array_move($unordered, $position, $direction);
            $before = md5(serialize($unordered));
            $after = md5(serialize($ordered));

            if ($before != $after) {
                foreach ($ordered as $k => $field) {
                    $viewModes = $field->view_modes;
                    $viewModes[$viewMode]['ordering'] = $k;
                    $field->set('view_modes', $viewModes);
                    $this->FieldInstances->save($field);
                }
            }
        }

        $this->title(__d('field', 'Change Field Order'));
        $this->redirect($this->referer());
    }
public function viewModeMove ( $ viewMode , $ id , $ direction ) { $ this -> _validateViewMode ( $ viewMode ) ; $ instance = $ this -> _getOrThrow ( $ id ) ; $ unordered = [ ] ; $ position = false ; $ k = 0 ; $ list = $ this -> _getInstances ( ) -> sortBy ( function ( $ fieldInstance ) use ( $ viewMode ) { if ( isset ( $ fieldInstance -> view_modes [ $ viewMode ] [ 'ordering' ] ) ) { return $ fieldInstance -> view_modes [ $ viewMode ] [ 'ordering' ] ; } return 0 ; } , SORT_ASC ) ; foreach ( $ list as $ field ) { if ( $ field -> id === $ instance -> id ) { $ position = $ k ; } $ unordered [ ] = $ field ; $ k ++ ; } if ( $ position !== false ) { $ ordered = array_move ( $ unordered , $ position , $ direction ) ; $ before = md5 ( serialize ( $ unordered ) ) ; $ after = md5 ( serialize ( $ ordered ) ) ; if ( $ before != $ after ) { foreach ( $ ordered as $ k => $ field ) { $ viewModes = $ field -> view_modes ; $ viewModes [ $ viewMode ] [ 'ordering' ] = $ k ; $ field -> set ( 'view_modes' , $ viewModes ) ; $ this -> FieldInstances -> save ( $ field ) ; } } } $ this -> title ( __d ( 'field' , 'Change Field Order' ) ) ; $ this -> redirect ( $ this -> referer ( ) ) ; }
Moves a field up or down within a view mode.

The ordering indicates the position they are displayed when entities are
rendered in a specific view mode.

@param string $viewMode View mode slug
@param int $id Field instance id
@param string $direction Direction, 'up' or 'down'
@return void Redirects to previous page
@throws \Cake\ORM\Exception\RecordNotFoundException When no field
instance was found
@throws \Cake\Network\Exception\NotFoundException When given view mode
does not exists
Moves a field up or down within a view mode .