How to change survey's field template?

slackero

New Member
Are there any hooks to change the rendering of the surveys without patching the core code? I cannot find how to override these the easy way. Any suggestions to customise the related view files – mostly field-display.php?

Bildschirmfoto 2020-11-27 um 08.52.11.png
 
We don't advise changing these right now for two reasons:
1. They changed a lot in v2, which for the user is transparent, but not for the developer, so v2 will break this if you change it.
2. @ghimes is working on a drag and drop builder for the list form, which will require further changes to this area in v2, which again, will be transparent for the user but not for the developer.

Your best bet right now is to limit the changes to css/js.
 
Thanks for the reply.

If it breaks in v2 – then I know it's easier to patch the core code right now with a simple extension or by just replacing manually. I have a repository with my changes to keep it on-par.

Would be great to have real template files to separate logic from template and which could be traversed on customer's basis.
 
Would be great to have real template files to separate logic from template and which could be traversed on customer's basis.
we can at least add some hooks there to facilitate access.
What exactly do you need to do, just render a different view file with same view data in it?
 
I don't know exactly how the templates will look in v2. Twig based templating especially for such usage would be better.

The easiest way here would be a hook that allows to overload the paths of all related $viewFile = realpath(dirname(__FILE__) . '/../views/field-display.php'); as it's hardcoded yet in function buildFieldArray($model)
 
@slackero - i was thinking to add the hook like:
PHP:
$hooks = Yii::app()->hooks;
        return $hooks->applyFilters('survey_field_builder_type_checkbox_responder_build_field_array', array(
            'sort_order' => (int)$field->sort_order,
            'field_html' => $fieldHtml,
        ), $model);
Then you get access to the whole array, and you can do:
PHP:
Yii::app()->hooks->addFilter('survey_field_builder_type_checkbox_responder_build_field_array', function($buildArray, $model){
    $viewFile   = 'your-own-view-file';
    $field      = $model->field;
    // overwrite default html with yours.
    $buildArray['field_html'] = $this->owner->renderInternal($viewFile, compact('model', 'field'), true)
    return $buildArray;
});
Does that sound okay to you?
 
@twisted1919 - this looks fine to me. Just a lot of additional hooks – right?

If I would develope something like this I would define an array with all paths to the related view files. I know your concept is a bit different.

PHP:
$default_survey_builder_view_files = [
    'checkbox' => 'path-to-related-view-file',
    'checkboxlist' => 'path-to-related-view-file',
    'consentcheckbox' => 'path-to-related-view-file',
    'country' => 'path-to-related-view-file',
    'date' => 'path-to-related-view-file',
    'datetime' => 'path-to-related-view-file',
    'dropdown' => 'path-to-related-view-file',
    'email' => 'path-to-related-view-file',
    'geocity' => 'path-to-related-view-file',
    'geocountry' => 'path-to-related-view-file',
    'geostate' => 'path-to-related-view-file',
    'multiselect' => 'path-to-related-view-file',
    'number' => 'path-to-related-view-file',
    'phonenumber' => 'path-to-related-view-file',
    'radiolist' => 'path-to-related-view-file',
    'rating' => 'path-to-related-view-file',
    'state' => 'path-to-related-view-file',
    'text' => 'path-to-related-view-file',
    'textarea' => 'path-to-related-view-file',
    'url' => 'path-to-related-view-file',
    'yearsrange' => 'path-to-related-view-file',
];

Then it's just a single hook to inject 1 … all new view files.
 
this looks fine to me. Just a lot of additional hooks – right?
True, but it has to be this way for the way we're doing things.

We added them all, it wasn't really a big deal.
When we release the next version, let us know if you have questions, otherwise:

All the new list hooks are under this form:
Code:
list_field_builder_type_{type}_subscriber_build_field_array
Where {type} can be any of the above you listed: url, textarea, number, etc.

All the new survey hooks are under this form:
Code:
survey_field_builder_type_{type}_responder_build_field_array
Where {type} can be any of the above you listed: url, textarea, number, etc.

Please beware that some field types, such as radiolist, checkboxlist, will make available more arguments for you to make use of, to give you an example, to alter the build array for a text field, we have this hook:
PHP:
return $hooks->applyFilters('survey_field_builder_type_text_responder_build_field_array', array(
            'sort_order' => (int)$field->sort_order,
            'field_html' => $fieldHtml,
        ), $model, $field);
So in our callback:
PHP:
Yii::app()->hooks->addFilter('survey_field_builder_type_text_responder_build_field_array', function($buildArray, $model, $field){
    $viewFile   = 'your-own-view-file';
    // overwrite default html with yours.
    $buildArray['field_html'] = $this->owner->renderInternal($viewFile, compact('model', 'field'), true)
    return $buildArray;
});
MailWizz makes available 3 params, the $buildArray which you must return, and then additional info, like the $model and the $field.

However, for example, for the checkboxlist filter, the hook signature looks like:
PHP:
return Yii::app()->hooks->applyFilters('survey_field_builder_type_checkboxlist_responder_build_field_array', array(
            'sort_order' => (int)$field->sort_order,
            'field_html' => $fieldHtml,
        ), $models[0], $field, $values, $options, $models);
which means the callback will look like:
PHP:
Yii::app()->hooks->addFilter('survey_field_builder_type_checkboxlist_responder_build_field_array', function($buildArray, $model, $field, $values, $options, $models){
    $viewFile   = 'your-own-view-file';
    // overwrite default html with yours.
    $buildArray['field_html'] = $this->owner->renderInternal($viewFile, compact('model', 'field', 'values', 'options'), true)

    return $buildArray;

});
Where, as you see, you have more arguments available for you.
 
Back
Top