protected function insertField(
        SwatDetailsViewField $field,
        SwatDetailsViewField $reference_field = null,
        $after = true
    ) {
        $this->validateField($field);

        if ($reference_field !== null) {
            $key = array_search($reference_field, $this->fields, true);

            if ($key === false) {
                throw new SwatWidgetNotFoundException(
                    'The reference field ' .
                        'could not be found in this details-view.'
                );
            }

            if ($after) {
                // insert after reference field
                array_splice($this->fields, $key, 1, array(
                    $reference_field,
                    $field
                ));
            } else {
                // insert before reference field
                array_splice($this->fields, $key, 1, array(
                    $field,
                    $reference_field
                ));
            }
        } else {
            if ($after) {
                // append to array
                $this->fields[] = $field;
            } else {
                // prepend to array
                array_unshift($this->fields, $field);
            }
        }

        if ($field->id !== null) {
            $this->fields_by_id[$field->id] = $field;
        }

        $field->view = $this; // deprecated reference
        $field->parent = $this;
    }