For a project I want to be able to add the country to a page. The obvious way to do this seemed to be use an attribute, but ideally with the user choosing from a list. There is already a country list in the standard Address attribute type, but I didn’t want an address, just the country. So, after some hunting around I managed to create a new Country attribute type using the code below.

The code is pretty simple and shows the power of the way C5 is put together, but the fact it took me a lot of hunting around to figure out how to get this working shows how poor their documentation is. In case it helps anyone else, this is what I did.

  1. Create the models/attribute/types/country directory.

2.Create the controller file for the attribute type as models/attribute/types/country/controller.php

class CountryAttributeTypeController extends DefaultAttributeTypeController<br></br>
    public function form()<br></br>
        $this->set('fieldPostName', $this->field('value'));<br></br>
        $val = is_object($this->attributeValue) ? $this->getAttributeValue()->getValue() : '';<br></br>
        $this->set('fieldValue', $val);<br></br>
 3. Create the form that will be displayed when adding/editing the attribute as **models/attribute/types/country/form.php**  

defined('C5_EXECUTE') or die(_("Access Denied."));

$f = Loader::helper('form');

$co = Loader::helper('lists/countries');

$countries = array_merge(array('' => t('Choose Country')), $co->getCountries());

select($fieldPostName, $countries, $fieldValue); ?>
  1. Add the attribute type from the Dashboard / System Settings / Attributes / Types

Huge thanks to Jordan Lev and his blog post for providing the final piece of the puzzle.

UPDATE: I’ve made this available on Github for anyone who wants it.