26

Composer! There's already a package for Stripe: https://packagist.org/packages/stripe/stripe-php Install composer. Add a composer.json file in your plugin's root folder. Add this to the composer.json file: { "require": { "stripe/stripe-php": "1.17.2" } } Run php composer.phar update from your plugin's root folder. The library will be ...


25

How are you trying to use it in the template? Below is a very basic way of how to display a setting in a front end twig template. First define your settings with the defineSettings() method (as you already have). protected function defineSettings() { return array( 'mySetting' => array(AttributeType::String, 'required' => true) ); } ...


24

You can use the GlobalsService service available with craft()->globals like so: // Get the settings global set $settings = craft()->globals->getSetByHandle('settings'); // Access the address field var_dump($settings->address) Or you could use ElementCriteria as you suggested: // Get the GlobalSet element critera $criteria = craft()->...


20

In Craft 3 this is a cinch: class PluginController extends Controller { // Disable CSRF validation for the entire controller public $enableCsrfValidation = false; ... See the Yii2 docs for more control over inividual actions: https://github.com/yiisoft/yii2/blob/master/docs/guide/security-best-practices.md#avoiding-csrf


19

Create a div with whatever contents you want in the modal, and with a class "modal". Then create a new Garnish.Modal object with it: var $div = $('<div class="modal">...</div>'); var myModal = new Garnish.Modal($div);


18

BaseElementModel::getContent() will return the element’s ContentModel. It has two purposes: To store the element’s data from the craft_content table table when fetching elements from the DB. To store the POST data that should be stored in the craft_content table when saving an element to the DB. In both cases, the ContentModel should not actually be ...


18

Saving Matrix fields programmatically got a lot easier in Craft 3 thanks to all the content refactoring. use craft\elements\Entry; use craft\elements\MatrixBlock; // Figure out the section & entry type $section = Craft::$app->sections->getSectionByHandle('news'); $entryTypes = $section->getEntryTypes(); $entryType = reset($entryTypes); // ...


16

I did this recently with my Ad Wizard plugin... There are several different parts which need to be brought together to make it work: In your Record file, add this to defineRelations: 'asset' => array(static::BELONGS_TO, 'AssetFileRecord') In your Model file, add this to defineAttributes: 'assetId' => AttributeType::Number Whichever template is ...


16

You can use the setFlash function in the userSession Service like this: In your Controller: craft()->userSession->setFlash('yourVariable', "Something happened yo!"); In your template (that the user gets redirected to): {% set message = craft.session.getFlash('yourVariable') %} After that just run an if-statement to see if the message is set or not....


15

Try: craft()->plugins->getPlugin('myPlugin')->getSettings()


15

You can get the rest of the path from your asset's source and folder, I think something like this should do it: $source = $asset->getSource(); if ($source->type != 'Local') { throw new Exception(Craft::t('Paths not available for non-local asset sources')); } $sourcePath = $source->settings['path']; $folderPath = $asset->getFolder()->...


14

Yep, you can do that with Craft.createElementSelectorModal(): var myModal = Craft.createElementSelectorModal('Entry', { resizable: true, storageKey: 'someUniqueID', sources: ['section:1', 'seciton:2'], criteria: { status: null }, multiSelect: true, disabledElementIds: [1, 2, 3], ...


14

You can do this in your plugin: ExamplePlugin::log('the is the log message'); the log method has the following signature: log($msg, $level = LogLevel::Info, $force = false) documentation is here your log will appear in the app/storage/runtime/logs/pluginname.log


14

You can use the HttpRequestService: craft()->request->redirect($myAwesomeUrl);


14

Craft has a pretty solid engine for relational queries, so unless what you want to do is totally outside the box, there's a good chance you can accomplish what you want by using the relatedTo parameter available to the ElementCriteriaModel. Note that the relatedTo entry in the docs pertain to templates and not plugin development per se, but aside from the ...


14

To encourage input, this is a community wiki post. Any user with at least 100 reputation points may contribute. Functions, filters, objects, macros and statements – when to use which A function is one of the simplest constructs in Twig – it takes one or more inputs and returns a single output: {{ round(42.9) }} will output 43 These are best used for ...


13

To do this you'll need to take a few steps: Load the necessary resources which include the redactor.css and redactor.js file. And if you are using Craft's macros for your fields, the form macro file. {% includeCssResource "lib/redactor/redactor.css" %} {% includeJsResource "lib/redactor/redactor.js" %} {% import "_includes/forms" as forms %} Add a class ...


13

I believe you need to set the on value of your lightswitch field. on: (settings.mailerShowInfo is defined) ? settings.mailerShowInfo : "" So, the full settings of your twig code would be: {{ forms.lightswitchField({ label: "Show info in sidebar?"|t, id: 'mailerShowInfo', name: 'mailerShowInfo', value: settings....


13

Is the registerSiteRoutes hook what you're looking for?


13

Craft is piggybacking off of Yii's CSRF support for 90% of the work here and they've implemented it at the Request level (specifically onBeginRequest). That happens way before Craft gets a chance to determine what type of request this is, much less figure out where to route it. I think your best bet is going to be to put some logic in Craft's general.php ...


13

I second Brad on this, if there is a composer package, use that. Whether you use a composer package or a library not found on packagist.org you'll still need to load it so that your plugin can make use of it. Generally speaking, there are plenty of places where you can import/require third party libraries from but my recommendation would be to do so from ...


13

For those coming here using Craft 3, it's now Craft::$app->getConfig()->general->devMode.


12

Yep, Craft makes pretty liberal use of try/catch blocks. Wrap your HTTP request in that, and return whatever makes sense in the context – null, false, or maybe an empty array. Probably also worth logging the error, so the developer can investigate why it’s not working, using Craft::log(). Remember when catching exceptions, that there’s a good chance the ...


12

You would do that using an ElementCriteriaModel: $criteria = craft()->elements->getCriteria(ElementType::Entry); $criteria->section = 'mySectionHandle'; $criteria->limit = null; $entries = $criteria->find(); You can read all about how to work with ElementCriteriaModel’s in the Working with Elements guide in the Craft docs.


12

All you need to return from the getStatuses() method is an associative array() where the key represents the status name and the value represents the status label, more or less. An example might be a set of statuses for a Payment element type: public function getStatuses() { return array( 'captured' => Craft::t('Captured'), 'canceled' =&...


12

You can save Matrix blocks directly, without having to re-save the entry. Craft 2 Use MatrixService::saveBlock(): $block = new MatrixBlockModel(); $block->fieldId = 5; $block->ownerId = 100; $block->ownerLocale = 'en_us'; $block->typeId = 2; $block->sortOrder = 10; $block->setContentFromPost(array( 'fieldHandle' => 'value', //...


12

Craft's HttpSessionService is a thin wrapper for PHP's global $_SESSION variable. You can use: craft()->httpSession->add($key, $value) and: craft()->httpSession->get($key) to save and retrieve values from it.


12

Records and models are similar in that they are both containers for data, but records have the additional functionality/overhead of being able to write and read from the database via the Active Record pattern. There is also not a 1:1 relationship between records and models. A model can define attributes from multiple records. In practice, records should ...


12

I'm a bit confused by your question. It sounds as though you want to check if the currently logged-in user is an Administrator, but it looks like you're actually checking whether the newly-registered user is an Administrator. Here's how to retrieve the currently logged-in User, and check whether they belong to a specific User Group. function (Event $event)...


12

There are two classes behind each Twig tag: a Token Parser and a Node. The Token Parser is responsible for actually parsing the template, once Twig has tokenized it, and returning your Node class to represent it. The Node class is responsible for compiling itself into actual PHP code. {% requireLogin %} is about as simple as it gets, because it doesn’t ...


Only top voted, non community-wiki answers of a minimum length are eligible