Skip to main content
Share Your Experience: Take the 2024 Developer Survey
14 votes

How can I check from a plugin config file if devMode is enabled?

For those coming here using Craft 3, it's now Craft::$app->getConfig()->general->devMode.
Nate Beaty's user avatar
13 votes

How do I upload an asset in Craft 3, via PHP

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 * @...
SteveEdson's user avatar
9 votes
Accepted

PHP notice unserialize(): Unexpected end of serialized data

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 ...
Brad Bell's user avatar
  • 67.5k
8 votes
Accepted

How can I check from a plugin config file if devMode is enabled?

It's as simple as if ( craft()->config->get( 'devMode' ) ) { //do stuff }
Luke Pearce's user avatar
  • 3,863
7 votes

Upload file size limits to 256MB

When determining the maximum upload file size, Craft first takes the php.ini file upload_max_filesize, post_max_size and memory_limit into account by getting the minimum value and only then applying ...
Brad Bell's user avatar
  • 67.5k
6 votes
Accepted

Adding new locale - hangs at Step 3 'ResaveAllElements'

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',
Mats Mikkel Rummelhoff's user avatar
6 votes
Accepted

Where can I run custom PHP scripts?

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 ...
Mats Mikkel Rummelhoff's user avatar
6 votes

CraftCMS > UserSessionService should be compatible with CWebUser

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 ...
Brad Bell's user avatar
  • 67.5k
5 votes

Getting Twig_Error_Runtime when trying to open the website

siteName is automatically global... you don't need to include the global. prefix. <title>{{ siteName }}</title>
Lindsey D's user avatar
  • 24k
5 votes
Accepted

Are there any advantages of updating from PHP 5.4.40 to 5.6?

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 ...
Tom Bauer's user avatar
  • 1,327
5 votes
Accepted

How do I "register" a service in a Craft 3 plugin?

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 ...
August Miller's user avatar
5 votes

How do I "register" a service in a Craft 3 plugin?

You can do this in Composer with the following: "extra": { "name": "My Plugin", "handle": "my-plugin", "hasCpSettings": false, "hasCpSection": false, "components": { "...
fxfuture's user avatar
  • 279
5 votes
Accepted

How can I get tasks to complete when not logged in to the CP?

To reiterate what has been said in the comments above and to present an answer, the best way to ensure that queue jobs are completed even without the CMS being logged into or the site being visited is ...
Ben Croker's user avatar
  • 7,526
5 votes
Accepted

How to access HTML input values from Twig and vice versa

You can do it without JavaScript (if you don't mind the page refreshing) by accessing the request variable: https://docs.craftcms.com/api/v3/craft-web-twig-variables-request.html#public-methods {{ ...
missmatsuko's user avatar
5 votes
Accepted

Local DB SQL 'CDbException' Errors on Craft 2 site

this is a MySQL/i version issue on Craft 2 sites. You'll need to add this to your database config (craft/config/db.php): 'initSQLs' => array("SET SESSION sql_mode='STRICT_TRANS_TABLES,...
Cole Henley's user avatar
  • 1,741
5 votes
Accepted

Craft 3 plugin - how to update entry with field of Entries type

Ok this was easy. Tried it at first but didn't work and was successful after another go. All it needed was a array of Entry IDs. Working code: $entry = Entry::find()->id($entryId)->one(); $...
biko's user avatar
  • 670
5 votes
Accepted

Undefined index: siteSettings when updating entry types

Even with 'useProjectConfigFile' => false, it is still active under the hood. Could you run ./craft project-config/rebuild and see if that fixes the issue?
Oli's user avatar
  • 7,495
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....
August Miller's user avatar
4 votes

Can we use PHP in our templates instead of Twig?

In craft 3 it is actually possible. {{ craft.app.view.evaluateDynamicContent('phpinfo();') }} As you can see above, php code is passed as string to evaluateDynamicContent function. On inside, ...
Piotr Pogorzelski's user avatar
4 votes
Accepted

Monitor PHP memory usage

If you enable devMode, then memory usage, along with tons of other debugging information (# of database queries, query profiles, execution timestamps, etc.) will be output to your browser's console.
Brad Bell's user avatar
  • 67.5k
4 votes
Accepted

Craft Commerce update variant field in plugin

This is the general mechanism for updating custom fields on elements - use setContentFromPost and then call the element's save function. $variant->setContentFromPost(array( '...
Jeremy Daalder's user avatar
4 votes

Need help with simple PHP plugin

I think you can do this in the template by using for example: craft.request.getSegments or craft.request.getParam( name ) See the docs: https://craftcms.com/docs/templating/craft.request
Floriskoch's user avatar
4 votes
Accepted

Installing Craft without MAMP

It depends what you are most comfortable with. PHP and Apache are shipped with macOS by standard but you'd still need to install and configure MySQL (or MariaDB etc), along with manual virtual host ...
Ian Young's user avatar
  • 584
4 votes
Accepted

Use pure PHP and HTML instead of Templates

You're really fighting against the way Craft CMS does things. I'd start by having a good read through the Craft Docs first, there really isn't a shortcut. The docs are really great though, and ...
Steve Holland's user avatar
4 votes

Twig Cycle - how to show different html based on cycle

I tried this and this worked out also: {% if loop.index is even %} <div></div> {% endif %} {% if loop.index is odd %} <div></div> {% endif %}
Montier Kess's user avatar
4 votes

Returning an array to Twig from a plugin

Your plugin should be able to handle PHP arrays just fine. craft/plugins/myplugin/variables/MyPluginVariable.php function getArray() { return array('hi', 'there'); } craft/templates/template....
Fuglsetrampen's user avatar
4 votes
Accepted

Upgraded to PHP 7.1 - GD or ImageMagick is required

You'll need to contact your hosting provider. There are any number of ways that this "PHP switching" can be implemented, but typically each major PHP version has its own separate modules/config files ...
andrew.welch's user avatar
  • 11.6k
4 votes
Accepted

PHP Fatal error during installation

See this issue: https://github.com/craftcms/cms/issues/4355#issuecomment-498829513 reproduced here for completeness: A yii variable 'isWindows' is apparently the way to correct this issue, but I ...
Ethan Davidson's user avatar
4 votes

ext-zip trouble

None of various attempts to get ext-zip going under Catalina had worked for me, including the above, but here’s what did, with thanks to Chad for showing the essence of the issue: “the Craft system ...
Jonathan Schofield's user avatar
4 votes

Programmatically add entries to an Entry field (craft\fields\Entries)

If you are running on Craft 3, as of 3.4.0 (and really since the release of craft 3), setting custom field values directly like $entry->customField = [id1, id2, id3]; is broken. You should ...
Nick's user avatar
  • 141

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