3

Is it possible to control what gets output in the template a custom fieldtype?

For example im working on a eventbrite plugin and it saves the id in the database but I want the field in the template to return a fully formatted register button.

Right now im doing this with a craft variable that i'm passing the id and returns the html for the button into but feels like there might be a simpler way?


Related to that right now i have to add | raw to the variable output

so its

{{ craft.eventBrite.registerButton(entry.eventbriteId) | raw }}

is there a way to prevent the escaping of whats being returned without the extra filter in the template?

  • I'm confused about the first part of your question... You want your field to automatically determine if an entry has already been loaded (ie: dynamic entry URLs)? It looks to me like you're already doing it right... How else would your registerButton know which eventbriteId it was working with? – Lindsey D Aug 13 '14 at 17:00
  • I guess i mean instead of making the customfield just be the id that the logic for the button could be in the field and output would come just by accessing the custom field... more like what dukt is doing wiht the video plugin for example.. dukt.net/craft/videos/docs/video-fields – Keith Mancuso Aug 13 '14 at 18:55
  • 1
    ahh looks like its the prepValue function in the fieldtype is what im looking for, lets you change the value saved in the database into whatever you want like an object with sub values – Keith Mancuso Aug 13 '14 at 18:59
  • 1
    I came across this question while searching for Event Brite plugins for CraftCMS... Will your Event Brite plugin be available for commercial or public use? – Sean Delaney Oct 15 '14 at 7:54
4

Run your HTML string through getRaw before sending it back to the template...

return TemplateHelper::getRaw($output);

This will basically "pre-raw" your HTML string.

  • nice! that takes care of that one. any thoughts on returning the html directly throught the field variable? is that even possible? – Keith Mancuso Aug 13 '14 at 16:43
2

So I found the answer i was looking for.

It looks like if you add a prepValue function to your fieldtypes class it can perform any action you want on the value from your custom field before returning it to the template

Looks something like this

class PLUGINNAME_FIELDNAMEFieldType extends BaseFieldType
{
    public function getName()
    {
        return Craft::t('My Field\'s Name');
    }

...
    public function prepValue($value)
    {
      // do stuff with value here like create a model, or just some conversion
      $newValue = $value." <strong>it works!</strong>";

      // newValue is whats returned to the template when you access via entry.customFieldName
      return TemplateHelper::getRaw($newValue);

      // Note: TemplateHelper::getRaw isnt required it just included cause the newValue includes html that i want to render on the page, default behavior is to escape all html

    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.