How to add a custom field to all lists?

RazvanM

New Member
Hello,

How to add a custom field to all lists (lists that exist already or lists that will be created in the future)?

Thank you!
 
Here's a quick example, add (edit it) this in apps/init-custom.php
PHP:
<?php

Yii::app()->hooks->addAction('after_list_created_list_default_fields', function ($params) {
    
    $fieldType = ListFieldType::model()->findByAttributes([
        'identifier' => 'text',
    ]);
    $model = new ListField();
    $model->list_id    = $params->list->list_id;
    $model->type_id    = $fieldType->type_id;
    $model->label      = 'First name';
    $model->tag        = 'FIRST_NAME';
    $model->required   = ListField::TEXT_NO;
    $model->sort_order = $params->lastSortOrder++;
    $model->save();
});
This will handle addition for new lists only.

For existing lists, you'll have to do it manually, or write a command line script to do it...
 
You mean I should add something like:

Code:
$model->sort_order = $params->lastSortOrder++; // after this line!
$model->country = '';

What I want to do, is from this article:


"Edit the list custom fields and be sure to have a field of type Country with the default value set to [SUBSCRIBER_GEO_COUNTRY]. Also a text field having this default value will work."

I want to create the custom field "Country" for ALL lists so that I can use that logic.
 
There you go:
Code:
<?php

Yii::app()->hooks->addAction('after_list_created_list_default_fields', function ($params) {
    
    $countryType = ListFieldType::model()->findByAttributes([
        'identifier' => 'country',
    ]);
    $model = new ListField();
    $model->list_id         = $params->list->list_id;
    $model->type_id         = $countryType->type_id;
    $model->label           = 'Country';
    $model->tag             = 'COUNTRY';
    $model->required        = ListField::TEXT_NO;
    $model->sort_order      = $params->lastSortOrder++;
    $model->default_value    = '[SUBSCRIBER_GEO_COUNTRY]';
    $model->save();
});
 
Back
Top