9 votes
Accepted

Creating sessions in Twig

Go to https://pluginfactory.io/ to generate a boilerplate Craft plugin. API Version: Craft CMS version 2.5.x PluginName: Twig Session Description: Anything Version: 1.0.0 Plugin Author: Anything ...
  • 66.5k
9 votes

How to render plugin templates in Craft CMS 3?

After becoming more familiar with Craft and Yii, I've found a more appropriate solution for my use case. I have a couple of ideal requirements: Templates are stored in a directory within my module or ...
  • 267
8 votes
Accepted

Controller action works but still gives 404

You're definitely on the right track, just missing out on a couple of key details! The actionUrl function generates a normal, full URL... it's not (by default) an AJAX call. So your browser is ...
  • 23.6k
8 votes
Accepted

How to render plugin templates in Craft CMS 3?

You don't specify, but it sounds like you're trying to have the plugin render its templates as a result of frontend requests? If so, you'd need to do: use Craft; use craft\web\View; $oldMode = \...
  • 11.1k
8 votes
Accepted

What are the different scenarios to plan for when using $allowAnonymous in a controller?

Here are the new constants that Craft 3.2 introduced: const ALLOW_ANONYMOUS_NEVER = 0; const ALLOW_ANONYMOUS_LIVE = 1; const ALLOW_ANONYMOUS_OFFLINE = 2; By default every controller will have the ...
  • 6,401
7 votes
Accepted

Cannot post data to a plugin controller action

There's alot of different things that could be going on here. First, if your action is named actionPayWithPaypal (as in the code above) the action url should be paypalPayments/payment/payWithPaypal I ...
  • 7,248
7 votes
Accepted

Rendering templates from plugin controller

Figured it out! I went ahead and took at look at how Craft's renderTemplate method works which cleared it up for me. A few things... Craft caches CSS/JS resources inside of getHeadHtml() and ...
  • 345
6 votes
Accepted

How to tell if user exists

Something like this should work: public function add($user) { $existingUser = craft()->users->getUserByUsernameOrEmail($user->username); if ($existingUser) { // They ...
  • 66.5k
6 votes

How can I set the HTTP status code?

In Craft3, the correct way of setting HTTP status codes is Craft::$app->getResponse()->setStatusCode(404);
6 votes

How can I set the HTTP status code?

I believe HeaderHelper::setHeader() is what you're looking for in the context of a plugin.
  • 66.5k
6 votes
Accepted

Have a custom plugin do something on every page load?

Add a init() method to your plugin and as long as it's installed and enabled, it'll be called on every request. public function init() { parent::init(); /* Do your stuff */ } If you want it ...
  • 66.5k
5 votes
Accepted

How to return errors from a model back to the template?

If you use $model->getErrors() instead, it should return a multidimensional array of errors indexed by attribute name like so: array( 'email' => array( 'First email error.', ...
  • 66.5k
5 votes
Accepted

How can I ensure using actionUrl in my template does not show script name in the generated URL?

actionUrl() and UrlHelper::getActionUrl() always force the script name to show up despite the omitScriptNameInUrls config setting for two reasons: Action URLs are generally not actually visible to ...
5 votes
Accepted

Constructor in custom controller?

I'm not a huge fan of it but for this situation it might be more appropriate to use init(), Looking at Craft\BaseController: /** * Initializes the controller. This method is called by the Craft ...
  • 4,361
5 votes
Accepted

How do I update a column value in my custom plugin with a simple controller?

You are currently performing a SELECTinstead of an UPDATE so no data will be updated. I'm guessing that entryId is the primary key of the record you are trying to update? If so, I would suggest you ...
  • 1,623
5 votes

Is it typical that data is fetched from within templates rather than in controllers?

From what I've seen so far of Craft and plugins developed for it, the requests are typically forwarded directly to Twig templates and those templates are responsible for analysing query strings and ...
  • 66.5k
5 votes
Accepted

Assets indexing from plugin controller

I guess you would need to have your own method in your controller to fire up the indexing tool. From what it looks like, you need a few things to get going: The classname of the tool i.e AssetIndex ...
  • 4,519
5 votes
Accepted

What should my controller return in a Craft.postActionRequest

If you're just looking to return true or an error message from your controller's action, then it should probably be something like: $this->returnJson(array('success' => true)); Or on error: $...
  • 66.5k
5 votes
Accepted

renderTemplate() renders the twig file without any CSS

I sincerely appreciate the responses! I've since resolved this issue, below is the changed code. My controller was extending the Craft web controller (\craft\web\Controller) which has its own ...
5 votes
Accepted

Is there a way to compare what has changed on an entry while it's being saved?

You're probably looking for Element::getDirtyAttributes() and Element::getDirtyFields()! The former is for native class properties/attributes (and is based on an ActiveRecord pattern in Yii, I believe....
4 votes
Accepted

Twig Filter in Plugin/PHP?

In one of your plugin services, you can render a template that handles the markdown filter and return the results as an HTML string. You can then send that HTML to wherever you want to preview. In ...
4 votes
Accepted

Anonymous access to controller action fails

I think your URL is wrong. Try /index.php/actions/sitemap/sitemap/xml Here is the section of the docs that explains URLs for plugin controllers: http://buildwithcraft.com/docs/plugins/controllers#...
4 votes

Call Controller Method with Plugin

Something like this should work: `craft()->runController('pluginHandle/controllerId/action');` Although I'd question the decisions you've made to get to the point where you had to ask this ...
  • 66.5k
4 votes

Is it typical that data is fetched from within templates rather than in controllers?

My question is, is this a typical and recommended approach to using Craft, or should I be using controllers to handle tasks such as filtering and fetching data and passing it to a view? This is a ...
4 votes
Accepted

How to catch errors in a service and return to a controller?

There are several ways you could go about this. One that I tend to prefer is to go ahead and have your controller grab whatever data it needs from POST and populate a Model with it. That model is ...
  • 66.5k
4 votes

Issues with returnJson not returning html?

It appears whitespace was the cause here, just running a quick whitespace filer on the return value resolved the issue: $userHtml = craft()->logic->renderSiteMacro('_macros/elements', 'listing',...
4 votes
Accepted

can i used craft.entries.section into my Controller

If you want to use ElementCriteria in your controller, this is how to go about it: $criteria = craft()->elements->getCriteria(ElementType::Entry); $criteria->section('products'); $criteria-&...
  • 6,308
4 votes
Accepted

Controller Action 404

When your plugin has a camel cased name you need to make sure you camel case your references to it, such as when you try and visit an action url. I assume this is due to when craft tries to resolve ...
  • 4,519
4 votes
Accepted

How do I call a service from my controller in a plugin?

Try it with a lowercase f in the middle... craft()->fulcrumDonation_fulcrumCyberSource->exampleService();
  • 23.6k
4 votes
Accepted

Business Logic Controller for Validating Address in Commerce

You can move it all into a Service and then call it from your main plugin file if you find it is getting unwieldy. file: craft/plugins/businesslogic/BusinessLogicPlugin.php: class ...
  • 4,361

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