This might be overkill, but if you really want custom output functionality and a default output if no property is accessed directly for a field you could create a FieldtypeTwitter extends FieldtypeText
first, create a TwitterData class, save it in your module's directory as TwitterData.php
<?php
class TwitterData extends WireData
{
protected static $urlPrefix = 'https://twitter.com/';
// for echo $field; directly
public function __toString()
{
return "@{$this->handle}";
}
// for echo $field->url;
public function __get($name)
{
if ($name === 'url') {
return self::$urlPrefix . $this->handle;
} else {
return parent::__get($name);
}
}
// this is some advanced functionality, you might not need it
// isset($field->url) returns true only, if 'handle' not empty
public function __isset($name)
{
if ($name === 'url' && $this->handle) return true;
return parent::__isset($name);
}
}
Then overwrite the formatValue method in your FieldtypeTwitter.module:
<?php
// require the TwitterData class from wherever you put it
require_once(__DIR__ . '/TwitterData.php');
class FieldtypeTwitter extends FieldtypeText
{
public function ___formatValue(Page $page, Field $field, $value)
{
$twitter = new TwitterData();
// the value that comes from the field
$twitter->handle = $value;
return $twitter;
}
}
Now in your template, you can use it like you stated above:
echo $page->twitter; // outputs @accountname
echo $page->twitter->url; // outputs https://twitter.com/accountname
Just tested, it works like intended. I used the same method on my FieldtypeYaml