Jump to content

Creating Custom Fields


MadHatter
 Share

Recommended Posts

Hi,

I've been trying to create a new field type that takes a Twitter username and formats the input value. I've managed to modify another module to perform the correct validation when the page is saved but I can't figure out how to extend the code so I can do other things. One example is to output a link to the Twitter page for the username. I was imagining somethign like this:

$page->twitter->url

Would output 'https://twitter.com/processwire' while doing

$page->twitter

Would output '@processwire'.

I've extended the InputfieldText and FieldtypeText classes but so far I have not figured out how to modify the value that ends up in the templates for me to use. Could someone help shed some light on this for me please? I'm aware that this could easily be handles in the template files but I wanted to do this as a learning experience to understand how the fieldtypes and Inputfields work.

Thanks,

MadHatter

Edited by kongondo
Moved your topic to this sub-board (the other is a support forum for released modules)
Link to comment
Share on other sites

That's kind of what I want to do but it would still only allow me to output to one format. I'm trying to figure out how to extend the text field with other functions, like how images have url and height/width fucntions. How would I do that? Can I use the format function to return a new class?

Link to comment
Share on other sites

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

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...