9

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 Author URL: Anything Github Name: Blank Uncheck: Tweet it out Check: Variables Click: Build Plugin Download twigsession.zip and unzip it. Place the unzipped ...


8

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 actually navigating away from the previous page, and pinging your controller as a completely separate HTTP request. Which leads to the next detail... Unless you're ...


7

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 guess? You won't be able to hit the cocktail recipes method directly from your browser since it requires the request to be a post. But you should be able to ...


7

To quote @Brandon from here: Yep you just need to include a $variables argument in your controller function, typeset to an array: public function actionArchive(array $variables = array()) { $variables['year'] ... }


7

Craft will only check for templates in your plugin’s templates/ folder during CP and action requests. When you start routing to your controller action via a site-facing route, it’s no longer either one of those; it’s just a normal Site request. So you will need to point Craft to your plugin’s templates folder explicitly. // Point Craft to this plugin's ...


6

Something like this should work: public function add($user) { $existingUser = craft()->users->getUserByUsernameOrEmail($user->username); if ($existingUser) { // They exist... log them in. craft()->userSession->loginByUserId($existingUser->id); } else { // They don't exist... create them. ...


6

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 = \Craft::$app->view->getTemplateMode(); Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_CP); $html = \Craft::$app->view->renderTemplate('...


6

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 plugin. Templates can be mixed with general application templates (e.g. my module or plugin template can extend from a general site layout template). It turns ...


6

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 following property: protected $allowAnonymous = self::ALLOW_ANONYMOUS_NEVER; And you can override it in your controller in various ways: // Allow anonymous ...


5

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 getFootHtml() which are automatically injected into every template rendered through renderTemplate. Injecting these into my template fixed the JS problem. I forgot to ...


5

I believe HeaderHelper::setHeader() is what you're looking for in the context of a plugin.


5

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.', 'Second email error.', ), 'firstName' => array( 'First firstName error.', ), ); Or you can get errors specifically for an attribute ...


5

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 end users, so there’s no point in hiding “index.php” from them (computers don’t care about aesthetics). They are often used for POST requests, and your index.php ...


5

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 before the controller starts to execute. * * @throws HttpException * @return null */ public function init() { } It seems a little odd to me to have a ...


5

Just an idea, but are you Allowing Anonymous Access to Actions? Maybe you are logged in on your Desktop, but not on your iPhone. Example: protected $allowAnonymous = true;


5

There’s no reason for your controller to return anything – it will never be checked. Many requests call more than one controller action, so it would be pretty presumptuous for Craft to think that it should actually output whatever your controller returns, and end the request. Instead, it’s up to the actual controller to do the outputting, and end the ...


5

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 find the record to make sure it exists, update its attributes (status) and then save it. // PluginController public function actionDeny() { $this->...


5

Right now that's probably the best way to do it. For Craft 2.2, I just added a static getConstants() method to BaseEnum, so you can do can do a PluginStatus::getConstants() call that will return an array with a key => value pair of the enum options and their values that should make that cleaner.


5

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 request data and fetching data from exposed template variables or built in "entries". Not necessarily required. You can register a custom CP route in your ...


5

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 An array of asset sources or * for them all An indexing session id An array of parameters to send to the tool So I managed to get something working with the ...


5

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 to only happen on front-end requests, you can use: if (craft()->request->isSiteRequest()) { ... }


5

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: $this->returnErrorJson($e->getMessage()); returnErrorJson will set an 'error' key and pass whatever mess you use as its value.


5

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 renderTemplate() method that I switched to, which fetches the Control Panel assets I wanted for styling the page. The template twig file remains identical as is above. ...


4

As that page in the docs says, dedicated controller action URLs will look like: http://example.com/actions/pluginHandle/actionName If your plugin’s name is “CocktailRecipes”, and you have a controller located at plugins/cocktailrecipes/controllers/CocktailRecipesController.php, and within that you have a function called actionSaveIngredient(), you would ...


4

The docs explain how to generate links to controller actions. Assuming your action trigger is "actions" (as it is by default) then your URL will look as follows: http://www.domain.com/actions/pluginName/controllerName/actionName Following the first example in the docs, the URL will look as follows: http://www.domain.com/actions/cocktailRecipes/ingredients/...


4

Any properties of the object you pass into Craft.postActionRequest()’s second argument will be available to Craft as POST parameters, so you can get to them with HttpRequestService::getPost(): $sData = craft()->request->getPost('Data'); foreach ($sData as $temp) { $wine = $temp['wine']; // ... }


4

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 difficult question to respond to, because there is no definite answer and any advice would be coloured by opinion. I'll share my thoughts on the matter, for what ...


4

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 question. ;)


4

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#how-controller-actions-fit-into-routing


4

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 your controller: public function actionPreviewContentAjax() { $this->requireAjaxRequest(); $content = craft()->request->getPost('...


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