1

I'm trying to figure out how to integrate Stripe into the CMS more natively, and a thought occurred to me. Would it be possible to create a fieldtype that was just a button (i.e. no data being stored) with its own javascript?

This would potentially allow you to add a lot of customization and functionality just by adding the button fieldtype to the field layout (rather than having a global javascript file throughout the CP) — like silently submitting data to an external service, handling the response, dynamically rewriting the CP form submit action temporarily to point to a custom controller, etc.

Bad idea? Or worth investigating?

2

I thought I would report back with the good, the bad, and the ugly.

The good It is very possible (and quite easy) to create a field type button element, and include your own custom javascript as is explained in the fieldtype plugin documentation. Setting the defineContentAttributes to return false will prevent columns from being created in the database. And name, value variables are not even needed in this case in templates->render array. Here is the basic MyPlugin_CustomButtonFieldType.php:

class MyPlugin_CustomButtonFieldType extends BaseFieldType
{
    public function getName()
    {
        return Craft::t('Custom Button');
    }
    public function defineContentAttribute()
    {
        return false;
    }
    public function getInputHtml($name, $value)
    {
        craft()->templates->includeJsResource('myplugin/js/custombutton.js');
        return craft()->templates->render('muplugin/fieldtypes/CustomButton/input', array(
        ));
    }
}

And in the fieldtype template file (for a default CP craft looking button):

<input id="custombtn" type="submit" class="btn submit" value="{{ 'Do Something'|t }}" >

So far so good. Imagine the possibilities!

The Bad It is basically impossible (as far as I can tell) to over-write the default actions (i.e. action/saveEntry) via javascript as it is often dynamically inserted into the DOM via javascript depending on if you're using the command+s save shortcut or the save button sub-nav, etc. So replacing the default controller action, doesn't look like an option.

Update: It does look like you can prevent the default form submission, though. And then perhaps write your own .click() listener.

$('#entry-form').submit(function(event) {
    event.preventDefault();
    return false;
}
$('input.save').click(function()){...}

The Ugly It is possible to prevent fields from getting saved to the database (i.e. credit cards, and cvc codes) by simply removing the 'name' attributes from the fields, using something like:

jQuery(function($) {
     // prevent the form from saving credit card and cvc codes
     $('input[name*="creditCardNumber"]').removeAttr('name');
     $('input[name*="cvc"]').removeAttr('name');
}

But it's pretty much a hack job and not very pretty.

So, custom field types with buttons and javascript look like a very easy and very powerful way to customize the CMS. And so far it seems pretty easy to send data via javascript to remote services, retrieve tokens, forward the token via ajax to a custom controller to get processed, saved and result returned.

Messing with the default actions on the other hand, not such a good idea.

|improve this answer|||||

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.