Hot answers tagged

24

No this is not possible. If you really need PHP, you can write a plugin with one of the following: a Template Variable class, which can be accessed from your templates via craft.myPluginHandle a Twig extension, which can add its own global filters, functions, or tags, etc. a controller, which can execute PHP before calling $this->renderTemplate(...) a ...


16

You can use Twig's date filter: {{ "now"|date('Y') }} Sitename. All rights reserved. More info in the Twig docs


14

There is no way of programmatically checking whether a server fulfils the minimum requirements in Craft without uploading all of the Craft files, however I have created a script that performs all of Craft's web server related checks. It does not connect to a database so cannot check the MySQL version or that the InnoDB storage engine is installed and enabled,...


11

I finally figured out how to upload an asset, however I still don't understand why the insertFileByLocalPath has been removed. /** * @param UploadedFile $uploadedFile * @param int $folderId * @return Asset * @throws BadRequestHttpException * @throws UploadFailedException */ protected static function uploadNewAsset(UploadedFile $uploadedFile, $folderId)...


9

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


8

The first two lines of code determine the start year and the current year. The next block of code compares the two years, and sets the copyrightYears variable accordingly. Finally, we simply output copyrightYears along with the rest of the copyright text. {% set startYear = 2010 %} {% set currentYear = now.year %} {% if startYear == currentYear %} {% ...


8

Have a look at plugin template variables. You can simply create a method in your plugins variable file that returns anything you want to the template, which would be accessible via {{ craft.myplugin.customVariable }}. <?php namespace Craft; class MypluginVariable { public function customVariable($someProperty = 'defaultValueIfNotDefined') { ...


8

Update: Craft 2.6.2957 added support for PHP 7.1 There's a lot going on here, but 1st thing is 1st. The vagrant box is running PHP 7.1 Craft 2 (and the current latest version of the Yii framework - 1.1.17) are not yet compatible with PHP 7.1. When the next Yii 1 release is out that addresses the framework compatibility issues, we'll get Craft 2 up and ...


7

It's as simple as if ( craft()->config->get( 'devMode' ) ) { //do stuff }


6

It's not documented, but UserGroupsService has a method assignUserToGroups($userId, int|array $groupIds). You'll have to save your user first, but once you have a user id, I believe you can just use that with something like this: // Original code $user = new UserModel; $user->username = 'username'; $user->email = 'user@email.com'; $user->firstName ...


6

Apparently PHP's garbage collection does not work when you've set a custom PHP session save path. Lame. If you set the overridePhpSessionLocation config setting to false, Craft will go with whatever path Apache/PHP is configured to save sessions to. If you're not on shared hosting, then it's a perfectly safe change, too. Setting that to false on a site ...


6

There is no event or hook that I'm aware of that can intercept the template rendering process directly. You would likely need to instigate this from within your template. I would suggest perhaps using a plugin variable either directly, or as a wrapper for your plugin service method (if you need to do something more complicated). Something like the ...


6

You can't have tokens in your plugin routes, but you can easily add a segment variable that recreates the asterisk token's functionality with some regex. According to the the official docs on routing, the asterisk represents (...) any string of characters, except for a forward slash (/) Here's what the defintion for a route with a "asterisk-like" segment ...


6

Brad nails the problem in his comment: The error is because you've got two named subpatterns with the same name (<slug>) And the solution is simple... Change the name(s) of your subpattern(s). The term slug is just a convention of Craft. It doesn't matter what the names are, they can be anything useful to you: (?P<domain>[^/]+)/(?P<...


6

You probably need to up Craft's internal memory limit, as well – the following should go into the array in your general.php configuration file: 'phpMaxMemoryLimit' => '1024M',


6

If you're simply looking to execute some custom scripts that are completely standalone (i.e. they don't interact with the Craft application in any way), those files should live somewhere below your public web root (i.e. the public folder, or wherever your index.php file resides). It isn't possible to have Craft route PHP files from the /templates directory. ...


6

It's a known issue you can safely ignore on Craft 2, that's been resolved for Craft 3. PHP 7.0 (or maybe it was 7.1) changed the way that particular error was reported from E_STRICT (which Craft 2 is suppressing) to E_WARNING, which Craft 2 isn't suppressing, so now it pops up on latter versions of PHP.


5

Hat-tip to Brad for finding this answer. This can be achieved with something like the following in your index.php: define('BRADS_FAVORITE_HOBBY', "Drinking"); Thanks to the power to Twig, you can then access this in your templates using: {{ constant('BRADS_FAVORITE_HOBBY') }}


5

You shouldn't need to create a custom plugin to do that. You can easily create a Matrix content field for your carousel. Create a new block called slide. From there you can create an asset field to upload the slide image. You can render this into whatever markup you'd like (even bootstrap). As for cropping an image and converting it into a jpg, check out ...


5

Short yes! You can find a detailed Benchmark on http://www.lornajane.net/posts/2014/php-5-6-benchmarks. If possible i would try to update from PHP 5.4.40 direct to PHP 7. The performance from PHP 5.4 to PHP 7 is massiv. A change to PHP7 made our Craft installation twice as fast. You can find some further information here -> https://pixelandtonic.com/blog/php-...


5

siteName is automatically global... you don't need to include the global. prefix. <title>{{ siteName }}</title>


5

It turns out there's a handy method for manually registering your services, or in Yii2 parlance, just "components": namespace example\myplugin; use example\myplugin\services\Example as ExampleService; use Craft; use craft\base\Plugin as BasePlugin; class MyPlugin extends BasePlugin { public static $plugin; public function init() { ...


4

Here's one answer from cleanup php session files. # Every 30 minutes, not on the hour<br> # Grabs maxlifetime directly from \`php -i\`<br> # doesn't care if /var/lib/php5 exists, errs go to /dev/null<br> 09,39 * * * * find /var/www/craft/storage/runtime/sessions -type f -cmin +60 -exec rm -f {} + >/dev/null 2>&1


4

If your plugin is pulling in a lot of data, then most likely you're just bumping into your php.ini file's memory_limit setting. Try bumping that up to 256M and see if that helps.


4

Sounds like an FTP upload or an unzipping operation didn't finish properly. Try downloading a fresh copy of Craft from http://buildwithcraft.com and replacing the old craft/app folder with the new one.


4

Please make sure the tag settings in CraftImportService.php are set properly.


4

As far as accessing the value of the setting, try: $plugin = craft()->plugins->getPlugin('pluginHandle'); $plugin->getSettings()->getAttribute('pluginSettings1'); If you need to access your settings throughout your services file you might also create a global variable. class PluginHandleService extends BaseApplicationComponent { public $...


4

You'll need to update the regex to allow for additional characters. For example, if you want to allow !, then this will work: return array( 'myroute/(?P<myVariable>[-\w\!]+)' => array('action' => 'myPlugin/myAction') ); Note that \w is essentially the same as writing [A-Za-z0-9_].


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('...


4

You're likely missing the PHP JSON extension, which is a requirement to run Craft.


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